react icon indicating copy to clipboard operation
react copied to clipboard

Provide a way to handle browser-autocompleted form values on controlled components

Open ericflo opened this issue 12 years ago • 88 comments

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)

ericflo avatar Feb 22 '14 02:02 ericflo

This seems to discuss it a bit more: http://stackoverflow.com/a/11710295

ericflo avatar Feb 22 '14 02:02 ericflo

atm i am using this https://github.com/tbosch/autofill-event

0x6a68 avatar Nov 17 '14 11:11 0x6a68

cc me

visionscaper avatar Apr 01 '15 23:04 visionscaper

(@visionscaper tip: press "Subscribe" on the right column.)

sophiebits avatar Apr 01 '15 23:04 sophiebits

Any updates or suggested best practices on this one? The autofill event polyfill seems like a sledgehammer solution.

uberllama avatar Apr 10 '15 17:04 uberllama

Safari (8) does dispatch change events on autofill, but they don't bubble so they don't reach the react handler.

sfnelson avatar Jul 14 '15 22:07 sfnelson

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.

vipulnsward avatar Oct 12 '15 23:10 vipulnsward

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.

jbaudanza avatar Oct 12 '15 23:10 jbaudanza

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.

reconbot avatar Oct 12 '15 23:10 reconbot

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.

reconbot avatar Oct 12 '15 23:10 reconbot

Issues are already being tracked at some places - https://github.com/angular/angular.js/issues/1460#issuecomment-53947546

vipulnsward avatar Oct 13 '15 00:10 vipulnsward

The Chrome issue is now closed, but it pretty clearly does not work in Chrome 50 with React 0.14.8.

ZebraFlesh avatar May 27 '16 16:05 ZebraFlesh

no fix yet?

devdlabs avatar May 30 '16 06:05 devdlabs

I feel like this was working for a while and broke again recently?

tirdadc avatar Jun 17 '16 22:06 tirdadc

any update on this issue?

okmanideep avatar Oct 17 '16 17:10 okmanideep

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

ghost avatar Dec 01 '16 16:12 ghost

Can anyone confirm that this issue was fixed with iOS 10.2? I can't reproduce it now...

krausest avatar Dec 19 '16 19:12 krausest

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...

irisSchaffer avatar Jan 13 '17 09:01 irisSchaffer

https://github.com/facebook/react/issues/8174

patriciarealini avatar Mar 01 '17 16:03 patriciarealini

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.

juannadin avatar May 09 '17 16:05 juannadin