react-arborist icon indicating copy to clipboard operation
react-arborist copied to clipboard

Add some getter for the "open" property of each node

Open GermanJablo opened this issue 1 year ago • 2 comments

The open property (well, collapsed actually) is part of my controlled data.

I want it to be like this so that the open or closed state is persistent.

I've tried using the onToggle property to keep my open state in sync with react-arborist's open, and it seems to work fine. The problem is that with the initial state they become desynchronized. openByDefault to true or false would not solve my problem, because some nodes should be able to be initialized open and others closed.

Two ideas have occurred to me:

  • Add an openAccessor or getOpen prop that works as a getter and use onToggle as a setter.
  • Add two properties getOpen and setOpen. It probably makes sense to deprecate onToggle in this case.

Something I don't like about onToggle is that instead of passing the node, it passes the id, and I have to do a lookup of my data to find it and access it.

Edit: I just discovered the initialOpenState property that I had overlooked. With this I have been able to solve it, although it is a bit of a boilerplate since I have to go through my entire data structure and form an OpenMap, not ideal.

GermanJablo avatar Jan 05 '24 17:01 GermanJablo