Skip to content

Commit 6b80423

Browse files
committed
fix(autocomplete): error when typing in input with disabled autocomplete and no panel
Fixes an edge case where the input will throw an error if it has a disabled autocomplete and no autocomplete panel. Fixes #11876.
1 parent d96adbe commit 6b80423

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
328328
this.activeOption._selectViaInteraction();
329329
this._resetActiveItem();
330330
event.preventDefault();
331-
} else {
331+
} else if (this.autocomplete) {
332332
const prevActiveItem = this.autocomplete._keyManager.activeItem;
333333
const isArrowKey = keyCode === UP_ARROW || keyCode === DOWN_ARROW;
334334

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,16 @@ describe('MatAutocomplete', () => {
522522
expect(boundingBox.getAttribute('dir')).toEqual('ltr');
523523
});
524524

525+
it('should not throw when typing in an element with a null and disabled autocomplete', () => {
526+
const fixture = createComponent(InputWithoutAutocompleteAndDisabled);
527+
fixture.detectChanges();
528+
529+
expect(() => {
530+
dispatchKeyboardEvent(fixture.nativeElement.querySelector('input'), 'keydown', SPACE);
531+
fixture.detectChanges();
532+
}).not.toThrow();
533+
});
534+
525535
describe('forms integration', () => {
526536
let fixture: ComponentFixture<SimpleAutocomplete>;
527537
let input: HTMLInputElement;
@@ -2377,3 +2387,10 @@ class AutocompleteWithDifferentOrigin {
23772387
selectedValue: string;
23782388
values = ['one', 'two', 'three'];
23792389
}
2390+
2391+
2392+
@Component({
2393+
template: '<input [matAutocomplete]="null" matAutocompleteDisabled>'
2394+
})
2395+
class InputWithoutAutocompleteAndDisabled {
2396+
}

0 commit comments

Comments
 (0)