Skip to content

fix(input): stuck in focused state if disabled while in focus #8637

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
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
21 changes: 21 additions & 0 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,27 @@ describe('MatInput without forms', () => {
expect(container.classList).toContain('mat-focused');
}));

it('should remove the focused class if the input becomes disabled while focused',
fakeAsync(() => {
const fixture = TestBed.createComponent(MatInputTextTestController);
fixture.detectChanges();

const input = fixture.debugElement.query(By.directive(MatInput)).injector.get(MatInput);
const container = fixture.debugElement.query(By.css('mat-form-field')).nativeElement;

// Call the focus handler directly to avoid flakyness where
// browsers don't focus elements if the window is minimized.
input._focusChanged(true);
fixture.detectChanges();

expect(container.classList).toContain('mat-focused');

input.disabled = true;
fixture.detectChanges();

expect(container.classList).not.toContain('mat-focused');
}));

it('should be able to animate the label up and lock it in position', fakeAsync(() => {
let fixture = TestBed.createComponent(MatInputTextTestController);
fixture.detectChanges();
Expand Down
11 changes: 10 additions & 1 deletion src/lib/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
/** Whether the element is disabled. */
@Input()
get disabled() { return this.ngControl ? this.ngControl.disabled : this._disabled; }
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }
set disabled(value: any) {
this._disabled = coerceBooleanProperty(value);

// Browsers may not fire the blur event if the input is disabled too quickly.
// Reset from here to ensure that the element doesn't become stuck.
if (this.focused) {
this.focused = false;
this.stateChanges.next();
}
}

/** Unique id of the element. */
@Input()
Expand Down