Skip to content

Commit d81e88e

Browse files
committed
fix(form-field): remove outline gap for empty labels
Removes the outline gap in outlined form fields, if the label element doesn't have any content.
1 parent c5cfede commit d81e88e

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,10 @@ export class MatFormField extends _MatFormFieldMixinBase
456456
* appearance.
457457
*/
458458
updateOutlineGap() {
459-
if (this.appearance === 'outline' && this._label && this._label.nativeElement.children.length) {
459+
const labelEl = this._label ? this._label.nativeElement : null;
460+
461+
if (this.appearance === 'outline' && labelEl && labelEl.children.length &&
462+
labelEl.textContent.trim()) {
460463
if (this._platform && !this._platform.isBrowser) {
461464
// getBoundingClientRect isn't available on the server.
462465
this._initialGapCalculated = true;
@@ -468,14 +471,16 @@ export class MatFormField extends _MatFormFieldMixinBase
468471

469472
const containerStart = this._getStartEnd(
470473
this._connectionContainerRef.nativeElement.getBoundingClientRect());
471-
const labelStart = this._getStartEnd(
472-
this._label.nativeElement.children[0].getBoundingClientRect());
474+
const labelStart = this._getStartEnd(labelEl.children[0].getBoundingClientRect());
473475
let labelWidth = 0;
474-
for (const child of this._label.nativeElement.children) {
476+
477+
for (const child of labelEl.children) {
475478
labelWidth += child.offsetWidth;
476479
}
480+
477481
this._outlineGapStart = labelStart - containerStart - outlineGapPadding;
478-
this._outlineGapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2;
482+
this._outlineGapWidth = labelWidth > 0 ?
483+
labelWidth * floatingLabelScale + outlineGapPadding * 2 : 0;
479484
} else {
480485
this._outlineGapStart = 0;
481486
this._outlineGapWidth = 0;

src/lib/input/input.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,25 @@ describe('MatInput with appearance', () => {
11511151
expect(parseInt(outlineStart.style.width)).toBeGreaterThan(0);
11521152
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
11531153
}));
1154+
1155+
it('should not set an outline gap if the label is empty', fakeAsync(() => {
1156+
fixture.destroy();
1157+
TestBed.resetTestingModule();
1158+
1159+
const outlineFixture = createComponent(MatInputWithAppearanceAndLabel);
1160+
1161+
outlineFixture.componentInstance.labelContent = '';
1162+
outlineFixture.detectChanges();
1163+
outlineFixture.componentInstance.appearance = 'outline';
1164+
outlineFixture.detectChanges();
1165+
flush();
1166+
outlineFixture.detectChanges();
1167+
1168+
const outlineGap = outlineFixture.nativeElement.querySelector('.mat-form-field-outline-gap');
1169+
1170+
expect(parseInt(outlineGap.style.width)).toBe(0);
1171+
}));
1172+
11541173
});
11551174

11561175
describe('MatFormField default options', () => {
@@ -1594,13 +1613,14 @@ class MatInputWithAppearance {
15941613
@Component({
15951614
template: `
15961615
<mat-form-field [appearance]="appearance">
1597-
<mat-label>Label</mat-label>
1616+
<mat-label>{{labelContent}}</mat-label>
15981617
<input matInput>
15991618
</mat-form-field>
16001619
`
16011620
})
16021621
class MatInputWithAppearanceAndLabel {
16031622
appearance: MatFormFieldAppearance;
1623+
labelContent = 'Label';
16041624
}
16051625

16061626
@Component({

0 commit comments

Comments
 (0)