Skip to content
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] Handle Chrome autofill #17436

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/material-ui/src/FormControl/FormControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ const FormControl = React.forwardRef(function FormControl(props, ref) {
};
}

const onFilled = React.useCallback(() => {
setFilled(true);
}, []);

const onEmpty = React.useCallback(() => {
setFilled(false);
}, []);

const childContext = {
adornedStart,
disabled,
Expand All @@ -154,16 +162,8 @@ const FormControl = React.forwardRef(function FormControl(props, ref) {
onBlur: () => {
setFocused(false);
},
onEmpty: () => {
if (filled) {
setFilled(false);
}
},
onFilled: () => {
if (!filled) {
setFilled(true);
}
},
onEmpty,
onFilled,
onFocus: () => {
setFocused(true);
},
Expand Down
32 changes: 27 additions & 5 deletions packages/material-ui/src/InputBase/InputBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ export const styles = theme => {
'&$disabled': {
opacity: 1, // Reset iOS opacity
},
'&:-webkit-autofill': {
animationDuration: '5000s',
animationName: '$auto-fill',
},
},
'@keyframes auto-fill': {
from: {},
},
/* Styles applied to the `input` element if `margin="dense"`. */
inputMarginDense: {
Expand Down Expand Up @@ -241,17 +248,20 @@ const InputBase = React.forwardRef(function InputBase(props, ref) {
}
}, [muiFormControl, disabled, focused, onBlur]);

const onFilled = muiFormControl && muiFormControl.onFilled;
const onEmpty = muiFormControl && muiFormControl.onEmpty;

const checkDirty = React.useCallback(
obj => {
if (isFilled(obj)) {
if (muiFormControl && muiFormControl.onFilled) {
muiFormControl.onFilled();
if (onFilled) {
onFilled();
}
} else if (muiFormControl && muiFormControl.onEmpty) {
muiFormControl.onEmpty();
} else if (onEmpty) {
onEmpty();
}
},
[muiFormControl],
[onFilled, onEmpty],
);

useEnhancedEffect(() => {
Expand Down Expand Up @@ -313,6 +323,12 @@ const InputBase = React.forwardRef(function InputBase(props, ref) {
}
};

// Check the input state on mount, in case it was filled by the user
// or auto filled by the browser before the hydration (for SSR).
React.useEffect(() => {
checkDirty(inputRef.current);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

const handleClick = event => {
if (inputRef.current && event.currentTarget === event.target) {
inputRef.current.focus();
Expand Down Expand Up @@ -356,6 +372,11 @@ const InputBase = React.forwardRef(function InputBase(props, ref) {
};
}

const handleAutoFill = () => {
// Provide a fake value as Chrome might not let you access it for security reasons.
checkDirty({ value: 'x' });
};

return (
<div
className={clsx(
Expand Down Expand Up @@ -401,6 +422,7 @@ const InputBase = React.forwardRef(function InputBase(props, ref) {
defaultValue={defaultValue}
disabled={fcs.disabled}
id={id}
onAnimationStart={handleAutoFill}
name={name}
onBlur={handleBlur}
onChange={handleChange}
Expand Down