Skip to content

Commit ad42a1b

Browse files
crisbetojelbourn
authored andcommitted
fix(list): don't handle selection keys while using typeahead in selection list (#17826)
Along the same lines as #17785. Doesn't handle space key presses while the user is using the typeahead so that we don't interrupt their typing.
1 parent 3b3c2ca commit ad42a1b

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/material/list/selection-list.spec.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {DOWN_ARROW, SPACE, ENTER, UP_ARROW, HOME, END, A} from '@angular/cdk/keycodes';
1+
import {DOWN_ARROW, SPACE, ENTER, UP_ARROW, HOME, END, A, D} from '@angular/cdk/keycodes';
22
import {
33
createKeyboardEvent,
44
dispatchFakeEvent,
@@ -501,6 +501,44 @@ describe('MatSelectionList without forms', () => {
501501
expect(manager.activeItemIndex).toBe(3);
502502
}));
503503

504+
it('should be able to skip to an item by typing', fakeAsync(() => {
505+
const manager = selectionList.componentInstance._keyManager;
506+
507+
expect(manager.activeItemIndex).not.toBe(3);
508+
509+
const event = createKeyboardEvent('keydown', D, 'd');
510+
selectionList.componentInstance._keydown(event);
511+
fixture.detectChanges();
512+
tick(200);
513+
514+
expect(manager.activeItemIndex).toBe(3);
515+
}));
516+
517+
it('should not select items while using the typeahead', fakeAsync(() => {
518+
const manager = selectionList.componentInstance._keyManager;
519+
const testListItem = listOptions[1].nativeElement as HTMLElement;
520+
const model =
521+
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;
522+
523+
dispatchFakeEvent(testListItem, 'focus');
524+
fixture.detectChanges();
525+
526+
expect(manager.activeItemIndex).toBe(1);
527+
expect(model.isEmpty()).toBe(true);
528+
529+
selectionList.componentInstance._keydown(createKeyboardEvent('keydown', D, 'd'));
530+
fixture.detectChanges();
531+
tick(100); // Tick only half the typeahead timeout.
532+
533+
selectionList.componentInstance._keydown(
534+
createKeyboardEvent('keydown', SPACE, undefined, testListItem));
535+
fixture.detectChanges();
536+
tick(100); // Tick the rest of the timeout.
537+
538+
expect(manager.activeItemIndex).toBe(3);
539+
expect(model.isEmpty()).toBe(true);
540+
}));
541+
504542
it('should be able to select all options', () => {
505543
const list: MatSelectionList = selectionList.componentInstance;
506544

src/material/list/selection-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
490490
switch (keyCode) {
491491
case SPACE:
492492
case ENTER:
493-
if (!hasModifier) {
493+
if (!hasModifier && !manager.isTyping()) {
494494
this._toggleFocusedOption();
495495
// Always prevent space from scrolling the page since the list has focus
496496
event.preventDefault();
@@ -504,7 +504,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
504504
}
505505
break;
506506
case A:
507-
if (hasModifierKey(event, 'ctrlKey')) {
507+
if (hasModifierKey(event, 'ctrlKey') && !manager.isTyping()) {
508508
this.options.find(option => !option.selected) ? this.selectAll() : this.deselectAll();
509509
event.preventDefault();
510510
}

0 commit comments

Comments
 (0)