Skip to content

Commit 57216a9

Browse files
authored
fix: Don't clear searchValue onMouseDown when autoClearSearchValue is false (#874)
* Fix autoclear search value onClick when autoClearSearchValue is false * Add test
1 parent 71432de commit 57216a9

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

docs/examples/multiple.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Test extends React.Component {
1818
showArrow: false,
1919
loading: false,
2020
value: ['a10'],
21+
searchValue: "",
2122
};
2223

2324
onChange = (value, options) => {
@@ -53,6 +54,12 @@ class Test extends React.Component {
5354
});
5455
};
5556

57+
setSearchValue = val => {
58+
this.setState({
59+
searchValue: val,
60+
})
61+
}
62+
5663
render() {
5764
const { useAnim, showArrow, loading, value } = this.state;
5865
return (
@@ -101,6 +108,31 @@ class Test extends React.Component {
101108
{children}
102109
</Select>
103110
</div>
111+
112+
113+
<h2>multiple select with autoClearSearchValue = false</h2>
114+
<div style={{ width: 300 }}>
115+
<Select
116+
value={value}
117+
style={{ width: 500 }}
118+
mode="multiple"
119+
autoClearSearchValue={false}
120+
showSearch={true}
121+
searchValue={this.state.searchValue}
122+
onSearch={this.setSearchValue}
123+
optionFilterProp="children"
124+
optionLabelProp="children"
125+
onSelect={this.onSelect}
126+
onDeselect={this.onDeselect}
127+
placeholder="please select"
128+
onChange={this.onChange}
129+
onFocus={() => console.log('focus')}
130+
onBlur={v => console.log('blur', v)}
131+
tokenSeparators={[' ', ',']}
132+
>
133+
{children}
134+
</Select>
135+
</div>
104136
</div>
105137
);
106138
}

src/Selector/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
109109
showSearch,
110110
tokenWithEnter,
111111

112+
autoClearSearchValue,
113+
112114
onSearch,
113115
onSearchSubmit,
114116
onToggleOpen,
@@ -233,7 +235,7 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
233235
}
234236

235237
if ((mode !== 'combobox' && (!showSearch || !inputMouseDown)) || !open) {
236-
if (open) {
238+
if (open && autoClearSearchValue !== false) {
237239
onSearch('', true, false);
238240
}
239241
onToggleOpen();

tests/Multiple.test.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ describe('Select.Multiple', () => {
634634
);
635635
expect(wrapper.find('input').props().value).toBe('');
636636
});
637-
it('search value should show when autoClearSearchValue is true', () => {
637+
it('search value should show when autoClearSearchValue is false', () => {
638638
const wrapper = mount(
639639
<Select
640640
mode="multiple"
@@ -646,5 +646,32 @@ describe('Select.Multiple', () => {
646646
);
647647
expect(wrapper.find('input').props().value).toBe('test');
648648
});
649+
it('search value should no clear when autoClearSearchValue is false', () => {
650+
const wrapper = mount(
651+
<Select
652+
mode="multiple"
653+
autoClearSearchValue={false}
654+
showSearch={true}
655+
searchValue="test"
656+
/>,
657+
);
658+
659+
toggleOpen(wrapper);
660+
toggleOpen(wrapper);
661+
expect(wrapper.find('input').props().value).toBe('test');
662+
});
663+
it('search value should clear when autoClearSearchValue is true', () => {
664+
const wrapper = mount(
665+
<Select
666+
mode="multiple"
667+
autoClearSearchValue={true}
668+
showSearch={true}
669+
searchValue="test"
670+
/>,
671+
);
672+
toggleOpen(wrapper);
673+
toggleOpen(wrapper);
674+
expect(wrapper.find('input').props().value).toBe('');
675+
});
649676
});
650677
});

0 commit comments

Comments
 (0)