Skip to content

fix(selection-list): support selecting all via ctrl + a #11502

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 1 commit into from
Jun 7, 2018
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
44 changes: 43 additions & 1 deletion src/lib/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} from '@angular/cdk/keycodes';
import {DOWN_ARROW, SPACE, ENTER, UP_ARROW, HOME, END, A} from '@angular/cdk/keycodes';
import {
createKeyboardEvent,
dispatchFakeEvent,
Expand Down Expand Up @@ -321,6 +321,48 @@ describe('MatSelectionList without forms', () => {
expect(event.defaultPrevented).toBe(true);
});

it('should select all items using ctrl + a', () => {
const event = createKeyboardEvent('keydown', A, selectionList.nativeElement);
Object.defineProperty(event, 'ctrlKey', {get: () => true});

expect(listOptions.some(option => option.componentInstance.selected)).toBe(false);

dispatchEvent(selectionList.nativeElement, event);
fixture.detectChanges();

expect(listOptions.every(option => option.componentInstance.selected)).toBe(true);
});

it('should select all items using ctrl + a if some items are selected', () => {
const event = createKeyboardEvent('keydown', A, selectionList.nativeElement);
Object.defineProperty(event, 'ctrlKey', {get: () => true});

listOptions.slice(0, 2).forEach(option => option.componentInstance.selected = true);
fixture.detectChanges();

expect(listOptions.some(option => option.componentInstance.selected)).toBe(true);

dispatchEvent(selectionList.nativeElement, event);
fixture.detectChanges();

expect(listOptions.every(option => option.componentInstance.selected)).toBe(true);
});

it('should deselect all with ctrl + a if all options are selected', () => {
const event = createKeyboardEvent('keydown', A, selectionList.nativeElement);
Object.defineProperty(event, 'ctrlKey', {get: () => true});

listOptions.forEach(option => option.componentInstance.selected = true);
fixture.detectChanges();

expect(listOptions.every(option => option.componentInstance.selected)).toBe(true);

dispatchEvent(selectionList.nativeElement, event);
fixture.detectChanges();

expect(listOptions.every(option => option.componentInstance.selected)).toBe(false);
});

it('should be able to jump focus down to an item by typing', fakeAsync(() => {
const listEl = selectionList.nativeElement;
const manager = selectionList.componentInstance._keyManager;
Expand Down
8 changes: 7 additions & 1 deletion src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {FocusableOption, FocusKeyManager} from '@angular/cdk/a11y';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {SelectionModel} from '@angular/cdk/collections';
import {SPACE, ENTER, HOME, END, UP_ARROW, DOWN_ARROW} from '@angular/cdk/keycodes';
import {SPACE, ENTER, HOME, END, UP_ARROW, DOWN_ARROW, A} from '@angular/cdk/keycodes';
import {
AfterContentInit,
Attribute,
Expand Down Expand Up @@ -394,6 +394,12 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
keyCode === HOME ? manager.setFirstItemActive() : manager.setLastItemActive();
event.preventDefault();
break;
case A:
if (event.ctrlKey) {
this.options.find(option => !option.selected) ? this.selectAll() : this.deselectAll();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the extra check necessary? Looks like _setAllOptionsSelected already checks the existing state before considering the item "changed"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to know whether there's at least one unselected option. The intended functionality is "deselect all if all are selected, otherwise select all".

event.preventDefault();
}
break;
default:
manager.onKeydown(event);
}
Expand Down