Skip to content

fix(input): unable to reset focused state of readonly input #14698

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
Jan 3, 2019
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
35 changes: 33 additions & 2 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,35 @@ describe('MatInput without forms', () => {
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 @@ -1833,11 +1862,13 @@ class MatInputOnPush {
@Component({
template: `
<mat-form-field>
<input matInput readonly value="Only for reading">
<input matInput [readonly]="isReadonly" value="Only for reading">
</mat-form-field>
`
})
class MatInputWithReadonlyInput {}
class MatInputWithReadonlyInput {
isReadonly = true;
}

@Component({
template: `
Expand Down
6 changes: 4 additions & 2 deletions src/lib/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,13 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
}

/** Focuses the input. */
focus(): void { this._elementRef.nativeElement.focus(); }
focus(): void {
this._elementRef.nativeElement.focus();
}

/** Callback for the cases where the focused state of the input changes. */
_focusChanged(isFocused: boolean) {
if (isFocused !== this.focused && !this.readonly) {
if (isFocused !== this.focused && (!this.readonly || !isFocused)) {
this.focused = isFocused;
this.stateChanges.next();
}
Expand Down