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
6 changes: 3 additions & 3 deletions packages/react-core/src/components/FormSelect/FormSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface FormSelectProps
onFocus?: (event: React.FormEvent<HTMLSelectElement>) => void;
/** Optional callback for updating when selection changes */
onChange?: (event: React.FormEvent<HTMLSelectElement>, value: string) => void;
/** Custom flag to show that the FormSelect requires an associated id or aria-label. */
/** Accessible label. Required unless an associated id or aria-labelledby is specified. */
'aria-label'?: string;
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
Expand All @@ -43,9 +43,9 @@ class FormSelect extends Component<FormSelectProps> {
static displayName = 'FormSelect';
constructor(props: FormSelectProps) {
super(props);
if (!props.id && !props['aria-label']) {
if (!props.id && !props['aria-label'] && !props['aria-labelledby']?.trim()) {
// eslint-disable-next-line no-console
console.error('FormSelect requires either an id or aria-label to be specified');
console.error('FormSelect requires an id, aria-label, or aria-labelledby to be specified');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,45 @@ describe('FormSelect', () => {
expect(myMock).toHaveBeenCalled();
expect(myMock.mock.calls[0][1]).toEqual('mr');
});

test.each(['select-label', ' select-label \t'])(
'uses aria-labelledby %p without generating a console error',
(labelledBy) => {
const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});

try {
render(
<>
<span id="select-label">Title</span>
<FormSelect aria-labelledby={labelledBy}>
<FormSelectOption value="mr" label="Mr" />
</FormSelect>
</>
);

expect(screen.getByRole('combobox', { name: 'Title' })).toHaveAttribute('aria-labelledby', labelledBy);
expect(consoleError).not.toHaveBeenCalled();
} finally {
consoleError.mockRestore();
}
}
);

test.each(['', ' ', '\t\n'])('warns when aria-labelledby is %p and no other label is provided', (labelledBy) => {
const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});

try {
render(
<FormSelect aria-labelledby={labelledBy}>
<FormSelectOption value="mr" label="Mr" />
</FormSelect>
);

expect(consoleError).toHaveBeenCalledWith(
'FormSelect requires an id, aria-label, or aria-labelledby to be specified'
);
} finally {
consoleError.mockRestore();
}
});
});