-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[TextField] Styling bug when controlled component uses autoFill #36448
Comments
I manually search through Mui versions to find that this bug appears to have been introduced between 5.10.11 and 5.10.12 |
I couldn't reproduce this issue on Firefox, mostly because I'm struggling with it to get the autofill to work. |
Thanks for reporting this. I was able to reproduce the issue using the Material UI version you specified. |
Thanks to this comment facebook/react#1159 (comment), I temporarily solved the problem and it may be helpful to you. const [wasInitiallyAutofilled, setWasInitiallyAutofilled] = useState(false)
useLayoutEffect(
() => {
/**
* You can access chrome://flags/#unsafely-treat-insecure-origin-as-secure in your navigation bar
* and add your domain there for enable autofill in local development
*/
let autofilled = false
const checkAutofill = () => {
if (autofilled) return
const inputElements = document.getElementsByTagName('input')
if (!inputElements) return
for (let i = 0; i < inputElements.length; i++) {
const input = inputElements[i]
const isAutofilled = input.matches('*:-webkit-autofill')
if (isAutofilled) {
autofilled = true
break
}
}
setWasInitiallyAutofilled(autofilled)
}
// The time when it's ready is not very stable, so check few times
setTimeout(checkAutofill, 500)
setTimeout(checkAutofill, 1000)
setTimeout(checkAutofill, 2000)
},
[],
)
<Form>
<FormTextField
name="email"
focused={wasInitiallyAutofilled ? true : undefined}
...
/>
<FormPassword
name="password"
focused={wasInitiallyAutofilled ? true : undefined}
...
/>
</Form> |
Not great, but a slightly better work-around than above, IMO:
|
Hi guys. I am new to this, but what I did find when I was busy with a form is that if there is a specific input that needs to be evaluated even when auto filled, and you can't find any solution yet, useEffect can help. When that specific value changes, whether filled or auto filled, it will get triggered. |
This is happening to me and the @scarabaeus solution did not work unfortunately, but clearly, we have this bug in MUI 5 in our project, for those who have the auto-fill activated and only access the page that already fills all available fields. |
Hi Ruffeng. I am not sure if you found any other solution yet... Check this out and let me know if this helps. ` function itterateInputs() { function updateInputClass(inputName) { |
You can also remove the error part in the code that I sent. I have found that the following code has been working for me, to add the "error-input" class to the input. |
Is there an ETA on this , we are facing the same issue and will be a huge timesaver for us to have this fixed. |
@DiegoAndai, I'm assigning this to you so you can prioritize it. |
Looked into this issue today. Bug explanationThere are two things that together cause the behavior:
Because said effect runs after SolutionsThere's an immediate solution which is this change: Click to toggle changediff --git a/packages/mui-material/src/FormControl/FormControl.js b/packages/mui-material/src/FormControl/FormControl.js
index e9d5d01b77..590e7eaf18 100644
--- a/packages/mui-material/src/FormControl/FormControl.js
+++ b/packages/mui-material/src/FormControl/FormControl.js
@@ -192,6 +192,14 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
};
}
+ const onFilled = React.useCallback(() => {
+ setFilled(true);
+ }, []);
+
+ const onEmpty = React.useCallback(() => {
+ setFilled(false);
+ }, []);
+
const childContext = React.useMemo(() => {
return {
adornedStart,
@@ -207,12 +215,8 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
onBlur: () => {
setFocused(false);
},
- onEmpty: () => {
- setFilled(false);
- },
- onFilled: () => {
- setFilled(true);
- },
+ onEmpty,
+ onFilled,
onFocus: () => {
setFocused(true);
},
@@ -229,6 +233,8 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
focused,
fullWidth,
hiddenLabel,
+ onEmpty,
+ onFilled,
registerEffect,
required,
size, But I think the correct solution will be to eventually refactor the autofill style implementation to rely on the So I propose going for the temporal solution and creating an issue to implement this refactor in the future. What do you think @aarongarciah? OtherA note on the reproAfter user interaction the text field value shows as <TextField
value={value}
- onChange={setValue}
+ onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
+ setValue(event.target.value);
+ }}
autoComplete="username"
id="outlined-basic"
label="username"
variant="outlined"
/> Thanks for the helpThanks @dwjohnston for pinning the exact version on which this was introduced, it was really helpful 👌🏼. |
@DiegoAndai makes sense, the temporal solution is an improvement already. |
Any updates on this ? |
any information? |
Sorry for the delay, I'll try to get a PR for this next week. |
For the people experiencing this issue, may I ask you to test with #44135 build and check if the issue is fixed? You can do so by doing the following on your project's "@mui/material": "https://pkg.csb.dev/mui/material-ui/commit/6b32256d/@mui/material", |
For me, it solves the issue. However, the input field's value only updates when the Chrome tab is in focus. This means the textField's value is not set until the user clicks on the website. |
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note @dwjohnston How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey. |
The fix has been merged and will be out in the next release (
We tried to fix this in #44135 but ultimately it seems to be a limitation on Chrome in which there's no way of accessing the value before user interaction (see #44135 (comment)). If anyone figures out a way to improve this, I'll gladly review it 😊 |
Steps to reproduce 🕹
Link to live example:
Steps:
3.Reload page - observe the styling issue.
Current behavior 😯
Expected behavior 🤔
Should not have that overlap when autofill is present.
Here is the same app using @mui/[email protected] and the issue is not present:
https://v1kf03.csb.app/?
Context 🔦
I upgraded from 5.9.3 to 5.11.12 and now my login page looks bad.
Your environment 🌎
See code sandbox.
The text was updated successfully, but these errors were encountered: