Skip to content

onAdd method can add new attribution when cloning; BUG FIXED: clone the DOM at first time add new attribution invalid #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dist/util.d.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 All @@ -22,7 +22,8 @@ export declare function createCustoms<T extends ItemInterface>(evt: MultiDragEve
/** moves items form old index to new index without breaking anything ideally. */
export declare function handleStateChanges<T extends ItemInterface>(normalized: Normalized<T>[], list: T[]): T[];
export declare function handleStateRemove<T extends ItemInterface>(normalized: Normalized<T>[], list: T[]): T[];
export declare function handleStateAdd<T extends ItemInterface>(normalized: Normalized<T>[], list: T[]): T[];
export declare function handleStateAdd<T extends ItemInterface>(normalized: Normalized<T>[], list: T[], evt?: Sortable.SortableEvent,
clone?: ((currentItem: T, evt: Sortable.SortableEvent) => T) | undefined): T[];
export declare function getMode(evt: MultiDragEvent): "multidrag" | "swap" | "normal";
export declare function createNormalized<T extends ItemInterface>(inputs: Input[], list: T[]): Normalized<T>[];
export interface Input {
Expand Down
1 change: 1 addition & 0 deletions examples/components/disable-sorting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function DisableSorting() {
list={list2}
setList={setList2}
animation={150}
clone={(item) => ({ ...item, id: createId(), createTag: Symbol(1) })}
group={{ name: "disable-group-name", pull: "clone" }}
>
{list2.map(item => (
Expand Down
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