Skip to content

Commit 307786a

Browse files
authored
chore: Select without combobox mode should always not submit form (#604)
* fix: Not submit when enter * test: Add test case
1 parent ed77ce1 commit 307786a

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/generate.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ import type {
3232
SingleType,
3333
OnClear,
3434
SelectSource,
35-
CustomTagProps} from './interface/generator';
36-
import {
37-
INTERNAL_PROPS_MARK
35+
CustomTagProps,
3836
} from './interface/generator';
37+
import { INTERNAL_PROPS_MARK } from './interface/generator';
3938
import type { OptionListProps, RefOptionListProps } from './OptionList';
4039
import { toInnerValue, toOuterValues, removeLastEnabledValue, getUUID } from './utils/commonUtil';
4140
import TransBtn from './TransBtn';
@@ -761,9 +760,16 @@ export default function generateSelector<
761760
const clearLock = getClearLock();
762761
const { which } = event;
763762

764-
// We only manage open state here, close logic should handle by list component
765-
if (!mergedOpen && which === KeyCode.ENTER) {
766-
onToggleOpen(true);
763+
if (which === KeyCode.ENTER) {
764+
// Do not submit form when type in the input
765+
if (mode !== 'combobox') {
766+
event.preventDefault();
767+
}
768+
769+
// We only manage open state here, close logic should handle by list component
770+
if (!mergedOpen) {
771+
onToggleOpen(true);
772+
}
767773
}
768774

769775
setClearLock(!!mergedSearchValue);

tests/Tags.test.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ describe('Select.Tags', () => {
329329
});
330330

331331
describe('OptGroup', () => {
332-
const createSelect = props => (
332+
const createSelect = (props) => (
333333
<div>
334334
<Select mode="tags" {...props}>
335335
<OptGroup key="Manager" label="Manager">
@@ -407,18 +407,21 @@ describe('Select.Tags', () => {
407407

408408
it('correctly handles the `tabIndex` prop', () => {
409409
const wrapper = mount(<Select mode="tags" tabIndex={0} />);
410-
expect(
411-
wrapper
412-
.find('.rc-select')
413-
.getDOMNode()
414-
.getAttribute('tabindex'),
415-
).toBeNull();
410+
expect(wrapper.find('.rc-select').getDOMNode().getAttribute('tabindex')).toBeNull();
416411

417412
expect(
418-
wrapper
419-
.find('input.rc-select-selection-search-input')
420-
.getDOMNode()
421-
.getAttribute('tabindex'),
413+
wrapper.find('input.rc-select-selection-search-input').getDOMNode().getAttribute('tabindex'),
422414
).toBe('0');
423415
});
416+
417+
it('press enter should not submit form', () => {
418+
const wrapper = mount(<Select mode="tags" open={false} />);
419+
const preventDefault = jest.fn();
420+
wrapper.find('input').simulate('keyDown', {
421+
which: KeyCode.ENTER,
422+
preventDefault,
423+
});
424+
425+
expect(preventDefault).toHaveBeenCalled();
426+
});
424427
});

typings/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ declare module 'rc-menu';
66

77
declare module 'rc-util/lib/Children/toArray';
88

9-
declare module 'rc-util/lib/KeyCode';
10-
119
declare module 'dom-scroll-into-view';
1210

1311
declare module 'rc-trigger';

0 commit comments

Comments
 (0)