Skip to content

Commit 98711d7

Browse files
crisbetommalerba
authored andcommitted
fix(input): unable to reset focused state of readonly input (#14698)
Currently we don't float the label if a readonly input is focused, however since we use the same handler for both `focus` and `blur`, it means that if the label is floated while the input is blurred, the user won't be able to reset it.
1 parent 36db1c0 commit 98711d7

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/lib/input/input.spec.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,35 @@ describe('MatInput without forms', () => {
853853
expect(container.classList).not.toContain('mat-focused');
854854
}));
855855

856+
it('should reset the highlight when a readonly input is blurred', fakeAsync(() => {
857+
const fixture = createComponent(MatInputWithReadonlyInput);
858+
fixture.detectChanges();
859+
860+
const inputDebugElement = fixture.debugElement.query(By.directive(MatInput));
861+
const input = inputDebugElement.injector.get<MatInput>(MatInput);
862+
const container = fixture.debugElement.query(By.css('mat-form-field')).nativeElement;
863+
864+
fixture.componentInstance.isReadonly = false;
865+
fixture.detectChanges();
866+
867+
// Call the focus handler directly to avoid flakyness where
868+
// browsers don't focus elements if the window is minimized.
869+
input._focusChanged(true);
870+
fixture.detectChanges();
871+
872+
expect(input.focused).toBe(true);
873+
expect(container.classList).toContain('mat-focused');
874+
875+
fixture.componentInstance.isReadonly = true;
876+
fixture.detectChanges();
877+
878+
input._focusChanged(false);
879+
fixture.detectChanges();
880+
881+
expect(input.focused).toBe(false);
882+
expect(container.classList).not.toContain('mat-focused');
883+
}));
884+
856885
it('should only show the native placeholder, when there is a label, on focus', () => {
857886
const fixture = createComponent(MatInputWithLabelAndPlaceholder);
858887
fixture.detectChanges();
@@ -1833,11 +1862,13 @@ class MatInputOnPush {
18331862
@Component({
18341863
template: `
18351864
<mat-form-field>
1836-
<input matInput readonly value="Only for reading">
1865+
<input matInput [readonly]="isReadonly" value="Only for reading">
18371866
</mat-form-field>
18381867
`
18391868
})
1840-
class MatInputWithReadonlyInput {}
1869+
class MatInputWithReadonlyInput {
1870+
isReadonly = true;
1871+
}
18411872

18421873
@Component({
18431874
template: `

src/lib/input/input.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,13 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
310310
}
311311

312312
/** Focuses the input. */
313-
focus(): void { this._elementRef.nativeElement.focus(); }
313+
focus(): void {
314+
this._elementRef.nativeElement.focus();
315+
}
314316

315317
/** Callback for the cases where the focused state of the input changes. */
316318
_focusChanged(isFocused: boolean) {
317-
if (isFocused !== this.focused && !this.readonly) {
319+
if (isFocused !== this.focused && (!this.readonly || !isFocused)) {
318320
this.focused = isFocused;
319321
this.stateChanges.next();
320322
}

0 commit comments

Comments
 (0)