Skip to content

Commit 947185f

Browse files
authored
fix: AutoComplete mode should support value control of searchValue (#444)
* init fix * update test case
1 parent f3f8c42 commit 947185f

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

examples/combobox.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ class Combobox extends React.Component {
7070
<button type="button" onClick={this.toggleDisabled}>
7171
toggle disabled
7272
</button>
73+
<button
74+
type="button"
75+
onClick={() => {
76+
this.setState({ value: '' });
77+
}}
78+
>
79+
reset
80+
</button>
7381
</p>
7482
<div>
7583
<Select
@@ -104,7 +112,11 @@ class Combobox extends React.Component {
104112
mode="combobox"
105113
style={{ width: 200 }}
106114
getInputElement={() => (
107-
<textarea style={{ background: 'red' }} rows={3} ref={this.textareaRef} />
115+
<textarea
116+
style={{ background: 'red' }}
117+
rows={3}
118+
ref={this.textareaRef}
119+
/>
108120
)}
109121
options={[{ value: 'light' }, { value: 'bamboo' }]}
110122
allowClear

src/generate.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,9 @@ export default function generateSelector<
406406
const [activeValue, setActiveValue] = React.useState<string>(null);
407407
const [innerSearchValue, setInnerSearchValue] = React.useState('');
408408
let mergedSearchValue = innerSearchValue;
409-
if (searchValue !== undefined) {
409+
if (mode === 'combobox' && value !== undefined) {
410+
mergedSearchValue = value as string;
411+
} else if (searchValue !== undefined) {
410412
mergedSearchValue = searchValue;
411413
} else if (inputValue) {
412414
mergedSearchValue = inputValue;

tests/Combobox.test.tsx

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import Select, { Option, SelectProps } from '../src';
66
import focusTest from './shared/focusTest';
77
import keyDownTest from './shared/keyDownTest';
88
import openControlledTest from './shared/openControlledTest';
9-
import { expectOpen, toggleOpen, selectItem, injectRunAllTimers } from './utils/common';
9+
import {
10+
expectOpen,
11+
toggleOpen,
12+
selectItem,
13+
injectRunAllTimers,
14+
} from './utils/common';
1015
import allowClearTest from './shared/allowClearTest';
1116
import throwOptionValue from './shared/throwOptionValue';
1217

@@ -50,7 +55,9 @@ describe('Select.Combobox', () => {
5055
);
5156

5257
expect(wrapper.find('input').props().value).toBe('');
53-
expect(wrapper.find('.rc-select-selection-placeholder').text()).toEqual('placeholder');
58+
expect(wrapper.find('.rc-select-selection-placeholder').text()).toEqual(
59+
'placeholder',
60+
);
5461
wrapper.find('input').simulate('change', { target: { value: '1' } });
5562
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy();
5663
expect(wrapper.find('input').props().value).toBe('1');
@@ -139,7 +146,9 @@ describe('Select.Combobox', () => {
139146
};
140147

141148
public render() {
142-
const options = this.state.data.map(item => <Option key={item.key}>{item.label}</Option>);
149+
const options = this.state.data.map(item => (
150+
<Option key={item.key}>{item.label}</Option>
151+
));
143152
return (
144153
<Select
145154
mode="combobox"
@@ -178,7 +187,9 @@ describe('Select.Combobox', () => {
178187
};
179188

180189
public render() {
181-
const options = this.state.data.map(item => <Option key={item.key}>{item.label}</Option>);
190+
const options = this.state.data.map(item => (
191+
<Option key={item.key}>{item.label}</Option>
192+
));
182193
return (
183194
<Select
184195
mode="combobox"
@@ -207,7 +218,13 @@ describe('Select.Combobox', () => {
207218
const handleChange = jest.fn();
208219
const handleSelect = jest.fn();
209220
const wrapper = mount(
210-
<Select mode="combobox" backfill open onChange={handleChange} onSelect={handleSelect}>
221+
<Select
222+
mode="combobox"
223+
backfill
224+
open
225+
onChange={handleChange}
226+
onSelect={handleSelect}
227+
>
211228
<Option value="One">One</Option>
212229
<Option value="Two">Two</Option>
213230
</Select>,
@@ -220,8 +237,14 @@ describe('Select.Combobox', () => {
220237

221238
input.simulate('keyDown', { which: KeyCode.ENTER });
222239
expect(wrapper.find('input').props().value).toEqual('One');
223-
expect(handleChange).toHaveBeenCalledWith('One', expect.objectContaining({ value: 'One' }));
224-
expect(handleSelect).toHaveBeenCalledWith('One', expect.objectContaining({ value: 'One' }));
240+
expect(handleChange).toHaveBeenCalledWith(
241+
'One',
242+
expect.objectContaining({ value: 'One' }),
243+
);
244+
expect(handleSelect).toHaveBeenCalledWith(
245+
'One',
246+
expect.objectContaining({ value: 'One' }),
247+
);
225248
});
226249

227250
it("should hide clear icon when value is ''", () => {
@@ -311,7 +334,11 @@ describe('Select.Combobox', () => {
311334
jest.useFakeTimers();
312335
const onDropdownVisibleChange = jest.fn();
313336
const wrapper = mount(
314-
<Select mode="combobox" open onDropdownVisibleChange={onDropdownVisibleChange}>
337+
<Select
338+
mode="combobox"
339+
open
340+
onDropdownVisibleChange={onDropdownVisibleChange}
341+
>
315342
<Option value="One">One</Option>
316343
<Option value="Two">Two</Option>
317344
</Select>,
@@ -324,4 +351,22 @@ describe('Select.Combobox', () => {
324351
expect(onDropdownVisibleChange).toHaveBeenCalledWith(false);
325352
jest.useRealTimers();
326353
});
354+
355+
it('should reset value by control', () => {
356+
const onChange = jest.fn();
357+
const wrapper = mount(
358+
<Select mode="combobox" value="" onChange={onChange}>
359+
<Option value="One">One</Option>
360+
<Option value="Two">Two</Option>
361+
</Select>,
362+
);
363+
364+
toggleOpen(wrapper);
365+
selectItem(wrapper);
366+
expect(onChange).toHaveBeenCalled();
367+
wrapper.update();
368+
expectOpen(wrapper, false);
369+
370+
expect(wrapper.find('input').props().value).toEqual('');
371+
});
327372
});

0 commit comments

Comments
 (0)