Skip to content

Commit 34cd0e3

Browse files
authored
fix: delay to close in combobox (#402)
* delay to close in combobox * update package.json * clean up
1 parent 5e02509 commit 34cd0e3

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
"port": 8003
2929
},
3030
"scripts": {
31-
"build": "rc-tools run storybook-build",
31+
"build": "rc-tools run build",
3232
"compile": "rc-tools run compile --babel-runtime",
33-
"gh-pages": "npm run build && rc-tools run storybook-gh-pages",
33+
"gh-pages": "rc-tools run gh-pages",
3434
"start": "rc-tools run storybook",
3535
"pub": "rc-tools run pub --babel-runtime",
3636
"lint": "rc-tools run lint",
@@ -39,10 +39,8 @@
3939
"test": "rc-tools run test",
4040
"prepublish": "rc-tools run guard",
4141
"init-tslint": "rc-tools run gen-lint-config",
42-
"init-storybook": "rc-tools run genStorybook",
4342
"coverage": "rc-tools run test --coverage",
4443
"pre-commit": "rc-tools run pre-commit",
45-
"storybook": "rc-tools run storybook",
4644
"lint-staged": "lint-staged",
4745
"now-build": "npm run build"
4846
},

src/Select.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class Select extends React.Component<Partial<ISelectProps>, ISelectState> {
233233
public dropdownContainer: Element | null = null;
234234
public blurTimer: number | null = null;
235235
public focusTimer: number | null = null;
236+
public comboboxTimer: number | null = null;
236237

237238
// tslint:disable-next-line:variable-name
238239
private _focused: boolean = false;
@@ -304,6 +305,7 @@ class Select extends React.Component<Partial<ISelectProps>, ISelectState> {
304305
public componentWillUnmount() {
305306
this.clearFocusTime();
306307
this.clearBlurTime();
308+
this.clearComboboxTime();
307309
if (this.dropdownContainer) {
308310
ReactDOM.unmountComponentAtNode(this.dropdownContainer);
309311
document.body.removeChild(this.dropdownContainer);
@@ -371,7 +373,7 @@ class Select extends React.Component<Partial<ISelectProps>, ISelectState> {
371373
};
372374

373375
public onInputKeyDown = (event: React.ChangeEvent<HTMLInputElement> | KeyboardEvent) => {
374-
const { disabled, combobox } = this.props;
376+
const { disabled, combobox, defaultActiveFirstOption } = this.props;
375377
if (disabled) {
376378
return;
377379
}
@@ -406,6 +408,13 @@ class Select extends React.Component<Partial<ISelectProps>, ISelectState> {
406408
if (isRealOpen || !combobox) {
407409
event.preventDefault();
408410
}
411+
412+
// Hard close popup to avoid lock of non option in combobox mode
413+
if (isRealOpen && combobox && defaultActiveFirstOption === false) {
414+
this.comboboxTimer = setTimeout(() => {
415+
this.setOpenState(false);
416+
});
417+
}
409418
} else if (keyCode === KeyCode.ESC) {
410419
if (state.open) {
411420
this.setOpenState(false);
@@ -800,6 +809,7 @@ class Select extends React.Component<Partial<ISelectProps>, ISelectState> {
800809
backfillValue: '',
801810
};
802811
// clear search input value when open is false in singleMode.
812+
// https://github.com/ant-design/ant-design/issues/16572
803813
if (!open && isSingleMode(props) && props.showSearch) {
804814
this.setInputValue('', fireSearch);
805815
}
@@ -961,6 +971,13 @@ class Select extends React.Component<Partial<ISelectProps>, ISelectState> {
961971
}
962972
};
963973

974+
public clearComboboxTime = () => {
975+
if (this.comboboxTimer) {
976+
clearTimeout(this.comboboxTimer);
977+
this.comboboxTimer = null;
978+
}
979+
};
980+
964981
public updateFocusClassName = () => {
965982
const rootRef = this.rootRef;
966983
const props = this.props;

tests/Select.combobox.spec.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,28 @@ describe('Select.combobox', () => {
374374
.simulate('click');
375375
expect(wrapper.find('input').prop('value')).toBe('xxx-1');
376376
});
377+
378+
// https://github.com/ant-design/ant-design/issues/16572
379+
it('close when enter press without active option', () => {
380+
jest.useFakeTimers();
381+
382+
const onDropdownVisibleChange = jest.fn();
383+
384+
const wrapper = mount<Select>(
385+
<Select combobox={true} open={true} onDropdownVisibleChange={onDropdownVisibleChange}>
386+
<Option value="One">One</Option>
387+
<Option value="Two">Two</Option>
388+
</Select>,
389+
);
390+
391+
wrapper.find('input').simulate('keyDown', {
392+
keyCode: KeyCode.ENTER,
393+
});
394+
395+
jest.runAllTimers();
396+
wrapper.update();
397+
expect(onDropdownVisibleChange).toBeCalledWith(false);
398+
399+
jest.useRealTimers();
400+
});
377401
});

0 commit comments

Comments
 (0)