I am using an array of objects to draw a tree recursively. I am able to bring up the tree with all the expand collapse related functionality. I have somewhat implemented the search functionality that searches across the tree. If leaf is matched, i show all the nodes expanded upto that node. When intermediate or root node is searched, I should show that in collapsed state which can then be expanded
But when I search the first level or intermediate parent nodes, the search gives me the proepr result, but when I try to open its children, its empty. Also the icon seems to be expanded when I search for any parent.
For example when i search Category1
the tree shows me Category1
with expanded icon (which shouldve been collapsed state), and when try to expand its empty.
When I search Applications
the tree shows right hierarchy from Category 1 to Applications, but Applications will be in expanded state and no children is shown there as well.
Can someone tell me whats wrong that I am doing
Sandbox: https://codesandbox.io/s/searching-c1m50i?file=/src/DrawnTree.jsx
Code I tried so far
import React, { useState } from "react";
import "./styles.css";
import { Node } from "./Node";
const DrawnTree = ({ treeData, currentActive, setCurrentActive }) => {
const [search, setSearch] = useState("");
const containsNodesWithTerm = (nodes, searchTerm) => {
const ids = [];
const _traverse = (nodes, searchTerm) => {
nodes.forEach((node) => {
if (node.name.toUpperCase().includes(searchTerm.toUpperCase())) {
ids.push(node.key);
}
if (node.nodes.length) {
_traverse(node.nodes, searchTerm);
}
});
};
_traverse(nodes, searchTerm);
return ids.length;
};
const filterNodes = (nodes, searchTerm = "") => {
const _filter = (nodes, searchTerm) => {
nodes.forEach((node) => {
if (
node.name.toUpperCase().includes(searchTerm.toUpperCase()) ||
containsNodesWithTerm(node.nodes, searchTerm)
) {
node.visible = true;
node.opened = true;
} else {
node.visible = false;
}
if (!searchTerm) {
node.opened = false;
}
if (node.nodes.length) {
_filter(node.nodes, searchTerm);
}
});
};
_filter(nodes, searchTerm);
return nodes;
};
const filteredTree = filterNodes(treeData, search);
return (
<div>
<input type="text" onChange={(e) => setSearch(e.target.value)} />
<div className="tree-list-section">
<div className="left-holder-bottom-list-section">
{filteredTree.map((node) => (
<Node
key={node.key}
node={node}
level={0}
currentActive={currentActive}
setCurrentActive={setCurrentActive}
/>
))}
</div>
</div>
</div>
);
};
export default DrawnTree;
import React, { useEffect, useState } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
export const Node = ({ node, level, currentActive, setCurrentActive }) => {
const { name, key, nodes, visible, opened } = node;
const [isOpen, setIsOpen] = useState(opened);
const hasChildren = !!node?.nodes?.length;
const nodeType = level === 0 ? "node" : !hasChildren ? "leaf" : "group";
const activeClassName = currentActive === name ? "active" : "";
useEffect(() => {
setIsOpen(opened);
}, [opened]);
if (!visible) {
return null;
}
return (
<>
<div
className={`list-row level-${level} ${nodeType} ${activeClassName}`}
onClick={() => {
setIsOpen((open) => !open);
if (!hasChildren) {
setCurrentActive((prevName) => (!(prevName === name) ? name : ""));
}
}}
key={key}
>
<div
className="list-item-holder"
style={{ paddingLeft: `${level === 0 ? "16" : 40 * level}px` }}
>
{hasChildren && (
<div className="list-item-expander-holder">
<span
className={`expand-collapse-icon ${
isOpen ? "collapse" : "expand"
}`}
>
<span className="expand-icon">
<FontAwesomeIcon icon="caret-down" />
</span>
<span className="collapse-icon">
<FontAwesomeIcon icon="caret-right" />
</span>
</span>
</div>
)}
<div className="list-item-details-holder">{name}</div>
</div>
</div>
{isOpen && hasChildren && (
<div className="list-row-children">
{nodes.map((node) => (
<Node
key={node.key}
node={node}
level={level + 1}
currentActive={currentActive}
setCurrentActive={setCurrentActive}
/>
))}
</div>
)}
</>
);
};