From d0f87488d31be9a1c3e0fc62d1d608bdc0227db3 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Mon, 27 Jul 2020 20:53:22 +0200 Subject: [PATCH] docs(tree): fix example that shows empty checklists as selected We determine whether a node is selected in the checklist example using `Array.prototype.every`, but the problem is that `every` returns true for empty arrays which looks weird in our example. Fixes #20085. --- .../tree-checklist/tree-checklist-example.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/components-examples/material/tree/tree-checklist/tree-checklist-example.ts b/src/components-examples/material/tree/tree-checklist/tree-checklist-example.ts index 6e5f1d1cb497..f602ad173371 100644 --- a/src/components-examples/material/tree/tree-checklist/tree-checklist-example.ts +++ b/src/components-examples/material/tree/tree-checklist/tree-checklist-example.ts @@ -162,7 +162,7 @@ export class TreeChecklistExample { : new TodoItemFlatNode(); flatNode.item = node.item; flatNode.level = level; - flatNode.expandable = !!node.children; + flatNode.expandable = !!node.children?.length; this.flatNodeMap.set(flatNode, node); this.nestedNodeMap.set(node, flatNode); return flatNode; @@ -171,9 +171,9 @@ export class TreeChecklistExample { /** Whether all the descendants of the node are selected. */ descendantsAllSelected(node: TodoItemFlatNode): boolean { const descendants = this.treeControl.getDescendants(node); - const descAllSelected = descendants.every(child => - this.checklistSelection.isSelected(child) - ); + const descAllSelected = descendants.length > 0 && descendants.every(child => { + return this.checklistSelection.isSelected(child); + }); return descAllSelected; } @@ -193,9 +193,7 @@ export class TreeChecklistExample { : this.checklistSelection.deselect(...descendants); // Force update for the parent - descendants.every(child => - this.checklistSelection.isSelected(child) - ); + descendants.forEach(child => this.checklistSelection.isSelected(child)); this.checkAllParentsSelection(node); } @@ -218,9 +216,9 @@ export class TreeChecklistExample { checkRootNodeSelection(node: TodoItemFlatNode): void { const nodeSelected = this.checklistSelection.isSelected(node); const descendants = this.treeControl.getDescendants(node); - const descAllSelected = descendants.every(child => - this.checklistSelection.isSelected(child) - ); + const descAllSelected = descendants.length > 0 && descendants.every(child => { + return this.checklistSelection.isSelected(child); + }); if (nodeSelected && !descAllSelected) { this.checklistSelection.deselect(node); } else if (!nodeSelected && descAllSelected) {