Skip to content

Commit b42eea5

Browse files
lazergcrisbeto
authored andcommitted
fix(material/form-field): update view when the control is swapped out (#33636)
(cherry picked from commit e9760b3)
1 parent 0b67c3c commit b42eea5

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ export const MAT_FORM_FIELD_DEFAULT_OPTIONS = new InjectionToken<MatFormFieldDef
108108

109109
/** Styles that are to be applied to the label elements in the outlined appearance. */
110110
type OutlinedLabelStyles =
111-
| [floatingLabelTransform: string, notchedOutlineWidth: number | null]
112-
| null;
111+
[floatingLabelTransform: string, notchedOutlineWidth: number | null] | null;
113112

114113
/** Default appearance used by the form field. */
115114
const DEFAULT_APPEARANCE: MatFormFieldAppearance = 'fill';
@@ -401,6 +400,7 @@ export class MatFormField
401400
}
402401

403402
this._previousControl = this._control;
403+
this._changeDetectorRef.markForCheck();
404404
}
405405

406406
// make sure the the control has been initialized.

src/material/input/input.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,27 @@ describe('MatInput without forms', () => {
458458
expect(inputEl.disabled).toBe(true);
459459
});
460460

461+
it('should update the disabled styling when the form field control is swapped out', () => {
462+
const fixture = TestBed.createComponent(MatInputWithSwappedControl);
463+
fixture.detectChanges();
464+
465+
const wrapperEl = fixture.debugElement.query(
466+
By.css('.mat-mdc-text-field-wrapper'),
467+
)!.nativeElement;
468+
469+
expect(wrapperEl.classList.contains('mdc-text-field--disabled'))
470+
.withContext(`Expected form field not to start out disabled.`)
471+
.toBe(false);
472+
473+
fixture.componentInstance.disabled = true;
474+
fixture.changeDetectorRef.markForCheck();
475+
fixture.detectChanges();
476+
477+
expect(wrapperEl.classList.contains('mdc-text-field--disabled'))
478+
.withContext(`Expected form field to look disabled after the control was swapped.`)
479+
.toBe(true);
480+
});
481+
461482
it('should be able to set an input as being disabled and interactive', () => {
462483
const fixture = TestBed.createComponent(MatInputWithDisabled);
463484
fixture.componentInstance.disabled = true;
@@ -1746,6 +1767,23 @@ class MatInputWithDisabled {
17461767
disabledInteractive = false;
17471768
}
17481769

1770+
@Component({
1771+
template: `
1772+
<mat-form-field>
1773+
@if (disabled) {
1774+
<input matInput [disabled]="true">
1775+
} @else {
1776+
<input matInput>
1777+
}
1778+
</mat-form-field>
1779+
`,
1780+
imports: [MatInputModule],
1781+
changeDetection: ChangeDetectionStrategy.Eager,
1782+
})
1783+
class MatInputWithSwappedControl {
1784+
disabled = false;
1785+
}
1786+
17491787
@Component({
17501788
template: `<mat-form-field><input matInput [required]="required"></mat-form-field>`,
17511789
imports: [MatInputModule],

0 commit comments

Comments
 (0)