Skip to content

Commit 2f1f7e2

Browse files
committed
feat(cdk/a11y): implement expansion methods
1 parent 4498435 commit 2f1f7e2

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
@@ -320,15 +320,65 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
320320
/**
321321
* If the item is already expanded, we collapse the item. Otherwise, we will focus the parent.
322322
*/
323-
private _collapseCurrentItem() {}
323+
private _collapseCurrentItem() {
324+
if (!this._activeItem) {
325+
return;
326+
}
327+
328+
if (!this._isCurrentItemExpanded()) {
329+
this._activeItem.collapse();
330+
} else {
331+
const parent = this._activeItem.getParent();
332+
if (!parent) {
333+
return;
334+
}
335+
this._setActiveItem(parent as T);
336+
}
337+
}
324338

325339
/**
326340
* If the item is already collapsed, we expand the item. Otherwise, we will focus the first child.
327341
*/
328-
private _expandCurrentItem() {}
342+
private _expandCurrentItem() {
343+
if (!this._activeItem) {
344+
return;
345+
}
346+
347+
if (!this._isCurrentItemExpanded()) {
348+
this._activeItem.expand();
349+
} else {
350+
coerceObservable(this._activeItem.getChildren())
351+
.pipe(take(1))
352+
.subscribe(children => {
353+
const firstChild = children[0];
354+
if (!firstChild) {
355+
return;
356+
}
357+
this._setActiveItem(firstChild as T);
358+
});
359+
}
360+
}
329361

330362
/** For all items that are the same level as the current item, we expand those items. */
331-
private _expandAllItemsAtCurrentItemLevel() {}
363+
private _expandAllItemsAtCurrentItemLevel() {
364+
if (!this._activeItem) {
365+
return;
366+
}
367+
368+
const parent = this._activeItem.getParent();
369+
let itemsToExpand;
370+
if (!parent) {
371+
itemsToExpand = observableOf(this._items.filter(item => item.getParent() === null));
372+
} else {
373+
itemsToExpand = coerceObservable(parent.getChildren());
374+
}
375+
376+
itemsToExpand.pipe(take(1)).subscribe(items => {
377+
for (const item of items) {
378+
item.expand();
379+
}
380+
});
381+
}
332382

333383
private _activateCurrentItem() {
334384
this._activeItem?.activate();

0 commit comments

Comments
 (0)