Skip to content

Commit afa9431

Browse files
crisbetoamysorto
authored andcommitted
fix(material/input): show focus indication for readonly inputs (#22847)
A long time ago we disabled focus indication on `readonly` inputs in order to mimic the native browser behavior. It looks like the native behavior has changed to show the focus indication now so these changes remove the old logic and the associated unit tests. Fixes #22783. (cherry picked from commit 4404d0a)
1 parent eba631a commit afa9431

File tree

3 files changed

+1
-115
lines changed

3 files changed

+1
-115
lines changed

src/material-experimental/mdc-input/input.spec.ts

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -770,52 +770,6 @@ describe('MatMdcInput without forms', () => {
770770
expect(container.classList).not.toContain('mat-focused');
771771
}));
772772

773-
it('should not highlight when focusing a readonly input', fakeAsync(() => {
774-
let fixture = createComponent(MatInputWithReadonlyInput);
775-
fixture.detectChanges();
776-
777-
let input =
778-
fixture.debugElement.query(By.directive(MatInput))!.injector.get<MatInput>(MatInput);
779-
let container = fixture.debugElement.query(By.css('.mat-mdc-form-field'))!.nativeElement;
780-
781-
// Call the focus handler directly to avoid flakiness where
782-
// browsers don't focus elements if the window is minimized.
783-
input._focusChanged(true);
784-
fixture.detectChanges();
785-
786-
expect(input.focused).toBe(false);
787-
expect(container.classList).not.toContain('mat-focused');
788-
}));
789-
790-
it('should reset the highlight when a readonly input is blurred', fakeAsync(() => {
791-
const fixture = createComponent(MatInputWithReadonlyInput);
792-
fixture.detectChanges();
793-
794-
const inputDebugElement = fixture.debugElement.query(By.directive(MatInput))!;
795-
const input = inputDebugElement.injector.get<MatInput>(MatInput);
796-
const container = fixture.debugElement.query(By.css('.mat-mdc-form-field'))!.nativeElement;
797-
798-
fixture.componentInstance.isReadonly = false;
799-
fixture.detectChanges();
800-
801-
// Call the focus handler directly to avoid flakyness where
802-
// browsers don't focus elements if the window is minimized.
803-
input._focusChanged(true);
804-
fixture.detectChanges();
805-
806-
expect(input.focused).toBe(true);
807-
expect(container.classList).toContain('mat-focused');
808-
809-
fixture.componentInstance.isReadonly = true;
810-
fixture.detectChanges();
811-
812-
input._focusChanged(false);
813-
fixture.detectChanges();
814-
815-
expect(input.focused).toBe(false);
816-
expect(container.classList).not.toContain('mat-focused');
817-
}));
818-
819773
it('should only show the native control placeholder, when there is a label, on focus', () => {
820774
const fixture = createComponent(MatInputWithLabelAndPlaceholder);
821775
fixture.detectChanges();
@@ -1654,17 +1608,6 @@ class MatInputOnPush {
16541608
formControl = new FormControl('');
16551609
}
16561610

1657-
@Component({
1658-
template: `
1659-
<mat-form-field>
1660-
<input matInput [readonly]="isReadonly" value="Only for reading">
1661-
</mat-form-field>
1662-
`
1663-
})
1664-
class MatInputWithReadonlyInput {
1665-
isReadonly = true;
1666-
}
1667-
16681611
@Component({
16691612
template: `
16701613
<mat-form-field>

src/material/input/input.spec.ts

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -905,52 +905,6 @@ describe('MatInput without forms', () => {
905905
expect(() => formField._animateAndLockLabel()).not.toThrow();
906906
}));
907907

908-
it('should not highlight when focusing a readonly input', fakeAsync(() => {
909-
const fixture = createComponent(MatInputWithReadonlyInput);
910-
fixture.detectChanges();
911-
912-
const input =
913-
fixture.debugElement.query(By.directive(MatInput))!.injector.get<MatInput>(MatInput);
914-
const container = fixture.debugElement.query(By.css('mat-form-field'))!.nativeElement;
915-
916-
// Call the focus handler directly to avoid flakyness where
917-
// browsers don't focus elements if the window is minimized.
918-
input._focusChanged(true);
919-
fixture.detectChanges();
920-
921-
expect(input.focused).toBe(false);
922-
expect(container.classList).not.toContain('mat-focused');
923-
}));
924-
925-
it('should reset the highlight when a readonly input is blurred', fakeAsync(() => {
926-
const fixture = createComponent(MatInputWithReadonlyInput);
927-
fixture.detectChanges();
928-
929-
const inputDebugElement = fixture.debugElement.query(By.directive(MatInput))!;
930-
const input = inputDebugElement.injector.get<MatInput>(MatInput);
931-
const container = fixture.debugElement.query(By.css('mat-form-field'))!.nativeElement;
932-
933-
fixture.componentInstance.isReadonly = false;
934-
fixture.detectChanges();
935-
936-
// Call the focus handler directly to avoid flakyness where
937-
// browsers don't focus elements if the window is minimized.
938-
input._focusChanged(true);
939-
fixture.detectChanges();
940-
941-
expect(input.focused).toBe(true);
942-
expect(container.classList).toContain('mat-focused');
943-
944-
fixture.componentInstance.isReadonly = true;
945-
fixture.detectChanges();
946-
947-
input._focusChanged(false);
948-
fixture.detectChanges();
949-
950-
expect(input.focused).toBe(false);
951-
expect(container.classList).not.toContain('mat-focused');
952-
}));
953-
954908
it('should only show the native placeholder, when there is a label, on focus', () => {
955909
const fixture = createComponent(MatInputWithLabelAndPlaceholder);
956910
fixture.detectChanges();
@@ -2117,17 +2071,6 @@ class MatInputOnPush {
21172071
formControl = new FormControl('');
21182072
}
21192073

2120-
@Component({
2121-
template: `
2122-
<mat-form-field>
2123-
<input matInput [readonly]="isReadonly" value="Only for reading">
2124-
</mat-form-field>
2125-
`
2126-
})
2127-
class MatInputWithReadonlyInput {
2128-
isReadonly = true;
2129-
}
2130-
21312074
@Component({
21322075
template: `
21332076
<mat-form-field>

src/material/input/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export class MatInput extends _MatInputBase implements MatFormFieldControl<any>,
350350
@HostListener('blur', ['false'])
351351
// tslint:enable:no-host-decorator-in-concrete
352352
_focusChanged(isFocused: boolean) {
353-
if (isFocused !== this.focused && (!this.readonly || !isFocused)) {
353+
if (isFocused !== this.focused) {
354354
this.focused = isFocused;
355355
this.stateChanges.next();
356356
}

0 commit comments

Comments
 (0)