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 23, 2017
1 parent 3571f68 commit 4fc0619
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('MdInput without forms', function () {
MdInputTextareaWithBindings,
MdInputWithNgIf,
MdInputOnPush,
MdInputWithReadonlyInput,
MdInputReadonly,
],
});

Expand Down Expand Up @@ -697,6 +697,22 @@ describe('MdInput 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(MdInputReadonly);
fixture.detectChanges();

let input = fixture.debugElement.query(By.directive(MdInput)).injector.get<MdInput>(MdInput);
let container = fixture.debugElement.query(By.css('md-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('MdInput with forms', () => {
Expand Down Expand Up @@ -1360,4 +1376,4 @@ class MdInputOnPush {
</md-form-field>
`
})
class MdInputWithReadonlyInput {}
class MdInputReadonly {}
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 MdInput implements MdFormFieldControl<any>, OnChanges, OnDestroy, D
protected _uid = `md-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 MdInput implements MdFormFieldControl<any>, OnChanges, OnDestroy, D
}
}

/** 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 MdInput implements MdFormFieldControl<any>, OnChanges, OnDestroy, D

/** 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 4fc0619

Please sign in to comment.