From 11f30faecf3f20619ec0c3f179ee4680b821ff9f Mon Sep 17 00:00:00 2001 From: Cheton Wu Date: Sat, 2 Jun 2018 01:32:25 +0800 Subject: [PATCH] Fix an issue that could cause the root node to stay in the close state (#32) --- src/infinite-tree.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/infinite-tree.js b/src/infinite-tree.js index 8a56617..683a938 100644 --- a/src/infinite-tree.js +++ b/src/infinite-tree.js @@ -673,6 +673,12 @@ class InfiniteTree extends events.EventEmitter { this.emit('willCloseNode', node); + // Cannot close the root node + if (node === this.state.rootNode) { + error('Cannot close the root node'); + return false; + } + // Retrieve node index const nodeIndex = this.nodes.indexOf(node); if (nodeIndex < 0) { @@ -1311,7 +1317,9 @@ class InfiniteTree extends events.EventEmitter { // Update parent node parentNode.children = []; - parentNode.state.open = parentNode.state.open && (parentNode.children.length > 0); + if (parentNode !== this.state.rootNode) { + parentNode.state.open = parentNode.state.open && (parentNode.children.length > 0); + } if (parentNodeIndex >= 0) { // Update nodes & rows @@ -1395,7 +1403,9 @@ class InfiniteTree extends events.EventEmitter { // Update parent node parentNode.children.splice(parentNode.children.indexOf(node), 1); - parentNode.state.open = parentNode.state.open && (parentNode.children.length > 0); + if (parentNode !== this.state.rootNode) { + parentNode.state.open = parentNode.state.open && (parentNode.children.length > 0); + } if (nodeIndex >= 0) { // Update nodes & rows