Skip to content

Commit 7f3c0b9

Browse files
crisbetommalerba
authored andcommitted
fix(select): skip disabled options when using ctrl + a
Along the same lines as #12543. Currently `mat-select` will select all options when pressing ctrl + a, no matter whether they're disabled. These changes add an extra check to ensure that the disabled ones are skipped.
1 parent 028746a commit 7f3c0b9

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/lib/select/select.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3989,6 +3989,33 @@ describe('MatSelect', () => {
39893989
]);
39903990
});
39913991

3992+
it('should skip disabled options when using ctrl + a', () => {
3993+
const selectElement = fixture.nativeElement.querySelector('mat-select');
3994+
const options = fixture.componentInstance.options.toArray();
3995+
3996+
for (let i = 0; i < 3; i++) {
3997+
options[i].disabled = true;
3998+
}
3999+
4000+
expect(testInstance.control.value).toBeFalsy();
4001+
4002+
fixture.componentInstance.select.open();
4003+
fixture.detectChanges();
4004+
4005+
const event = createKeyboardEvent('keydown', A, selectElement);
4006+
Object.defineProperty(event, 'ctrlKey', {get: () => true});
4007+
dispatchEvent(selectElement, event);
4008+
fixture.detectChanges();
4009+
4010+
expect(testInstance.control.value).toEqual([
4011+
'sandwich-3',
4012+
'chips-4',
4013+
'eggs-5',
4014+
'pasta-6',
4015+
'sushi-7'
4016+
]);
4017+
});
4018+
39924019
it('should select all options when pressing ctrl + a when some options are selected', () => {
39934020
const selectElement = fixture.nativeElement.querySelector('mat-select');
39944021
const options = fixture.componentInstance.options.toArray();

src/lib/select/select.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,13 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
714714
manager.activeItem._selectViaInteraction();
715715
} else if (this._multiple && keyCode === A && event.ctrlKey) {
716716
event.preventDefault();
717-
const hasDeselectedOptions = this.options.some(option => !option.selected);
718-
this.options.forEach(option => hasDeselectedOptions ? option.select() : option.deselect());
717+
const hasDeselectedOptions = this.options.some(opt => !opt.disabled && !opt.selected);
718+
719+
this.options.forEach(option => {
720+
if (!option.disabled) {
721+
hasDeselectedOptions ? option.select() : option.deselect();
722+
}
723+
});
719724
} else {
720725
const previouslyFocusedIndex = manager.activeItemIndex;
721726

0 commit comments

Comments
 (0)