bkrem / react-d3-tree

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

Setting initial collapsed state of nodes #402

Open paulfedory opened 2 years ago

paulfedory commented 2 years ago

Are you reporting a bug, or opening a feature request?

Question or Feature Request

What is the behavior/output you expect?

How do you set the initial collapsed/expanded state of any particular set of nodes in the Tree data? In v1, we were able to do this via the _collapsed attribute - but setting the __rd3t.collapsed attribute does not have the same effect.

What version of react-d3-tree are you using?

3.3

sockthedev commented 1 year ago

If anyone knows of any workarounds to this, e.g. via an imperative API call, please do let me know. 💜

sockthedev commented 1 year ago

Copying across the example I posted of what I am trying to achieve;

I would like to load the tree with a specific path expanded (everything else collapsed).

For example, given the following tree;

             A
           /   \
          /     \
         B       E
        / \
       /   \
      C     D

I would like to be able to set the path of A -> B -> D to be be visible/expanded when the tree initially loads;

             A
           /   
          /     
         B 
          \
           \
            D
hikmert commented 1 year ago

hey, @sockthedev. Is there any chance that you resolved the issue? I'd highly appreciate if you reply. Ciao

sockthedev commented 1 year ago

Hey @hikmert, in the end I rendered the entire tree, but then updated the style for the active path so that it was "brighter" than the rest of the nodes. I would have preferred the above, but for now this is working. I'll see how the solution scales though.

Bishtman12 commented 1 year ago

Hey, @sockthedev. Were you able to resolve the issue? I basically had search kind of thing built on this. So, Stuck with using v1.

danielbeeke commented 5 months ago

Something like this patch can work: https://gist.github.com/danielbeeke/ca8e4c6a95928d11cc9b8df0f7c18628

jjjhill commented 3 months ago

Something like this patch can work: https://gist.github.com/danielbeeke/ca8e4c6a95928d11cc9b8df0f7c18628

@danielbeeke What exactly does this enable us to do? I'm trying to control the collapsed state of individual nodes as well.

danielbeeke commented 3 months ago

Exactly that, with this you can control if a node should be opened on load.

jjjhill commented 3 months ago

I guess my question is, how do I use it? I just see a patch to the source code of the library, but how do i use it from my application?

danielbeeke commented 3 months ago

This might help: https://www.npmjs.com/package/patch-package

jjjhill commented 3 months ago

that does help with patching the library. But how do i control these states in my app? Thanks

jjjhill commented 3 months ago

Figured out more or less how i can control the collapsed state from my app, using the patch from @danielbeeke I will post my solution here eventually (after my vacation)

PaulStat commented 1 day ago

I think there must be fix , so that is would be possible to predefine "collapsed" value on time of providing Tree data, like it was in previous version. If it is not predefined. it should take default value.