Skip to content

fix(material/input): show focus indication for readonly inputs #22847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -351,7 +351,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