From 17bbe3da268a206c5831a74f062d4912b7236594 Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Fri, 13 Dec 2024 11:55:09 -0700 Subject: [PATCH] fix(material/list): enable MacOS select all with command+a --- src/material/list/selection-list.spec.ts | 25 +++++++++++++++++++++++- src/material/list/selection-list.ts | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/material/list/selection-list.spec.ts b/src/material/list/selection-list.spec.ts index ec13b1283864..fa623c82ab9e 100644 --- a/src/material/list/selection-list.spec.ts +++ b/src/material/list/selection-list.spec.ts @@ -363,7 +363,7 @@ describe('MatSelectionList without forms', () => { expect(event.defaultPrevented).toBe(true); }); - it('should select all items using ctrl + a', () => { + it('should select and deselect all items using ctrl + a', () => { listOptions.forEach(option => (option.componentInstance.disabled = false)); fixture.changeDetectorRef.markForCheck(); fixture.detectChanges(); @@ -375,6 +375,29 @@ describe('MatSelectionList without forms', () => { fixture.detectChanges(); expect(listOptions.every(option => option.componentInstance.selected)).toBe(true); + + dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {control: true}); + fixture.detectChanges(); + + expect(listOptions.every(option => option.componentInstance.selected)).toBe(false); + }); + + it('should select and deselect all items using meta + a', () => { + listOptions.forEach(option => (option.componentInstance.disabled = false)); + fixture.changeDetectorRef.markForCheck(); + 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); + dispatchKeyboardEvent(listOptions[2].nativeElement, 'keydown', A, 'A', {meta: true}); + fixture.detectChanges(); + + expect(listOptions.every(option => option.componentInstance.selected)).toBe(false); }); it('should not select disabled items when pressing ctrl + a', () => { diff --git a/src/material/list/selection-list.ts b/src/material/list/selection-list.ts index c75002c2e38c..b17d1fe73e8b 100644 --- a/src/material/list/selection-list.ts +++ b/src/material/list/selection-list.ts @@ -375,7 +375,7 @@ export class MatSelectionList event.keyCode === A && this.multiple && !this._keyManager.isTyping() && - hasModifierKey(event, 'ctrlKey') + hasModifierKey(event, 'ctrlKey', 'metaKey') ) { const shouldSelect = this.options.some(option => !option.disabled && !option.selected); event.preventDefault();