-
Notifications
You must be signed in to change notification settings - Fork 47k
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 'email' as controlled component does not control white spaces #6368
Comments
Would you mind creating a JSFiddle reproducing the issue? It’s preferable that you use React 15 RC2 to verify it happens with the latest code. Thank you for reporting! |
Hey @marcolanaro @gaearon I put together a JSFiddle attempting to reproduce what you're describing with If there is no
|
@gaearon @aweary Our goal is to validate the email field striping out the spaces as soon as the user is typing. You'll see that the two controlled input fields behave differently if you type spaces. |
@marcolanaro I see. It seems like both examples demonstrate a root issue with how |
I tested the fiddle in Firefox 45.0.1 and both controlled inputs behave correctly (but it seems like due to a bug in Mozilla); Mozilla does not trim the This issue is similar to redux-form/redux-form#417. |
I tested it with chrome 49 and the email input does not behave correctly. |
Just to clarify - is this a new issue with 15 or was this happening in 14 as well? Edit: I tried myself - looks like this isn't new to 15 so we'll have to come back to this and see if there's anything we can possibly do after we ship. Thanks for filing and the repro! |
This type of thing is literally the worst. Engineers try to be "helpful" by automatically trimming the value, and end up making life more difficult. This is why code should only ever do the trivially simple thing and just pass values along. Ironically, if we fired an onChange when the user hit the space bar, all controlled email inputs would necessarily strip spaces (even if that wasn't the desired behavior). I suppose we could try to track keystrokes and derive our own backing state, but @sebmarkbage said that supporting i18n makes the custom handling of text input incredibly difficult (borderline impossible) to get right. I'm almost tempted to say this is a "won't fix", unless someone has an idea for what we could reasonably do in this situation. |
This type of things is not something that the engineer does because he likes to push the library to the edge; it's a business requirement to decrease friction and increase conversion. The fact that all this things are working with the only exception of the spaces on an email input field let me think that is a gotcha in the library. |
The browser DOM node returns a value that does not accurately reflect what the user sees. That's what I was complaining about. It's a gotcha in the spec that propagates to become a gotcha in the browser that propagates to become a gotcha in the library. @marcolanaro When I said "Engineers trying to be 'helpful'", I was referring to the engineers who wrote the W3C spec, not to you and other users of React. My commentary was intended to apply to infrastructure code (browsers/react/etc) and not to application code. Sorry for the ambiguity. |
Circling back to this after 2 years, it seems like the root of the problem remains that the input value reports with trimmed spaces, without any way to detect additional white space. As far as I see it, this is out of our control, and an implementation that tracked user input through a sophisticated key-press system feels fraught with edge cases. I'm going to close this out, but let's keep the conversation going if a new idea for how to approach this comes up. |
Same problem as nhunzaker in 2019. On Quick fix for me was adding
|
Anyone have a fix for this issue. I'm still having this problem! |
Thank you very much @jon617 found your solution really helpful |
…table working with type 'email'. Bugs in react: facebook/react#14168 facebook/react#6368
A controlled input filed of type email behave differently from a controlled field of type text.
If you manage a controlled input field of type email, the actual state and the rendered DOM are different if the user digit spaces, e.g. ' '.
With an input element like this:
<input type="text" value={'stringFromTheState'} />
if you try to digit letters or spaces, you will always get rendered the string 'stringFromTheState'.
With an input element like this:
<input type="email" value={'stringFromTheState'} />
if you try to digit letters you will always get rendered the string 'stringFromTheState'.
if you try to digit spaces you will get a new rendered string that compose the state with the spaces.
This is problematic is before saving the state you need to validate the field and force it to do something smart like strip the white spaces.
The text was updated successfully, but these errors were encountered: