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

[FEATURE] Expose moved item index in Reorder.group #2603

Open
thecrowkeep opened this issue Apr 1, 2024 · 0 comments
Open

[FEATURE] Expose moved item index in Reorder.group #2603

thecrowkeep opened this issue Apr 1, 2024 · 0 comments
Labels
feature New feature or request

Comments

@thecrowkeep
Copy link

thecrowkeep commented Apr 1, 2024

Is your feature request related to a problem? Please describe.
Using a Redis DB where items are ordered in a list, to move an item you need to:

  1. Delete the item in the list that is to be moved
  2. Insert the item before or after the position in the list you want it to be in

This is much more performant than rewriting an array every single time a position changes.

However because onReorder only exposes the new state, you have to do a bunch of unnecessary computation to find the difference in the two arrays. It's also awkward to implement, especially considering how simple it would be to use a moved item index and find where in the new array its new index is.

Describe the solution you'd like
Either onReorder implements (newItems: any[], movedItem: number) => void or a new method is implemented (movedItem: number, {before: number} | {after: number}) or really any other method that at the very least exposes the moved item index.

Describe alternatives you've considered

  • Store the last moved item's using the Reorder.Item onDrag method. onDrag={()=>callback(value)}.
    • Find the index of the moved item's value const index = newOrder.indexOf(movedItem)
    • The following is specific to my needs of reordering in a Redis db, if anyone wants a solution for that specifically
    • Find the next/previous indexes const next = index + 1; const prev = index - 1
    • Determine whether we should move after or before an index next < newOrder.length ? after : prev >= 0 ? before : null
    • Update DB
  • Calculating the difference between the old array and the new array, to find the item that was moved. (I'm not going to figure out an example because the first alternative is much better.)
@thecrowkeep thecrowkeep added the feature New feature or request label Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant