@@ -115,6 +115,13 @@ export const styles = theme => {
115
115
'&$disabled' : {
116
116
opacity : 1 , // Reset iOS opacity
117
117
} ,
118
+ '&:-webkit-autofill' : {
119
+ animationDuration : '5000s' ,
120
+ animationName : '$auto-fill' ,
121
+ } ,
122
+ } ,
123
+ '@keyframes auto-fill' : {
124
+ from : { } ,
118
125
} ,
119
126
/* Styles applied to the `input` element if `margin="dense"`. */
120
127
inputMarginDense : {
@@ -241,17 +248,20 @@ const InputBase = React.forwardRef(function InputBase(props, ref) {
241
248
}
242
249
} , [ muiFormControl , disabled , focused , onBlur ] ) ;
243
250
251
+ const onFilled = muiFormControl && muiFormControl . onFilled ;
252
+ const onEmpty = muiFormControl && muiFormControl . onEmpty ;
253
+
244
254
const checkDirty = React . useCallback (
245
255
obj => {
246
256
if ( isFilled ( obj ) ) {
247
- if ( muiFormControl && muiFormControl . onFilled ) {
248
- muiFormControl . onFilled ( ) ;
257
+ if ( onFilled ) {
258
+ onFilled ( ) ;
249
259
}
250
- } else if ( muiFormControl && muiFormControl . onEmpty ) {
251
- muiFormControl . onEmpty ( ) ;
260
+ } else if ( onEmpty ) {
261
+ onEmpty ( ) ;
252
262
}
253
263
} ,
254
- [ muiFormControl ] ,
264
+ [ onFilled , onEmpty ] ,
255
265
) ;
256
266
257
267
useEnhancedEffect ( ( ) => {
@@ -313,6 +323,12 @@ const InputBase = React.forwardRef(function InputBase(props, ref) {
313
323
}
314
324
} ;
315
325
326
+ // Check the input state on mount, in case it was filled by the user
327
+ // or auto filled by the browser before the hydration (for SSR).
328
+ React . useEffect ( ( ) => {
329
+ checkDirty ( inputRef . current ) ;
330
+ } , [ ] ) ; // eslint-disable-line react-hooks/exhaustive-deps
331
+
316
332
const handleClick = event => {
317
333
if ( inputRef . current && event . currentTarget === event . target ) {
318
334
inputRef . current . focus ( ) ;
@@ -356,6 +372,11 @@ const InputBase = React.forwardRef(function InputBase(props, ref) {
356
372
} ;
357
373
}
358
374
375
+ const handleAutoFill = ( ) => {
376
+ // Provide a fake value as Chrome might not let you access it for security reasons.
377
+ checkDirty ( { value : 'x' } ) ;
378
+ } ;
379
+
359
380
return (
360
381
< div
361
382
className = { clsx (
@@ -401,6 +422,7 @@ const InputBase = React.forwardRef(function InputBase(props, ref) {
401
422
defaultValue = { defaultValue }
402
423
disabled = { fcs . disabled }
403
424
id = { id }
425
+ onAnimationStart = { handleAutoFill }
404
426
name = { name }
405
427
onBlur = { handleBlur }
406
428
onChange = { handleChange }
0 commit comments