@@ -324,12 +324,44 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
324
324
/**
325
325
* If the item is already expanded, we collapse the item. Otherwise, we will focus the parent.
326
326
*/
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
+ }
328
342
329
343
/**
330
344
* If the item is already collapsed, we expand the item. Otherwise, we will focus the first child.
331
345
*/
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
+ }
333
365
334
366
private _isCurrentItemExpanded ( ) {
335
367
if ( ! this . _activeItem ) {
@@ -345,7 +377,25 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> {
345
377
}
346
378
347
379
/** 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
+ }
349
399
350
400
private _activateCurrentItem ( ) {
351
401
this . _activeItem ?. activate ( ) ;
0 commit comments