motiondivision / motiondivision/motion

[FEATURE] Expose moved item index in Reorder.group

Open
#2,603 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
TypeScript
Stars
33.7k
Forks
1.4k
Avg merge
1d 10h
Merged PRs (30d)
14

Description

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.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Reorder.group implementation and its onReorder callback, then review Reorder.Item and its onDrag API. Compare the proposed callback and method shapes with the existing public API and determine which moved-index behavior is consistent; done means the selected API exposes the moved item index without requiring array-difference computation.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.