react icon indicating copy to clipboard operation
react copied to clipboard

input[type='number'] value isn't updated

Open youen123 opened this issue 7 years ago • 11 comments

Do you want to request a feature or report a bug? bug What is the current behavior? when I enter "01" into input[type=number],I set the value to 1, but it doesn't work. It still show "01"

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

https://codesandbox.io/s/20ywk1x71n

What is the expected behavior? when I enter "01", it should show "1"

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? 15.6.2. I think it should update in "updatewrapper" in https://github.com/facebook/react/blob/master/packages/react-dom/src/client/ReactDOMInput.js.

youen123 avatar Sep 14 '18 13:09 youen123

@nhunzaker What are your thoughts on this? I'm not actually sure this is a bug.

gaearon avatar Sep 14 '18 14:09 gaearon

I guess this is the default behaviour of the input element, not related to React, properly.

https://www.w3.org/TR/2010/WD-html-markup-20101019/input.number.html#input.number.attrs.value

oirodolfo avatar Sep 14 '18 14:09 oirodolfo

Assuming I'm thinking about this correctly, it looks like the issue is that the value attribute is reporting as "01" when it should be aligned with state.

This is happening because the value attribute gets synced based on node.value here:

https://github.com/facebook/react/blob/8bc0bcabe7505a991e9e40a4b8ad1d3eb9b5723f/packages/react-dom/src/events/ChangeEventPlugin.js#L242-L245

I think this is a bug. To fix it, we'd need to figure out the best way to get props.value at this point. We'd also need to make sure it worked if the value was set in an event callback on blur.

nhunzaker avatar Sep 14 '18 14:09 nhunzaker

If this is in regard to the value property (what the user sees when they type "01"):

React does not strictly modify the value property on number inputs because it's tricky. User input can be in progress, and different character strings can represent the same value, or be important as a user is typing. It's too easy for React to accidentally take a way a user's input or change it in an unexpected way.

For example:

  1. A user starts to type 1.001
  2. They make it to 1.
  3. The user types 0
  4. Converting "1.0" to a number turns it into "1"
  5. The user's input is lost as the input reverts back to "1"

nhunzaker avatar Sep 14 '18 14:09 nhunzaker

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

stale[bot] avatar Jan 10 '20 11:01 stale[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

stale[bot] avatar Jan 19 '20 01:01 stale[bot]

I guess this is the default behaviour of the input element, not related to React, properly.

https://www.w3.org/TR/2010/WD-html-markup-20101019/input.number.html#input.number.attrs.value

I've just run into this problem. Can you clarify to me why this works with native js and HTML? https://codesandbox.io/s/ecstatic-rosalind-h3u09

mallchel avatar Nov 14 '20 18:11 mallchel

Worth to notice that I can around this weird situation by wrapping parseInt(e.target.value, 10) with String(), and then 01 will update on 1 https://codesandbox.io/s/magical-snow-eih6o

mallchel avatar Nov 14 '20 18:11 mallchel

@gaearon I think this issue is same with this issue. please close this issue.

khg0712 avatar Sep 21 '21 13:09 khg0712

This works fine with input[type='text'] but not with input[type='number']. However once unfocused from input number field the value attribute gets updated. (You can inspect this in browser dev tools) . For text fields, this value attribute gets updated immediately

asankasira avatar Oct 26 '23 02:10 asankasira

Experiencing the same/similar issue. A controlled number input produced unexpected behavior.

For example: <input type="number" value={3} />

Typing any non-zero number in the input produces no change, as expected from a controlled input. Typing leading zeros changes the input. Was really confused at first since I assumed controlled inputs can't be changed if the value state is not updating.

Connorelsea avatar Apr 26 '24 18:04 Connorelsea