Skip to content

Commit 1daf881

Browse files
committed
fix: only submit input as tag on Enter when dropdown closed
1 parent fe73bbf commit 1daf881

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

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: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ import openControlledTest from './shared/openControlledTest';
1313
import removeSelectedTest from './shared/removeSelectedTest';
1414
import maxTagRenderTest from './shared/maxTagRenderTest';
1515
import throwOptionValue from './shared/throwOptionValue';
16-
import { injectRunAllTimers, findSelection, expectOpen, toggleOpen, keyDown } from './utils/common';
16+
import {
17+
injectRunAllTimers,
18+
findSelection,
19+
expectOpen,
20+
toggleOpen,
21+
keyDown,
22+
keyUp,
23+
} from './utils/common';
1724
import type { CustomTagProps } from '@/BaseSelect';
1825

1926
describe('Select.Tags', () => {
@@ -140,6 +147,29 @@ describe('Select.Tags', () => {
140147
expectOpen(container, false);
141148
});
142149

150+
it('should trigger onSelect once when pressing Enter to select option in tags mode (dropdown open)', () => {
151+
const handleSelect = jest.fn();
152+
const { container } = render(
153+
<Select
154+
mode="tags"
155+
open
156+
showSearch
157+
onSelect={handleSelect}
158+
options={[
159+
{ label: 'Dacryoadenitis', value: 'opt1' },
160+
{ label: 'Dacryocystitis', value: 'opt2' },
161+
]}
162+
/>,
163+
);
164+
fireEvent.change(container.querySelector('input'), { target: { value: 'da' } });
165+
jest.runAllTimers();
166+
fireEvent.mouseMove(container.querySelectorAll('.rc-select-item-option')[0]);
167+
keyDown(container.querySelector('input'), KeyCode.ENTER);
168+
keyUp(container.querySelector('input'), KeyCode.ENTER);
169+
jest.runAllTimers();
170+
expect(handleSelect).toHaveBeenCalledTimes(1);
171+
});
172+
143173
// Paste tests
144174
[
145175
{

0 commit comments

Comments
 (0)