Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -656,13 +656,25 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
}

// ============================ Context =============================
// The built-in select-only input can expose a text label as its native value.
const label = displayValues.length === 1 ? displayValues[0].label : undefined;
const readOnlyValue =
!mode &&
!showSearch &&
!mergedComponents.root &&
!mergedComponents.input &&
(typeof label === 'string' || typeof label === 'number')
? String(label)
: undefined;

const baseSelectContext = React.useMemo<BaseSelectContextProps>(
() => ({
...props,
notFoundContent,
open: mergedOpen,
triggerOpen: mergedOpen,
rawOpen,
readOnlyValue,
id,
showSearch,
multiple,
Expand All @@ -681,6 +693,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
multiple,
mergedOpen,
rawOpen,
readOnlyValue,
showScrollBar,
styles,
classNames,
Expand Down Expand Up @@ -826,7 +839,10 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)

return (
<BaseSelectContext.Provider value={baseSelectContext}>
<Polite visible={focused && !mergedOpen} values={displayValues} />
<Polite
visible={focused && !mergedOpen && readOnlyValue === undefined}
values={displayValues}
/>
{renderNode}
</BaseSelectContext.Provider>
);
Expand Down
12 changes: 10 additions & 2 deletions src/SelectInput/Content/SingleContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
({ inputProps }, ref) => {
const { prefixCls, searchValue, activeValue, displayValues, maxLength, mode, components } =
useSelectInputContext();
const { triggerOpen, title: rootTitle, showSearch, classNames, styles } = useBaseProps();
const {
triggerOpen,
title: rootTitle,
showSearch,
readOnlyValue,
classNames,
styles,
} = useBaseProps();
const selectContext = React.useContext(SelectContext);

const [inputChanged, setInputChanged] = React.useState(false);
Expand Down Expand Up @@ -109,7 +116,8 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
<Input
ref={ref}
{...inputProps}
value={mergedSearchValue}
value={readOnlyValue ?? mergedSearchValue}
style={readOnlyValue !== undefined ? { opacity: 0 } : undefined}
maxLength={mode === 'combobox' ? maxLength : undefined}
onChange={(e) => {
setInputChanged(true);
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useBaseProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { BaseSelectProps } from '../BaseSelect';
export interface BaseSelectContextProps extends BaseSelectProps {
triggerOpen: boolean;
rawOpen: boolean;
readOnlyValue?: string;
multiple: boolean;
toggleOpen: (open?: boolean) => void;
lockOptions: boolean;
Expand Down
43 changes: 43 additions & 0 deletions tests/Accessibility.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,49 @@ describe('Select.Accessibility', () => {
expect(container.querySelector('input')!.getAttribute('aria-label')).toEqual('light');
});

// https://github.com/ant-design/ant-design/issues/52987
it('exposes the selected label as the value of a select-only combobox', () => {
const options = [
{ value: 'lucy', label: 'Lucy' },
{ value: 'zero', label: 0 },
];
const { getByRole, container, rerender } = render(
<Select aria-label="Person" value="lucy" options={options} />,
);
const input = getByRole('combobox', { name: 'Person' });
fireEvent.focus(input);

expect(input).toHaveValue('Lucy');
expect(container.querySelector('[aria-live="polite"]')).toBeNull();

rerender(<Select aria-label="Person" value="zero" options={options} />);
expect(input).toHaveValue('0');

rerender(<Select aria-label="Person" value={null} options={options} />);
expect(input).toHaveValue('');
});

it('keeps the search input independent of the selected label', () => {
const { getByRole } = render(
<Select showSearch defaultValue="lucy" options={[{ value: 'lucy', label: 'Lucy' }]} />,
);
const input = getByRole('combobox');
expect(input).toHaveValue('');
fireEvent.change(input, { target: { value: 'lu' } });
expect(input).toHaveValue('lu');
});

it('keeps the live region for a non-text selection label', () => {
const { getByRole, container } = render(
<Select defaultValue="lucy" options={[{ value: 'lucy', label: <b>Lucy</b> }]} />,
);
const input = getByRole('combobox');
fireEvent.focus(input);

expect(input).toHaveValue('');
expect(container.querySelector('[aria-live="polite"]')).toHaveTextContent('lucy');
});

// https://github.com/ant-design/ant-design/issues/56841
it('spacebar opens dropdown (ARIA combobox)', () => {
const { container } = render(
Expand Down