Skip to content

Commit 06a8cba

Browse files
committed
fix: expose the selected label as the value of a select-only combobox
1 parent fcbd87e commit 06a8cba

4 files changed

Lines changed: 71 additions & 3 deletions

File tree

‎src/BaseSelect/index.tsx‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,13 +656,25 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
656656
}
657657

658658
// ============================ Context =============================
659+
// The built-in select-only input can expose a text label as its native value.
660+
const label = displayValues.length === 1 ? displayValues[0].label : undefined;
661+
const readOnlyValue =
662+
!mode &&
663+
!showSearch &&
664+
!mergedComponents.root &&
665+
!mergedComponents.input &&
666+
(typeof label === 'string' || typeof label === 'number')
667+
? String(label)
668+
: undefined;
669+
659670
const baseSelectContext = React.useMemo<BaseSelectContextProps>(
660671
() => ({
661672
...props,
662673
notFoundContent,
663674
open: mergedOpen,
664675
triggerOpen: mergedOpen,
665676
rawOpen,
677+
readOnlyValue,
666678
id,
667679
showSearch,
668680
multiple,
@@ -681,6 +693,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
681693
multiple,
682694
mergedOpen,
683695
rawOpen,
696+
readOnlyValue,
684697
showScrollBar,
685698
styles,
686699
classNames,
@@ -826,7 +839,10 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
826839

827840
return (
828841
<BaseSelectContext.Provider value={baseSelectContext}>
829-
<Polite visible={focused && !mergedOpen} values={displayValues} />
842+
<Polite
843+
visible={focused && !mergedOpen && readOnlyValue === undefined}
844+
values={displayValues}
845+
/>
830846
{renderNode}
831847
</BaseSelectContext.Provider>
832848
);

‎src/SelectInput/Content/SingleContent.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
1212
({ inputProps }, ref) => {
1313
const { prefixCls, searchValue, activeValue, displayValues, maxLength, mode, components } =
1414
useSelectInputContext();
15-
const { triggerOpen, title: rootTitle, showSearch, classNames, styles } = useBaseProps();
15+
const {
16+
triggerOpen,
17+
title: rootTitle,
18+
showSearch,
19+
readOnlyValue,
20+
classNames,
21+
styles,
22+
} = useBaseProps();
1623
const selectContext = React.useContext(SelectContext);
1724

1825
const [inputChanged, setInputChanged] = React.useState(false);
@@ -109,7 +116,8 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
109116
<Input
110117
ref={ref}
111118
{...inputProps}
112-
value={mergedSearchValue}
119+
value={readOnlyValue ?? mergedSearchValue}
120+
style={readOnlyValue !== undefined ? { opacity: 0 } : undefined}
113121
maxLength={mode === 'combobox' ? maxLength : undefined}
114122
onChange={(e) => {
115123
setInputChanged(true);

‎src/hooks/useBaseProps.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { BaseSelectProps } from '../BaseSelect';
99
export interface BaseSelectContextProps extends BaseSelectProps {
1010
triggerOpen: boolean;
1111
rawOpen: boolean;
12+
readOnlyValue?: string;
1213
multiple: boolean;
1314
toggleOpen: (open?: boolean) => void;
1415
lockOptions: boolean;

‎tests/Accessibility.test.tsx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,49 @@ describe('Select.Accessibility', () => {
2121
expect(container.querySelector('input')!.getAttribute('aria-label')).toEqual('light');
2222
});
2323

24+
// https://github.com/ant-design/ant-design/issues/52987
25+
it('exposes the selected label as the value of a select-only combobox', () => {
26+
const options = [
27+
{ value: 'lucy', label: 'Lucy' },
28+
{ value: 'zero', label: 0 },
29+
];
30+
const { getByRole, container, rerender } = render(
31+
<Select aria-label="Person" value="lucy" options={options} />,
32+
);
33+
const input = getByRole('combobox', { name: 'Person' });
34+
fireEvent.focus(input);
35+
36+
expect(input).toHaveValue('Lucy');
37+
expect(container.querySelector('[aria-live="polite"]')).toBeNull();
38+
39+
rerender(<Select aria-label="Person" value="zero" options={options} />);
40+
expect(input).toHaveValue('0');
41+
42+
rerender(<Select aria-label="Person" value={null} options={options} />);
43+
expect(input).toHaveValue('');
44+
});
45+
46+
it('keeps the search input independent of the selected label', () => {
47+
const { getByRole } = render(
48+
<Select showSearch defaultValue="lucy" options={[{ value: 'lucy', label: 'Lucy' }]} />,
49+
);
50+
const input = getByRole('combobox');
51+
expect(input).toHaveValue('');
52+
fireEvent.change(input, { target: { value: 'lu' } });
53+
expect(input).toHaveValue('lu');
54+
});
55+
56+
it('keeps the live region for a non-text selection label', () => {
57+
const { getByRole, container } = render(
58+
<Select defaultValue="lucy" options={[{ value: 'lucy', label: <b>Lucy</b> }]} />,
59+
);
60+
const input = getByRole('combobox');
61+
fireEvent.focus(input);
62+
63+
expect(input).toHaveValue('');
64+
expect(container.querySelector('[aria-live="polite"]')).toHaveTextContent('lucy');
65+
});
66+
2467
// https://github.com/ant-design/ant-design/issues/56841
2568
it('spacebar opens dropdown (ARIA combobox)', () => {
2669
const { container } = render(

0 commit comments

Comments
 (0)