Skip to content

fix(list): don't handle selection keys while using typeahead in selection list #17826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DOWN_ARROW, SPACE, ENTER, UP_ARROW, HOME, END, A} from '@angular/cdk/keycodes';
import {DOWN_ARROW, SPACE, ENTER, UP_ARROW, HOME, END, A, D} from '@angular/cdk/keycodes';
import {
createKeyboardEvent,
dispatchFakeEvent,
Expand Down Expand Up @@ -501,6 +501,44 @@ describe('MatSelectionList without forms', () => {
expect(manager.activeItemIndex).toBe(3);
}));

it('should be able to skip to an item by typing', fakeAsync(() => {
const manager = selectionList.componentInstance._keyManager;

expect(manager.activeItemIndex).not.toBe(3);

const event = createKeyboardEvent('keydown', D, 'd');
selectionList.componentInstance._keydown(event);
fixture.detectChanges();
tick(200);

expect(manager.activeItemIndex).toBe(3);
}));

it('should not select items while using the typeahead', fakeAsync(() => {
const manager = selectionList.componentInstance._keyManager;
const testListItem = listOptions[1].nativeElement as HTMLElement;
const model =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;

dispatchFakeEvent(testListItem, 'focus');
fixture.detectChanges();

expect(manager.activeItemIndex).toBe(1);
expect(model.isEmpty()).toBe(true);

selectionList.componentInstance._keydown(createKeyboardEvent('keydown', D, 'd'));
fixture.detectChanges();
tick(100); // Tick only half the typeahead timeout.

selectionList.componentInstance._keydown(
createKeyboardEvent('keydown', SPACE, undefined, testListItem));
fixture.detectChanges();
tick(100); // Tick the rest of the timeout.

expect(manager.activeItemIndex).toBe(3);
expect(model.isEmpty()).toBe(true);
}));

it('should be able to select all options', () => {
const list: MatSelectionList = selectionList.componentInstance;

Expand Down
4 changes: 2 additions & 2 deletions src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
switch (keyCode) {
case SPACE:
case ENTER:
if (!hasModifier) {
if (!hasModifier && !manager.isTyping()) {
this._toggleFocusedOption();
// Always prevent space from scrolling the page since the list has focus
event.preventDefault();
Expand All @@ -504,7 +504,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements CanD
}
break;
case A:
if (hasModifierKey(event, 'ctrlKey')) {
if (hasModifierKey(event, 'ctrlKey') && !manager.isTyping()) {
this.options.find(option => !option.selected) ? this.selectAll() : this.deselectAll();
event.preventDefault();
}
Expand Down