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