Skip to content

Commit

Permalink
fix(input): stuck in focused state if disabled while in focus
Browse files Browse the repository at this point in the history
Fixes the input element becoming stuck in its focused state, if it becomes disabled while it's in focus.

Fixes angular#8634.
  • Loading branch information
crisbeto committed Nov 24, 2017
1 parent 6e865b7 commit 3079f7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,27 @@ describe('MatInput without forms', function () {
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 placeholder 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 @@ -97,7 +97,16 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
/** 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

0 comments on commit 3079f7c

Please sign in to comment.