Skip to content

Commit 349121d

Browse files
crisbetokara
authored andcommitted
fix(input): don't highlight container when readonly input is focused (#5776)
Fixes #5749.
1 parent 643023d commit 349121d

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/lib/input/input-container.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe('MdInputContainer without forms', function () {
6262
MdTextareaWithBindings,
6363
MdInputContainerWithNgIf,
6464
MdInputContainerOnPush,
65+
MdInputContainerWithReadonlyInput,
6566
],
6667
});
6768

@@ -604,6 +605,39 @@ describe('MdInputContainer without forms', function () {
604605

605606
expect(placeholder.classList).not.toContain('mat-empty', 'Input no longer empty');
606607
});
608+
609+
it('should set the focused class when the input is focused', () => {
610+
let fixture = TestBed.createComponent(MdInputContainerTextTestController);
611+
fixture.detectChanges();
612+
613+
let input = fixture.debugElement.query(By.directive(MdInputDirective))
614+
.injector.get<MdInputDirective>(MdInputDirective);
615+
let container = fixture.debugElement.query(By.css('md-input-container')).nativeElement;
616+
617+
// Call the focus handler directly to avoid flakyness where
618+
// browsers don't focus elements if the window is minimized.
619+
input._onFocus();
620+
fixture.detectChanges();
621+
622+
expect(container.classList).toContain('mat-focused');
623+
});
624+
625+
it('should not highlight when focusing a readonly input', () => {
626+
let fixture = TestBed.createComponent(MdInputContainerWithReadonlyInput);
627+
fixture.detectChanges();
628+
629+
let input = fixture.debugElement.query(By.directive(MdInputDirective))
630+
.injector.get<MdInputDirective>(MdInputDirective);
631+
let container = fixture.debugElement.query(By.css('md-input-container')).nativeElement;
632+
633+
// Call the focus handler directly to avoid flakyness where
634+
// browsers don't focus elements if the window is minimized.
635+
input._onFocus();
636+
fixture.detectChanges();
637+
638+
expect(input.focused).toBe(false);
639+
expect(container.classList).not.toContain('mat-focused');
640+
});
607641
});
608642

609643
describe('MdInputContainer with forms', () => {
@@ -1230,3 +1264,12 @@ class MdInputContainerWithNgIf {
12301264
class MdInputContainerOnPush {
12311265
formControl = new FormControl('');
12321266
}
1267+
1268+
@Component({
1269+
template: `
1270+
<md-input-container>
1271+
<input mdInput readonly value="Only for reading">
1272+
</md-input-container>
1273+
`
1274+
})
1275+
class MdInputContainerWithReadonlyInput {}

src/lib/input/input-container.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export class MdInputDirective {
141141
private _placeholder: string = '';
142142
private _disabled = false;
143143
private _required = false;
144+
private _readonly = false;
144145
private _id: string;
145146
private _cachedUid: string;
146147
private _errorOptions: ErrorOptions;
@@ -196,6 +197,11 @@ export class MdInputDirective {
196197
}
197198
}
198199

200+
/** Whether the element is readonly. */
201+
@Input()
202+
get readonly() { return this._readonly; }
203+
set readonly(value: any) { this._readonly = coerceBooleanProperty(value); }
204+
199205
/** A function used to control when error messages are shown. */
200206
@Input() errorStateMatcher: ErrorStateMatcher;
201207

@@ -247,7 +253,11 @@ export class MdInputDirective {
247253
/** Focuses the input element. */
248254
focus() { this._elementRef.nativeElement.focus(); }
249255

250-
_onFocus() { this.focused = true; }
256+
_onFocus() {
257+
if (!this._readonly) {
258+
this.focused = true;
259+
}
260+
}
251261

252262
_onBlur() { this.focused = false; }
253263

0 commit comments

Comments
 (0)