Provide a way to handle browser-autocompleted form values on controlled components
When there's a controlled component for form names that the user has saved in their browser (common with username/password fields), the browser will sometimes render the page with values in those fields without firing onChange events. If the user submits the form, the component state does not reflect what is showing to the user.
In experimenting with this, it appears that the data is there on load (tested by logging this.refs.myinput.getDOMNode().value)
This seems to discuss it a bit more: http://stackoverflow.com/a/11710295
atm i am using this https://github.com/tbosch/autofill-event
cc me
(@visionscaper tip: press "Subscribe" on the right column.)
Any updates or suggested best practices on this one? The autofill event polyfill seems like a sledgehammer solution.
Safari (8) does dispatch change events on autofill, but they don't bubble so they don't reach the react handler.
A related discussion on https://github.com/angular/angular.js/issues/1460 was closed, with some suggestions, one of them being making use of https://github.com/tbosch/autofill-event to fire change event on autofill, manually.
I think, we can close here as well.
It would be nice to have the autofill-event as part of React, possible as an addon. This functionality is going to be necessary for virtually everyone using React for form validation. The autofill-event script also adds a dependency for jQuery, which may be undesirable in many cases.
This is a browser bug that effects all browsers a little differently. I think just because angular punted on trying to work around or fix it doesn't mean react should. It's a common thing but I understand if this is a "wontfix".
I'm not super well versed on the various browser's bug trackers, but it would be nice if someone who is could find or open issues for this. I think the react team has more weight then most users would when opening an issue about it. And we could tract the tickets here for the react developers who are interested.
I caught up with the angular thread. Firefox has issues with password fields (https://bugzilla.mozilla.org/show_bug.cgi?id=950510 is still open), no word on safari's bug #, chrome is fixed.
Issues are already being tracked at some places - https://github.com/angular/angular.js/issues/1460#issuecomment-53947546
The Chrome issue is now closed, but it pretty clearly does not work in Chrome 50 with React 0.14.8.
no fix yet?
I feel like this was working for a while and broke again recently?
any update on this issue?
Here an extract of my solution for this issue:
export default class Input extends Component {
static propTypes = {
value: PropTypes.string,
onFieldChange: PropTypes.func,
};
static defaultProps = {
value: '',
}
componentDidMount() {
this._listener = setInterval(() => {
if (!this.input || this._previousValue === this.input.value) {
return;
}
this._previousValue = this.input.value;
const evt = document.createEvent('HTMLEvents');
evt.initEvent('input', true, true);
this.input.dispatchEvent(evt);
}, 20);
}
componentWillUnmount() {
clearInterval(this._listener);
}
refInput = (input) => this.input = input;
render() {
const { label,
value,
onFieldChange,
} = this.props;
this.input = this.input || { value };
return (
<input
value={this.input.value}
onChange={onFieldChange}
ref={this.refInput}
/>
);
}
}
NOTE: value comes from the state and onFieldChange updates the state with the new value
The setInterval code is from the https://github.com/Pephers/react-autofill
Can anyone confirm that this issue was fixed with iOS 10.2? I can't reproduce it now...
I'm still having problems on iOS 10.2 with Chrome version 55.0.2883.79... Contrary to what is described above though, for me the autofilled content flashes up briefly in the form and is then removed again. So it's now consistent with what is stored in the state, however, autofill still doesn't work...
https://github.com/facebook/react/issues/8174
I'm reproducing the same issue encountered by @irisSchaffer. I have an isomorphic application, where I render a static page with react and express, then on the client I export the props and use the same page component.
Even when not handling the password input value through react, the client react component deletes the saved password from the input, even when chrome shows it briefly. When I disable javascript from the browser, the saved password stays in its place.
This issue is visible on Chrome only, using its last version, 58.
Not related to react, the saved password is not accessible through element.value until there's an event fired on the dom, like focus, any key event, etc. Tried simulate an event fire through javascript, with no luck on the fill of the value property.