react icon indicating copy to clipboard operation
react copied to clipboard

Bug: Firefox: Autocomplete/Autofill still not working

Open Hadsch opened this issue 5 years ago • 7 comments

Description, steps to reproduce and sandbox example see #17022! The native autocomplete function from firefox is still not working.

This also affects other frameworks (antd, material UI):

  • https://github.com/mui-org/material-ui/issues/16943
  • https://github.com/ant-design/ant-design/issues/21232

It would be nice if someone of the react community could give it a shot because I think that the native autocomplete is a very important UX feature.

Hadsch avatar May 23 '20 08:05 Hadsch

I've looked into this and it seems to happen because React sets the defaultValue of the input DOM node to the current value for controlled inputs. When you submit something in Firefox where node.value === node.defaultValue it doesn't save it for autofill. It's not documented anywhere, I just found out testing (without React too).

Removing this ""solves"" the issue: https://github.com/facebook/react/blob/1d85bb3ce13401644a5e345f8bc84522c59a951c/packages/react-dom/src/client/ReactDOMInput.js#L420

The reason why React does assign the defaultValue to the current value is because if you have an input with type="reset" and you click it, you'd get blank fields in your controlled inputs if you removed that line (next re-render would fix them though). At least it's what I understand by browsing through the code.

You could make autocomplete/autofill work on Firefox on a controlled input by making defaultValue return an empty string:

  function handleChange(event) {
    Object.defineProperty(event.target, "defaultValue", {
      configurable: true,
      get() { return "" },
      set() {},
    });
    setValue(event.target.value);
  }

This works as long as you don't have a type="reset". Otherwise you'll notice that all fields are reset as blank (next re-render fixes them though). I wouldn't recommend this though, it's a "hack" and I haven't used it anywhere to be able to say that it's 100% reliable.

Anyways, this is probably a bug in Firefox .. or a feature-(bug?) but I haven't found anything in the specs, MDN, or elsewhere that describes why node.value === node.defaultValue should bail out of saving for autofill 🤷‍♀️

I am/was working on a PR to solve this .. but, maybe Firefox could solve it on their side? 🙈 (unclear if it's a bug) It'd be great to know if anyone from React core team is aware of the issue and/or what we could do about it.

Edit: this is a demo that demonstrates the issue in Firefox. It's plain HTML + 3 line JS code: https://codesandbox.io/s/naughty-feather-vwl6v?file=/index.html So it isn't a React issue per se 🤔

eddyw avatar May 30 '20 16:05 eddyw

If anyone interested, I opened this: https://bugzilla.mozilla.org/show_bug.cgi?id=1642570 😅

eddyw avatar Jun 12 '20 15:06 eddyw

I believe this is a duplicate of #15739

mnoorenberghe avatar Jul 21 '20 17:07 mnoorenberghe

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

stale[bot] avatar Dec 25 '20 15:12 stale[bot]

bump

jan-wilhelm avatar Jan 12 '21 18:01 jan-wilhelm

Firefox seems to have fixed this a while ago in FF 105, see https://bugzilla.mozilla.org/show_bug.cgi?id=1642570

Tested it with FF 114 and works just fine 👍

ahoisl avatar Jun 18 '23 20:06 ahoisl

Looks like all these methods with autofill prop are simple ignored by Google Chrome since 2022. I suggest a different method: using readOnly={true} until focus event

Keep in mind that on mobile safari keyboard will not shown if input field change readonly state after focus event, so you need to process onTouchStart too. Heres the link to the source code of usePreventAutofill hook

import { usePreventAutofill } from 'react-declarative';

...

const preventAutofill = usePreventAutofill();

...

return (
  <InputBase

      ...

      {...preventAutofill}
  />   
);
 

tripolskypetr avatar Dec 30 '23 14:12 tripolskypetr

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

github-actions[bot] avatar Apr 10 '24 17:04 github-actions[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

github-actions[bot] avatar Apr 17 '24 18:04 github-actions[bot]