Skip to content

Commit fe94e97

Browse files
zombieJclaude
andauthored
fix: Allow space character in Select input (#1210)
* 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 root cause was that preventDefault() was always called when mode was not 'combobox', which prevented typing spaces even when the input was editable in showSearch mode. Changes: - Modified space key handling to only call preventDefault() when the input is not editable - Input is considered editable when mode is 'combobox' or showSearch is true - This allows users to type spaces in editable Select inputs while preventing default behavior (scroll/submit) in non-editable Selects This fix allows users to type spaces in search terms within Select components with showSearch enabled. * Test: Add test cases for space key behavior with showSearch Added test coverage to verify space key behavior in different Select modes: 1. showSearch enabled: Space should NOT call preventDefault, allowing space to be typed 2. showSearch disabled: Space SHOULD call preventDefault, preventing page scroll 3. combobox mode: Space should NOT call preventDefault, allowing space to be typed These tests ensure the fix correctly handles space key input based on whether the input is editable. * chore: add comment explaining preventDefault behavior for Space key Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: refine enter/space key preventDefault logic Separate isCombobox check and apply different preventDefault logic: - Space: prevent only when not editable - Enter: prevent only when not combobox Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8327910 commit fe94e97

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/BaseSelect/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,9 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
471471
// Enter or Space opens dropdown (ARIA combobox: spacebar should open)
472472
if (isEnterKey || isSpaceKey) {
473473
// Do not submit form when type in the input; prevent Space from scrolling page
474-
if (mode !== 'combobox') {
474+
const isCombobox = mode === 'combobox';
475+
const isEditable = isCombobox || showSearch;
476+
if ((isSpaceKey && !isEditable) || (isEnterKey && !isCombobox)) {
475477
event.preventDefault();
476478
}
477479

tests/Select.test.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,4 +2985,64 @@ describe('Select.Basic', () => {
29852985

29862986
expect(selectedItem).not.toHaveAttribute('xxx');
29872987
});
2988+
2989+
describe('Space key behavior with showSearch', () => {
2990+
it('should not call preventDefault on space when showSearch is enabled', () => {
2991+
const { container } = render(
2992+
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
2993+
);
2994+
2995+
const input = container.querySelector('input');
2996+
input.focus();
2997+
2998+
const keyDownEvent = new KeyboardEvent('keydown', {
2999+
key: ' ',
3000+
code: 'Space',
3001+
bubbles: true,
3002+
});
3003+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
3004+
3005+
input.dispatchEvent(keyDownEvent);
3006+
3007+
expect(preventDefaultSpy).not.toHaveBeenCalled();
3008+
});
3009+
3010+
it('should call preventDefault on space when showSearch is disabled', () => {
3011+
const { container } = render(<Select options={[{ value: 'test', label: 'test' }]} />);
3012+
3013+
const input = container.querySelector('input');
3014+
input.focus();
3015+
3016+
const keyDownEvent = new KeyboardEvent('keydown', {
3017+
key: ' ',
3018+
code: 'Space',
3019+
bubbles: true,
3020+
});
3021+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
3022+
3023+
input.dispatchEvent(keyDownEvent);
3024+
3025+
expect(preventDefaultSpy).toHaveBeenCalled();
3026+
});
3027+
3028+
it('should not call preventDefault on space in combobox mode', () => {
3029+
const { container } = render(
3030+
<Select mode="combobox" options={[{ value: 'test', label: 'test' }]} />,
3031+
);
3032+
3033+
const input = container.querySelector('input');
3034+
input.focus();
3035+
3036+
const keyDownEvent = new KeyboardEvent('keydown', {
3037+
key: ' ',
3038+
code: 'Space',
3039+
bubbles: true,
3040+
});
3041+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
3042+
3043+
input.dispatchEvent(keyDownEvent);
3044+
3045+
expect(preventDefaultSpy).not.toHaveBeenCalled();
3046+
});
3047+
});
29883048
});

0 commit comments

Comments
 (0)