@@ -14,15 +14,17 @@ import {
1414 ElementRef ,
1515 Inject ,
1616 Input ,
17+ NgZone ,
1718 OnChanges ,
1819 OnDestroy ,
1920 Optional ,
2021 Self ,
2122} from '@angular/core' ;
2223import { FormGroupDirective , NgControl , NgForm } from '@angular/forms' ;
23- import { ErrorStateMatcher , mixinErrorState , CanUpdateErrorState } from '@angular/material/core' ;
24+ import { CanUpdateErrorState , ErrorStateMatcher , mixinErrorState } from '@angular/material/core' ;
2425import { MatFormFieldControl } from '@angular/material/form-field' ;
2526import { Subject } from 'rxjs/Subject' ;
27+ import { AutofillMonitor } from './autofill' ;
2628import { getMatInputUnsupportedTypeError } from './input-errors' ;
2729import { MAT_INPUT_VALUE_ACCESSOR } from './input-value-accessor' ;
2830
@@ -58,6 +60,9 @@ export const _MatInputMixinBase = mixinErrorState(MatInputBase);
5860 selector : `input[matInput], textarea[matInput]` ,
5961 exportAs : 'matInput' ,
6062 host : {
63+ /**
64+ * @deletion -target 7.0.0 remove .mat-form-field-autofill-control in favor of AutofillMonitor.
65+ */
6166 'class' : 'mat-input-element mat-form-field-autofill-control' ,
6267 '[class.mat-input-server]' : '_isServer' ,
6368 // Native input properties that are overwritten by Angular inputs need to be synced with
@@ -105,6 +110,12 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
105110 */
106111 controlType : string = 'mat-input' ;
107112
113+ /**
114+ * Implemented as part of MatFormFieldControl.
115+ * @docs -private
116+ */
117+ autofilled = false ;
118+
108119 /**
109120 * Implemented as part of MatFormFieldControl.
110121 * @docs -private
@@ -206,7 +217,9 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
206217 @Optional ( ) _parentForm : NgForm ,
207218 @Optional ( ) _parentFormGroup : FormGroupDirective ,
208219 _defaultErrorStateMatcher : ErrorStateMatcher ,
209- @Optional ( ) @Self ( ) @Inject ( MAT_INPUT_VALUE_ACCESSOR ) inputValueAccessor : any ) {
220+ @Optional ( ) @Self ( ) @Inject ( MAT_INPUT_VALUE_ACCESSOR ) inputValueAccessor : any ,
221+ private _autofillMonitor : AutofillMonitor ,
222+ ngZone : NgZone ) {
210223 super ( _defaultErrorStateMatcher , _parentForm , _parentFormGroup , ngControl ) ;
211224 // If no input value accessor was explicitly specified, use the element as the input value
212225 // accessor.
@@ -233,6 +246,12 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
233246 } ) ;
234247 }
235248
249+ this . _autofillMonitor . monitor ( this . _elementRef . nativeElement )
250+ . subscribe ( event => ngZone . run ( ( ) => {
251+ this . autofilled = event . isAutofilled ;
252+ this . stateChanges . next ( ) ;
253+ } ) ) ;
254+
236255 this . _isServer = ! this . _platform . isBrowser ;
237256 }
238257
@@ -242,6 +261,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
242261
243262 ngOnDestroy ( ) {
244263 this . stateChanges . complete ( ) ;
264+ this . _autofillMonitor . stopMonitoring ( this . _elementRef . nativeElement ) ;
245265 }
246266
247267 ngDoCheck ( ) {
@@ -324,7 +344,8 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
324344 * @docs -private
325345 */
326346 get empty ( ) : boolean {
327- return ! this . _isNeverEmpty ( ) && ! this . _elementRef . nativeElement . value && ! this . _isBadInput ( ) ;
347+ return ! this . _isNeverEmpty ( ) && ! this . _elementRef . nativeElement . value && ! this . _isBadInput ( ) &&
348+ ! this . autofilled ;
328349 }
329350
330351 /**
0 commit comments