Skip to content

Commit

Permalink
Avoid validation warning when inputs change type
Browse files Browse the repository at this point in the history
For controlled inputs, `updateWrapper` was getting called before the
`type` prop had a chance to update. This could lead to a case where
switching from the `text` to `number` type caused a validation error
that would prevent the proper input value from being assigned.

This commit moves the call to `ReactDOMInput.updateWrapper` below
`_updateProperties` to avoid this situation.
  • Loading branch information
nhunzaker committed Jul 22, 2016
1 parent 3fd5826 commit 16dd9e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,26 @@ describe('ReactDOMInput', function() {
);
expect(input.value).toBe('hi');
});

it('does not raise a validation warning when it switches types', function() {
var Input = React.createClass({
getInitialState() {
return { type: 'number', value: 1000 };
},
render() {
var { value, type } = this.state;
return (<input onChange={() => {}} type={type} value={value} />);
},
});

var input = ReactTestUtils.renderIntoDocument(<Input />);
var node = ReactDOM.findDOMNode(input);

// If the value is not set until after the type is updated,
// it will not set the value.
input.setState({ type: 'text', value: 'Test' });

expect(node.value).toEqual('Test');
});

});
8 changes: 7 additions & 1 deletion src/renderers/dom/shared/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,6 @@ ReactDOMComponent.Mixin = {
nextProps = ReactDOMButton.getHostProps(this, nextProps);
break;
case 'input':
ReactDOMInput.updateWrapper(this);
lastProps = ReactDOMInput.getHostProps(this, lastProps);
nextProps = ReactDOMInput.getHostProps(this, nextProps);
break;
Expand Down Expand Up @@ -935,6 +934,13 @@ ReactDOMComponent.Mixin = {
context
);

// Update the wrappers around inputs *after* updating props. This has to
// to happen after `_updateDOMProperties`. Otherwise HTML5 input validations
// raise warnings.
if (this._tag === 'input') {
ReactDOMInput.updateWrapper(this);
}

if (this._tag === 'select') {
// <select> value update needs to occur after <option> children
// reconciliation
Expand Down

0 comments on commit 16dd9e1

Please sign in to comment.