forked from neolution-ch/react-data-table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableRows.tsx
More file actions
91 lines (87 loc) · 3.07 KB
/
TableRows.tsx
File metadata and controls
91 lines (87 loc) · 3.07 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
import { useSortable } from "@dnd-kit/sortable";
import { Row, flexRender } from "@tanstack/react-table";
import { CSSProperties } from "react";
import { CSS } from "@dnd-kit/utilities";
import { getCommonPinningStyles } from "../utils/getCommonPinningStyles";
import { FilterModel } from "../types/TableState";
import { ReactDataTableProps } from "./ReactDataTableProps";
interface TableRowProps<TData, TFilter extends FilterModel = Record<string, never>> extends Pick<
ReactDataTableProps<TData, TFilter>,
"onRowClick" | "enableRowClick" | "subRowComponent"
> {
row: Row<TData>;
enableRowSelection?: boolean | ((row: Row<TData>) => boolean);
fullRowSelectable?: boolean;
enableExpanding?: boolean | ((row: Row<TData>) => boolean);
rowStyle?: CSSProperties;
setNodeRef?: (node: HTMLElement | null) => void;
hasPinnedColumns?: boolean;
}
const InternalTableRow = <TData, TFilter extends FilterModel = Record<string, never>>(props: TableRowProps<TData, TFilter>) => {
const {
row,
rowStyle,
setNodeRef,
enableRowSelection = false,
fullRowSelectable = true,
onRowClick,
enableRowClick,
hasPinnedColumns,
subRowComponent,
} = props;
const isRowSelectionEnabled =
(typeof enableRowSelection === "function" ? enableRowSelection(row) : enableRowSelection) && fullRowSelectable;
const isRowClickable = typeof enableRowClick === "function" ? enableRowClick(row) : enableRowClick;
return (
<>
<tr
key={row.id}
ref={setNodeRef}
onClick={async () => {
if (isRowSelectionEnabled) {
row.toggleSelected();
} else if (isRowClickable && onRowClick) {
await onRowClick(row);
} else {
// Nothing to execute
}
}}
className={isRowSelectionEnabled || isRowClickable ? "cursor-pointer" : undefined}
style={rowStyle}
>
{row.getVisibleCells().map((cell) => (
<td
key={cell.id}
style={{
...cell.column.columnDef.meta?.cellStyle,
...(hasPinnedColumns ? getCommonPinningStyles(cell.column) : {}),
}}
className={cell.column.columnDef.meta?.cellClassName}
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
{subRowComponent && row.getIsExpanded() && subRowComponent(row)}
</>
);
};
const DraggableRow = <TData, TFilter extends FilterModel = Record<string, never>>(props: TableRowProps<TData, TFilter>) => {
const { row, rowStyle } = props;
const { transform, transition, setNodeRef, isDragging } = useSortable({ id: row.id });
const draggableStyle: CSSProperties = {
transform: CSS.Transform.toString(transform),
transition: transition,
opacity: isDragging ? 0.8 : 1,
zIndex: isDragging ? 1 : 0,
position: "relative",
};
return (
<InternalTableRow<TData, TFilter>
{...props}
setNodeRef={setNodeRef}
rowStyle={rowStyle ? { ...rowStyle, ...draggableStyle } : draggableStyle}
/>
);
};
export { InternalTableRow, DraggableRow };