Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,9 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
// Enter or Space opens dropdown (ARIA combobox: spacebar should open)
if (isEnterKey || isSpaceKey) {
// Do not submit form when type in the input; prevent Space from scrolling page
if (mode !== 'combobox') {
const isCombobox = mode === 'combobox';
const isEditable = isCombobox || showSearch;
if ((isSpaceKey && !isEditable) || (isEnterKey && !isCombobox)) {
event.preventDefault();
}

Expand Down
60 changes: 60 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2985,4 +2985,64 @@ describe('Select.Basic', () => {

expect(selectedItem).not.toHaveAttribute('xxx');
});

describe('Space key behavior with showSearch', () => {
it('should not call preventDefault on space when showSearch is enabled', () => {
const { container } = render(
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
);

const input = container.querySelector('input');
input.focus();

const keyDownEvent = new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
bubbles: true,
});
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');

input.dispatchEvent(keyDownEvent);

expect(preventDefaultSpy).not.toHaveBeenCalled();
});

it('should call preventDefault on space when showSearch is disabled', () => {
const { container } = render(<Select options={[{ value: 'test', label: 'test' }]} />);

const input = container.querySelector('input');
input.focus();

const keyDownEvent = new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
bubbles: true,
});
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');

input.dispatchEvent(keyDownEvent);

expect(preventDefaultSpy).toHaveBeenCalled();
});

it('should not call preventDefault on space in combobox mode', () => {
const { container } = render(
<Select mode="combobox" options={[{ value: 'test', label: 'test' }]} />,
);

const input = container.querySelector('input');
input.focus();

const keyDownEvent = new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
bubbles: true,
});
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');

input.dispatchEvent(keyDownEvent);

expect(preventDefaultSpy).not.toHaveBeenCalled();
});
});
});
Loading