Skip to content

Commit 0fbd3e7

Browse files
authored
fix(material/autocomplete): error if keydown is dispatched too early (#22513)
Fixes an error that is thrown if a `keydown` event is dispatched before the autocomplete panel has been initialized. Fixes #22511.
1 parent 04d2f77 commit 0fbd3e7

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/material-experimental/mdc-autocomplete/autocomplete.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,21 @@ describe('MDC-based MatAutocomplete', () => {
24462446
expect(fixture.componentInstance.selectedValue).toBe(1337);
24472447
}));
24482448

2449+
it('should not focus the option when DOWN key is pressed', fakeAsync(() => {
2450+
const fixture = createComponent(SimpleAutocomplete);
2451+
const input = fixture.debugElement.query(By.css('input'))!.nativeElement;
2452+
fixture.detectChanges();
2453+
const spy = spyOn(console, 'error');
2454+
2455+
dispatchKeyboardEvent(input, 'keydown', DOWN_ARROW);
2456+
dispatchKeyboardEvent(input, 'keydown', DOWN_ARROW);
2457+
fixture.detectChanges();
2458+
2459+
// Note: for some reason the error here gets logged using console.error, rather than being
2460+
// thrown, hence why we use a spy to assert against it, rather than `.not.toThrow`.
2461+
expect(spy).not.toHaveBeenCalled();
2462+
}));
2463+
24492464
});
24502465

24512466
it('should have correct width when opened', () => {

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
737737
// scroll the list to the top. This is better UX than scrolling the list to the
738738
// top of the option, because it allows the user to read the top group's label.
739739
autocomplete._setScrollTop(0);
740-
} else {
740+
} else if (autocomplete.panel) {
741741
const option = autocomplete.options.toArray()[index];
742742

743743
if (option) {

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,21 @@ describe('MatAutocomplete', () => {
24432443
expect(fixture.componentInstance.selectedValue).toBe(1337);
24442444
}));
24452445

2446+
it('should not focus the option when DOWN key is pressed', fakeAsync(() => {
2447+
const fixture = createComponent(SimpleAutocomplete);
2448+
const input = fixture.debugElement.query(By.css('input'))!.nativeElement;
2449+
fixture.detectChanges();
2450+
const spy = spyOn(console, 'error');
2451+
2452+
dispatchKeyboardEvent(input, 'keydown', DOWN_ARROW);
2453+
dispatchKeyboardEvent(input, 'keydown', DOWN_ARROW);
2454+
fixture.detectChanges();
2455+
2456+
// Note: for some reason the error here gets logged using console.error, rather than being
2457+
// thrown, hence why we use a spy to assert against it, rather than `.not.toThrow`.
2458+
expect(spy).not.toHaveBeenCalled();
2459+
}));
2460+
24462461
});
24472462

24482463
it('should have correct width when opened', () => {

0 commit comments

Comments
 (0)