@@ -365,7 +365,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
365
365
isExpanded ( dataNode : T ) : boolean {
366
366
return (
367
367
this . treeControl ?. isExpanded ( dataNode ) ??
368
- this . _expansionModel ?. isSelected ( this . _trackExpansionKey ( dataNode ) ) ??
368
+ this . _expansionModel ?. isSelected ( this . _getExpansionKey ( dataNode ) ) ??
369
369
false
370
370
) ;
371
371
}
@@ -375,7 +375,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
375
375
if ( this . treeControl ) {
376
376
this . treeControl . toggle ( dataNode ) ;
377
377
} else if ( this . _expansionModel ) {
378
- this . _expansionModel . toggle ( this . _trackExpansionKey ( dataNode ) ) ;
378
+ this . _expansionModel . toggle ( this . _getExpansionKey ( dataNode ) ) ;
379
379
}
380
380
}
381
381
@@ -384,7 +384,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
384
384
if ( this . treeControl ) {
385
385
this . treeControl . expand ( dataNode ) ;
386
386
} else if ( this . _expansionModel ) {
387
- this . _expansionModel . select ( this . _trackExpansionKey ( dataNode ) ) ;
387
+ this . _expansionModel . select ( this . _getExpansionKey ( dataNode ) ) ;
388
388
}
389
389
}
390
390
@@ -393,7 +393,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
393
393
if ( this . treeControl ) {
394
394
this . treeControl . collapse ( dataNode ) ;
395
395
} else if ( this . _expansionModel ) {
396
- this . _expansionModel . deselect ( this . _trackExpansionKey ( dataNode ) ) ;
396
+ this . _expansionModel . deselect ( this . _getExpansionKey ( dataNode ) ) ;
397
397
}
398
398
}
399
399
@@ -422,9 +422,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
422
422
} else if ( this . _expansionModel ) {
423
423
const expansionModel = this . _expansionModel ;
424
424
this . _getDescendants ( dataNode )
425
- . pipe ( takeUntil ( this . _onDestroy ) )
425
+ . pipe ( take ( 1 ) , takeUntil ( this . _onDestroy ) )
426
426
. subscribe ( children => {
427
- expansionModel . select ( ...children . map ( child => this . _trackExpansionKey ( child ) ) ) ;
427
+ expansionModel . select ( ...children . map ( child => this . _getExpansionKey ( child ) ) ) ;
428
428
} ) ;
429
429
}
430
430
}
@@ -436,9 +436,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
436
436
} else if ( this . _expansionModel ) {
437
437
const expansionModel = this . _expansionModel ;
438
438
this . _getDescendants ( dataNode )
439
- . pipe ( takeUntil ( this . _onDestroy ) )
439
+ . pipe ( take ( 1 ) , takeUntil ( this . _onDestroy ) )
440
440
. subscribe ( children => {
441
- expansionModel . deselect ( ...children . map ( child => this . _trackExpansionKey ( child ) ) ) ;
441
+ expansionModel . deselect ( ...children . map ( child => this . _getExpansionKey ( child ) ) ) ;
442
442
} ) ;
443
443
}
444
444
}
@@ -450,9 +450,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
450
450
} else if ( this . _expansionModel ) {
451
451
const expansionModel = this . _expansionModel ;
452
452
this . _getAllDescendants ( )
453
- . pipe ( takeUntil ( this . _onDestroy ) )
453
+ . pipe ( take ( 1 ) , takeUntil ( this . _onDestroy ) )
454
454
. subscribe ( children => {
455
- expansionModel . select ( ...children . map ( child => this . _trackExpansionKey ( child ) ) ) ;
455
+ expansionModel . select ( ...children . map ( child => this . _getExpansionKey ( child ) ) ) ;
456
456
} ) ;
457
457
}
458
458
}
@@ -464,9 +464,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
464
464
} else if ( this . _expansionModel ) {
465
465
const expansionModel = this . _expansionModel ;
466
466
this . _getAllDescendants ( )
467
- . pipe ( takeUntil ( this . _onDestroy ) )
467
+ . pipe ( take ( 1 ) , takeUntil ( this . _onDestroy ) )
468
468
. subscribe ( children => {
469
- expansionModel . deselect ( ...children . map ( child => this . _trackExpansionKey ( child ) ) ) ;
469
+ expansionModel . deselect ( ...children . map ( child => this . _getExpansionKey ( child ) ) ) ;
470
470
} ) ;
471
471
}
472
472
}
@@ -516,9 +516,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
516
516
if ( this . childrenAccessor ) {
517
517
return this . _getChildrenRecursively ( dataNode ) . pipe (
518
518
reduce (
519
- ( memo : T [ ] , next ) => {
520
- memo . push ( ...next ) ;
521
- return memo ;
519
+ ( allChildren : T [ ] , nextChildren ) => {
520
+ allChildren . push ( ...nextChildren ) ;
521
+ return allChildren ;
522
522
} ,
523
523
[ dataNode ] ,
524
524
) ,
@@ -527,6 +527,12 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
527
527
throw getTreeControlMissingError ( ) ;
528
528
}
529
529
530
+ /**
531
+ * Gets all children and sub-children of the provided node.
532
+ *
533
+ * This will emit multiple times, in the order that the children will appear
534
+ * in the tree, and can be combined with a `reduce` operator.
535
+ */
530
536
private _getChildrenRecursively ( dataNode : T ) : Observable < T [ ] > {
531
537
if ( ! this . childrenAccessor ) {
532
538
return observableOf ( [ ] ) ;
@@ -543,7 +549,14 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
543
549
) ;
544
550
}
545
551
546
- private _trackExpansionKey ( dataNode : T ) : K {
552
+ private _getExpansionKey ( dataNode : T ) : K {
553
+ // In the case that a key accessor function was not provided by the
554
+ // tree user, we'll default to using the node object itself as the key.
555
+ //
556
+ // This cast is safe since:
557
+ // - if an expansionKey is provided, TS will infer the type of K to be
558
+ // the return type.
559
+ // - if it's not, then K will be defaulted to T.
547
560
return this . expansionKey ?.( dataNode ) ?? ( dataNode as unknown as K ) ;
548
561
}
549
562
}
@@ -580,7 +593,7 @@ export class CdkTreeNode<T, K = T> implements FocusableOption, OnDestroy, OnInit
580
593
581
594
@Input ( )
582
595
get isExpanded ( ) : boolean {
583
- return ! ! this . _tree . isExpanded ( this . _data ) ;
596
+ return this . _tree . isExpanded ( this . _data ) ;
584
597
}
585
598
set isExpanded ( isExpanded : boolean ) {
586
599
if ( isExpanded ) {
0 commit comments