-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
200 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React, { useState } from 'react' | ||
import { useSortable } from '@dnd-kit/sortable' | ||
import { CSS } from '@dnd-kit/utilities' | ||
|
||
import { | ||
DndContext, | ||
closestCenter, | ||
KeyboardSensor, | ||
PointerSensor, | ||
useSensor, | ||
useSensors, | ||
} from '@dnd-kit/core' | ||
import { | ||
arrayMove, | ||
SortableContext, | ||
sortableKeyboardCoordinates, | ||
verticalListSortingStrategy, | ||
} from '@dnd-kit/sortable' | ||
|
||
import styles from '~/components/nft-collection/styles.css' | ||
|
||
export const links = () => [{ rel: 'stylesheet', href: styles }] | ||
|
||
export function SortableItem(props: any) { | ||
const { attributes, listeners, setNodeRef, transform, transition } = | ||
useSortable({ id: props.id }) | ||
|
||
const style = { | ||
transform: CSS.Transform.toString(transform), | ||
transition, | ||
} | ||
|
||
return ( | ||
<div className="list-row" ref={setNodeRef} style={style}> | ||
<span>{props.id}</span> | ||
<button {...attributes} {...listeners}> | ||
⣿ | ||
</button> | ||
<span>{props.id}</span> | ||
</div> | ||
) | ||
} | ||
|
||
export default function App() { | ||
const [items, setItems] = useState(['1', '2', '3']) | ||
const sensors = useSensors( | ||
useSensor(PointerSensor), | ||
useSensor(KeyboardSensor, { | ||
coordinateGetter: sortableKeyboardCoordinates, | ||
}) | ||
) | ||
|
||
console.log(items) | ||
return ( | ||
<DndContext | ||
sensors={sensors} | ||
collisionDetection={closestCenter} | ||
onDragEnd={handleDragEnd} | ||
> | ||
<SortableContext items={items} strategy={verticalListSortingStrategy}> | ||
{items.map((id) => ( | ||
<SortableItem key={id} id={id} /> | ||
))} | ||
</SortableContext> | ||
</DndContext> | ||
) | ||
|
||
function handleDragEnd(event: any) { | ||
const { active, over } = event | ||
|
||
if (active.id !== over.id) { | ||
setItems((items) => { | ||
const oldIndex = items.indexOf(active.id) | ||
const newIndex = items.indexOf(over.id) | ||
|
||
return arrayMove(items, oldIndex, newIndex) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters