react: Clear button on iOS date input does not return correct event value
Bug
For iOS only.
When pressing clear on a date input, the onChange event is fired but event.target.value is showing the original value rather than an empty string.
What is the current behavior?
On Chrome and Android, when the clear button is pressed the onChange event has a value of ''.
On iOS when the clear button is pressed the onChange event has a value of previousValue.
Demo https://output.jsbin.com/zojuteloto/5/ Try on Chrome/Android. Then on iOS.
What is the expected behavior? Value should be returned an an empty string.
Versions Affects React 15+ & iOS 10. Unsure of previous versions.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 4
- Comments: 20 (9 by maintainers)
The workarounds listed so far didn’t work for me. The following worked perfectly though on
iOS 11.2.2, no ref needed -Here is another solution to this issue in case someone needs it.
https://codepen.io/joseplatas/pen/yWQYeq
I’m still encountering this. Tapping on
cleardoes not clear the field, nor restore it to itsdefaultValue.Took a quick pass using the core mechanic behind~~http://codepen.io/nhunzaker/pen/dNqvyM~~inputValueTracking:Seems to work fine. I’ll work my way out from there.☝️ Sorry, bit of user error… Going to keep digging.
Purely speculative, but I wonder if
clearis restoring the input to itsdefaultValue, which is stored under thevalueattribute on controlled fields, much likeform.reset()(which React controlled inputs don’t support).Happy to triage this, @aweary.
Hitting Clear will indeed revert to the
defaultValueDOM prop (representing the HTML attribute value) which works as expected for uncontrolled inputs. For controlled inputs, as React will update bothvalueanddefaultValueDOM props to the new value on every change, hitting Clear will have no noticeable effect. Setting thedefaultValueDOM prop to the empty string manually after each change fixes this.I was experimenting with the solution of @joseplatas above for a while, but couldn’t get the refs part working. Then I figured that making Clear work on first click can be done if I can clear defaultValue at the time of focusing the input, so attaching a callback to
onFocuswas all I needed.Without React: http://codepen.io/nhunzaker/pen/VPGPzw
Seems to pick it up no problem. iOS 10 7+ emulator: