Skip to content

fix(mat-selection-list): do not allow ctrl + a when mat-selection-list is disabled #12543

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
38 changes: 35 additions & 3 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ describe('MatSelectionList without forms', () => {
dispatchEvent(selectionList.nativeElement, event);
fixture.detectChanges();

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

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

Expand Down Expand Up @@ -364,6 +364,23 @@ describe('MatSelectionList without forms', () => {
expect(listOptions.every(option => option.componentInstance.selected)).toBe(false);
});

it('should select only non-disabled options when using ctrl + a', () => {
const event = createKeyboardEvent('keydown', A, selectionList.nativeElement);
Object.defineProperty(event, 'ctrlKey', {get: () => true});

const selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;

expect(selectList.selected.length).toBe(0);
expect(listOptions[0].componentInstance.selected).toBe(false);

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

expect(selectList.selected.length).toBe(3);
expect(listOptions[0].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 Expand Up @@ -391,7 +408,7 @@ describe('MatSelectionList without forms', () => {
list.selectAll();
fixture.detectChanges();

expect(list.options.toArray().every(option => option.selected)).toBe(true);
expect(list.options.toArray().filter(option => option.selected).length).toBe(3);
});

it('should be able to deselect all options', () => {
Expand Down Expand Up @@ -628,6 +645,21 @@ describe('MatSelectionList without forms', () => {
expect(selectList.selected.length).toBe(0);
});

it('should not allow select all using ctrl + a on disabled selection-list', () => {
const event = createKeyboardEvent('keydown', A, selectionList.nativeElement);
Object.defineProperty(event, 'ctrlKey', {get: () => true});

const selectList =
selectionList.injector.get<MatSelectionList>(MatSelectionList).selectedOptions;

expect(selectList.selected.length).toBe(0);

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

expect(selectList.selected.length).toBe(0);
});

it('should update state of options if list state has changed', () => {
// To verify that the template of the list options has been re-rendered after the disabled
// property of the selection list has been updated, the ripple directive can be used.
Expand Down
4 changes: 2 additions & 2 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
event.preventDefault();
break;
case A:
if (event.ctrlKey) {
if (event.ctrlKey && !this.disabled) {
this.options.find(option => !option.selected) ? this.selectAll() : this.deselectAll();
event.preventDefault();
}
Expand Down Expand Up @@ -519,7 +519,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
let hasChanged = false;

this.options.forEach(option => {
if (option._setSelected(isSelected)) {
if (!option.disabled && option._setSelected(isSelected)) {
hasChanged = true;
}
});
Expand Down