From 5ff3f4a989162f08269a66fc325f22fcb600cfd2 Mon Sep 17 00:00:00 2001 From: varon <40785860+cangshudada@users.noreply.github.com> Date: Fri, 25 Sep 2020 18:42:06 +0800 Subject: [PATCH] fix: passes clone function to onAdd if cloning (#173) * onAdd method can add new attribution when cloning; BUG FIXED: clone the DOM at first time add new attribution invalid --- src/react-sortable.tsx | 4 ++-- src/util.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/react-sortable.tsx b/src/react-sortable.tsx index 65b2869..ed74c96 100644 --- a/src/react-sortable.tsx +++ b/src/react-sortable.tsx @@ -216,11 +216,11 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl // SORTABLE DOM HANDLING onAdd(evt: MultiDragEvent) { - const { list, setList } = this.props; + const { list, setList, clone } = this.props; const otherList = [...store.dragging!.props.list]; const customs = createCustoms(evt, otherList); removeNodes(customs); - const newList = handleStateAdd(customs, list); + const newList = handleStateAdd(customs, list, evt, clone).map(item => ({ ...item, selected: false })); setList(newList, this.sortable, store); } diff --git a/src/util.ts b/src/util.ts index 27c2488..e459f6c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,5 +1,5 @@ import { PropsWithChildren } from "react"; -import { Options } from "sortablejs"; +import Sortable, { Options } from "sortablejs"; import { MultiDragEvent } from "./react-sortable"; import { AllMethodNames, ItemInterface, ReactSortableProps } from "./types"; @@ -116,10 +116,15 @@ export function handleStateRemove( export function handleStateAdd( normalized: Normalized[], - list: T[] + list: T[], + evt?: Sortable.SortableEvent, + clone?: ((currentItem: T, evt: Sortable.SortableEvent) => T) | undefined ): T[] { const newList = [...list]; - normalized.forEach(curr => newList.splice(curr.newIndex, 0, curr.item)); + normalized.forEach(curr => { + const newItem = (clone && evt) && clone(curr.item, evt); + newList.splice(curr.newIndex, 0, newItem || curr.item) + }); return newList; }