Skip to content

Instantly share code, notes, and snippets.

@FrozenHearth
Created October 27, 2020 18:39
Show Gist options
  • Save FrozenHearth/7f764457d65536fe595762863c1808e4 to your computer and use it in GitHub Desktop.
Save FrozenHearth/7f764457d65536fe595762863c1808e4 to your computer and use it in GitHub Desktop.
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