Skip to content

Commit d75fa75

Browse files
crisbetojelbourn
authored andcommitted
fix(form-field): label gap not being calculated when switching to outline dynamically (#11658)
Fixes the form field gap not being calculated if the field switches to the `outline` appearance after init. Fixes #11653.
1 parent a831bf6 commit d75fa75

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ export class MatFormField extends _MatFormFieldMixinBase
148148
return this._appearance || this._defaultOptions && this._defaultOptions.appearance || 'legacy';
149149
}
150150
set appearance(value: MatFormFieldAppearance) {
151+
// If we're switching to `outline` from another appearance, we have to recalculate the gap.
152+
if (value !== this._appearance && value === 'outline') {
153+
this._initialGapCalculated = false;
154+
}
155+
151156
this._appearance = value;
152157
}
153158
_appearance: MatFormFieldAppearance;

src/lib/input/input.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,30 @@ describe('MatInput with appearance', () => {
11271127
expect(testComponent.formField.floatLabel).toBe('auto');
11281128
}
11291129
}));
1130+
1131+
it('should recalculate gaps when switching to outline appearance after init', fakeAsync(() => {
1132+
fixture.destroy();
1133+
TestBed.resetTestingModule();
1134+
1135+
const outlineFixture = createComponent(MatInputWithAppearanceAndLabel);
1136+
1137+
outlineFixture.detectChanges();
1138+
outlineFixture.componentInstance.appearance = 'legacy';
1139+
outlineFixture.detectChanges();
1140+
flush();
1141+
1142+
outlineFixture.componentInstance.appearance = 'outline';
1143+
outlineFixture.detectChanges();
1144+
flush();
1145+
outlineFixture.detectChanges();
1146+
1147+
const wrapperElement = outlineFixture.nativeElement;
1148+
const outlineStart = wrapperElement.querySelector('.mat-form-field-outline-start');
1149+
const outlineGap = wrapperElement.querySelector('.mat-form-field-outline-gap');
1150+
1151+
expect(parseInt(outlineStart.style.width)).toBeGreaterThan(0);
1152+
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
1153+
}));
11301154
});
11311155

11321156
describe('MatFormField default options', () => {
@@ -1567,6 +1591,18 @@ class MatInputWithAppearance {
15671591
appearance: MatFormFieldAppearance;
15681592
}
15691593

1594+
@Component({
1595+
template: `
1596+
<mat-form-field [appearance]="appearance">
1597+
<mat-label>Label</mat-label>
1598+
<input matInput>
1599+
</mat-form-field>
1600+
`
1601+
})
1602+
class MatInputWithAppearanceAndLabel {
1603+
appearance: MatFormFieldAppearance;
1604+
}
1605+
15701606
@Component({
15711607
template: `
15721608
<mat-form-field>

0 commit comments

Comments
 (0)