Skip to content

Commit 21095f5

Browse files
josephperrottjelbourn
authored andcommitted
fix(form-field): allow for measuring outline gap when label is not in dom (#12782)
1 parent 3da390e commit 21095f5

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

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

Lines changed: 17 additions & 8 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() {
@@ -451,21 +455,24 @@ export class MatFormField extends _MatFormFieldMixinBase
451455
return;
452456
}
453457

458+
if (this._platform && !this._platform.isBrowser) {
459+
// getBoundingClientRect isn't available on the server.
460+
return;
461+
}
462+
// If the element is not present in the DOM, the outline gap will need to be calculated
463+
// the next time it is checked and in the DOM.
464+
if (!document.documentElement.contains(this._elementRef.nativeElement)) {
465+
this._outlineGapCalculationNeeded = true;
466+
return;
467+
}
468+
454469
let startWidth = 0;
455470
let gapWidth = 0;
456471
const startEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
457472
'.mat-form-field-outline-start');
458473
const gapEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
459474
'.mat-form-field-outline-gap');
460475
if (this._label && this._label.nativeElement.children.length) {
461-
if (this._platform && !this._platform.isBrowser) {
462-
// getBoundingClientRect isn't available on the server.
463-
return;
464-
}
465-
if (!document.documentElement.contains(this._elementRef.nativeElement)) {
466-
return;
467-
}
468-
469476
const containerStart = this._getStartEnd(
470477
this._connectionContainerRef.nativeElement.getBoundingClientRect());
471478
const labelStart = this._getStartEnd(labelEl.children[0].getBoundingClientRect());
@@ -484,6 +491,8 @@ export class MatFormField extends _MatFormFieldMixinBase
484491
for (let i = 0; i < gapEls.length; i++) {
485492
gapEls.item(i).style.width = `${gapWidth}px`;
486493
}
494+
495+
this._outlineGapCalculationNeeded = false;
487496
}
488497

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

0 commit comments

Comments
 (0)