I have a controlled number input in a React app, and when I remove all the digits right of the decimal point, the decimal point itself is removed. So when I have 1.1
, and remove the rightmost 1, it should leave 1.
; but the point is also removed automatically, leaving 1
, and the cursor is moved to the front.
For a minimal example, see this fiddle: https://jsfiddle.net/sashee/e00s7h9d/ . Just press backspace on the input to observe the behavior.
After some googling, I think the root of the problem is that 1.
is treated as 1
, and there is no way to extract the current value from the input.
Therefore, when I remove the rightmost 1, the following happens:
- The change handler is called
- The value is 1
- React updates the value in the state to 1
- The input now has the value of 1, instead of 1.
As I see, this is not a bug, but a weird outcome of the architecture. Are there any workarounds that use number inputs? Or should I fall back to regular text inputs?
Update:
It works fine in Firefox, but not in Chrome.