-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Input type='number' unexpected transforms #1876
Comments
Related to #1119 |
May be it's convenient to make input behave like |
This will be somewhat solved by #2255 , in the sense you'll be able to opt out of this functionality. I like the fact that it automatically converts to numbers (and if you use typescript, you expect one probably), though having a desynchronized temporary value would be useful. My personal favorite solution would be something like this: const handleChange = (event) => {
const nextValue = props.parse(event.target.value);
setFieldValue(nextValue); // could be NaN or something, so it would hopefully fail to validate()
// it didn't parse correctly, but the user could be fixing it with the next keystroke
setFieldLocalValue(
nextValue === undefined || nextValue === NaN
? event.target.value
: setFieldLocalValue(nextValue);
);
}
// then pass fieldLocalValue to the input |
🐛 Bug report
Current Behavior
Input with
type='number'
do not sync with state. As far as I understand it is React issue: facebook/react#1549. Even when this issue was solved, it brings more problems. You could't input float numbers, because when you try to type","
or"."
after digits it would be parsed as integer:parseFloat("123,")
->123
. If you paste something like"2e2"
it would be parsed as '200'. If you paste"e2"
it becomesNaN
and input would have an empty value.Expected behavior
Synced state and user-friendly input without unexpected transformations
Reproducible example
Try to type
123...
and then click reset button:https://codesandbox.io/s/fragrant-water-edtg2
If you remove
type='number'
, you getparseFloat
problemsSuggested solution(s)
Do not parse integers or parse it on submit (without changing
values
state). The second solution requires storing input types somewhere. I found that formik somehow register fields on mount, maybe it could store input types too.Your environment
The text was updated successfully, but these errors were encountered: