Skip to content

fix(form-field): label gap not being calculated when switching to outline dynamically #11658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ export class MatFormField extends _MatFormFieldMixinBase
return this._appearance || this._defaultOptions && this._defaultOptions.appearance || 'legacy';
}
set appearance(value: MatFormFieldAppearance) {
// If we're switching to `outline` from another appearance, we have to recalculate the gap.
if (value !== this._appearance && value === 'outline') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a test for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to get it to measure the element within a unit test. IIRC I wasn't able to get that Promise.resolve to flush correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you try just doing a real async test?

Copy link
Member Author

@crisbeto crisbeto Jun 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got some time to take a better look and it turns out that my test wasn't working because it didn't have a label. I've pushed a test that fails when we expect it to.

this._initialGapCalculated = false;
}

this._appearance = value;
}
_appearance: MatFormFieldAppearance;
Expand Down
36 changes: 36 additions & 0 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,30 @@ describe('MatInput with appearance', () => {
expect(testComponent.formField.floatLabel).toBe('auto');
}
}));

it('should recalculate gaps when switching to outline appearance after init', fakeAsync(() => {
fixture.destroy();
TestBed.resetTestingModule();

const outlineFixture = createComponent(MatInputWithAppearanceAndLabel);

outlineFixture.detectChanges();
outlineFixture.componentInstance.appearance = 'legacy';
outlineFixture.detectChanges();
flush();

outlineFixture.componentInstance.appearance = 'outline';
outlineFixture.detectChanges();
flush();
outlineFixture.detectChanges();

const wrapperElement = outlineFixture.nativeElement;
const outlineStart = wrapperElement.querySelector('.mat-form-field-outline-start');
const outlineGap = wrapperElement.querySelector('.mat-form-field-outline-gap');

expect(parseInt(outlineStart.style.width)).toBeGreaterThan(0);
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
}));
});

describe('MatFormField default options', () => {
Expand Down Expand Up @@ -1567,6 +1591,18 @@ class MatInputWithAppearance {
appearance: MatFormFieldAppearance;
}

@Component({
template: `
<mat-form-field [appearance]="appearance">
<mat-label>Label</mat-label>
<input matInput>
</mat-form-field>
`
})
class MatInputWithAppearanceAndLabel {
appearance: MatFormFieldAppearance;
}

@Component({
template: `
<mat-form-field>
Expand Down