From 6b1a672c4051ace1957cf2e2d0661d2a3b7cf3c6 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 10 Jul 2018 16:33:56 +0200 Subject: [PATCH] fix(autocomplete): error when typing in input with disabled autocomplete 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. --- src/lib/autocomplete/autocomplete-trigger.ts | 2 +- src/lib/autocomplete/autocomplete.spec.ts | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/lib/autocomplete/autocomplete-trigger.ts b/src/lib/autocomplete/autocomplete-trigger.ts index 6116754b087..f60c5ec8bb0 100644 --- a/src/lib/autocomplete/autocomplete-trigger.ts +++ b/src/lib/autocomplete/autocomplete-trigger.ts @@ -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; diff --git a/src/lib/autocomplete/autocomplete.spec.ts b/src/lib/autocomplete/autocomplete.spec.ts index 8bd6d3521b1..adca356ce05 100644 --- a/src/lib/autocomplete/autocomplete.spec.ts +++ b/src/lib/autocomplete/autocomplete.spec.ts @@ -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; let input: HTMLInputElement; @@ -2464,7 +2474,6 @@ class AutocompleteWithDifferentOrigin { values = ['one', 'two', 'three']; } - @Component({ template: ` @@ -2474,3 +2483,9 @@ class AutocompleteWithDifferentOrigin { class AutocompleteWithNativeAutocompleteAttribute { value: string; } + +@Component({ + template: '' +}) +class InputWithoutAutocompleteAndDisabled { +}