Skip to content

Commit 0c61753

Browse files
authored
fix: prevent onSelect firing twice on Enter when dropdown is open (#1219)
* fix: only submit input as tag on Enter when dropdown closed * fix: remove unuse code * test: modify test case
1 parent fe73bbf commit 0c61753

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/SelectInput/Input.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
9595
const { value: nextVal } = event.currentTarget;
9696

9797
// Handle Enter key submission - referencing Selector implementation
98-
if (key === 'Enter' && mode === 'tags' && !compositionStatusRef.current && onSearchSubmit) {
98+
if (
99+
key === 'Enter' &&
100+
mode === 'tags' &&
101+
!open &&
102+
!compositionStatusRef.current &&
103+
onSearchSubmit
104+
) {
99105
onSearchSubmit(nextVal);
100106
}
101107

tests/Tags.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ describe('Select.Tags', () => {
140140
expectOpen(container, false);
141141
});
142142

143+
it('should trigger onSelect once when pressing Enter to select option in tags mode (dropdown open)', () => {
144+
const handleSelect = jest.fn();
145+
const { container } = render(
146+
<Select
147+
mode="tags"
148+
open
149+
showSearch
150+
onSelect={handleSelect}
151+
options={[
152+
{ label: 'opt1', value: 'opt1' },
153+
{ label: 'opt2', value: 'opt2' },
154+
]}
155+
/>,
156+
);
157+
fireEvent.change(container.querySelector('input'), { target: { value: 'op' } });
158+
jest.runAllTimers();
159+
keyDown(container.querySelector('input'), KeyCode.DOWN);
160+
keyDown(container.querySelector('input'), KeyCode.ENTER);
161+
jest.runAllTimers();
162+
expect(handleSelect).toHaveBeenCalledTimes(1);
163+
expect(handleSelect).toHaveBeenCalledWith('opt1', expect.anything());
164+
});
165+
143166
// Paste tests
144167
[
145168
{

0 commit comments

Comments
 (0)