I'm a little weird, I'm dealing with currency in my application. On the model side, I save the currency as cents before sending it to the server, since I do not want to deal with decimal points on the server side. However, I want the normal currency displayed, not cents.
So, I have this input field, where I take data from dollars and change them to cents:
<input name="balance" type="number" step="0.01" min="0" placeholder="Balance in cents" onChange={this.handleUpdate} value={this.props.user.balance / 100)} />
And when there is a change in the input value, I change it back to cents before sending it upstream:
handleUpdate: function(e) { var value = e.target.value;
This baffles me, so I donβt have to enter a decimal point. "Let me demonstrate:
33 in the input field β becomes 3300 in the parent state β returns as 33 in the prop component - everything is fine
33.3 in the input field β becomes 3330 in the parent state β returns as 33.3 in the prop component - all is good
33. in the input field β becomes 3300 in the parent state β returns as 33 in the prop component - this is the problem
As you can see in case 3, when the user first enters "." . it does not translate to the same number using "."
Since this is a controlled input, there is basically no way to write "."
I tried using an uncontrolled element with defaultValue , but the quantity support is not ready by the time the component is displayed, so it's just empty.
http://jsfiddle.net/fpbhu1hs/
javascript reactjs
Michael
source share