Skip to content

Commit 9abb1f2

Browse files
authored
fix: trigger search value change when reset controlled combobox (#821)
* fix: trigger search value change when reset controlled combobox * chore: code clean
1 parent cd8ef41 commit 9abb1f2

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

docs/examples/combobox.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ class Combobox extends React.Component {
7979
reset
8080
</button>
8181
</p>
82+
<Select
83+
value={value}
84+
mode="combobox"
85+
onChange={this.onChange}
86+
filterOption={(inputValue, option) => {
87+
if (!inputValue) {
88+
return true;
89+
}
90+
return (option.value as string).includes(inputValue);
91+
}}
92+
>
93+
{['1', '2', '3'].map((i) => (
94+
<Option value={i} key={i}>
95+
{i}
96+
</Option>
97+
))}
98+
</Select>
8299
<div>
83100
<Select
84101
disabled={disabled}

src/Select.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import OptGroup from './OptGroup';
4949
import Option from './Option';
5050
import OptionList from './OptionList';
5151
import SelectContext from './SelectContext';
52-
import { toArray, hasValue } from './utils/commonUtil';
52+
import { hasValue, toArray } from './utils/commonUtil';
5353
import { fillFieldNames, flattenOptions, injectPropsWithOption } from './utils/valueUtil';
5454
import warningProps, { warningNullOptions } from './utils/warningPropsUtil';
5555

@@ -295,7 +295,7 @@ const Select = React.forwardRef(
295295
const values = convert2LabelValues(internalValue);
296296

297297
// combobox no need save value when it's no value
298-
if (mode === 'combobox' && !hasValue(values[0]?.value)) {
298+
if (mode === 'combobox' && !values[0]?.value) {
299299
return [];
300300
}
301301

@@ -333,10 +333,7 @@ const Select = React.forwardRef(
333333
React.useEffect(() => {
334334
if (mode === 'combobox') {
335335
const strValue = mergedValues[0]?.value;
336-
337-
if (strValue !== undefined && strValue !== null) {
338-
setSearchValue(String(strValue));
339-
}
336+
setSearchValue(hasValue(strValue) ? String(strValue) : '');
340337
}
341338
}, [mergedValues]);
342339

tests/Combobox.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ describe('Select.Combobox', () => {
3939
expect(wrapper.render()).toMatchSnapshot();
4040
});
4141

42+
it('renders controlled correctly', () => {
43+
const wrapper = mount(
44+
<Select value="" mode="combobox" placeholder="Search">
45+
<Option value="1">1</Option>
46+
<Option value="2">2</Option>
47+
</Select>,
48+
);
49+
50+
expect(wrapper.render()).toMatchSnapshot();
51+
});
52+
4253
it('set inputValue based on value', () => {
4354
const wrapper = mount(
4455
<Select mode="combobox" value="1">

tests/__snapshots__/Combobox.test.tsx.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Select.Combobox renders controlled correctly 1`] = `
4+
<div
5+
class="rc-select rc-select-single rc-select-show-search"
6+
>
7+
<div
8+
class="rc-select-selector"
9+
>
10+
<span
11+
class="rc-select-selection-search"
12+
>
13+
<input
14+
aria-activedescendant="rc_select_TEST_OR_SSR_list_0"
15+
aria-autocomplete="list"
16+
aria-controls="rc_select_TEST_OR_SSR_list"
17+
aria-haspopup="listbox"
18+
aria-owns="rc_select_TEST_OR_SSR_list"
19+
autocomplete="off"
20+
class="rc-select-selection-search-input"
21+
id="rc_select_TEST_OR_SSR"
22+
role="combobox"
23+
type="search"
24+
value=""
25+
/>
26+
</span>
27+
<span
28+
class="rc-select-selection-placeholder"
29+
>
30+
Search
31+
</span>
32+
</div>
33+
</div>
34+
`;
35+
336
exports[`Select.Combobox renders correctly 1`] = `
437
<div
538
class="rc-select rc-select-single rc-select-show-search"

0 commit comments

Comments
 (0)