Skip to content

Commit c832105

Browse files
committed
fixup! refactor(cdk-experimental/ui-patterns): track focus by item not index
1 parent c641624 commit c832105

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

src/cdk-experimental/ui-patterns/accordion/accordion.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ export class AccordionGroupPattern {
7070
}
7171

7272
/** Inputs for the AccordionTriggerPattern. */
73-
export type AccordionTriggerInputs = ListNavigationItem &
74-
ListFocusItem &
73+
export type AccordionTriggerInputs = Omit<ListNavigationItem & ListFocusItem, 'index'> &
7574
Omit<ExpansionItem, 'expansionId' | 'expandable'> & {
7675
/** A local unique identifier for the trigger. */
7776
value: SignalLike<string>;
@@ -110,6 +109,9 @@ export class AccordionTriggerPattern {
110109
/** Whether the trigger is disabled. Disabling an accordion group disables all the triggers. */
111110
disabled = computed(() => this.inputs.disabled() || this.inputs.accordionGroup().disabled());
112111

112+
/** The index of the trigger within its accordion group. */
113+
index = computed(() => this.inputs.accordionGroup().items().indexOf(this));
114+
113115
constructor(readonly inputs: AccordionTriggerInputs) {
114116
this.id = inputs.id;
115117
this.element = inputs.element;

src/cdk-experimental/ui-patterns/behaviors/list-focus/list-focus.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export interface ListFocusItem {
1919

2020
/** Whether an item is disabled. */
2121
disabled: SignalLike<boolean>;
22+
23+
/** The index of the item in the list. */
24+
index: SignalLike<number>;
2225
}
2326

2427
/** Represents the required inputs for a collection that contains focusable items. */
@@ -45,14 +48,10 @@ export class ListFocus<T extends ListFocusItem> {
4548
prevActiveItem = signal<T | undefined>(undefined);
4649

4750
/** The index of the last item that was active. */
48-
prevActiveIndex = computed(() => {
49-
return this.prevActiveItem() ? this.inputs.items().indexOf(this.prevActiveItem()!) : -1;
50-
});
51+
prevActiveIndex = computed(() => this.prevActiveItem()?.index() ?? -1);
5152

5253
/** The current active index in the list. */
53-
activeIndex = computed(() => {
54-
return this.inputs.activeItem() ? this.inputs.items().indexOf(this.inputs.activeItem()!) : -1;
55-
});
54+
activeIndex = computed(() => this.inputs.activeItem()?.index() ?? -1);
5655

5756
constructor(readonly inputs: ListFocusInputs<T>) {}
5857

src/cdk-experimental/ui-patterns/listbox/option.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface ListboxPattern<V> {
2020
}
2121

2222
/** Represents the required inputs for an option in a listbox. */
23-
export interface OptionInputs<V> extends ListItem<V> {
23+
export interface OptionInputs<V> extends Omit<ListItem<V>, 'index'> {
2424
listbox: SignalLike<ListboxPattern<V> | undefined>;
2525
}
2626

@@ -33,12 +33,7 @@ export class OptionPattern<V> {
3333
value: SignalLike<V>;
3434

3535
/** The position of the option in the list. */
36-
index = computed(
37-
() =>
38-
this.listbox()
39-
?.inputs.items()
40-
.findIndex(i => i.id() === this.id()) ?? -1,
41-
);
36+
index = computed(() => this.listbox()?.inputs.items().indexOf(this) ?? -1);
4237

4338
/** Whether the option is active. */
4439
active = computed(() => this.listbox()?.inputs.activeItem() === this);

src/cdk-experimental/ui-patterns/radio-group/radio-button.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface RadioGroupLike<V> {
2020
}
2121

2222
/** Represents the required inputs for a radio button in a radio group. */
23-
export interface RadioButtonInputs<V> extends Omit<ListItem<V>, 'searchTerm'> {
23+
export interface RadioButtonInputs<V> extends Omit<ListItem<V>, 'searchTerm' | 'index'> {
2424
/** A reference to the parent radio group. */
2525
group: SignalLike<RadioGroupLike<V> | undefined>;
2626
}
@@ -34,12 +34,7 @@ export class RadioButtonPattern<V> {
3434
value: SignalLike<V>;
3535

3636
/** The position of the radio button within the group. */
37-
index = computed(
38-
() =>
39-
this.group()
40-
?.listBehavior.inputs.items()
41-
.findIndex(i => i.id() === this.id()) ?? -1,
42-
);
37+
index = computed(() => this.group()?.listBehavior.inputs.items().indexOf(this) ?? -1);
4338

4439
/** Whether the radio button is currently the active one (focused). */
4540
active = computed(() => this.group()?.listBehavior.inputs.activeItem() === this);

src/cdk-experimental/ui-patterns/tabs/tabs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {List, ListInputs, ListItem} from '../behaviors/list/list';
2020

2121
/** The required inputs to tabs. */
2222
export interface TabInputs
23-
extends Omit<ListItem<string>, 'searchTerm'>,
23+
extends Omit<ListItem<string>, 'searchTerm' | 'index'>,
2424
Omit<ExpansionItem, 'expansionId' | 'expandable'> {
2525
/** The parent tablist that controls the tab. */
2626
tablist: SignalLike<TabListPattern>;
@@ -37,6 +37,9 @@ export class TabPattern {
3737
/** A global unique identifier for the tab. */
3838
readonly id: SignalLike<string>;
3939

40+
/** The index of the tab. */
41+
readonly index = computed(() => this.inputs.tablist().inputs.items().indexOf(this));
42+
4043
/** A local unique identifier for the tab. */
4144
readonly value: SignalLike<string>;
4245

src/cdk-experimental/ui-patterns/tree/tree.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {ExpansionItem, ExpansionControl, ListExpansion} from '../behaviors/expan
1313
import {KeyboardEventManager, PointerEventManager, Modifier} from '../behaviors/event-manager';
1414

1515
/** Represents the required inputs for a tree item. */
16-
export interface TreeItemInputs<V> extends ListItem<V> {
16+
export interface TreeItemInputs<V> extends Omit<ListItem<V>, 'index'> {
1717
/** The parent item. */
1818
parent: SignalLike<TreeItemPattern<V> | TreePattern<V>>;
1919

@@ -32,6 +32,9 @@ export interface TreeItemPattern<V> extends TreeItemInputs<V> {}
3232
* Represents an item in a Tree.
3333
*/
3434
export class TreeItemPattern<V> implements ExpansionItem {
35+
/** The position of this item among its siblings. */
36+
readonly index = computed(() => this.parent().children().indexOf(this));
37+
3538
/** The unique identifier used by the expansion behavior. */
3639
readonly expansionId: SignalLike<string>;
3740

0 commit comments

Comments
 (0)