Skip to content

Commit

Permalink
fix(form-field): allow for measuring outline gap when label is not in…
Browse files Browse the repository at this point in the history
… dom
  • Loading branch information
josephperrott committed Aug 22, 2018
1 parent 9f06502 commit 3a8b9ff
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/lib/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const MAT_FORM_FIELD_DEFAULT_OPTIONS =
export class MatFormField extends _MatFormFieldMixinBase
implements AfterContentInit, AfterContentChecked, AfterViewInit, CanColor {
private _labelOptions: LabelOptions;
private _outlineGapCalculationNeeded = false;

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

ngAfterContentChecked() {
this._validateControlChild();
if (this._outlineGapCalculationNeeded) {
this.updateOutlineGap();
}
}

ngAfterViewInit() {
Expand Down Expand Up @@ -448,6 +452,13 @@ export class MatFormField extends _MatFormFieldMixinBase
return;
}

// If the element is not present in the DOM, the outline gap will need to be calculated
// the next time it is checked and in the DOM.
if (document && !document.documentElement.contains(this._elementRef.nativeElement)) {
this._outlineGapCalculationNeeded = true;
return;
}

let startWidth = 0;
let gapWidth = 0;
const startEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
Expand All @@ -459,9 +470,6 @@ export class MatFormField extends _MatFormFieldMixinBase
// getBoundingClientRect isn't available on the server.
return;
}
if (!document.documentElement.contains(this._elementRef.nativeElement)) {
return;
}

const containerStart = this._getStartEnd(
this._connectionContainerRef.nativeElement.getBoundingClientRect());
Expand All @@ -481,6 +489,8 @@ export class MatFormField extends _MatFormFieldMixinBase
for (let i = 0; i < gapEls.length; i++) {
gapEls.item(i).style.width = `${gapWidth}px`;
}

this._outlineGapCalculationNeeded = false;
}

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

0 comments on commit 3a8b9ff

Please sign in to comment.