Skip to content

Commit

Permalink
fix: passes clone function to onAdd if cloning (#173)
Browse files Browse the repository at this point in the history
* onAdd method can add new attribution when cloning; BUG FIXED: clone the DOM at first time add new attribution invalid
  • Loading branch information
cangshudada committed Sep 25, 2020
1 parent 096c276 commit 5ff3f4a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/react-sortable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
11 changes: 8 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -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";

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

export function handleStateAdd<T extends ItemInterface>(
normalized: Normalized<T>[],
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;
}

Expand Down

0 comments on commit 5ff3f4a

Please sign in to comment.