Skip to content

Commit a0c77e2

Browse files
authored
fix(form-field): make outline appearance work in situations where the… (#10943)
* fix(form-field): make outline appearance work in situations where the form field is not initially in the DOM * fix ssr
1 parent deedebd commit a0c77e2

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/demo-app/input/input-demo.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,23 @@ <h3>&lt;textarea&gt; with ngModel</h3>
647647
</form>
648648
</mat-card-content>
649649
</mat-card>
650+
651+
<mat-card class="demo-card demo-basic">
652+
<mat-toolbar color="primary">Outline form field in a tab</mat-toolbar>
653+
<mat-card-content>
654+
<mat-tab-group>
655+
<mat-tab label="Tab 1">
656+
<mat-form-field appearance="outline">
657+
<mat-label>Tab 1 input</mat-label>
658+
<input matInput value="test">
659+
</mat-form-field>
660+
</mat-tab>
661+
<mat-tab label="Tab 2">
662+
<mat-form-field appearance="outline">
663+
<mat-label>Tab 2 input</mat-label>
664+
<input matInput value="test">
665+
</mat-form-field>
666+
</mat-tab>
667+
</mat-tab-group>
668+
</mat-card-content>
669+
</mat-card>

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ export class MatFormField extends _MatFormFieldMixinBase
195195

196196
_outlineGapStart = 0;
197197

198+
_initialGapCalculated = false;
199+
198200
/**
199201
* @deprecated
200202
* @deletion-target 7.0.0
@@ -265,15 +267,13 @@ export class MatFormField extends _MatFormFieldMixinBase
265267
this._syncDescribedByIds();
266268
this._changeDetectorRef.markForCheck();
267269
});
268-
269-
Promise.resolve().then(() => {
270-
this.updateOutlineGap();
271-
this._changeDetectorRef.markForCheck();
272-
});
273270
}
274271

275272
ngAfterContentChecked() {
276273
this._validateControlChild();
274+
if (!this._initialGapCalculated) {
275+
Promise.resolve().then(() => this.updateOutlineGap());
276+
}
277277
}
278278

279279
ngAfterViewInit() {
@@ -417,6 +417,10 @@ export class MatFormField extends _MatFormFieldMixinBase
417417
if (this.appearance === 'outline' && this._label && this._label.nativeElement.children.length) {
418418
if (this._platform && !this._platform.isBrowser) {
419419
// getBoundingClientRect isn't available on the server.
420+
this._initialGapCalculated = true;
421+
return;
422+
}
423+
if (!document.contains(this._elementRef.nativeElement)) {
420424
return;
421425
}
422426

@@ -434,6 +438,7 @@ export class MatFormField extends _MatFormFieldMixinBase
434438
this._outlineGapStart = 0;
435439
this._outlineGapWidth = 0;
436440
}
441+
this._initialGapCalculated = true;
437442
this._changeDetectorRef.markForCheck();
438443
}
439444

0 commit comments

Comments
 (0)