Skip to content

Commit

Permalink
[#9712] fix <input type="number" /> value '.98' should not be equal t…
Browse files Browse the repository at this point in the history
…o '0.98'. (#9714) (#9929)

* [#9712] fix <input type="number" /> value ".98" should not be equal to "0.98".

* fix eslint error

* fix label error
  • Loading branch information
flarnie committed Jun 12, 2017
1 parent 3c89893 commit ce3ecfb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const React = window.React;

import Fixture from '../../Fixture';

class NumberInputDecimal extends React.Component {
state = { value: '.98' };
changeValue = () => {
this.setState({
value: '0.98',
});
}
render() {
const {value} = this.state;
return (
<Fixture>
<div>{this.props.children}</div>

<div className="control-box">
<input
type="number"
value={value}
onChange={(e) => {
this.setState({value: e.target.value});
}}
/>
<button onClick={this.changeValue}>change.98 to 0.98</button>
</div>
</Fixture>
);
}
}

export default NumberInputDecimal;
8 changes: 6 additions & 2 deletions src/renderers/dom/client/wrappers/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,12 @@ var ReactDOMInput = {
// Simulate `input.valueAsNumber`. IE9 does not support it
var valueAsNumber = parseFloat(node.value, 10) || 0;

// eslint-disable-next-line
if (value != valueAsNumber) {
if (
// eslint-disable-next-line
value != valueAsNumber ||
// eslint-disable-next-line
(value == valueAsNumber && node.value != value)
) {
// Cast `value` to a string to ensure the value is set correctly. While
// browsers typically do this as necessary, jsdom doesn't.
node.value = '' + value;
Expand Down
17 changes: 17 additions & 0 deletions src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ describe('ReactDOMInput', () => {
});
});

it('does change the string ".98" to "0.98" with no change handler', () => {
class Stub extends React.Component {
state = {
value: '.98',
};
render() {
return <input type="number" value={this.state.value} />;
}
}

var stub = ReactTestUtils.renderIntoDocument(<Stub />);
var node = ReactDOM.findDOMNode(stub);
stub.setState({value: '0.98'});

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

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

0 comments on commit ce3ecfb

Please sign in to comment.