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

Fix problem with expandSubsequently resolving before expanding all items #403

Closed kamilkazmierczakMtab closed 1 month ago

kamilkazmierczakMtab commented 1 month ago

Issue solved: expandSubsequently does not wait to expand all items provided in argument. In rare cases it leads to errors.

Additional info:

  1. Tests are failing for some reason but they are failing even without my changes (locally)
  2. This is fixing a lot of problems but not all - e.g. in your story (https://rct.lukasbach.com/storybook/?path=/story/core-finding-items--custom-finder) after typing pizza you will get error Cannot read properties of undefined (reading 'index') this is similar to error that I was getting but my fix is not addressing that because Europe item is not expanded before this function resolves - this is because onExpandItem is not returning promise so I can't wait for it.

@lukasbach

ajhous44 commented 1 month ago

+1

lukasbach commented 1 month ago

Thank you for the contribution! I will have another look at why the other error seems to come up.

lukasbach commented 1 month ago

In regards to the error in your second point, the logged error here seems to come from the call to focus the "Pizza" item in the story, but exandSubsequently was just called on all items above that, with expandSubsequently(path.slice(0, path.length - 1)) it just expanded "root", "Meals" and "Europe", but then called the focus method on "Pizza" without waiting for it to be loaded in first.

As a dirty fix, it actually works fine when the target leaf is also included in the expandSubsequently call with expandSubsequently(path), this will make the call wait until the Pizza leaf is loaded in as well. So something like this will fix it: https://github.com/lukasbach/react-complex-tree/commit/58e3ed9b3e372b0ec9e4f340eb0cc9bb2e106dea

This does have the disadvantage that the searched item will also be expanded if it is a folder, if this is not desired, some custom waiting would need to be implemented to make sure the item is loaded in before proceeding with doing things with it.