Skip to content

Commit ae60fb2

Browse files
committed
fix(material/input): show focus indication for readonly inputs
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.
1 parent ade0901 commit ae60fb2

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
@@ -901,52 +901,6 @@ describe('MatInput without forms', () => {
901901
expect(() => formField._animateAndLockLabel()).not.toThrow();
902902
}));
903903

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

2116-
@Component({
2117-
template: `
2118-
<mat-form-field>
2119-
<input matInput [readonly]="isReadonly" value="Only for reading">
2120-
</mat-form-field>
2121-
`
2122-
})
2123-
class MatInputWithReadonlyInput {
2124-
isReadonly = true;
2125-
}
2126-
21272070
@Component({
21282071
template: `
21292072
<mat-form-field>

src/material/input/input.ts

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

0 commit comments

Comments
 (0)