Skip to content

Commit a326ee0

Browse files
crisbetojelbourn
authored andcommitted
fix(form-field): remove outline gap for empty labels (#12637)
Removes the outline gap in outlined form fields, if the label element doesn't have any content.
1 parent da3b5e0 commit a326ee0

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ export class MatFormField extends _MatFormFieldMixinBase
444444
* appearance.
445445
*/
446446
updateOutlineGap() {
447-
if (this.appearance !== 'outline') {
447+
const labelEl = this._label ? this._label.nativeElement : null;
448+
449+
if (this.appearance !== 'outline' || !labelEl || !labelEl.children.length ||
450+
!labelEl.textContent.trim()) {
448451
return;
449452
}
450453

@@ -465,14 +468,14 @@ export class MatFormField extends _MatFormFieldMixinBase
465468

466469
const containerStart = this._getStartEnd(
467470
this._connectionContainerRef.nativeElement.getBoundingClientRect());
468-
const labelStart = this._getStartEnd(
469-
this._label.nativeElement.children[0].getBoundingClientRect());
471+
const labelStart = this._getStartEnd(labelEl.children[0].getBoundingClientRect());
470472
let labelWidth = 0;
471-
for (const child of this._label.nativeElement.children) {
473+
474+
for (const child of labelEl.children) {
472475
labelWidth += child.offsetWidth;
473476
}
474477
startWidth = labelStart - containerStart - outlineGapPadding;
475-
gapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2;
478+
gapWidth = labelWidth > 0 ? labelWidth * floatingLabelScale + outlineGapPadding * 2 : 0;
476479
}
477480

478481
for (let i = 0; i < startEls.length; i++) {

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)).toBeFalsy();
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)