Created
October 27, 2020 18:39
-
-
Save FrozenHearth/7f764457d65536fe595762863c1808e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const applyDrag = (arr, dragResult) => { | |
const { removedIndex, addedIndex, payload } = dragResult; | |
if (removedIndex === null && addedIndex === null) return arr; | |
const result = [...arr]; | |
let itemToAdd = payload; | |
if (removedIndex !== null) { | |
itemToAdd = result.splice(removedIndex, 1)[0]; | |
} | |
if (addedIndex !== null) { | |
result.splice(addedIndex, 0, itemToAdd); | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment