Skip to content

Commit cd121cd

Browse files
authored
Fix picker value issue (#1425)
* Fix how we handle internal picker value change for custom picker in multi mode * Fix issues with picker value updates
1 parent 074ee03 commit cd121cd

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/components/picker/index.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,22 @@ class Picker extends Component {
182182
}
183183

184184
static getDerivedStateFromProps(nextProps, prevState) {
185-
if (!_.isEmpty(nextProps.value) && prevState.value !== nextProps.value) {
186-
if (prevState.prevValue !== prevState.value) {
187-
// for this.setState() updates to 'value'
188-
// NOTE: this.setState() already updated the 'value' so here we only updating the 'prevValue'
189-
return {
190-
prevValue: prevState.value
191-
};
192-
} else {
193-
// for prop update to 'value'
194-
return {
195-
value: nextProps.value
196-
};
197-
}
198-
} else if (_.isFunction(nextProps.renderPicker) && prevState.value !== nextProps.value) {
185+
const hasNextValue = !_.isEmpty(nextProps.value) || _.isNumber(nextProps.value);
186+
/* Relevant for keeping the value prop controlled - react when user change value prop */
187+
const externalValueChanged = hasNextValue && prevState.value !== nextProps.value;
188+
/* Relevant for multi select mode when we keep an internal value state */
189+
const internalValueChanged = prevState.value !== prevState.prevValue;
190+
if (internalValueChanged && nextProps.mode === Picker.modes.MULTI) {
191+
/* for this.setState() updates to 'value'
192+
NOTE: this.setState() already updated the 'value' so here we only updating the 'prevValue' */
193+
return {
194+
prevValue: prevState.value
195+
};
196+
} else if (externalValueChanged) {
197+
return {
198+
value: nextProps.value
199+
};
200+
} else if (_.isFunction(nextProps.renderPicker) && externalValueChanged) {
199201
return {
200202
prevValue: prevState.value,
201203
value: nextProps.value

0 commit comments

Comments
 (0)