ValentinH / react-easy-sort

A React component to sort items in lists or grids
https://ValentinH.github.io/react-easy-sort
MIT License
160 stars 25 forks source link

Not working properly with filtered list #30

Closed p32929 closed 2 years ago

p32929 commented 2 years ago

Hello, first of all, thanks for making/maintaining this amazing library.

Describe the bug Showing weird behavior if a filtered list is used to rerrange

To Reproduce

  1. Go to https://codesandbox.io/s/misty-tree-0ispk4
  2. Try to rearrange A with C
  3. You will see, A becomes B
  4. Try to rearrange G with I
  5. You will see, it doesn't work

Here's a gif with a demo of the issue explained above: res

Expected behavior The rearranged list should work properly/correctly

ValentinH commented 2 years ago

Thanks for the kind words!

The issue in your example comes from the fact that the onSortEnd callback gives you the indexes of the items that changed in the "filtered" list (the one that is passed to the SortableList but you apply the change using these indexes on the original non-filtered array.

I see 2 possible solutions:

p32929 commented 2 years ago

Hi, thanks a lot for the amazing explanation.

Pardon my ignorance, but I think, the explanation only explains why A becomes B while rearranging. but doesn't explain why G & I doesn't rearrange.

ValentinH commented 2 years ago

When switching G and I, you can see that E becomes D. This is because G and I are indexes 3 and 4 in the filtered array: when inverting these indexes in the original array, you actually switch D and E. That's why you see D becoming E. 🙂

p32929 commented 2 years ago

Awesome. Thanks a lot for the explanation. Have a wonderful day...