Skip to content
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

new child is added to the parent when collapsing the parent #939

Open
akhilatadi98 opened this issue Mar 22, 2023 · 1 comment
Open

new child is added to the parent when collapsing the parent #939

akhilatadi98 opened this issue Mar 22, 2023 · 1 comment

Comments

@akhilatadi98
Copy link

hi team
i have a tree where in the sub childrens they have 2 sub children which are having the same id

parent 1 (id:1)
---child 1(id:2)
--- child 12(id:3)
-- child121(id:4)
--- child(1211)(id:5)
--child 2(id2)
--child 21(id:4)
-- child 212(id:5)

when we closed the child121 we expected to hide the child1211 but its not hiding

we are using the changeNodeAtPath to update the tree

@gaeundev
Copy link

gaeundev commented Jun 4, 2024

I also had a hard time with this problem, and I solved it using path!

<SortableTree
   {...props}
   getNodeKey={({ node }) => node.id.toString()}
   onVisibilityToggle={(args) => {
       const { matches } = find({
           treeData: treeData,
           getNodeKey: (data) => data.node.id.toString(),
           searchMethod: (data: SearchData<DataListItemT>) => {
              return data.path.includes(args.node.id.toString());
        }});
        
        if (matches.length > 0) {
              for (const match of matches) {
                 match.node.expanded = args.expanded;
                
                 setTreeData((prev) => {
                     const changeNode = changeNodeAtPath<DataListItemT>({
                         treeData: prev,
                         path: match.path,
                         getNodeKey: (data) => data.node.id.toString(),
                         newNode: { ...match.node },
                     });
                     
                     return changeNode;
                  });
              }
        }
   }}
/>

Because your tree nodes have duplicate ids,
The question and the function of searchMethod may be different, but I think you can easily solve the problem by using path!

hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants