Skip to content

fix(material-experimental/form-field): fix notch width after appearance change #19682

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 6 commits into from
Jun 18, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
// we do not want to overwrite. *Note*: We need to have increased specificity for this
// override because the `right` property will be set with higher specificity in RTL mode.
.mat-mdc-text-field-wrapper .mat-mdc-form-field-infix .mdc-floating-label {
left: 0;
right: 0;
left: auto;
right: auto;
}

// MDC sets the input elements in outline appearance to "display: flex". There seems to
Expand Down
8 changes: 5 additions & 3 deletions src/material-experimental/mdc-form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,12 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
set appearance(value: MatFormFieldAppearance) {
const oldValue = this._appearance;
this._appearance = value || (this._defaults && this._defaults.appearance) || DEFAULT_APPEARANCE;
// If the appearance has been switched to `outline`, the label offset needs to be updated.
// The update can happen once the view has been re-checked, but not immediately because
// the view has not been updated and the notched-outline floating label is not present.
if (this._appearance === 'outline' && this._appearance !== oldValue) {
this._refreshOutlineNotchWidth();

// If the appearance has been switched to `outline`, the label offset needs to be updated.
// The update can happen once the view has been re-checked, but not immediately because
// the view has not been updated and the notched-outline floating label is not present.
this._needsOutlineLabelOffsetUpdateOnStable = true;
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/material-experimental/mdc-input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,23 @@ describe('MatMdcInput with forms', () => {
expect(formField._control.empty).toBe(false);
}));

it('should update notch size after changing appearance to outline', fakeAsync(() => {
const fixture = createComponent(MatInputWithAppearance);
fixture.detectChanges();

expect(fixture.nativeElement.querySelector('.mdc-notched-outline__notch')).toBe(null);

fixture.componentInstance.appearance = 'outline';
fixture.detectChanges();

let notch = fixture.nativeElement.querySelector('.mdc-notched-outline__notch')! as HTMLElement;
expect(notch.style.width).toBeFalsy();

fixture.nativeElement.querySelector('input')!.focus();
fixture.detectChanges();

expect(notch.style.width).toBeTruthy();
}));
});

describe('MatFormField default options', () => {
Expand Down Expand Up @@ -1498,6 +1515,7 @@ class MatInputWithLabel {}
@Component({
template: `
<mat-form-field [appearance]="appearance">
<mat-label>My Label</mat-label>
<input matInput placeholder="Placeholder">
</mat-form-field>
`
Expand Down