Skip to content

Commit 2fd748b

Browse files
authored
Fix selection trigger open logic (#467)
* fix logic * add test case
1 parent 39e283f commit 2fd748b

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

src/Selector/index.tsx

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import * as React from 'react';
1212
import KeyCode from 'rc-util/lib/KeyCode';
1313
import MultipleSelector from './MultipleSelector';
1414
import SingleSelector from './SingleSelector';
15-
import {
16-
LabelValueType,
17-
RawValueType,
18-
CustomTagProps,
19-
} from '../interface/generator';
15+
import { LabelValueType, RawValueType, CustomTagProps } from '../interface/generator';
2016
import { RenderNode, Mode } from '../interface';
2117
import useLock from '../hooks/useLock';
2218

@@ -36,15 +32,9 @@ export interface InnerSelectorProps {
3632
open: boolean;
3733
tabIndex?: number;
3834

39-
onInputKeyDown: React.KeyboardEventHandler<
40-
HTMLInputElement | HTMLTextAreaElement
41-
>;
42-
onInputMouseDown: React.MouseEventHandler<
43-
HTMLInputElement | HTMLTextAreaElement
44-
>;
45-
onInputChange: React.ChangeEventHandler<
46-
HTMLInputElement | HTMLTextAreaElement
47-
>;
35+
onInputKeyDown: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement>;
36+
onInputMouseDown: React.MouseEventHandler<HTMLInputElement | HTMLTextAreaElement>;
37+
onInputChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement>;
4838
}
4939

5040
export interface RefSelectorProps {
@@ -75,9 +65,7 @@ export interface SelectorProps {
7565
// Tags
7666
maxTagCount?: number;
7767
maxTagTextLength?: number;
78-
maxTagPlaceholder?:
79-
| React.ReactNode
80-
| ((omittedValues: LabelValueType[]) => React.ReactNode);
68+
maxTagPlaceholder?: React.ReactNode | ((omittedValues: LabelValueType[]) => React.ReactNode);
8169
tagRender?: (props: CustomTagProps) => React.ReactElement;
8270

8371
// Motion
@@ -87,9 +75,7 @@ export interface SelectorProps {
8775
/** `onSearch` returns go next step boolean to check if need do toggle open */
8876
onSearch: (searchValue: string) => boolean;
8977
onSelect: (value: RawValueType, option: { selected: boolean }) => void;
90-
onInputKeyDown?: React.KeyboardEventHandler<
91-
HTMLInputElement | HTMLTextAreaElement
92-
>;
78+
onInputKeyDown?: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement>;
9379

9480
/**
9581
* @private get real dom for trigger align.
@@ -98,17 +84,15 @@ export interface SelectorProps {
9884
domRef: React.Ref<HTMLDivElement>;
9985
}
10086

101-
const Selector: React.RefForwardingComponent<
102-
RefSelectorProps,
103-
SelectorProps
104-
> = (props, ref) => {
87+
const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> = (props, ref) => {
10588
const inputRef = React.useRef<HTMLInputElement>(null);
10689

10790
const {
10891
prefixCls,
10992
multiple,
11093
open,
11194
mode,
95+
showSearch,
11296

11397
onSearch,
11498
onToggleOpen,
@@ -130,9 +114,7 @@ const Selector: React.RefForwardingComponent<
130114
// ====================== Input ======================
131115
const [getInputMouseDown, setInputMouseDown] = useLock(0);
132116

133-
const onInternalInputKeyDown: React.KeyboardEventHandler<
134-
HTMLInputElement
135-
> = event => {
117+
const onInternalInputKeyDown: React.KeyboardEventHandler<HTMLInputElement> = event => {
136118
const { which } = event;
137119

138120
if (which === KeyCode.UP || which === KeyCode.DOWN) {
@@ -143,11 +125,7 @@ const Selector: React.RefForwardingComponent<
143125
onInputKeyDown(event);
144126
}
145127

146-
if (
147-
![KeyCode.SHIFT, KeyCode.TAB, KeyCode.BACKSPACE, KeyCode.ESC].includes(
148-
which,
149-
)
150-
) {
128+
if (![KeyCode.SHIFT, KeyCode.TAB, KeyCode.BACKSPACE, KeyCode.ESC].includes(which)) {
151129
onToggleOpen(true);
152130
}
153131
};
@@ -156,9 +134,7 @@ const Selector: React.RefForwardingComponent<
156134
* We can not use `findDOMNode` sine it will get warning,
157135
* have to use timer to check if is input element.
158136
*/
159-
const onInternalInputMouseDown: React.MouseEventHandler<
160-
HTMLInputElement
161-
> = () => {
137+
const onInternalInputMouseDown: React.MouseEventHandler<HTMLInputElement> = () => {
162138
setInputMouseDown(true);
163139
};
164140

@@ -177,11 +153,12 @@ const Selector: React.RefForwardingComponent<
177153
};
178154

179155
const onMouseDown: React.MouseEventHandler<HTMLElement> = event => {
180-
if (event.target !== inputRef.current && !getInputMouseDown()) {
156+
const inputMouseDown = getInputMouseDown();
157+
if (event.target !== inputRef.current && !inputMouseDown) {
181158
event.preventDefault();
182159
}
183160

184-
if (mode !== 'combobox' || !open) {
161+
if ((mode !== 'combobox' && (!showSearch || !inputMouseDown)) || !open) {
185162
onToggleOpen();
186163
}
187164
};
@@ -212,9 +189,7 @@ const Selector: React.RefForwardingComponent<
212189
);
213190
};
214191

215-
const ForwardSelector = React.forwardRef<RefSelectorProps, SelectorProps>(
216-
Selector,
217-
);
192+
const ForwardSelector = React.forwardRef<RefSelectorProps, SelectorProps>(Selector);
218193
ForwardSelector.displayName = 'Selector';
219194

220195
export default ForwardSelector;

tests/Multiple.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ describe('Select.Multiple', () => {
145145
selectItem(wrapper, 1);
146146

147147
toggleOpen(wrapper);
148+
expectOpen(wrapper, false);
148149
removeSelection(wrapper);
149150
expectOpen(wrapper, false);
150151
expect(wrapper.find('Selector').props().values).toEqual([

tests/Select.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,20 @@ describe('Select.Basic', () => {
461461
expect(handleSearch).not.toHaveBeenCalled();
462462
});
463463

464+
it('not close when click on the input', () => {
465+
const wrapper = mount(
466+
<Select showSearch>
467+
<Option value="1">1</Option>
468+
<Option value="2">2</Option>
469+
</Select>,
470+
);
471+
472+
for (let i = 0; i < 10; i += 1) {
473+
wrapper.find('input').simulate('mousedown');
474+
expectOpen(wrapper);
475+
}
476+
});
477+
464478
// Should always trigger search event:
465479
// https://github.com/ant-design/ant-design/issues/16223
466480
// https://github.com/ant-design/ant-design/issues/10817

0 commit comments

Comments
 (0)