Skip to content

fix(material/list): add command+A for MacOs "select all" functionality. #24788

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

Closed
Closed
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
81 changes: 81 additions & 0 deletions src/material-experimental/mdc-list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,87 @@ describe('MDC-based MatSelectionList without forms', () => {
);
});

it('should select all items using command(metaKey) + a', () => {
listOptions.forEach(option => (option.componentInstance.disabled = false));
fixture.detectChanges();

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

listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {meta: true});
fixture.detectChanges();

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

it('should not select disabled items when pressing command(metaKey) + a', () => {
listOptions.slice(0, 2).forEach(option => (option.componentInstance.disabled = true));
fixture.detectChanges();

expect(listOptions.map(option => option.componentInstance.selected)).toEqual([
false,
false,
false,
false,
false,
]);

listOptions[3].nativeElement.focus();
dispatchKeyboardEvent(listOptions[3].nativeElement, 'keydown', A, 'A', {meta: true});
fixture.detectChanges();

expect(listOptions.map(option => option.componentInstance.selected)).toEqual([
false,
false,
true,
true,
true,
]);
});

it('should select all items using command(metaKey) + a if some items are selected', () => {
listOptions.slice(0, 2).forEach(option => (option.componentInstance.selected = true));
fixture.detectChanges();

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

listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {meta: true});
fixture.detectChanges();

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

it('should deselect all with command(metaKey) + a if all options are selected', () => {
listOptions.forEach(option => (option.componentInstance.selected = true));
fixture.detectChanges();

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

listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {meta: true});
fixture.detectChanges();

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

it('should dispatch the selectionChange event when selecting via command(metaKey) + a', () => {
const spy = spyOn(fixture.componentInstance, 'onSelectionChange');
listOptions.forEach(option => (option.componentInstance.disabled = false));
fixture.detectChanges();

listOptions[2].nativeElement.focus();
dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {meta: true});
fixture.detectChanges();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(
jasmine.objectContaining({
options: listOptions.map(option => option.componentInstance),
}),
);
});

it('should be able to jump focus down to an item by typing', fakeAsync(() => {
const firstOption = listOptions[0].nativeElement;

Expand Down
82 changes: 82 additions & 0 deletions src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,88 @@ describe('MatSelectionList without forms', () => {
);
});

it('should select all items using command(metaKey) + a', () => {
listOptions.forEach(option => (option.componentInstance.disabled = false));
const event = createKeyboardEvent('keydown', A, undefined, {meta: 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 not select disabled items when pressing command(metaKey) + a', () => {
const event = createKeyboardEvent('keydown', A, undefined, {meta: true});

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

expect(listOptions.map(option => option.componentInstance.selected)).toEqual([
false,
false,
false,
false,
false,
]);

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

expect(listOptions.map(option => option.componentInstance.selected)).toEqual([
false,
false,
true,
true,
true,
]);
});

it('should select all items using command(metaKey) + a if some items are selected', () => {
const event = createKeyboardEvent('keydown', A, undefined, {meta: 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 command(metaKey) + a if all options are selected', () => {
const event = createKeyboardEvent('keydown', A, undefined, {meta: 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 dispatch the selectionChange event when selecting via command(metaKey) + a', () => {
const spy = spyOn(fixture.componentInstance, 'onSelectionChange');
listOptions.forEach(option => (option.componentInstance.disabled = false));
const event = createKeyboardEvent('keydown', A, undefined, {meta: true});

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

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(
jasmine.objectContaining({
options: listOptions.map(option => option.componentInstance),
}),
);
});

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
2 changes: 1 addition & 1 deletion src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ export class MatSelectionList
if (
keyCode === A &&
this.multiple &&
hasModifierKey(event, 'ctrlKey') &&
hasModifierKey(event, 'ctrlKey', 'metaKey') &&
!manager.isTyping()
) {
const shouldSelect = this.options.some(option => !option.disabled && !option.selected);
Expand Down