Skip to content

Commit 726533b

Browse files
crisbetovictoriaaa234
authored andcommitted
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.
1 parent 00b153b commit 726533b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
334334
this.activeOption._selectViaInteraction();
335335
this._resetActiveItem();
336336
event.preventDefault();
337-
} else {
337+
} else if (this.autocomplete) {
338338
const prevActiveItem = this.autocomplete._keyManager.activeItem;
339339
const isArrowKey = keyCode === UP_ARROW || keyCode === DOWN_ARROW;
340340

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ describe('MatAutocomplete', () => {
531531
expect(input.getAttribute('autocomplete')).toBe('changed');
532532
});
533533

534+
it('should not throw when typing in an element with a null and disabled autocomplete', () => {
535+
const fixture = createComponent(InputWithoutAutocompleteAndDisabled);
536+
fixture.detectChanges();
537+
538+
expect(() => {
539+
dispatchKeyboardEvent(fixture.nativeElement.querySelector('input'), 'keydown', SPACE);
540+
fixture.detectChanges();
541+
}).not.toThrow();
542+
});
543+
534544
describe('forms integration', () => {
535545
let fixture: ComponentFixture<SimpleAutocomplete>;
536546
let input: HTMLInputElement;
@@ -2464,7 +2474,6 @@ class AutocompleteWithDifferentOrigin {
24642474
values = ['one', 'two', 'three'];
24652475
}
24662476

2467-
24682477
@Component({
24692478
template: `
24702479
<input autocomplete="changed" [(ngModel)]="value" [matAutocomplete]="auto"/>
@@ -2474,3 +2483,9 @@ class AutocompleteWithDifferentOrigin {
24742483
class AutocompleteWithNativeAutocompleteAttribute {
24752484
value: string;
24762485
}
2486+
2487+
@Component({
2488+
template: '<input [matAutocomplete]="null" matAutocompleteDisabled>'
2489+
})
2490+
class InputWithoutAutocompleteAndDisabled {
2491+
}

0 commit comments

Comments
 (0)