Skip to content
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

fix(autocomplete): prevent opening using arrow keys on readonly input #9229

Merged
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
12 changes: 9 additions & 3 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {

if (this.panelOpen || keyCode === TAB) {
this.autocomplete._keyManager.onKeydown(event);
} else if (isArrowKey) {
} else if (isArrowKey && this._canOpen()) {
this.openPanel();
}

Expand All @@ -308,14 +308,14 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
// We need to ensure that the input is focused, because IE will fire the `input`
// event on focus/blur/load if the input has a placeholder. See:
// https://connect.microsoft.com/IE/feedback/details/885747/
if (document.activeElement === event.target) {
if (this._canOpen() && document.activeElement === event.target) {
this._onChange((event.target as HTMLInputElement).value);
this.openPanel();
}
}

_handleFocus(): void {
if (!this._element.nativeElement.readOnly) {
if (this._canOpen()) {
this._attachOverlay();
this._floatLabel(true);
}
Expand Down Expand Up @@ -513,4 +513,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
this.autocomplete._keyManager.setActiveItem(-1);
}

/** Determines whether the panel can be opened. */
private _canOpen(): boolean {
const element: HTMLInputElement = this._element.nativeElement;
return !element.readOnly && !element.disabled;
}

}
13 changes: 13 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ describe('MatAutocomplete', () => {
expect(trigger.panelOpen).toBe(false, 'Expected panel to stay closed.');
}));

it('should not open using the arrow keys when the input is readonly', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
input.readOnly = true;
fixture.detectChanges();

expect(trigger.panelOpen).toBe(false, 'Expected panel state to start out closed.');
dispatchKeyboardEvent(input, 'keydown', DOWN_ARROW);
flush();

fixture.detectChanges();
expect(trigger.panelOpen).toBe(false, 'Expected panel to stay closed.');
}));

it('should open the panel programmatically', () => {
expect(fixture.componentInstance.trigger.panelOpen)
.toBe(false, `Expected panel state to start out closed.`);
Expand Down