Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form-field): properly handle measuring outline gap when label is not initially in the dom #12782

Merged
merged 1 commit into from
Aug 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 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 @@ -451,21 +455,24 @@ export class MatFormField extends _MatFormFieldMixinBase
return;
}

if (this._platform && !this._platform.isBrowser) {
// getBoundingClientRect isn't available on the server.
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.documentElement.contains(this._elementRef.nativeElement)) {
this._outlineGapCalculationNeeded = true;
return;
}

let startWidth = 0;
let gapWidth = 0;
const startEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
'.mat-form-field-outline-start');
const gapEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
'.mat-form-field-outline-gap');
if (this._label && this._label.nativeElement.children.length) {
if (this._platform && !this._platform.isBrowser) {
// getBoundingClientRect isn't available on the server.
return;
}
if (!document.documentElement.contains(this._elementRef.nativeElement)) {
return;
}

const containerStart = this._getStartEnd(
this._connectionContainerRef.nativeElement.getBoundingClientRect());
const labelStart = this._getStartEnd(labelEl.children[0].getBoundingClientRect());
Expand All @@ -484,6 +491,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