Skip to content

Commit

Permalink
fix(autocomplete): error when typing in input with disabled autocompl…
Browse files Browse the repository at this point in the history
…ete and no panel (#11881)

Fixes an edge case where the input will throw an error if it has a disabled autocomplete and no autocomplete panel.

Fixes #11876.
  • Loading branch information
crisbeto authored and victoriaaa234 committed Jul 25, 2018
1 parent 00b153b commit 726533b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/autocomplete/autocomplete-trigger.ts
Expand Up @@ -334,7 +334,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
this.activeOption._selectViaInteraction();
this._resetActiveItem();
event.preventDefault();
} else {
} else if (this.autocomplete) {
const prevActiveItem = this.autocomplete._keyManager.activeItem;
const isArrowKey = keyCode === UP_ARROW || keyCode === DOWN_ARROW;

Expand Down
17 changes: 16 additions & 1 deletion src/lib/autocomplete/autocomplete.spec.ts
Expand Up @@ -531,6 +531,16 @@ describe('MatAutocomplete', () => {
expect(input.getAttribute('autocomplete')).toBe('changed');
});

it('should not throw when typing in an element with a null and disabled autocomplete', () => {
const fixture = createComponent(InputWithoutAutocompleteAndDisabled);
fixture.detectChanges();

expect(() => {
dispatchKeyboardEvent(fixture.nativeElement.querySelector('input'), 'keydown', SPACE);
fixture.detectChanges();
}).not.toThrow();
});

describe('forms integration', () => {
let fixture: ComponentFixture<SimpleAutocomplete>;
let input: HTMLInputElement;
Expand Down Expand Up @@ -2464,7 +2474,6 @@ class AutocompleteWithDifferentOrigin {
values = ['one', 'two', 'three'];
}


@Component({
template: `
<input autocomplete="changed" [(ngModel)]="value" [matAutocomplete]="auto"/>
Expand All @@ -2474,3 +2483,9 @@ class AutocompleteWithDifferentOrigin {
class AutocompleteWithNativeAutocompleteAttribute {
value: string;
}

@Component({
template: '<input [matAutocomplete]="null" matAutocompleteDisabled>'
})
class InputWithoutAutocompleteAndDisabled {
}

0 comments on commit 726533b

Please sign in to comment.