Skip to content

Commit 1e8b9b6

Browse files
fix: open dropdown on spacekey for keyboard accessibility (#1200)
* fix: open dropdown on spacekey for keyboard accessibility * fix: add test
1 parent a3ffbcf commit 1e8b9b6

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/BaseSelect/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,11 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
463463
const { key } = event;
464464

465465
const isEnterKey = key === 'Enter';
466+
const isSpaceKey = key === ' ';
466467

467-
if (isEnterKey) {
468-
// Do not submit form when type in the input
468+
// Enter or Space opens dropdown (ARIA combobox: spacebar should open)
469+
if (isEnterKey || isSpaceKey) {
470+
// Do not submit form when type in the input; prevent Space from scrolling page
469471
if (mode !== 'combobox') {
470472
event.preventDefault();
471473
}
@@ -507,7 +509,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
507509
}
508510
}
509511

510-
if (mergedOpen && (!isEnterKey || !keyLockRef.current)) {
512+
if (mergedOpen && (!isEnterKey || !keyLockRef.current) && !isSpaceKey) {
511513
// Lock the Enter key after it is pressed to avoid repeated triggering of the onChange event.
512514
if (isEnterKey) {
513515
keyLockRef.current = true;

tests/Accessibility.test.tsx

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

24+
// https://github.com/ant-design/ant-design/issues/56841
25+
it('spacebar opens dropdown (ARIA combobox)', () => {
26+
const { container } = render(
27+
<Select
28+
options={[
29+
{ label: 'Bamboo', value: 'bamboo' },
30+
{ label: 'Light', value: 'light' },
31+
]}
32+
/>,
33+
);
34+
35+
const selector = container.querySelector('.rc-select') as HTMLElement;
36+
expectOpen(container, false);
37+
38+
fireEvent.focus(container.querySelector('input')!);
39+
keyDown(selector, 32, { key: ' ' });
40+
act(() => {
41+
jest.runAllTimers();
42+
});
43+
expectOpen(container);
44+
});
45+
2446
// https://github.com/ant-design/ant-design/issues/31850
2547
it('active index should keep', async () => {
2648
const onActive = jest.fn();

0 commit comments

Comments
 (0)