Skip to content

Commit 5ef23ba

Browse files
authored
fix(material-experimental/form-field): fix notch width after appearance change (#19682)
* fix(material-experimental/form-field): fix notch width after appearance change * adjust CSS to remove need for CD * use fixture.nativeElement instead of document * remove unnecessary import * adjust test expectations * use auto instead of blocklisted initial
1 parent b8fba33 commit 5ef23ba

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/material-experimental/mdc-form-field/_mdc-text-field-structure-overrides.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
// we do not want to overwrite. *Note*: We need to have increased specificity for this
5757
// override because the `right` property will be set with higher specificity in RTL mode.
5858
.mat-mdc-text-field-wrapper .mat-mdc-form-field-infix .mdc-floating-label {
59-
left: 0;
60-
right: 0;
59+
left: auto;
60+
right: auto;
6161
}
6262

6363
// MDC sets the input elements in outline appearance to "display: flex". There seems to

src/material-experimental/mdc-form-field/form-field.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,12 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
172172
set appearance(value: MatFormFieldAppearance) {
173173
const oldValue = this._appearance;
174174
this._appearance = value || (this._defaults && this._defaults.appearance) || DEFAULT_APPEARANCE;
175-
// If the appearance has been switched to `outline`, the label offset needs to be updated.
176-
// The update can happen once the view has been re-checked, but not immediately because
177-
// the view has not been updated and the notched-outline floating label is not present.
178175
if (this._appearance === 'outline' && this._appearance !== oldValue) {
176+
this._refreshOutlineNotchWidth();
177+
178+
// If the appearance has been switched to `outline`, the label offset needs to be updated.
179+
// The update can happen once the view has been re-checked, but not immediately because
180+
// the view has not been updated and the notched-outline floating label is not present.
179181
this._needsOutlineLabelOffsetUpdateOnStable = true;
180182
}
181183
}

src/material-experimental/mdc-input/input.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,23 @@ describe('MatMdcInput with forms', () => {
10941094
expect(formField._control.empty).toBe(false);
10951095
}));
10961096

1097+
it('should update notch size after changing appearance to outline', fakeAsync(() => {
1098+
const fixture = createComponent(MatInputWithAppearance);
1099+
fixture.detectChanges();
1100+
1101+
expect(fixture.nativeElement.querySelector('.mdc-notched-outline__notch')).toBe(null);
1102+
1103+
fixture.componentInstance.appearance = 'outline';
1104+
fixture.detectChanges();
1105+
1106+
let notch = fixture.nativeElement.querySelector('.mdc-notched-outline__notch')! as HTMLElement;
1107+
expect(notch.style.width).toBeFalsy();
1108+
1109+
fixture.nativeElement.querySelector('input')!.focus();
1110+
fixture.detectChanges();
1111+
1112+
expect(notch.style.width).toBeTruthy();
1113+
}));
10971114
});
10981115

10991116
describe('MatFormField default options', () => {
@@ -1498,6 +1515,7 @@ class MatInputWithLabel {}
14981515
@Component({
14991516
template: `
15001517
<mat-form-field [appearance]="appearance">
1518+
<mat-label>My Label</mat-label>
15011519
<input matInput placeholder="Placeholder">
15021520
</mat-form-field>
15031521
`

0 commit comments

Comments
 (0)