When the type of TextField is set to number and give it a initial value, once the entered number is deleted, the displayed value becomes 0. If you then enter another number, the zero will stay unless the page is refreshed.
I've checked the value received in the onChange function, which doesn't come with the leading zero.
If the 'value' attribute is not set, this issue won't occur.
If the entered number is deleted, it'd be better to not show the default 0 (as seen in Component Demos).
OR
Show the default 0, but remove it if the first digit entered is not 0.
The leading zero won't go away.
This behaviour is okay if the field is used for mobile number.
In the case where it's used for currency or quantity, it could be a bit confusing.
| Tech | Version |
|--------------|---------|
| Material-UI | 1.0.0-beta.12 |
| React | 15.0.0 |
| browser | Chrome Version 60.0.3112.113 |
| etc | |
I can't reproduce the behavior on the live demo. I'm not sure where you issue is coming from. Make sure the behavior isn't the same with a native input and provide a live reproduction example so we can have a look at it. For now. I'm closing. Thanks for your time.
I have same problem with 1.0.0-beta.40
If deposit is 0, it is showing 0, when you start type numbers 0 does not removes.
it is look like this 012345
<FormControl fullWidth={true} required={true}>
<InputLabel htmlFor="deposits">Deposit</InputLabel>
<Input id="deposits" name="deposits" type="number" value={Number(this.state.deposits)} onChange={this.handleDepositAmountChange} />
</FormControl>
Thanks for your time.
@sersart As far as I know, you will have the same behavior with a native input.
Yes, you correct. But in native input you can remove zero, before you typing. In this case we can not remove zero
I think the issue is a combination of converting the value to Number() and (as far as I observed the behavior) onChange is fired for every value change.
According to the HTML5 Spec this is actually the behavior of onInput, whereas onChange should only fire when the Input field loses focus. Would it be possible to have both callback options?
Update: binding onChange to the onInput DOM Event is apparently an inconsistency introduced by React....
Hi! I had the same issue. Managed to sort it out by converting the value from number to string and back.
TextField:
<TextField
...
value={item.value.toString()}
...
/>
Handler:
...
value = parseInt(event.target.value);
...
This will eliminate leading zeros.
That is a general bug in react, as e.g: parseFloat('045') === parseFloat('45')
the dom input value is not updated. See https://github.com/facebook/react/issues/9402
@peterrogov Your solution is amazing! 馃帀
If you have floating numbers, @peterrogov's solution will work, but use Number(event.target.value)
instead of parseInt
Most helpful comment
Hi! I had the same issue. Managed to sort it out by converting the value from number to string and back.
TextField:
Handler:
This will eliminate leading zeros.