bkrem / react-d3-tree

:deciduous_tree: React component to create interactive D3 tree graphs
https://bkrem.github.io/react-d3-tree
MIT License
1.06k stars 268 forks source link

Possible to Toggle All Nodes to expand and collapse on button click? #460

Open sikandarchishty opened 1 year ago

sikandarchishty commented 1 year ago

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.

-

ranjith1509 commented 10 months ago

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:

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.

2019geguangpu commented 3 months ago

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.