Skip to content

Commit 5570beb

Browse files
tinayuangaojelbourn
authored andcommitted
fix(tree): fix nested node cannot expand/collapse multiple times (#10671)
1 parent b85ee8c commit 5570beb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/cdk/tree/nested-node.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
9393
if (this.nodeOutlet.length && this._children) {
9494
const viewContainer = this.nodeOutlet.first.viewContainer;
9595
this._tree.renderNodeChanges(this._children, this._dataDiffer, viewContainer);
96+
} else {
97+
// Reset the data differ if there's no children nodes displayed
98+
this._dataDiffer.diff([]);
9699
}
97100
}
98101

99102
/** Clear the children dataNodes. */
100103
protected _clear(): void {
101104
if (this.nodeOutlet && this.nodeOutlet.first) {
102105
this.nodeOutlet.first.viewContainer.clear();
106+
this._dataDiffer.diff([]);
103107
}
104108
}
105109
}

src/cdk/tree/tree.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ describe('CdkTree', () => {
442442
fixture.detectChanges();
443443
});
444444

445-
it('should expand/collapse the node', () => {
445+
it('should expand/collapse the node multiple times', () => {
446446
component.toggleRecursively = false;
447447
let data = dataSource.data;
448448
const child = dataSource.addChild(data[1], false);
@@ -477,6 +477,17 @@ describe('CdkTree', () => {
477477
[`topping_3 - cheese_3 + base_3`]);
478478
expect(component.treeControl.expansionModel.selected.length)
479479
.toBe(0, `Expect node collapsed`);
480+
481+
(getNodes(treeElement)[1] as HTMLElement).click();
482+
fixture.detectChanges();
483+
484+
expect(component.treeControl.expansionModel.selected.length)
485+
.toBe(1, `Expect node expanded`);
486+
expectNestedTreeToMatch(treeElement,
487+
[`topping_1 - cheese_1 + base_1`],
488+
[`topping_2 - cheese_2 + base_2`],
489+
[_, `topping_4 - cheese_4 + base_4`],
490+
[`topping_3 - cheese_3 + base_3`]);
480491
});
481492

482493
it('should expand/collapse the node recursively', () => {

0 commit comments

Comments
 (0)