Skip to content

Commit 3a8b9ff

Browse files
committed
fix(form-field): allow for measuring outline gap when label is not in dom
1 parent 9f06502 commit 3a8b9ff

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/lib/form-field/form-field.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const MAT_FORM_FIELD_DEFAULT_OPTIONS =
141141
export class MatFormField extends _MatFormFieldMixinBase
142142
implements AfterContentInit, AfterContentChecked, AfterViewInit, CanColor {
143143
private _labelOptions: LabelOptions;
144+
private _outlineGapCalculationNeeded = false;
144145

145146
/** The form-field appearance style. */
146147
@Input()
@@ -300,6 +301,9 @@ export class MatFormField extends _MatFormFieldMixinBase
300301

301302
ngAfterContentChecked() {
302303
this._validateControlChild();
304+
if (this._outlineGapCalculationNeeded) {
305+
this.updateOutlineGap();
306+
}
303307
}
304308

305309
ngAfterViewInit() {
@@ -448,6 +452,13 @@ export class MatFormField extends _MatFormFieldMixinBase
448452
return;
449453
}
450454

455+
// If the element is not present in the DOM, the outline gap will need to be calculated
456+
// the next time it is checked and in the DOM.
457+
if (document && !document.documentElement.contains(this._elementRef.nativeElement)) {
458+
this._outlineGapCalculationNeeded = true;
459+
return;
460+
}
461+
451462
let startWidth = 0;
452463
let gapWidth = 0;
453464
const startEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
@@ -459,9 +470,6 @@ export class MatFormField extends _MatFormFieldMixinBase
459470
// getBoundingClientRect isn't available on the server.
460471
return;
461472
}
462-
if (!document.documentElement.contains(this._elementRef.nativeElement)) {
463-
return;
464-
}
465473

466474
const containerStart = this._getStartEnd(
467475
this._connectionContainerRef.nativeElement.getBoundingClientRect());
@@ -481,6 +489,8 @@ export class MatFormField extends _MatFormFieldMixinBase
481489
for (let i = 0; i < gapEls.length; i++) {
482490
gapEls.item(i).style.width = `${gapWidth}px`;
483491
}
492+
493+
this._outlineGapCalculationNeeded = false;
484494
}
485495

486496
/** Gets the start end of the rect considering the current directionality. */

0 commit comments

Comments
 (0)