mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-05-02 08:06:41 +07:00
added useSortableActions hook
This commit is contained in:
parent
3345939670
commit
586ebb7480
7 changed files with 86 additions and 72 deletions
src/hooks/sortable
1
src/hooks/sortable/index.ts
Normal file
1
src/hooks/sortable/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from './useSortableActions';
|
57
src/hooks/sortable/useSortableActions.ts
Normal file
57
src/hooks/sortable/useSortableActions.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import { useCallback, useMemo, useState } from 'react';
|
||||
|
||||
import { DragStartEvent, MouseSensor, TouchSensor, useSensor, useSensors } from '@dnd-kit/core';
|
||||
import { DragEndEvent } from '@dnd-kit/core/dist/types';
|
||||
|
||||
import { moveArrItem } from '~/utils/fn';
|
||||
|
||||
export const useSortableActions = <T>(
|
||||
items: T[],
|
||||
getID: (item: T) => string | number,
|
||||
onSortEnd: (items: T[]) => void
|
||||
) => {
|
||||
const [draggingItem, setDraggingItem] = useState<T | null>(null);
|
||||
|
||||
const ids = useMemo(() => items.map(getID), [items]);
|
||||
|
||||
const onDragEnd = useCallback(
|
||||
({ active, over }: DragEndEvent) => {
|
||||
setDraggingItem(null);
|
||||
|
||||
if (!over?.id || active.id === over.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const oldIndex = items.findIndex(it => getID(it) === active.id);
|
||||
const newIndex = items.findIndex(it => getID(it) === over.id);
|
||||
|
||||
onSortEnd(moveArrItem(oldIndex, newIndex, items));
|
||||
},
|
||||
[items]
|
||||
);
|
||||
|
||||
const onDragStart = useCallback(
|
||||
({ active }: DragStartEvent) => {
|
||||
if (!active.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const activeItem = items.find(it => getID(it) === active.id);
|
||||
|
||||
setDraggingItem(activeItem ?? null);
|
||||
},
|
||||
[items]
|
||||
);
|
||||
|
||||
const sensors = useSensors(
|
||||
useSensor(TouchSensor, {
|
||||
activationConstraint: {
|
||||
delay: 200,
|
||||
tolerance: 5,
|
||||
},
|
||||
}),
|
||||
useSensor(MouseSensor)
|
||||
);
|
||||
|
||||
return { sensors, onDragEnd, onDragStart, draggingItem, ids };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue