Skip to content

Commit de05bb3

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

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
<mat-tab-group selectedIndex="0">
2+
<mat-tab label="One">
3+
<p>one</p>
4+
</mat-tab>
5+
<mat-tab label="Two">
6+
<mat-form-field appearance="outline">
7+
<mat-label>Label two</mat-label>
8+
<input matInput [formControl]="two">
9+
</mat-form-field>
10+
</mat-tab>
11+
<mat-tab label="three">
12+
<p>three</p>
13+
</mat-tab>
14+
</mat-tab-group>
15+
16+
<!--
117
<mat-card class="demo-card demo-basic">
218
<mat-toolbar color="primary">Basic</mat-toolbar>
319
<mat-card-content>
@@ -673,3 +689,4 @@ <h3>&lt;textarea&gt; with bindable autosize </h3>
673689
</mat-tab-group>
674690
</mat-card-content>
675691
</mat-card>
692+
-->

src/demo-app/input/input-demo.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA
2323
styleUrls: ['input-demo.css'],
2424
})
2525
export class InputDemo {
26+
27+
28+
one = new FormControl('...');
29+
two = new FormControl('...');
30+
three = new FormControl('...');
2631
floatingLabel = 'auto';
2732
color: boolean;
2833
requiredField: boolean;

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

Lines changed: 11 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,11 @@ export class MatFormField extends _MatFormFieldMixinBase
448452
return;
449453
}
450454

455+
if (!document.documentElement.contains(this._elementRef.nativeElement)) {
456+
this._outlineGapCalculationNeeded = true;
457+
return;
458+
}
459+
451460
let startWidth = 0;
452461
let gapWidth = 0;
453462
const startEls = this._connectionContainerRef.nativeElement.querySelectorAll<HTMLElement>(
@@ -459,9 +468,6 @@ export class MatFormField extends _MatFormFieldMixinBase
459468
// getBoundingClientRect isn't available on the server.
460469
return;
461470
}
462-
if (!document.documentElement.contains(this._elementRef.nativeElement)) {
463-
return;
464-
}
465471

466472
const containerStart = this._getStartEnd(
467473
this._connectionContainerRef.nativeElement.getBoundingClientRect());
@@ -481,6 +487,8 @@ export class MatFormField extends _MatFormFieldMixinBase
481487
for (let i = 0; i < gapEls.length; i++) {
482488
gapEls.item(i).style.width = `${gapWidth}px`;
483489
}
490+
491+
this._outlineGapCalculationNeeded = false;
484492
}
485493

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

0 commit comments

Comments
 (0)