Skip to content

Commit bec4cfe

Browse files
crisbetotinayuangao
authored andcommitted
fix(input): stuck in focused state if disabled while in focus (#8637)
Fixes the input element becoming stuck in its focused state, if it becomes disabled while it's in focus. Fixes #8634.
1 parent 7e5f2da commit bec4cfe

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/lib/input/input.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,27 @@ describe('MatInput without forms', () => {
688688
expect(container.classList).toContain('mat-focused');
689689
}));
690690

691+
it('should remove the focused class if the input becomes disabled while focused',
692+
fakeAsync(() => {
693+
const fixture = TestBed.createComponent(MatInputTextTestController);
694+
fixture.detectChanges();
695+
696+
const input = fixture.debugElement.query(By.directive(MatInput)).injector.get(MatInput);
697+
const container = fixture.debugElement.query(By.css('mat-form-field')).nativeElement;
698+
699+
// Call the focus handler directly to avoid flakyness where
700+
// browsers don't focus elements if the window is minimized.
701+
input._focusChanged(true);
702+
fixture.detectChanges();
703+
704+
expect(container.classList).toContain('mat-focused');
705+
706+
input.disabled = true;
707+
fixture.detectChanges();
708+
709+
expect(container.classList).not.toContain('mat-focused');
710+
}));
711+
691712
it('should be able to animate the label up and lock it in position', fakeAsync(() => {
692713
let fixture = TestBed.createComponent(MatInputTextTestController);
693714
fixture.detectChanges();

src/lib/input/input.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,16 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
109109
/** Whether the element is disabled. */
110110
@Input()
111111
get disabled() { return this.ngControl ? this.ngControl.disabled : this._disabled; }
112-
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }
112+
set disabled(value: any) {
113+
this._disabled = coerceBooleanProperty(value);
114+
115+
// Browsers may not fire the blur event if the input is disabled too quickly.
116+
// Reset from here to ensure that the element doesn't become stuck.
117+
if (this.focused) {
118+
this.focused = false;
119+
this.stateChanges.next();
120+
}
121+
}
113122

114123
/** Unique id of the element. */
115124
@Input()

0 commit comments

Comments
 (0)