Skip to content

Commit

Permalink
fix(input): don't highlight container when readonly input is focused
Browse files Browse the repository at this point in the history
Prevents readonly inputs from being highlighted when they're focused. This is consistent with the native input behavior.

Note: This was introduced initially through angular#5776, but it looks like it didn't make it through the transition to `mat-form-field`.
  • Loading branch information
crisbeto committed Sep 29, 2017
1 parent 53c42a4 commit e570709
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,22 @@ describe('MatInput without forms', function () {
expect(inputContainer._shouldAlwaysFloat).toBe(true);
expect(inputContainer.floatPlaceholder).toBe('always');
});

it('should not highlight when focusing a readonly input', () => {
let fixture = TestBed.createComponent(MatInputWithReadonlyInput);
fixture.detectChanges();

let input = fixture.debugElement.query(By.directive(MatInput)).injector.get<MatInput>(MatInput);
let 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(input.focused).toBe(false);
expect(container.classList).not.toContain('mat-focused');
});
});

describe('MatInput with forms', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/lib/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
protected _uid = `mat-input-${nextUniqueId++}`;
protected _errorOptions: ErrorOptions;
protected _previousNativeValue = this.value;
private _readonly = false;

/** Whether the input is focused. */
focused = false;
Expand Down Expand Up @@ -141,6 +142,11 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
}
}

/** Whether the element is readonly. */
@Input()
get readonly() { return this._readonly; }
set readonly(value: any) { this._readonly = coerceBooleanProperty(value); }

protected _neverEmptyInputTypes = [
'date',
'datetime',
Expand Down Expand Up @@ -205,7 +211,7 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,

/** Callback for the cases where the focused state of the input changes. */
_focusChanged(isFocused: boolean) {
if (isFocused !== this.focused) {
if (isFocused !== this.focused && !this.readonly) {
this.focused = isFocused;
this.stateChanges.next();
}
Expand Down

0 comments on commit e570709

Please sign in to comment.