File tree Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -1130,6 +1130,21 @@ describe('MatInput with forms', () => {
1130
1130
expect ( el ) . not . toBeNull ( ) ;
1131
1131
expect ( el . classList . contains ( 'mat-form-field-empty' ) ) . toBe ( false ) ;
1132
1132
} ) ) ;
1133
+
1134
+ it ( 'should update when the form field value is patched without emitting' , fakeAsync ( ( ) => {
1135
+ const fixture = TestBed . createComponent ( MatInputWithFormControl ) ;
1136
+ fixture . detectChanges ( ) ;
1137
+
1138
+ const el = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
1139
+
1140
+ expect ( el . classList ) . toContain ( 'mat-form-field-empty' ) ;
1141
+
1142
+ fixture . componentInstance . formControl . patchValue ( 'value' , { emitEvent : false } ) ;
1143
+ fixture . detectChanges ( ) ;
1144
+
1145
+ expect ( el . classList ) . not . toContain ( 'mat-form-field-empty' ) ;
1146
+ } ) ) ;
1147
+
1133
1148
} ) ;
1134
1149
1135
1150
@Component ( {
@@ -1183,7 +1198,10 @@ class MatInputPlaceholderElementTestComponent {
1183
1198
}
1184
1199
1185
1200
@Component ( {
1186
- template : `<mat-form-field><input matInput [formControl]="formControl"></mat-form-field>`
1201
+ template : `
1202
+ <mat-form-field>
1203
+ <input matInput placeholder="Hello" [formControl]="formControl">
1204
+ </mat-form-field>`
1187
1205
} )
1188
1206
class MatInputWithFormControl {
1189
1207
formControl = new FormControl ( ) ;
Original file line number Diff line number Diff line change @@ -229,11 +229,12 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
229
229
// error triggers that we can't subscribe to (e.g. parent form submissions). This means
230
230
// that whatever logic is in here has to be super lean or we risk destroying the performance.
231
231
this . updateErrorState ( ) ;
232
- } else {
233
- // When the input isn't used together with `@angular/forms`, we need to check manually for
234
- // changes to the native `value` property in order to update the floating label.
235
- this . _dirtyCheckNativeValue ( ) ;
236
232
}
233
+
234
+ // We need to dirty-check the native element's value, because there are some cases where
235
+ // we won't be notified when it changes (e.g. the consumer isn't using forms or they're
236
+ // updating the value using `emitEvent: false`).
237
+ this . _dirtyCheckNativeValue ( ) ;
237
238
}
238
239
239
240
focus ( ) { this . _elementRef . nativeElement . focus ( ) ; }
You can’t perform that action at this time.
0 commit comments