lukasbach / react-complex-tree

Unopinionated Accessible Tree Component with Multi-Select and Drag-And-Drop
https://rct.lukasbach.com
MIT License
944 stars 74 forks source link

Unable to collapseall when using ControlledTreeEnvironment #240

Closed khamphou closed 1 year ago

khamphou commented 1 year ago

Describe the bug ExpandAll does not work, no sub nodes are expanded CollapseAll collapse only one parent folder at a time

To Reproduce

Expected behavior all sub nodes should open, none of them are expanded using the controlledtreeenvironment. it works fine using uncontrolledtreeenvironment.

Screenshots image

Desktop (please complete the following information):

Additional context react-complex-tree 2.1.1

lukasbach commented 1 year ago

Hi, I just had a look, and there seems to be some issues with closures in your code. Replace the onExpandItem and onCollapseItem props with


      onExpandItem={(item) => {
        console.log("expand", item);
        setExpandedItems((items) => [...items, item?.index]);
      }}
      onCollapseItem={(item) =>
        setExpandedItems((items) =>
          items.filter((expandedItemIndex) => expandedItemIndex !== item?.index)
        )
      }

Then it works: https://codesandbox.io/s/react-complex-tree-expandall-closure-fix-ekssib

khamphou commented 1 year ago

thank you for your help. sorry for the response delay. i was sick last weeks.