Skip to content

Commit

Permalink
fix(autocomplete): prevent opening using arrow keys on readonly input (
Browse files Browse the repository at this point in the history
…#9229)

A while back we started blocking being able to open an autocomplete by focusing a readonly input, however that didn't cover opening using the keyboard shortcuts. These changes also prevent users from being able to open using the keyboard.

Fixes #9227.
  • Loading branch information
crisbeto authored and jelbourn committed Jan 24, 2018
1 parent 1e7eeab commit 9d152c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,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 @@ -319,14 +319,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 @@ -524,4 +524,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

0 comments on commit 9d152c0

Please sign in to comment.