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

Skip `scrollTo` on node selection

Open lanesawyer opened this issue 1 year ago • 1 comments

Hi there! This library has been fantastic to use so far, but I'm running into some difficulties with an advanced use case that doesn't seem to be fully supported with the current API.

I have a large tree structure with every node having a check box, and I want to select/deselect all the children whenever a parent is selected in the checkbox. But I was hitting performance issues when calling .multiSelect a bunch of times to track all children if a parent was selected, especially at the more root levels of the tree which could call .multiSelect upwards of 10k times. I tried selectContiguous but that didn't work because I need to preserve selections in different areas of the tree if a parent was selected at a deeper level.

To improve performance, I implemented some code that only calls selectMulti on the parents, then compute the checkbox checked state based on whether some ancestor is checked within react-arborist. That works just fine and everything is working smoothly now except for an occasional change in the view where the checkbox I clicked doesn't stay in the same place.

I believe this is because of the scrollTo that takes place in the selectMulti function.

Because I am selecting and deselecting a bunch of nodes in order to get the check boxes working correctly, I think it scrolls to the nodes that are being selected in different areas of the tree than the one I just clicked (e.g., a grandparent was checked then I uncheck a child, which means the grandparent shouldn't be fully selected anymore so I set the aunts/uncles of that clicked child to checked, along with its siblings). So react-arborist scrolls to those aunts and uncles.

I've tried calling tree.scrollTo(originallyClickedNode) at the end of my click handler to try to keep the scroll in place on the node I clicked, but none of the Align values have solved the problem. The default smart value usually does a decent job, but still shifts slightly at times.

Would it be possible to add an option to not automatically scroll to whatever node was selected?

Alternatively, if you see a better way for me to implement the checkbox hierarchy than what I described above, I'd be happy to give that a shot as well.

lanesawyer avatar May 11 '23 08:05 lanesawyer

Thank you for taking the time to write this up. A few people have had this use case now. I think it would be good to allow the selection to change without scrolling or focusing on any other nodes.

I'm glad you have a workaround for now, but I'll plan to address this in the next release.

jameskerr avatar May 11 '23 17:05 jameskerr