Skip to content

Commit e28d53e

Browse files
committed
Fix: Allow space character in Select input
Fixed issue where pressing the space key in Select input would not add a space character to the input field. The space key was always calling preventDefault(), which prevented the default behavior of adding a space character to the input. Changes: - Modified space key handling in BaseSelect to allow space to be typed when dropdown is open and user has typed content - Space still prevents default behavior when dropdown is closed (opens dropdown) or when input is empty (prevents page scroll) - Added test cases to verify the fix This fix allows users to type spaces in search terms within Select components while maintaining the existing behavior for opening the dropdown.
1 parent 8327910 commit e28d53e

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/BaseSelect/index.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,14 +470,15 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
470470

471471
// Enter or Space opens dropdown (ARIA combobox: spacebar should open)
472472
if (isEnterKey || isSpaceKey) {
473-
// Do not submit form when type in the input; prevent Space from scrolling page
474-
if (mode !== 'combobox') {
475-
event.preventDefault();
476-
}
477-
478-
// We only manage open state here, close logic should handle by list component
479473
if (!mergedOpen) {
474+
if (mode !== 'combobox') {
475+
event.preventDefault();
476+
}
480477
triggerOpen(true);
478+
} else if (!(isSpaceKey && mergedSearchValue !== '')) {
479+
if (mode !== 'combobox') {
480+
event.preventDefault();
481+
}
481482
}
482483
}
483484

tests/spaceInput.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as React from 'react';
2+
import { render } from '@testing-library/react';
3+
import Select from '../src/Select';
4+
5+
describe('Space key input in Select', () => {
6+
it('should allow typing space when typing in the input', () => {
7+
const { container } = render(
8+
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
9+
);
10+
11+
const input = container.querySelector('input');
12+
input.focus();
13+
input.value = 'hello';
14+
15+
const event = new Event('input', { bubbles: true });
16+
input.dispatchEvent(event);
17+
18+
const keyDownEvent = new KeyboardEvent('keydown', {
19+
key: ' ',
20+
code: 'Space',
21+
bubbles: true,
22+
});
23+
input.dispatchEvent(keyDownEvent);
24+
25+
expect(input.value).toBe('hello');
26+
});
27+
28+
it('should open dropdown on space when input is empty', () => {
29+
const { container } = render(
30+
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
31+
);
32+
33+
const input = container.querySelector('input');
34+
input.focus();
35+
36+
const keyDownEvent = new KeyboardEvent('keydown', {
37+
key: ' ',
38+
code: 'Space',
39+
bubbles: true,
40+
});
41+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
42+
43+
input.dispatchEvent(keyDownEvent);
44+
45+
expect(preventDefaultSpy).toHaveBeenCalled();
46+
});
47+
});

0 commit comments

Comments
 (0)