-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible to Toggle All Nodes to expand and collapse on button click? #460
Comments
To control the initial expansion state of nodes in your tree component and prevent automatic expansion of all nodes when the initialDepth is not set, you can follow these steps:Keep expand and setExpand in a useState Hook:
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.
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.
The text was updated successfully, but these errors were encountered: