From f5cb52185ef4d5ca5f204e06de94a614885fa9e7 Mon Sep 17 00:00:00 2001 From: lazerg Date: Wed, 5 Aug 2026 14:24:05 +0500 Subject: [PATCH] fix(material/form-field): update view when the control is swapped out --- src/material/form-field/form-field.ts | 4 +-- src/material/input/input.spec.ts | 38 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/material/form-field/form-field.ts b/src/material/form-field/form-field.ts index 5ce0506e73c7..eaf3ccc9340c 100644 --- a/src/material/form-field/form-field.ts +++ b/src/material/form-field/form-field.ts @@ -108,8 +108,7 @@ export const MAT_FORM_FIELD_DEFAULT_OPTIONS = new InjectionToken { expect(inputEl.disabled).toBe(true); }); + it('should update the disabled styling when the form field control is swapped out', () => { + const fixture = TestBed.createComponent(MatInputWithSwappedControl); + fixture.detectChanges(); + + const wrapperEl = fixture.debugElement.query( + By.css('.mat-mdc-text-field-wrapper'), + )!.nativeElement; + + expect(wrapperEl.classList.contains('mdc-text-field--disabled')) + .withContext(`Expected form field not to start out disabled.`) + .toBe(false); + + fixture.componentInstance.disabled = true; + fixture.changeDetectorRef.markForCheck(); + fixture.detectChanges(); + + expect(wrapperEl.classList.contains('mdc-text-field--disabled')) + .withContext(`Expected form field to look disabled after the control was swapped.`) + .toBe(true); + }); + it('should be able to set an input as being disabled and interactive', () => { const fixture = TestBed.createComponent(MatInputWithDisabled); fixture.componentInstance.disabled = true; @@ -1746,6 +1767,23 @@ class MatInputWithDisabled { disabledInteractive = false; } +@Component({ + template: ` + + @if (disabled) { + + } @else { + + } + + `, + imports: [MatInputModule], + changeDetection: ChangeDetectionStrategy.Eager, +}) +class MatInputWithSwappedControl { + disabled = false; +} + @Component({ template: ``, imports: [MatInputModule],