-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SelectCheckbox] fix onChange behavior #1414
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great comments, and code cleaning.
Overall, I don't like the fact we are using the selectedValues of the state, instead of the props even in the case of legacyOnChangeFalse.
Else, there are some minor corrections to do
if (this.props.onChange) { | ||
this.props.onChange(key, newStatus); | ||
return; | ||
} | ||
const selectedValues = this.state.selectedValues; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugly, this is modifying the state directly.
This should be something like
const selectedValues = newStatus ? this.state.selectedValues.concat([key]) : this.state.selectedValues.filter(elt => elt !== key);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I didn't push enough the upgrade
this.setState({value: selectedValues}); | ||
|
||
if (this.props.onChange) { | ||
if (this.props.legacyOnChange) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid nested if : it is not clear
I think in this case it is better with
if(this.props.onChange && this.props.legacyOnChange) {
this.props.onChange(key, newStatus);
} else if(this.props.onChange) {
this.props.onChange(selectedValues);
} else {
this.setState({ value: selectedValues });
}
@c3dr0x Maybe one day we'll clean this up, so the state is not used if value is taken from props, and onChange too |
Fix issue #1412
Current behaviour
onChange
is called with two parameters :key
andnewStatus
New behaviour
onChange
is now called by default with the newly selected values as an array of keylegacyOnChange
is available to get the old behaviorCaution
The
field-component-behaviour._wrappedOnChange
method will truncate the second parameter. Therefore legacyonChange
will not work when inside afieldFor