Skip to content

Commit 2c17b60

Browse files
authored
fix: allow clearing the value via keyboard (#1247)
* fix: clear button keyboard Enter/Space press * Add tests
1 parent dcd39c2 commit 2c17b60

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎src/SelectInput/index.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
211211
onMouseDown?.(event);
212212
});
213213

214+
// ===================== Clear ======================
215+
// The clear button lives inside the select root, whose `onKeyDown` treats
216+
// Enter/Space as "open the dropdown" and calls `preventDefault` on them.
217+
// That would cancel the native button activation, so keyboard users would
218+
// never get the `click` event which performs the clear. Keep the activation
219+
// keys scoped to the button itself.
220+
const onClearKeyDown: React.KeyboardEventHandler<HTMLButtonElement> = (event) => {
221+
if (event.key === 'Enter' || event.key === ' ') {
222+
event.stopPropagation();
223+
}
224+
};
225+
214226
// =================== Components ===================
215227
const { root: RootComponent } = components;
216228

@@ -298,6 +310,7 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
298310
e.preventDefault();
299311
(e.nativeEvent as any)._select_lazy = true;
300312
}}
313+
onKeyDown={onClearKeyDown}
301314
// Clearing happens on click so it works for both pointer and
302315
// keyboard (Enter/Space) activation.
303316
onClick={onClearMouseDown}

‎tests/shared/allowClearTest.tsx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function allowClearTest(mode: any, value: any) {
2323
fireEvent(clear, mouseDownEvent);
2424
expect(mouseDownEvent.defaultPrevented).toBe(true);
2525
});
26+
2627
it('clears value', () => {
2728
const onClear = jest.fn();
2829
const onChange = jest.fn();
@@ -62,5 +63,47 @@ export default function allowClearTest(mode: any, value: any) {
6263
expect(container.querySelector('input').value).toEqual('');
6364
expect(onClear).toHaveBeenCalled();
6465
});
66+
67+
it('clears value with keyboard', () => {
68+
['Enter', ' '].forEach((key) => {
69+
const onClear = jest.fn();
70+
const onChange = jest.fn();
71+
const onDeselect = jest.fn();
72+
const useArrayValue = ['tags', 'multiple'].includes(mode);
73+
74+
const { container } = render(
75+
<Select
76+
defaultValue={useArrayValue ? ['1'] : '1'}
77+
mode={mode}
78+
allowClear
79+
onClear={onClear}
80+
onChange={onChange}
81+
onDeselect={onDeselect}
82+
>
83+
<Option value="1">1</Option>
84+
<Option value="2">2</Option>
85+
</Select>,
86+
);
87+
const clear = container.querySelector('.rc-select-clear');
88+
const keyDownEvent = createEvent.keyDown(clear, { key });
89+
90+
fireEvent(clear, keyDownEvent);
91+
92+
expect(keyDownEvent.defaultPrevented).toBe(false);
93+
expect(container.querySelector('.rc-select-open')).toBeFalsy();
94+
95+
// The native button activation should fire a click
96+
fireEvent.click(clear);
97+
98+
if (useArrayValue) {
99+
expect(onChange).toHaveBeenCalledWith([], []);
100+
} else {
101+
expect(onChange).toHaveBeenCalledWith(undefined, undefined);
102+
}
103+
expect(onDeselect).not.toBeCalled();
104+
expect(container.querySelector('input').value).toEqual('');
105+
expect(onClear).toHaveBeenCalled();
106+
});
107+
});
65108
});
66109
}

0 commit comments

Comments
 (0)