diff --git a/src/cdk-experimental/ui-patterns/behaviors/list-selection/list-selection.spec.ts b/src/cdk-experimental/ui-patterns/behaviors/list-selection/list-selection.spec.ts index aceb03718cd2..aab64dbf3970 100644 --- a/src/cdk-experimental/ui-patterns/behaviors/list-selection/list-selection.spec.ts +++ b/src/cdk-experimental/ui-patterns/behaviors/list-selection/list-selection.spec.ts @@ -219,6 +219,34 @@ describe('List Selection', () => { selection.selectOne(); // [0] expect(selection.inputs.value()).toEqual([0]); }); + + it('should do nothing if the current active item is disabled', () => { + const selection = getSelection({multi: signal(true)}); + const items = selection.inputs.items() as TestItem[]; + + selection.inputs.focusManager.focus(items[1]); + selection.select(); + expect(selection.inputs.value()).toEqual([1]); + + selection.inputs.focusManager.focus(items[0]); + items[0].disabled.set(true); + selection.selectOne(); + expect(selection.inputs.value()).toEqual([1]); + }); + + it('should not select an item if the list is not multiselectable and not all items are deselected', () => { + const selection = getSelection({multi: signal(false)}); + const items = selection.inputs.items() as TestItem[]; + + selection.inputs.focusManager.focus(items[1]); + selection.select(); + expect(selection.inputs.value()).toEqual([1]); + + items[1].disabled.set(true); + selection.inputs.focusManager.focus(items[2]); + selection.selectOne(); + expect(selection.inputs.value()).toEqual([1]); + }); }); describe('#selectRange', () => { diff --git a/src/cdk-experimental/ui-patterns/behaviors/list-selection/list-selection.ts b/src/cdk-experimental/ui-patterns/behaviors/list-selection/list-selection.ts index 3fc336327fce..339759cd3f93 100644 --- a/src/cdk-experimental/ui-patterns/behaviors/list-selection/list-selection.ts +++ b/src/cdk-experimental/ui-patterns/behaviors/list-selection/list-selection.ts @@ -120,7 +120,16 @@ export class ListSelection, V> { /** Sets the selection to only the current active item. */ selectOne() { + if (this.inputs.focusManager.activeItem().disabled()) { + return; + } + this.deselectAll(); + + if (this.inputs.value().length > 0 && !this.inputs.multi()) { + return; + } + this.select(); } diff --git a/src/cdk-experimental/ui-patterns/listbox/listbox.spec.ts b/src/cdk-experimental/ui-patterns/listbox/listbox.spec.ts index 615de0149069..fb80cf822d3e 100644 --- a/src/cdk-experimental/ui-patterns/listbox/listbox.spec.ts +++ b/src/cdk-experimental/ui-patterns/listbox/listbox.spec.ts @@ -549,7 +549,7 @@ describe('Listbox Pattern', () => { listbox.onKeydown(down()); expect(listbox.inputs.value()).toEqual(['Apricot']); listbox.onKeydown(down()); - expect(listbox.inputs.value()).toEqual([]); + expect(listbox.inputs.value()).toEqual(['Apricot']); listbox.onKeydown(down()); expect(listbox.inputs.value()).toEqual(['Blackberry']); }); diff --git a/src/cdk-experimental/ui-patterns/tabs/tabs.ts b/src/cdk-experimental/ui-patterns/tabs/tabs.ts index 244b15cf5e82..c20eb423d3b3 100644 --- a/src/cdk-experimental/ui-patterns/tabs/tabs.ts +++ b/src/cdk-experimental/ui-patterns/tabs/tabs.ts @@ -289,7 +289,7 @@ export class TabListPattern { /** Handles updating selection for the tablist. */ private _select(opts?: SelectOptions) { - if (opts?.select && !this.focusManager.activeItem().disabled()) { + if (opts?.select) { this.selection.selectOne(); this.expansionManager.open(this.focusManager.activeItem()); }