Skip to content

Commit b4aeced

Browse files
committed
feat(cdk/a11y): add _getItems translation layer
1 parent dba9f12 commit b4aeced

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/cdk/a11y/key-manager/tree-key-manager.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
105105
/** Function to determine equivalent items. */
106106
private _trackByFn: (item: T) => unknown = (item: T) => item;
107107

108+
private _items: Observable<T[]> | QueryList<T> | T[];
109+
108110
constructor({
109111
items,
110112
skipPredicate,
@@ -126,6 +128,8 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
126128
this._activationFollowsFocus = activationFollowsFocus;
127129
}
128130

131+
this._items = items;
132+
129133
// We allow for the items to be an array or Observable because, in some cases, the consumer may
130134
// not have access to a QueryList of the items they want to manage (e.g. when the
131135
// items aren't being collected via `ViewChildren` or `ContentChildren`).
@@ -168,6 +172,26 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
168172
return this._activeItem;
169173
}
170174

175+
private _setActiveItem(index: number) {
176+
this._getItems()
177+
.pipe(take(1))
178+
.subscribe(items => {
179+
const activeItem = items[index];
180+
181+
// Explicitly check for `null` and `undefined` because other falsy values are valid.
182+
this._activeItem = activeItem == null ? null : activeItem;
183+
this._activeItemIndex = index;
184+
185+
if (!this._activeItem) {
186+
return;
187+
}
188+
this._activeItem.focus();
189+
if (this._activationFollowsFocus) {
190+
this._activeItem.activate();
191+
}
192+
});
193+
}
194+
171195
private _updateActiveItemIndex(newItems: T[]) {
172196
if (this._activeItem) {
173197
const newIndex = newItems.indexOf(this._activeItem);
@@ -177,6 +201,10 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
177201
}
178202
}
179203
}
204+
205+
private _getItems(): Observable<T[]> {
206+
return coerceObservable(this._items);
207+
}
180208
}
181209

182210
// tslint:enable

0 commit comments

Comments
 (0)