Skip to content

Commit 5ff3f4a

Browse files
authored
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
1 parent 096c276 commit 5ff3f4a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/react-sortable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl
216216
// SORTABLE DOM HANDLING
217217

218218
onAdd(evt: MultiDragEvent) {
219-
const { list, setList } = this.props;
219+
const { list, setList, clone } = this.props;
220220
const otherList = [...store.dragging!.props.list];
221221
const customs = createCustoms(evt, otherList);
222222
removeNodes(customs);
223-
const newList = handleStateAdd(customs, list);
223+
const newList = handleStateAdd(customs, list, evt, clone).map(item => ({ ...item, selected: false }));
224224
setList(newList, this.sortable, store);
225225
}
226226

src/util.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PropsWithChildren } from "react";
2-
import { Options } from "sortablejs";
2+
import Sortable, { Options } from "sortablejs";
33
import { MultiDragEvent } from "./react-sortable";
44
import { AllMethodNames, ItemInterface, ReactSortableProps } from "./types";
55

@@ -116,10 +116,15 @@ export function handleStateRemove<T extends ItemInterface>(
116116

117117
export function handleStateAdd<T extends ItemInterface>(
118118
normalized: Normalized<T>[],
119-
list: T[]
119+
list: T[],
120+
evt?: Sortable.SortableEvent,
121+
clone?: ((currentItem: T, evt: Sortable.SortableEvent) => T) | undefined
120122
): T[] {
121123
const newList = [...list];
122-
normalized.forEach(curr => newList.splice(curr.newIndex, 0, curr.item));
124+
normalized.forEach(curr => {
125+
const newItem = (clone && evt) && clone(curr.item, evt);
126+
newList.splice(curr.newIndex, 0, newItem || curr.item)
127+
});
123128
return newList;
124129
}
125130

0 commit comments

Comments
 (0)