Skip to content

Commit f076390

Browse files
crisbetoandrewseguin
authored andcommitted
fix(input): don't highlight container when readonly input is focused (#7273)
Prevents readonly inputs from being highlighted when they're focused. This is consistent with the native input behavior. Note: This was introduced initially through #5776, but it looks like it didn't make it through the transition to `mat-form-field`.
1 parent 39543a3 commit f076390

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/lib/input/input.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,22 @@ describe('MatInput without forms', function () {
697697
expect(inputContainer._shouldAlwaysFloat).toBe(true);
698698
expect(inputContainer.floatPlaceholder).toBe('always');
699699
});
700+
701+
it('should not highlight when focusing a readonly input', () => {
702+
let fixture = TestBed.createComponent(MatInputWithReadonlyInput);
703+
fixture.detectChanges();
704+
705+
let input = fixture.debugElement.query(By.directive(MatInput)).injector.get<MatInput>(MatInput);
706+
let container = fixture.debugElement.query(By.css('mat-form-field')).nativeElement;
707+
708+
// Call the focus handler directly to avoid flakyness where
709+
// browsers don't focus elements if the window is minimized.
710+
input._focusChanged(true);
711+
fixture.detectChanges();
712+
713+
expect(input.focused).toBe(false);
714+
expect(container.classList).not.toContain('mat-focused');
715+
});
700716
});
701717

702718
describe('MatInput with forms', () => {

src/lib/input/input.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
7676
protected _uid = `mat-input-${nextUniqueId++}`;
7777
protected _errorOptions: ErrorOptions;
7878
protected _previousNativeValue = this.value;
79+
private _readonly = false;
7980

8081
/** Whether the input is focused. */
8182
focused = false;
@@ -141,6 +142,11 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
141142
}
142143
}
143144

145+
/** Whether the element is readonly. */
146+
@Input()
147+
get readonly() { return this._readonly; }
148+
set readonly(value: any) { this._readonly = coerceBooleanProperty(value); }
149+
144150
protected _neverEmptyInputTypes = [
145151
'date',
146152
'datetime',
@@ -205,7 +211,7 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
205211

206212
/** Callback for the cases where the focused state of the input changes. */
207213
_focusChanged(isFocused: boolean) {
208-
if (isFocused !== this.focused) {
214+
if (isFocused !== this.focused && !this.readonly) {
209215
this.focused = isFocused;
210216
this.stateChanges.next();
211217
}

0 commit comments

Comments
 (0)