Skip to content

Commit 854b6ab

Browse files
authored
Merge branch 'master' into github-releases
2 parents c05cea1 + d943481 commit 854b6ab

File tree

6 files changed

+389
-14
lines changed

6 files changed

+389
-14
lines changed

packages/search-field/package.json

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
"author": "Crave Food Systems and AUTHORS",
66
"license": "MIT",
77
"main": "lib/index.js",
8-
"files": [
9-
"AUTHORS",
10-
"lib"
11-
],
8+
"files": ["AUTHORS", "lib"],
129
"publishConfig": {
1310
"access": "public"
1411
},
@@ -24,12 +21,7 @@
2421
"url": "https://github.com/CraveFood/farmblocks/issues"
2522
},
2623
"homepage": "https://github.com/CraveFood/farmblocks#readme",
27-
"keywords": [
28-
"farmblocks",
29-
"react",
30-
"search",
31-
"input"
32-
],
24+
"keywords": ["farmblocks", "react", "search", "input"],
3325
"peerDependencies": {
3426
"prop-types": "^15.6.0",
3527
"react": "^16.0.0",
@@ -43,6 +35,7 @@
4335
"@crave/farmblocks-input-select": "^0.8.2",
4436
"@crave/farmblocks-theme": "^1.6.0",
4537
"lodash.debounce": "^4.0.8",
38+
"lodash.isequal": "^4.5.0",
4639
"react-lifecycles-compat": "^3.0.4",
4740
"recompose": "^0.27.1"
4841
}

packages/search-field/src/SearchField.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { polyfill } from "react-lifecycles-compat";
33
import PropTypes from "prop-types";
44
import { compose } from "recompose";
55
import debounce from "lodash.debounce";
6+
import isEqual from "lodash.isequal";
67
import disabledTooltip, {
78
disabledTooltipProps
89
} from "@crave/farmblocks-hoc-disabled-tooltip";
@@ -28,6 +29,7 @@ class SearchField extends React.Component {
2829
focused: false,
2930
inputValue: "",
3031
lastValue: "",
32+
lastItems: [],
3133
selectedItem: null
3234
};
3335

@@ -64,13 +66,17 @@ class SearchField extends React.Component {
6466
};
6567

6668
static getDerivedStateFromProps = (props, state) => {
67-
if (props.value !== state.lastValue) {
69+
if (
70+
props.value !== state.lastValue ||
71+
!isEqual(props.items, state.lastItems)
72+
) {
6873
const selectedItem =
6974
props.items && props.items.find(item => item.value === props.value);
7075
return {
7176
inputValue: selectedItem ? selectedItem.label : "",
7277
selectedItem,
73-
lastValue: props.value
78+
lastValue: props.value,
79+
lastItems: props.items
7480
};
7581
}
7682
return null;

packages/search-field/src/SearchField.story.js

+41
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,45 @@ storiesOf("Search Field", module)
194194
</div>
195195
</div>
196196
))
197+
)
198+
.add(
199+
"update items",
200+
withInfo()(() => {
201+
class Test extends React.Component {
202+
state = {
203+
items,
204+
value: "2"
205+
};
206+
207+
render() {
208+
return (
209+
<div>
210+
<button
211+
onClick={() => {
212+
this.setState({
213+
value: "444"
214+
});
215+
216+
setTimeout(() => {
217+
this.setState({
218+
items: [{ value: "444", label: "Avocado", image }]
219+
});
220+
}, 1000);
221+
}}
222+
>
223+
Update items
224+
</button>
225+
226+
<SearchField
227+
value={this.state.value}
228+
items={this.state.items}
229+
label="Fruits"
230+
/>
231+
</div>
232+
);
233+
}
234+
}
235+
236+
return <Test />;
237+
})
197238
);

packages/search-field/src/SearchField.test.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,28 @@ describe("SearchField", () => {
140140
test("should return new state if value changes", () => {
141141
const item = { value: "ab", label: "AB" };
142142
const props = { value: "ab", items: [item] };
143-
const state = { lastValue: "a" };
143+
const state = { lastValue: "a", lastItems: [item] };
144+
const result = SearchField.getDerivedStateFromProps(props, state);
145+
146+
expect(result).toEqual({
147+
selectedItem: item,
148+
inputValue: item.label,
149+
lastValue: props.value,
150+
lastItems: props.items
151+
});
152+
});
153+
154+
test("should return new state if items changes", () => {
155+
const item = { value: "ab", label: "AB" };
156+
const props = { value: "ab", items: [item] };
157+
const state = { lastValue: "ab", lastItems: [] };
144158
const result = SearchField.getDerivedStateFromProps(props, state);
145159

146160
expect(result).toEqual({
147161
selectedItem: item,
148162
inputValue: item.label,
149-
lastValue: props.value
163+
lastValue: props.value,
164+
lastItems: props.items
150165
});
151166
});
152167

0 commit comments

Comments
 (0)