Skip to content

Commit

Permalink
fix(material/input): show focus indication for readonly inputs (#22847)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crisbeto authored Jul 16, 2021
1 parent d090617 commit 4404d0a
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 115 deletions.
57 changes: 0 additions & 57 deletions src/material-experimental/mdc-input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,52 +770,6 @@ describe('MatMdcInput without forms', () => {
expect(container.classList).not.toContain('mat-focused');
}));

it('should not highlight when focusing a readonly input', fakeAsync(() => {
let fixture = createComponent(MatInputWithReadonlyInput);
fixture.detectChanges();

let input =
fixture.debugElement.query(By.directive(MatInput))!.injector.get<MatInput>(MatInput);
let container = fixture.debugElement.query(By.css('.mat-mdc-form-field'))!.nativeElement;

// Call the focus handler directly to avoid flakiness where
// browsers don't focus elements if the window is minimized.
input._focusChanged(true);
fixture.detectChanges();

expect(input.focused).toBe(false);
expect(container.classList).not.toContain('mat-focused');
}));

it('should reset the highlight when a readonly input is blurred', fakeAsync(() => {
const fixture = createComponent(MatInputWithReadonlyInput);
fixture.detectChanges();

const inputDebugElement = fixture.debugElement.query(By.directive(MatInput))!;
const input = inputDebugElement.injector.get<MatInput>(MatInput);
const container = fixture.debugElement.query(By.css('.mat-mdc-form-field'))!.nativeElement;

fixture.componentInstance.isReadonly = false;
fixture.detectChanges();

// Call the focus handler directly to avoid flakyness where
// browsers don't focus elements if the window is minimized.
input._focusChanged(true);
fixture.detectChanges();

expect(input.focused).toBe(true);
expect(container.classList).toContain('mat-focused');

fixture.componentInstance.isReadonly = true;
fixture.detectChanges();

input._focusChanged(false);
fixture.detectChanges();

expect(input.focused).toBe(false);
expect(container.classList).not.toContain('mat-focused');
}));

it('should only show the native control placeholder, when there is a label, on focus', () => {
const fixture = createComponent(MatInputWithLabelAndPlaceholder);
fixture.detectChanges();
Expand Down Expand Up @@ -1654,17 +1608,6 @@ class MatInputOnPush {
formControl = new FormControl('');
}

@Component({
template: `
<mat-form-field>
<input matInput [readonly]="isReadonly" value="Only for reading">
</mat-form-field>
`
})
class MatInputWithReadonlyInput {
isReadonly = true;
}

@Component({
template: `
<mat-form-field>
Expand Down
57 changes: 0 additions & 57 deletions src/material/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,52 +905,6 @@ describe('MatInput without forms', () => {
expect(() => formField._animateAndLockLabel()).not.toThrow();
}));

it('should not highlight when focusing a readonly input', fakeAsync(() => {
const fixture = createComponent(MatInputWithReadonlyInput);
fixture.detectChanges();

const input =
fixture.debugElement.query(By.directive(MatInput))!.injector.get<MatInput>(MatInput);
const container = fixture.debugElement.query(By.css('mat-form-field'))!.nativeElement;

// Call the focus handler directly to avoid flakyness where
// browsers don't focus elements if the window is minimized.
input._focusChanged(true);
fixture.detectChanges();

expect(input.focused).toBe(false);
expect(container.classList).not.toContain('mat-focused');
}));

it('should reset the highlight when a readonly input is blurred', fakeAsync(() => {
const fixture = createComponent(MatInputWithReadonlyInput);
fixture.detectChanges();

const inputDebugElement = fixture.debugElement.query(By.directive(MatInput))!;
const input = inputDebugElement.injector.get<MatInput>(MatInput);
const container = fixture.debugElement.query(By.css('mat-form-field'))!.nativeElement;

fixture.componentInstance.isReadonly = false;
fixture.detectChanges();

// Call the focus handler directly to avoid flakyness where
// browsers don't focus elements if the window is minimized.
input._focusChanged(true);
fixture.detectChanges();

expect(input.focused).toBe(true);
expect(container.classList).toContain('mat-focused');

fixture.componentInstance.isReadonly = true;
fixture.detectChanges();

input._focusChanged(false);
fixture.detectChanges();

expect(input.focused).toBe(false);
expect(container.classList).not.toContain('mat-focused');
}));

it('should only show the native placeholder, when there is a label, on focus', () => {
const fixture = createComponent(MatInputWithLabelAndPlaceholder);
fixture.detectChanges();
Expand Down Expand Up @@ -2117,17 +2071,6 @@ class MatInputOnPush {
formControl = new FormControl('');
}

@Component({
template: `
<mat-form-field>
<input matInput [readonly]="isReadonly" value="Only for reading">
</mat-form-field>
`
})
class MatInputWithReadonlyInput {
isReadonly = true;
}

@Component({
template: `
<mat-form-field>
Expand Down
2 changes: 1 addition & 1 deletion src/material/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export class MatInput extends _MatInputBase implements MatFormFieldControl<any>,
@HostListener('blur', ['false'])
// tslint:enable:no-host-decorator-in-concrete
_focusChanged(isFocused: boolean) {
if (isFocused !== this.focused && (!this.readonly || !isFocused)) {
if (isFocused !== this.focused) {
this.focused = isFocused;
this.stateChanges.next();
}
Expand Down

0 comments on commit 4404d0a

Please sign in to comment.