Skip to content

Commit 4cceae3

Browse files
committed
feat(cdk/a11y): implement expansion methods
1 parent 8ac1671 commit 4cceae3

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,44 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
324324
/**
325325
* If the item is already expanded, we collapse the item. Otherwise, we will focus the parent.
326326
*/
327-
private _collapseCurrentItem() {}
327+
private _collapseCurrentItem() {
328+
if (!this._activeItem) {
329+
return;
330+
}
331+
332+
if (!this._isCurrentItemExpanded()) {
333+
this._activeItem.collapse();
334+
} else {
335+
const parent = this._activeItem.getParent();
336+
if (!parent) {
337+
return;
338+
}
339+
this._setActiveItem(parent as T);
340+
}
341+
}
328342

329343
/**
330344
* If the item is already collapsed, we expand the item. Otherwise, we will focus the first child.
331345
*/
332-
private _expandCurrentItem() {}
346+
private _expandCurrentItem() {
347+
if (!this._activeItem) {
348+
return;
349+
}
350+
351+
if (!this._isCurrentItemExpanded()) {
352+
this._activeItem.expand();
353+
} else {
354+
coerceObservable(this._activeItem.getChildren())
355+
.pipe(take(1))
356+
.subscribe(children => {
357+
const firstChild = children[0];
358+
if (!firstChild) {
359+
return;
360+
}
361+
this._setActiveItem(firstChild as T);
362+
});
363+
}
364+
}
333365

334366
private _isCurrentItemExpanded() {
335367
if (!this._activeItem) {
@@ -345,7 +377,25 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
345377
}
346378

347379
/** For all items that are the same level as the current item, we expand those items. */
348-
private _expandAllItemsAtCurrentItemLevel() {}
380+
private _expandAllItemsAtCurrentItemLevel() {
381+
if (!this._activeItem) {
382+
return;
383+
}
384+
385+
const parent = this._activeItem.getParent();
386+
let itemsToExpand;
387+
if (!parent) {
388+
itemsToExpand = observableOf(this._items.filter(item => item.getParent() === null));
389+
} else {
390+
itemsToExpand = coerceObservable(parent.getChildren());
391+
}
392+
393+
itemsToExpand.pipe(take(1)).subscribe(items => {
394+
for (const item of items) {
395+
item.expand();
396+
}
397+
});
398+
}
349399

350400
private _activateCurrentItem() {
351401
this._activeItem?.activate();

0 commit comments

Comments
 (0)