|
| 1 | +import { DraggableRow, InternalTableRow } from "./TableRows"; |
| 2 | +import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable"; |
| 3 | +import { Row, Table } from "@tanstack/react-table"; |
| 4 | +import { CSSProperties, useMemo } from "react"; |
| 5 | +import { FilterModel } from "../types/TableState"; |
| 6 | +import { ReactDataTableProps } from "./ReactDataTableProps"; |
| 7 | +import { Virtualizer } from "@tanstack/react-virtual"; |
| 8 | + |
| 9 | +interface TableBodyProps<TData, TFilter extends FilterModel = Record<string, never>> |
| 10 | + extends Pick<ReactDataTableProps<TData, TFilter>, "enableRowClick" | "onRowClick" | "subRowComponent"> { |
| 11 | + enableDragAndDrop: boolean; |
| 12 | + table: Table<TData>; |
| 13 | + rowStyle?: (row: TData) => CSSProperties; |
| 14 | + virtualizer?: Virtualizer<HTMLDivElement, Element>; |
| 15 | +} |
| 16 | + |
| 17 | +interface InternalRow<TData> { |
| 18 | + row: Row<TData>; |
| 19 | + rowStyle?: CSSProperties; |
| 20 | +} |
| 21 | + |
| 22 | +const TableBody = <TData, TFilter extends FilterModel = Record<string, never>>(props: TableBodyProps<TData, TFilter>) => { |
| 23 | + const { enableDragAndDrop, table, rowStyle, enableRowClick, onRowClick, virtualizer, subRowComponent } = props; |
| 24 | + |
| 25 | + if (enableDragAndDrop && !table.options.getRowId) { |
| 26 | + throw new Error("You must provide 'getRowId()' to data-table options in order to use the drag-and-drop feature."); |
| 27 | + } |
| 28 | + |
| 29 | + const { |
| 30 | + options: { enableRowSelection, enableExpanding, fullRowSelectable }, |
| 31 | + } = table; |
| 32 | + |
| 33 | + const { rows } = table.getRowModel(); |
| 34 | + |
| 35 | + const virtualizerRows = virtualizer?.getVirtualItems(); |
| 36 | + |
| 37 | + const rowsToRender: InternalRow<TData>[] = useMemo( |
| 38 | + () => |
| 39 | + virtualizerRows |
| 40 | + ? virtualizerRows.map((virtualRow, index) => ({ |
| 41 | + row: rows[virtualRow.index], |
| 42 | + rowStyle: { |
| 43 | + height: `${virtualRow.size}px`, |
| 44 | + transform: `translateY(${virtualRow.start - index * virtualRow.size}px)`, |
| 45 | + }, |
| 46 | + })) |
| 47 | + : rows.map((row) => ({ row })), |
| 48 | + [rows, virtualizerRows], |
| 49 | + ); |
| 50 | + |
| 51 | + return enableDragAndDrop ? ( |
| 52 | + <SortableContext items={table.getRowModel().rows.map((row) => row.id)} strategy={verticalListSortingStrategy}> |
| 53 | + {rowsToRender.map((x, index) => { |
| 54 | + const { row } = x; |
| 55 | + return ( |
| 56 | + <DraggableRow<TData, TFilter> |
| 57 | + key={index} |
| 58 | + row={row} |
| 59 | + enableRowClick={enableRowClick as ReactDataTableProps<TData, TFilter>["enableRowClick"]} |
| 60 | + onRowClick={onRowClick as ReactDataTableProps<TData, TFilter>["onRowClick"]} |
| 61 | + enableRowSelection={enableRowSelection as boolean | ((row: Row<TData>) => boolean)} |
| 62 | + enableExpanding={enableExpanding as boolean | ((row: Row<TData>) => boolean)} |
| 63 | + rowStyle={{ |
| 64 | + ...(x.rowStyle ?? {}), |
| 65 | + ...(rowStyle ? rowStyle(row.original) : {}), |
| 66 | + }} |
| 67 | + fullRowSelectable={fullRowSelectable} |
| 68 | + subRowComponent={subRowComponent as ReactDataTableProps<TData, TFilter>["subRowComponent"]} |
| 69 | + /> |
| 70 | + ); |
| 71 | + })} |
| 72 | + </SortableContext> |
| 73 | + ) : ( |
| 74 | + <> |
| 75 | + {rowsToRender.map((x, index) => { |
| 76 | + const { row } = x; |
| 77 | + return ( |
| 78 | + <InternalTableRow<TData, TFilter> |
| 79 | + key={index} |
| 80 | + row={row} |
| 81 | + enableRowClick={enableRowClick as ReactDataTableProps<TData, TFilter>["enableRowClick"]} |
| 82 | + onRowClick={onRowClick as ReactDataTableProps<TData, TFilter>["onRowClick"]} |
| 83 | + enableRowSelection={enableRowSelection as boolean | ((row: Row<TData>) => boolean)} |
| 84 | + enableExpanding={enableExpanding as boolean | ((row: Row<TData>) => boolean)} |
| 85 | + rowStyle={{ |
| 86 | + ...(x.rowStyle ?? {}), |
| 87 | + ...(rowStyle ? rowStyle(row.original) : {}), |
| 88 | + }} |
| 89 | + fullRowSelectable={fullRowSelectable} |
| 90 | + hasPinnedColumns={table.getIsSomeColumnsPinned()} |
| 91 | + subRowComponent={subRowComponent as ReactDataTableProps<TData, TFilter>["subRowComponent"]} |
| 92 | + /> |
| 93 | + ); |
| 94 | + })} |
| 95 | + </> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +export { TableBody }; |
0 commit comments