import classNames from 'classnames'; import { Dispatch, MutableRefObject, ReactNode, SetStateAction, forwardRef, useEffect, useState, } from 'react'; import BodyTr from './BodyTr'; import {Empty} from 'antd'; import {HeaderGroup, Row, RowModel} from '@tanstack/react-table'; import { DndContext, DragEndEvent, DragOverlay, DragStartEvent, PointerSensor, closestCenter, useSensor, } from '@dnd-kit/core'; import { SortableContext, arrayMove, verticalListSortingStrategy, } from '@dnd-kit/sortable'; import {debounce} from 'lodash-es'; type Props = { scrollProgess: 'start' | 'end' | 'process', getCenterTotalSize: () => number, getRowModel: () => RowModel>, highlightValue?: unknown, hightlightKey?: any, getHeaderGroups: () => HeaderGroup>[], enableDnd?: boolean, rawKey?: any, data: any[], setData: Dispatch>, isSecondLevel?: boolean, renderSubComponent?: (row: Row>) => ReactNode, }; type ActiveState = { row: Row>, } | null; export default forwardRef(function TableBody( { scrollProgess, getCenterTotalSize, getRowModel, highlightValue, hightlightKey, getHeaderGroups, data, enableDnd, rawKey, setData, isSecondLevel, renderSubComponent, }, ref, ) { const sensor = useSensor(PointerSensor, { activationConstraint: {distance: 10}, }); const [active, setActive] = useState(null); function onDragStart(event: DragStartEvent) { setActive(event.active.data.current as ActiveState); } function onDragEnd({over, active}: DragEndEvent) { setActive(null); if (!over) return; const {id: activeId} = active, {id: overId} = over; if (activeId === overId) return; setData(function(prev) { const fromIdx = prev.findIndex(val => val[rawKey ?? 'id'] === activeId), toIdx = prev.findIndex(val => val[rawKey ?? 'id'] === overId); return arrayMove(prev, fromIdx, toIdx); }); } const [tableWidth, setTableWidth] = useState(0); useEffect(function() { if (!enableDnd) return; const dom = (ref as MutableRefObject).current; const obsever = new ResizeObserver(debounce( function([entrie]) { setTableWidth(entrie.contentRect.width); }, 300, {leading: false, trailing: true}, )); obsever.observe(dom); return () => obsever.disconnect(); }, [enableDnd, ref]); return ( val[rawKey ?? 'id'])} strategy={verticalListSortingStrategy} >
{getRowModel().rows.length > 0 ? ( getRowModel().rows.map(function(row) { return ( ); }) ) : ( )}
{active && (
)}
); });