Skip to content
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
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