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

Fix uncontrolled input decimal point "chopping" on number inputs, and validation warnings on email inputs #7750

Merged
merged 2 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion src/renderers/dom/client/wrappers/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,17 @@ var ReactDOMInput = {
}
} else {
if (props.value == null && props.defaultValue != null) {
node.defaultValue = '' + props.defaultValue;
// In Chrome, assigning defaultValue to certain input types triggers input validation.
// For number inputs, the display value loses trailing decimal points. For email inputs,
// Chrome raises "The specified value <x> is not a valid email address".
//
// Here we check to see if the defaultValue has actually changed, avoiding these problems
// when the user is inputting text
//
// https:/facebook/react/issues/7253
if (node.defaultValue !== '' + props.defaultValue) {
node.defaultValue = '' + props.defaultValue;
}
}
if (props.checked == null && props.defaultChecked != null) {
node.defaultChecked = !!props.defaultChecked;
Expand Down
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 @@ -51,6 +51,28 @@ describe('ReactDOMInput', () => {
expect(node.value).toBe('0');
});

it('only assigns defaultValue if it changes', () => {
class Test extends React.Component {
render() {
return (<input defaultValue="0" />);
}
}

var component = ReactTestUtils.renderIntoDocument(<Test />);
var node = ReactDOM.findDOMNode(component);

Object.defineProperty(node, 'defaultValue', {
get() {
return '0';
},
set(value) {
throw new Error(`defaultValue was assigned ${value}, but it did not change!`);
},
});

component.forceUpdate();
});

it('should display "true" for `defaultValue` of `true`', () => {
var stub = <input type="text" defaultValue={true} />;
stub = ReactTestUtils.renderIntoDocument(stub);
Expand Down