|
| 1 | +import React, { useRef, useState } from "react" |
| 2 | +import { useDraggable } from "react-draggable-plus" |
| 3 | +import PreviewList from "react-draggable-plus/builtins/PreviewList" |
| 4 | +import Sortable from "sortablejs" |
| 5 | + |
| 6 | +const Function = () => { |
| 7 | + const el1 = useRef<any>(null) |
| 8 | + const el2 = useRef<any>(null) |
| 9 | + |
| 10 | + const data = [ |
| 11 | + { |
| 12 | + name: 'Joao', |
| 13 | + id: '1' |
| 14 | + }, |
| 15 | + { |
| 16 | + name: 'Jean', |
| 17 | + id: '2' |
| 18 | + }, |
| 19 | + { |
| 20 | + name: 'Johanna', |
| 21 | + id:'3' |
| 22 | + }, |
| 23 | + { |
| 24 | + name: 'Juan', |
| 25 | + id: '4' |
| 26 | + } |
| 27 | + ] |
| 28 | + const group:Sortable.GroupOptions = { name: "people", pull: "clone", put: false} |
| 29 | + const [list1, setList1] = useState(data) |
| 30 | + const [list2, setList2] = useState(data.map(item => ({ |
| 31 | + name: `${item.name}-2`, |
| 32 | + id: `${item.id}-2` |
| 33 | + }))) |
| 34 | + |
| 35 | + useDraggable(el1, list1, setList1, { |
| 36 | + animation: 150, |
| 37 | + group, |
| 38 | + sort: false, |
| 39 | + clone: (element: Record<'name' | 'id', string>) => { |
| 40 | + const len = list2.length |
| 41 | + return { |
| 42 | + name: `${element.name}-clone-${len}`, |
| 43 | + id: `${element.id}-clone-${len}` |
| 44 | + } |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + useDraggable(el2, list2, setList2, { |
| 49 | + animation: 150, |
| 50 | + group: 'people', |
| 51 | + }) |
| 52 | + |
| 53 | + return <> |
| 54 | + <div className="flex"> |
| 55 | + <div |
| 56 | + className="flex flex-col gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded overflow-auto" |
| 57 | + ref={el1} |
| 58 | + > |
| 59 | + { |
| 60 | + list1.map(item => { |
| 61 | + return <div |
| 62 | + key={item.id} |
| 63 | + className="cursor-move h-30 bg-gray-500/5 rounded p-3" |
| 64 | + > |
| 65 | + { item.name } |
| 66 | + </div> |
| 67 | + }) |
| 68 | + } |
| 69 | + </div> |
| 70 | + <div |
| 71 | + className="flex flex-col gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded overflow-auto" |
| 72 | + ref={el2} |
| 73 | + > |
| 74 | + { |
| 75 | + list2.map(item => { |
| 76 | + return <div |
| 77 | + key={item.id} |
| 78 | + className="cursor-move h-30 bg-gray-500/5 rounded p-3" |
| 79 | + > |
| 80 | + { item.name } |
| 81 | + </div> |
| 82 | + }) |
| 83 | + } |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + <div className="flex justify-between"> |
| 87 | + <PreviewList list={list1} className="m-auto"></PreviewList> |
| 88 | + <PreviewList list={list2} className="m-auto"></PreviewList> |
| 89 | + </div> |
| 90 | +</> |
| 91 | +} |
| 92 | + |
| 93 | +export default Function |
0 commit comments