Setup test component for drag and drop hook.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { useState, useRef, useCallback, useEffect } from "react";
|
||||
|
||||
type Position = {
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
|
||||
type DragAndDropConfig = {
|
||||
longPressEnabled?: boolean;
|
||||
longPressDelay?: number;
|
||||
dragHandleSelector?: string;
|
||||
};
|
||||
|
||||
type DragAndDropState = {
|
||||
isDragging: boolean;
|
||||
currentIndex: number | null;
|
||||
targetIndex: number | null;
|
||||
};
|
||||
|
||||
export function useDragAndDrop<T>(
|
||||
items: T[],
|
||||
onReorder: (newOrder: T[]) => void,
|
||||
config: DragAndDropConfig = {},
|
||||
) {
|
||||
const {
|
||||
longPressEnabled = false,
|
||||
longPressDelay = 300,
|
||||
dragHandleSelector,
|
||||
} = config;
|
||||
|
||||
const itemRefs = useRef<(HTMLElement | null)[]>([]);
|
||||
const containerRef = useRef<HTMLElement | null>(null);
|
||||
|
||||
const [dragState, setDragState] = useState<DragAndDropState>({
|
||||
isDragging: false,
|
||||
currentIndex: null,
|
||||
targetIndex: null,
|
||||
});
|
||||
|
||||
const getItemProps = (index: number) => {
|
||||
return { style: {} };
|
||||
};
|
||||
|
||||
return {
|
||||
containerRef,
|
||||
itemRefs,
|
||||
isDragging: dragState.isDragging,
|
||||
currentIndex: dragState.currentIndex,
|
||||
targetIndex: dragState.targetIndex,
|
||||
getItemProps,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user