@@ -21,7 +21,6 @@ import {
21
21
Inject ,
22
22
InjectionToken ,
23
23
Input ,
24
- NgZone ,
25
24
Optional ,
26
25
QueryList ,
27
26
ViewChild ,
@@ -148,11 +147,6 @@ export class MatFormField extends _MatFormFieldMixinBase
148
147
return this . _appearance || this . _defaultOptions && this . _defaultOptions . appearance || 'legacy' ;
149
148
}
150
149
set appearance ( value : MatFormFieldAppearance ) {
151
- // If we're switching to `outline` from another appearance, we have to recalculate the gap.
152
- if ( value !== this . _appearance && value === 'outline' ) {
153
- this . _initialGapCalculated = false ;
154
- }
155
-
156
150
this . _appearance = value ;
157
151
}
158
152
_appearance : MatFormFieldAppearance ;
@@ -219,15 +213,14 @@ export class MatFormField extends _MatFormFieldMixinBase
219
213
220
214
_outlineGapWidth = 0 ;
221
215
_outlineGapStart = 0 ;
222
- _initialGapCalculated = false ;
223
216
224
217
/**
225
218
* @deprecated
226
219
* @breaking -change 7.0.0
227
220
*/
228
221
@ViewChild ( 'underline' ) underlineRef : ElementRef ;
229
222
230
- @ViewChild ( 'connectionContainer' ) _connectionContainerRef : ElementRef ;
223
+ @ViewChild ( 'connectionContainer' ) _connectionContainerRef : ElementRef < HTMLDivElement > ;
231
224
@ViewChild ( 'inputContainer' ) _inputContainerRef : ElementRef ;
232
225
@ViewChild ( 'label' ) private _label : ElementRef ;
233
226
@ContentChild ( MatFormFieldControl ) _control : MatFormFieldControl < any > ;
@@ -245,9 +238,8 @@ export class MatFormField extends _MatFormFieldMixinBase
245
238
@Optional ( ) private _dir : Directionality ,
246
239
@Optional ( ) @Inject ( MAT_FORM_FIELD_DEFAULT_OPTIONS ) private _defaultOptions :
247
240
MatFormFieldDefaultOptions ,
248
- // @breaking -change 7.0.0 _platform, _ngZone and _animationMode to be made required.
241
+ // @breaking -change 7.0.0 _platform and _animationMode to be made required.
249
242
private _platform ?: Platform ,
250
- private _ngZone ?: NgZone ,
251
243
@Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) _animationMode ?: string ) {
252
244
super ( _elementRef ) ;
253
245
@@ -298,20 +290,7 @@ export class MatFormField extends _MatFormFieldMixinBase
298
290
299
291
ngAfterContentChecked ( ) {
300
292
this . _validateControlChild ( ) ;
301
-
302
- if ( ! this . _initialGapCalculated ) {
303
- // @breaking -change 7.0.0 Remove this check and else block once _ngZone is required.
304
- if ( this . _ngZone ) {
305
- // It's important that we run this outside the `_ngZone`, because the `Promise.resolve`
306
- // can kick us into an infinite change detection loop, if the `_initialGapCalculated`
307
- // wasn't flipped on for some reason.
308
- this . _ngZone . runOutsideAngular ( ( ) => {
309
- Promise . resolve ( ) . then ( ( ) => this . updateOutlineGap ( ) ) ;
310
- } ) ;
311
- } else {
312
- Promise . resolve ( ) . then ( ( ) => this . updateOutlineGap ( ) ) ;
313
- }
314
- }
293
+ this . updateOutlineGap ( ) ;
315
294
}
316
295
317
296
ngAfterViewInit ( ) {
@@ -456,10 +435,19 @@ export class MatFormField extends _MatFormFieldMixinBase
456
435
* appearance.
457
436
*/
458
437
updateOutlineGap ( ) {
459
- if ( this . appearance === 'outline' && this . _label && this . _label . nativeElement . children . length ) {
438
+ if ( this . appearance !== 'outline' ) {
439
+ return ;
440
+ }
441
+
442
+ let startWidth = 0 ;
443
+ let gapWidth = 0 ;
444
+ const startEls = this . _connectionContainerRef . nativeElement . querySelectorAll < HTMLDivElement > (
445
+ '.mat-form-field-outline > .mat-form-field-outline-start' ) ;
446
+ const gapEls = this . _connectionContainerRef . nativeElement . querySelectorAll < HTMLDivElement > (
447
+ '.mat-form-field-outline > .mat-form-field-outline-gap' ) ;
448
+ if ( this . _label && this . _label . nativeElement . children . length ) {
460
449
if ( this . _platform && ! this . _platform . isBrowser ) {
461
450
// getBoundingClientRect isn't available on the server.
462
- this . _initialGapCalculated = true ;
463
451
return ;
464
452
}
465
453
if ( ! document . documentElement . contains ( this . _elementRef . nativeElement ) ) {
@@ -474,14 +462,16 @@ export class MatFormField extends _MatFormFieldMixinBase
474
462
for ( const child of this . _label . nativeElement . children ) {
475
463
labelWidth += child . offsetWidth ;
476
464
}
477
- this . _outlineGapStart = labelStart - containerStart - outlineGapPadding ;
478
- this . _outlineGapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2 ;
479
- } else {
480
- this . _outlineGapStart = 0 ;
481
- this . _outlineGapWidth = 0 ;
465
+ startWidth = labelStart - containerStart - outlineGapPadding ;
466
+ gapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2 ;
467
+ }
468
+
469
+ for ( let i = 0 ; i < startEls . length ; i ++ ) {
470
+ startEls . item ( i ) . style . width = `${ startWidth } px` ;
471
+ }
472
+ for ( let i = 0 ; i < gapEls . length ; i ++ ) {
473
+ gapEls . item ( i ) . style . width = `${ gapWidth } px` ;
482
474
}
483
- this . _initialGapCalculated = true ;
484
- this . _changeDetectorRef . markForCheck ( ) ;
485
475
}
486
476
487
477
/** Gets the start end of the rect considering the current directionality. */
0 commit comments