Skip to content
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

Replace string ref usage with callback refs #170

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion es/ReactTelephoneInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ export var ReactTelephoneInput = createReactClass({
return !isEqual(nextProps, this.props) || !isEqual(nextState, this.state);
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
this.setState(this._mapPropsToState(nextProps));
var prevFormattedNumber = this.state.formattedNumber;
this.setState(this._mapPropsToState(nextProps), function () {
// If formatted number has changed, call on change
if (this.state.formattedNumber !== prevFormattedNumber) {
this.props.onChange(this.state.formattedNumber, this.state.selectedCountry);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this component maintains its own state, if there are changes to the value of the telephone field that were caused by the incoming props these changes will be applied in _mapPropsToState. Prior to this fix, such a change in the state will not call onChange. This fixes it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukeshsoni any objections to this change? Perhaps this was suggested in the past?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

componentWillReceiveProps is doing a lot in this component. I shouldn't have done this in the first place. This piece of code has been the source of a lot of bugs in this component. I am thinking about completely removing componentWillReceiveProps and give an alternate solution making it a controlled/uncontrolled/(controlled + uncontrolled) component

});
},
componentWillUnmount: function componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeydown);
Expand Down
8 changes: 7 additions & 1 deletion lib/ReactTelephoneInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@ var ReactTelephoneInput = exports.ReactTelephoneInput = createReactClass({
return !isEqual(nextProps, this.props) || !isEqual(nextState, this.state);
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
this.setState(this._mapPropsToState(nextProps));
var prevFormattedNumber = this.state.formattedNumber;
this.setState(this._mapPropsToState(nextProps), function () {
// If formatted number has changed, call on change
if (this.state.formattedNumber !== prevFormattedNumber) {
this.props.onChange(this.state.formattedNumber, this.state.selectedCountry);
}
});
},
componentWillUnmount: function componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeydown);
Expand Down
8 changes: 7 additions & 1 deletion src/ReactTelephoneInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ export var ReactTelephoneInput = createReactClass({
)
},
componentWillReceiveProps(nextProps) {
this.setState(this._mapPropsToState(nextProps))
const prevFormattedNumber = this.state.formattedNumber;
this.setState(this._mapPropsToState(nextProps), function() {
// If formatted number has changed, call on change
if (this.state.formattedNumber !== prevFormattedNumber) {
this.props.onChange(this.state.formattedNumber, this.state.selectedCountry)
}
})
},
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeydown)
Expand Down