Skip to content

Commit 6ebc9ff

Browse files
authored
fix: SearchInput doesn't updated when searchList property change (#1058)
1 parent 882205e commit 6ebc9ff

File tree

2 files changed

+69
-29
lines changed

2 files changed

+69
-29
lines changed

src/SearchInput/SearchInput.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ import InputGroup from '../InputGroup/InputGroup';
77
import Menu from '../Menu/Menu';
88
import Popover from '../Popover/Popover';
99
import PropTypes from 'prop-types';
10-
import React, { Component } from 'react';
10+
import React, { PureComponent } from 'react';
1111

12-
class SearchInput extends Component {
12+
class SearchInput extends PureComponent {
1313
constructor(props) {
1414
super(props);
1515
this.state = {
1616
isExpanded: false,
1717
searchExpanded: false,
18-
value: props.inputProps?.value ? props.inputProps.value : '',
19-
searchList: props.searchList,
20-
filteredResult: props.inputProps?.value ? this.filterList(props.searchList, props.inputProps.value) : props.searchList
18+
value: props.inputProps?.value ? props.inputProps.value : ''
2119
};
2220
}
2321

@@ -32,26 +30,24 @@ class SearchInput extends Component {
3230
};
3331

3432
handleListItemClick = (event, item) => {
35-
this.setState((prevState) => ({
33+
this.setState({
3634
value: item.text,
3735
isExpanded: false,
38-
searchExpanded: false,
39-
filteredResult: this.filterList(prevState.searchList, item.text)
40-
}), () => {
36+
searchExpanded: false
37+
}, () => {
4138
item?.callback();
4239
this.props.onSelect(event, item);
4340
});
4441
};
4542

4643
handleChange = event => {
4744
let filteredResult;
48-
if (this.state.searchList) {
49-
filteredResult = this.filterList(this.state.searchList, event.target.value);
45+
if (this.props.searchList) {
46+
filteredResult = this.filterList(this.props.searchList, event.target.value);
5047
}
5148
this.setState({
5249
value: event.target.value,
53-
isExpanded: true,
54-
filteredResult: filteredResult
50+
isExpanded: true
5551
}, () => {
5652
this.props.onChange(event, filteredResult);
5753
});
@@ -90,9 +86,7 @@ class SearchInput extends Component {
9086
this.setState({
9187
isExpanded: false,
9288
searchExpanded: false,
93-
value: '',
94-
searchList: this.props.searchList,
95-
filteredResult: this.props.searchList
89+
value: ''
9690
});
9791
}
9892
};
@@ -135,12 +129,12 @@ class SearchInput extends Component {
135129
[`is-${validationState?.state}`]: validationState?.state
136130
}
137131
) : inputGroupClasses;
138-
132+
let filteredResult = this.state.value && this.props.searchList ? this.filterList(this.props.searchList, this.state.value) : this.props.searchList;
139133
const popoverBody = (
140134
<Menu disableStyles={disableStyles}>
141135
<Menu.List {...listProps}>
142-
{this.state.filteredResult && this.state.filteredResult.length > 0 ? (
143-
this.state.filteredResult.map((item, index) => {
136+
{filteredResult && filteredResult.length > 0 ? (
137+
filteredResult.map((item, index) => {
144138
return (
145139
<Menu.Item
146140
key={index}
@@ -166,11 +160,11 @@ class SearchInput extends Component {
166160
body={
167161
(<>
168162
{validationState &&
169-
<FormMessage
170-
disableStyles={disableStyles}
171-
type={validationState.state}>
172-
{validationState.text}
173-
</FormMessage>
163+
<FormMessage
164+
disableStyles={disableStyles}
165+
type={validationState.state}>
166+
{validationState.text}
167+
</FormMessage>
174168
}
175169
{popoverBody}
176170
</>)}

src/SearchInput/SearchInput.test.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ describe('<SearchInput />', () => {
2121
{ text: 'orange', callback: jest.fn() }
2222
];
2323

24+
const searchDataNew = [
25+
{ text: 'peaches', callback: jest.fn() },
26+
{ text: 'pear', callback: jest.fn() }
27+
];
28+
2429
const defaultSearchInput = (
2530
<SearchInput
2631
className='blue'
@@ -54,11 +59,7 @@ describe('<SearchInput />', () => {
5459

5560
expect(wrapper.state(['value'])).toBe(searchData[0].text);
5661
expect(wrapper.state(['isExpanded'])).toBe(true);
57-
expect(wrapper.state(['filteredResult'])).toEqual(
58-
expect.arrayContaining([
59-
expect.objectContaining({ text: searchData[0].text })
60-
])
61-
);
62+
6263
});
6364

6465
test('should dispatch the onChange callback with the event', () => {
@@ -233,5 +234,50 @@ describe('<SearchInput />', () => {
233234
).toBe('Sample');
234235

235236
});
237+
238+
test('should allow props list to be changed after creation', () => {
239+
let ref;
240+
class Test extends React.Component {
241+
constructor(props) {
242+
super(props);
243+
ref = React.createRef();
244+
this.state = {
245+
list: searchData
246+
};
247+
}
248+
249+
handleChange = () => {
250+
if (ref.current.value === 'pe') {
251+
this.setState({
252+
list: searchDataNew
253+
});
254+
}
255+
}
256+
render = () => (<SearchInput inputProps={{ ref: ref }} onChange={this.handleChange}
257+
searchList={this.state.list} />);
258+
}
259+
const wrapper = mount(<Test />);
260+
261+
wrapper.find('.fd-input').simulate('click');
262+
let rows = wrapper.find('li');
263+
expect(rows).toHaveLength(searchData.length);
264+
265+
wrapper
266+
.find(searchInput)
267+
.simulate('change', { target: { value: 'pe' } });
268+
269+
rows = wrapper.find('li');
270+
271+
expect(rows).toHaveLength(searchDataNew.length);
272+
273+
wrapper
274+
.find(searchInput)
275+
.simulate('change', { target: { value: searchDataNew[0].text } });
276+
277+
rows = wrapper.find('li');
278+
279+
expect(rows).toHaveLength(1);
280+
281+
});
236282
});
237283
});

0 commit comments

Comments
 (0)