Open sikandarchishty opened 1 year ago
Keep expand and setExpand in a useState Hook:
import React, { useState } from 'react';
const [expand, setExpand] = useState(false);
Add a Button to Toggle the Expand State:
<button onClick={() => setExpand(!expand)}>Toggle Expand</button>
Render the Tree Component with Conditional initialDepth:
{!expand && (
<Tree initialDepth={0}
data={data}
// Other Tree props
/>)}
{!expand &&(
<Tree
data={data}
//remove iniitialdepth which makes all node opens
// Other Tree props
/>)}
Above you can render two tree component separate based on condition which works fine than below one which renders in dom but it takes some time to paint.
<Tree
// Other Tree props
initialDepth={expand ? 0 : undefined}
// Your other Tree configuration
/>
Here's what this accomplishes:
We maintain the expand state in a useState hook to keep track of whether nodes should be initially expanded or not.
The button allows you to toggle the expand state. Clicking the button changes the value of expand, which affects the initialDepth prop.
When expand is true, the initialDepth is set to 0, meaning all nodes start expanded. When expand is false, we set initialDepth to undefined or you can remove, which prevents automatic expansion of all nodes.
This way, you can control the initial expansion state of nodes in your tree component and avoid the issue of automatic expansion when initialDepth is not set.
I have a new idea. Namely, we can customize the methods, the first one is pathFunc and the other one is renderCustomNodeElement. we add collapse attribute to each item in the data property passed to the Tree component. When executing these two functions, we can control whether they are rendered or not based on the collapse attribute for the purpose of collapsing or expanding. Of course, we need to modify the collapse attribute in the Tree component's internal data __rd3t in parallel, so that it can be expanded and collapsed when an item is clicked.
Thank you for taking the time to report an issue with react-d3-tree!
Feel free to delete any questions that do not apply.
Are you reporting a bug, or opening a feature request?
-
What is the actual behavior/output?
cannot find anythong about it in docs (or I am blind)
What is the behavior/output you expect?
Ability to Open and Close all nodes on button click.
Can you consistently reproduce the issue/create a reproduction case (e.g. on https://codesandbox.io)?
-
What version of react-d3-tree are you using?
3.5.2
If react-d3-tree crashed with a traceback, please paste the full traceback below.
-