Skip to content

Commit a975e37

Browse files
BobobUnicornandrewseguin
authored andcommitted
fix(cdk/tree): address comments from review
1 parent 829f5cd commit a975e37

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/cdk/tree/tree.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
365365
isExpanded(dataNode: T): boolean {
366366
return (
367367
this.treeControl?.isExpanded(dataNode) ??
368-
this._expansionModel?.isSelected(this._trackExpansionKey(dataNode)) ??
368+
this._expansionModel?.isSelected(this._getExpansionKey(dataNode)) ??
369369
false
370370
);
371371
}
@@ -375,7 +375,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
375375
if (this.treeControl) {
376376
this.treeControl.toggle(dataNode);
377377
} else if (this._expansionModel) {
378-
this._expansionModel.toggle(this._trackExpansionKey(dataNode));
378+
this._expansionModel.toggle(this._getExpansionKey(dataNode));
379379
}
380380
}
381381

@@ -384,7 +384,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
384384
if (this.treeControl) {
385385
this.treeControl.expand(dataNode);
386386
} else if (this._expansionModel) {
387-
this._expansionModel.select(this._trackExpansionKey(dataNode));
387+
this._expansionModel.select(this._getExpansionKey(dataNode));
388388
}
389389
}
390390

@@ -393,7 +393,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
393393
if (this.treeControl) {
394394
this.treeControl.collapse(dataNode);
395395
} else if (this._expansionModel) {
396-
this._expansionModel.deselect(this._trackExpansionKey(dataNode));
396+
this._expansionModel.deselect(this._getExpansionKey(dataNode));
397397
}
398398
}
399399

@@ -422,9 +422,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
422422
} else if (this._expansionModel) {
423423
const expansionModel = this._expansionModel;
424424
this._getDescendants(dataNode)
425-
.pipe(takeUntil(this._onDestroy))
425+
.pipe(take(1), takeUntil(this._onDestroy))
426426
.subscribe(children => {
427-
expansionModel.select(...children.map(child => this._trackExpansionKey(child)));
427+
expansionModel.select(...children.map(child => this._getExpansionKey(child)));
428428
});
429429
}
430430
}
@@ -436,9 +436,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
436436
} else if (this._expansionModel) {
437437
const expansionModel = this._expansionModel;
438438
this._getDescendants(dataNode)
439-
.pipe(takeUntil(this._onDestroy))
439+
.pipe(take(1), takeUntil(this._onDestroy))
440440
.subscribe(children => {
441-
expansionModel.deselect(...children.map(child => this._trackExpansionKey(child)));
441+
expansionModel.deselect(...children.map(child => this._getExpansionKey(child)));
442442
});
443443
}
444444
}
@@ -450,9 +450,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
450450
} else if (this._expansionModel) {
451451
const expansionModel = this._expansionModel;
452452
this._getAllDescendants()
453-
.pipe(takeUntil(this._onDestroy))
453+
.pipe(take(1), takeUntil(this._onDestroy))
454454
.subscribe(children => {
455-
expansionModel.select(...children.map(child => this._trackExpansionKey(child)));
455+
expansionModel.select(...children.map(child => this._getExpansionKey(child)));
456456
});
457457
}
458458
}
@@ -464,9 +464,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
464464
} else if (this._expansionModel) {
465465
const expansionModel = this._expansionModel;
466466
this._getAllDescendants()
467-
.pipe(takeUntil(this._onDestroy))
467+
.pipe(take(1), takeUntil(this._onDestroy))
468468
.subscribe(children => {
469-
expansionModel.deselect(...children.map(child => this._trackExpansionKey(child)));
469+
expansionModel.deselect(...children.map(child => this._getExpansionKey(child)));
470470
});
471471
}
472472
}
@@ -516,9 +516,9 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
516516
if (this.childrenAccessor) {
517517
return this._getChildrenRecursively(dataNode).pipe(
518518
reduce(
519-
(memo: T[], next) => {
520-
memo.push(...next);
521-
return memo;
519+
(allChildren: T[], nextChildren) => {
520+
allChildren.push(...nextChildren);
521+
return allChildren;
522522
},
523523
[dataNode],
524524
),
@@ -527,6 +527,12 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
527527
throw getTreeControlMissingError();
528528
}
529529

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+
*/
530536
private _getChildrenRecursively(dataNode: T): Observable<T[]> {
531537
if (!this.childrenAccessor) {
532538
return observableOf([]);
@@ -543,7 +549,14 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
543549
);
544550
}
545551

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.
547560
return this.expansionKey?.(dataNode) ?? (dataNode as unknown as K);
548561
}
549562
}
@@ -580,7 +593,7 @@ export class CdkTreeNode<T, K = T> implements FocusableOption, OnDestroy, OnInit
580593

581594
@Input()
582595
get isExpanded(): boolean {
583-
return !!this._tree.isExpanded(this._data);
596+
return this._tree.isExpanded(this._data);
584597
}
585598
set isExpanded(isExpanded: boolean) {
586599
if (isExpanded) {

0 commit comments

Comments
 (0)