Skip to content

Commit 2349166

Browse files
crisbetojelbourn
authored andcommitted
fix(select): skip disabled options when using ctrl + a (#12553)
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 3596e9d commit 2349166

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
@@ -3966,6 +3966,33 @@ describe('MatSelect', () => {
39663966
]);
39673967
});
39683968

3969+
it('should skip disabled options when using ctrl + a', () => {
3970+
const selectElement = fixture.nativeElement.querySelector('mat-select');
3971+
const options = fixture.componentInstance.options.toArray();
3972+
3973+
for (let i = 0; i < 3; i++) {
3974+
options[i].disabled = true;
3975+
}
3976+
3977+
expect(testInstance.control.value).toBeFalsy();
3978+
3979+
fixture.componentInstance.select.open();
3980+
fixture.detectChanges();
3981+
3982+
const event = createKeyboardEvent('keydown', A, selectElement);
3983+
Object.defineProperty(event, 'ctrlKey', {get: () => true});
3984+
dispatchEvent(selectElement, event);
3985+
fixture.detectChanges();
3986+
3987+
expect(testInstance.control.value).toEqual([
3988+
'sandwich-3',
3989+
'chips-4',
3990+
'eggs-5',
3991+
'pasta-6',
3992+
'sushi-7'
3993+
]);
3994+
});
3995+
39693996
it('should select all options when pressing ctrl + a when some options are selected', () => {
39703997
const selectElement = fixture.nativeElement.querySelector('mat-select');
39713998
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
@@ -708,8 +708,13 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
708708
manager.activeItem._selectViaInteraction();
709709
} else if (this._multiple && keyCode === A && event.ctrlKey) {
710710
event.preventDefault();
711-
const hasDeselectedOptions = this.options.some(option => !option.selected);
712-
this.options.forEach(option => hasDeselectedOptions ? option.select() : option.deselect());
711+
const hasDeselectedOptions = this.options.some(opt => !opt.disabled && !opt.selected);
712+
713+
this.options.forEach(option => {
714+
if (!option.disabled) {
715+
hasDeselectedOptions ? option.select() : option.deselect();
716+
}
717+
});
713718
} else {
714719
const previouslyFocusedIndex = manager.activeItemIndex;
715720

0 commit comments

Comments
 (0)