首先,我对混乱的代码表示抱歉,这是我第一次发布问题!
所以我正在使用react-ui-tree。我需要实现一个递归函数,该函数深入到树中并更改某些布尔值,以便在切换折叠按钮时只有一个节点(被切换)保持打开/关闭状态。
目前我传递给Array.filter的回调函数没有递归地检查到树的深处(我只是手动使它起作用) 如果有人可以给我解决方案,该如何将handleNesting函数转换为递归函数。
toggleCollapse = nodeId => {
const tree = this.state.tree;
const index = tree.getIndex(nodeId);
const node = index.node;
node.collapsed = !node.collapsed;
const handleNesting = (parent) => {
if (parent.children && parent.children.find(inner => inner.ID ===
node.ID)) {
parent.collapsed = false
parent.children.filter(inner => {
if (inner.ID !== node.ID) inner.collapsed = true;
return inner;
});
}
if (parent.ID !== node.ID && parent.children && !parent.children.find(inner => inner.ID === node.ID)){
parent.collapsed = true
return parent;
}
return parent
}
tree.obj.children.filter(handleNesting) // collapses all the
//opened folder except the one which has been toggled(including its //parent folder)