@brotskyllc/react-data-table 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/THIRD_PARTY_NOTICES.md +27 -0
- package/dist/ColumnResizeHandle.d.ts +9 -0
- package/dist/ColumnResizeHandle.js +49 -0
- package/dist/DataTableDraggableRows.d.ts +21 -0
- package/dist/DataTableDraggableRows.js +29 -0
- package/dist/DataTableRows.d.ts +22 -0
- package/dist/DataTableRows.js +62 -0
- package/dist/DataTableSkeletonRows.d.ts +8 -0
- package/dist/DataTableSkeletonRows.js +9 -0
- package/dist/DataTableTableContent.d.ts +28 -0
- package/dist/DataTableTableContent.js +46 -0
- package/dist/TableCell.d.ts +7 -0
- package/dist/TableCell.js +20 -0
- package/dist/TableHeader.d.ts +16 -0
- package/dist/TableHeader.js +42 -0
- package/dist/TableRow.d.ts +26 -0
- package/dist/TableRow.js +31 -0
- package/dist/cn.d.ts +2 -0
- package/dist/cn.js +3 -0
- package/dist/columnResizeTypes.d.ts +18 -0
- package/dist/columnResizeTypes.js +1 -0
- package/dist/columnTypes.d.ts +25 -0
- package/dist/columnTypes.js +1 -0
- package/dist/columnWidthStorage.d.ts +13 -0
- package/dist/columnWidthStorage.js +53 -0
- package/dist/compat.d.ts +17 -0
- package/dist/compat.js +19 -0
- package/dist/getColumnWidthStyle.d.ts +1 -0
- package/dist/getColumnWidthStyle.js +7 -0
- package/dist/getTableHeaderFlexClassName.d.ts +2 -0
- package/dist/getTableHeaderFlexClassName.js +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/isRowClickSelectTarget.d.ts +1 -0
- package/dist/isRowClickSelectTarget.js +34 -0
- package/dist/rowIds.d.ts +2 -0
- package/dist/rowIds.js +2 -0
- package/dist/rowPickerOpenState.d.ts +11 -0
- package/dist/rowPickerOpenState.js +41 -0
- package/dist/selectionTypes.d.ts +10 -0
- package/dist/selectionTypes.js +1 -0
- package/dist/sortOrder.d.ts +5 -0
- package/dist/sortOrder.js +1 -0
- package/dist/sortRowsByColumn.d.ts +9 -0
- package/dist/sortRowsByColumn.js +40 -0
- package/dist/stripImportantWidthClasses.d.ts +1 -0
- package/dist/stripImportantWidthClasses.js +9 -0
- package/dist/types.d.ts +47 -0
- package/dist/types.js +1 -0
- package/dist/ui/skeleton.d.ts +2 -0
- package/dist/ui/skeleton.js +6 -0
- package/dist/ui/table-scroll-region.d.ts +11 -0
- package/dist/ui/table-scroll-region.js +30 -0
- package/dist/ui/table.d.ts +51 -0
- package/dist/ui/table.js +20 -0
- package/dist/ui/tableScrollRegion.d.ts +15 -0
- package/dist/ui/tableScrollRegion.js +10 -0
- package/dist/ui/tooltip.d.ts +12 -0
- package/dist/ui/tooltip.js +10 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +5 -0
- package/package.json +65 -4
- package/src/ColumnResizeHandle.tsx +102 -0
- package/src/DataTableDraggableRows.tsx +132 -0
- package/src/DataTableRows.tsx +239 -0
- package/src/DataTableSkeletonRows.tsx +46 -0
- package/src/DataTableTableContent.tsx +160 -0
- package/src/TableCell.tsx +39 -0
- package/src/TableHeader.tsx +205 -0
- package/src/TableRow.tsx +151 -0
- package/src/cn.ts +3 -0
- package/src/columnResizeTypes.ts +26 -0
- package/src/columnTypes.ts +39 -0
- package/src/columnWidthStorage.ts +94 -0
- package/src/compat.ts +19 -0
- package/src/getColumnWidthStyle.ts +11 -0
- package/src/getTableHeaderFlexClassName.ts +14 -0
- package/src/index.ts +13 -0
- package/src/isRowClickSelectTarget.ts +39 -0
- package/src/rowIds.ts +11 -0
- package/src/rowPickerOpenState.tsx +98 -0
- package/src/selectionTypes.ts +16 -0
- package/src/sortOrder.ts +2 -0
- package/src/sortRowsByColumn.ts +72 -0
- package/src/stripImportantWidthClasses.ts +16 -0
- package/src/types.ts +51 -0
- package/src/ui/skeleton.tsx +15 -0
- package/src/ui/table-scroll-region.tsx +68 -0
- package/src/ui/table.tsx +147 -0
- package/src/ui/tableScrollRegion.ts +33 -0
- package/src/ui/tooltip.tsx +36 -0
- package/src/utils.ts +5 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useId, useState } from "react";
|
|
3
|
+
import { Table, TableCell, TableFooter, TableRow } from "./ui/table.js";
|
|
4
|
+
import { cn } from "./cn.js";
|
|
5
|
+
import type { ColumnResizeApi } from "./columnResizeTypes.js";
|
|
6
|
+
import { DataTableRows } from "./DataTableRows.js";
|
|
7
|
+
import { RowPickerOpenStateProvider } from "./rowPickerOpenState.js";
|
|
8
|
+
import { stripImportantWidthClasses } from "./stripImportantWidthClasses.js";
|
|
9
|
+
import { DataTableHeader } from "./TableHeader.js";
|
|
10
|
+
import type { DataTableProps, TableColumn } from "./types.js";
|
|
11
|
+
|
|
12
|
+
export type DataTableTableContentProps<T> = {
|
|
13
|
+
columns: TableColumn<T>[];
|
|
14
|
+
data: DataTableProps<T>["data"];
|
|
15
|
+
sort?: DataTableProps<T>["sort"];
|
|
16
|
+
selection?: DataTableProps<T>["selection"];
|
|
17
|
+
draggable?: DataTableProps<T>["draggable"];
|
|
18
|
+
expandableRows?: DataTableProps<T>["expandableRows"];
|
|
19
|
+
tableClassName?: DataTableProps<T>["tableClassName"];
|
|
20
|
+
scrollAreaLabel?: DataTableProps<T>["scrollAreaLabel"];
|
|
21
|
+
rowClassName?: DataTableProps<T>["rowClassName"];
|
|
22
|
+
getRowDataTestId?: DataTableProps<T>["getRowDataTestId"];
|
|
23
|
+
onRowClick?: DataTableProps<T>["onRowClick"];
|
|
24
|
+
onRowMouseEnter?: DataTableProps<T>["onRowMouseEnter"];
|
|
25
|
+
onRowMouseLeave?: DataTableProps<T>["onRowMouseLeave"];
|
|
26
|
+
footer?: DataTableProps<T>["footer"];
|
|
27
|
+
columnResize?: ColumnResizeApi | null;
|
|
28
|
+
isTableLoading?: boolean;
|
|
29
|
+
loadingRowCount?: number;
|
|
30
|
+
emptyState?: DataTableProps<T>["emptyState"];
|
|
31
|
+
allSelected?: boolean;
|
|
32
|
+
hasSelectableRows?: boolean;
|
|
33
|
+
onSort: (column: TableColumn<T>) => void;
|
|
34
|
+
onSelectAll: () => void;
|
|
35
|
+
onRowSelect: (rowId: string) => void;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const DataTableTableContent = <T,>({
|
|
39
|
+
columns,
|
|
40
|
+
data,
|
|
41
|
+
sort,
|
|
42
|
+
selection,
|
|
43
|
+
draggable,
|
|
44
|
+
expandableRows,
|
|
45
|
+
tableClassName,
|
|
46
|
+
scrollAreaLabel,
|
|
47
|
+
rowClassName,
|
|
48
|
+
getRowDataTestId,
|
|
49
|
+
onRowClick,
|
|
50
|
+
onRowMouseEnter,
|
|
51
|
+
onRowMouseLeave,
|
|
52
|
+
footer,
|
|
53
|
+
columnResize,
|
|
54
|
+
isTableLoading = false,
|
|
55
|
+
loadingRowCount = 8,
|
|
56
|
+
emptyState,
|
|
57
|
+
allSelected = false,
|
|
58
|
+
hasSelectableRows = false,
|
|
59
|
+
onSort,
|
|
60
|
+
onSelectAll,
|
|
61
|
+
onRowSelect,
|
|
62
|
+
}: DataTableTableContentProps<T>) => {
|
|
63
|
+
const [uncontrolledExpandedRowIds, setUncontrolledExpandedRowIds] = useState<
|
|
64
|
+
Set<string>
|
|
65
|
+
>(() => new Set());
|
|
66
|
+
const tableInstanceId = useId();
|
|
67
|
+
|
|
68
|
+
const controlledExpandedRowIds =
|
|
69
|
+
expandableRows?.onToggleExpandedRowId &&
|
|
70
|
+
expandableRows.expandedRowIds !== undefined
|
|
71
|
+
? expandableRows.expandedRowIds
|
|
72
|
+
: undefined;
|
|
73
|
+
const isExpandControlled = controlledExpandedRowIds !== undefined;
|
|
74
|
+
const expandedRowIds = controlledExpandedRowIds ?? uncontrolledExpandedRowIds;
|
|
75
|
+
|
|
76
|
+
const toggleRowExpansion = (rowId: string) => {
|
|
77
|
+
if (expandableRows?.onToggleExpandedRowId && isExpandControlled) {
|
|
78
|
+
expandableRows.onToggleExpandedRowId(rowId);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
setUncontrolledExpandedRowIds((prev) => {
|
|
83
|
+
const next = new Set(prev);
|
|
84
|
+
if (next.has(rowId)) {
|
|
85
|
+
next.delete(rowId);
|
|
86
|
+
} else {
|
|
87
|
+
next.add(rowId);
|
|
88
|
+
}
|
|
89
|
+
return next;
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
const resizeTableStyle =
|
|
93
|
+
columnResize?.isFixedLayout && columnResize.totalWidth !== null
|
|
94
|
+
? {
|
|
95
|
+
tableLayout: "fixed" as const,
|
|
96
|
+
width: `${columnResize.totalWidth}px`,
|
|
97
|
+
minWidth: `${columnResize.totalWidth}px`,
|
|
98
|
+
}
|
|
99
|
+
: undefined;
|
|
100
|
+
const resolvedTableClassName = resizeTableStyle
|
|
101
|
+
? stripImportantWidthClasses(tableClassName)
|
|
102
|
+
: tableClassName;
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<RowPickerOpenStateProvider>
|
|
106
|
+
<Table
|
|
107
|
+
className={cn(
|
|
108
|
+
resolvedTableClassName,
|
|
109
|
+
columnResize && "[&_td]:overflow-hidden [&_th]:overflow-hidden",
|
|
110
|
+
columnResize?.resizingColumnKey && "select-none",
|
|
111
|
+
)}
|
|
112
|
+
style={resizeTableStyle}
|
|
113
|
+
scrollAreaLabel={scrollAreaLabel}
|
|
114
|
+
>
|
|
115
|
+
<DataTableHeader
|
|
116
|
+
columns={columns}
|
|
117
|
+
sort={sort}
|
|
118
|
+
selection={selection}
|
|
119
|
+
draggable={draggable}
|
|
120
|
+
expandableRows={expandableRows}
|
|
121
|
+
allSelected={allSelected}
|
|
122
|
+
headerCheckboxDisabled={selection ? !hasSelectableRows : false}
|
|
123
|
+
columnResize={columnResize}
|
|
124
|
+
onSort={onSort}
|
|
125
|
+
onSelectAll={onSelectAll}
|
|
126
|
+
/>
|
|
127
|
+
<DataTableRows
|
|
128
|
+
data={data}
|
|
129
|
+
columns={columns}
|
|
130
|
+
draggable={draggable}
|
|
131
|
+
selection={selection}
|
|
132
|
+
expandableRows={expandableRows}
|
|
133
|
+
expandedRowIds={expandedRowIds}
|
|
134
|
+
onToggleRowExpansion={toggleRowExpansion}
|
|
135
|
+
onRowSelect={onRowSelect}
|
|
136
|
+
tableInstanceId={tableInstanceId}
|
|
137
|
+
isTableLoading={isTableLoading}
|
|
138
|
+
loadingRowCount={loadingRowCount}
|
|
139
|
+
emptyState={emptyState}
|
|
140
|
+
rowClassName={rowClassName}
|
|
141
|
+
getRowDataTestId={getRowDataTestId}
|
|
142
|
+
onRowClick={onRowClick}
|
|
143
|
+
onRowMouseEnter={onRowMouseEnter}
|
|
144
|
+
onRowMouseLeave={onRowMouseLeave}
|
|
145
|
+
/>
|
|
146
|
+
{footer && footer.cells.length > 0 && (
|
|
147
|
+
<TableFooter className={footer.className}>
|
|
148
|
+
<TableRow>
|
|
149
|
+
{footer.cells.map((cell) => (
|
|
150
|
+
<TableCell key={cell.key} className={cell.className}>
|
|
151
|
+
{cell.content}
|
|
152
|
+
</TableCell>
|
|
153
|
+
))}
|
|
154
|
+
</TableRow>
|
|
155
|
+
</TableFooter>
|
|
156
|
+
)}
|
|
157
|
+
</Table>
|
|
158
|
+
</RowPickerOpenStateProvider>
|
|
159
|
+
);
|
|
160
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { TableCell } from "./ui/table.js";
|
|
2
|
+
import { cn } from "./cn.js";
|
|
3
|
+
import type { TableColumn } from "./types.js";
|
|
4
|
+
import { getNestedValue } from "./utils.js";
|
|
5
|
+
|
|
6
|
+
interface TableCellProps<T> {
|
|
7
|
+
column: TableColumn<T>;
|
|
8
|
+
row: T;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const DataTableCell = <T,>({ column, row }: TableCellProps<T>) => {
|
|
12
|
+
const value = getNestedValue(row, column.key as string);
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
<TableCell
|
|
16
|
+
className={cn("text-gray-600 dark:text-gray-300", column.className)}
|
|
17
|
+
style={
|
|
18
|
+
column.width
|
|
19
|
+
? {
|
|
20
|
+
width:
|
|
21
|
+
typeof column.width === "number"
|
|
22
|
+
? `${column.width}px`
|
|
23
|
+
: column.width,
|
|
24
|
+
minWidth:
|
|
25
|
+
typeof column.width === "number"
|
|
26
|
+
? `${column.width}px`
|
|
27
|
+
: column.width,
|
|
28
|
+
maxWidth:
|
|
29
|
+
typeof column.width === "number"
|
|
30
|
+
? `${column.width}px`
|
|
31
|
+
: column.width,
|
|
32
|
+
}
|
|
33
|
+
: undefined
|
|
34
|
+
}
|
|
35
|
+
>
|
|
36
|
+
{column.render ? column.render(value, row) : String(value ?? "")}
|
|
37
|
+
</TableCell>
|
|
38
|
+
);
|
|
39
|
+
};
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { ChevronDown, ChevronUp, HelpCircle } from "lucide-react";
|
|
2
|
+
import {
|
|
3
|
+
TableHead,
|
|
4
|
+
TableRow,
|
|
5
|
+
TableHeader as UITableHeader,
|
|
6
|
+
} from "./ui/table.js";
|
|
7
|
+
import {
|
|
8
|
+
Tooltip,
|
|
9
|
+
TooltipContent,
|
|
10
|
+
TooltipProvider,
|
|
11
|
+
TooltipTrigger,
|
|
12
|
+
} from "./ui/tooltip.js";
|
|
13
|
+
import { SortOrder } from "./sortOrder.js";
|
|
14
|
+
import { cn } from "./cn.js";
|
|
15
|
+
import { ColumnResizeHandle } from "./ColumnResizeHandle.js";
|
|
16
|
+
import type { ColumnResizeApi } from "./columnResizeTypes.js";
|
|
17
|
+
import {
|
|
18
|
+
readColumnWidth,
|
|
19
|
+
UTILITY_COLUMN_WIDTH_PX,
|
|
20
|
+
} from "./columnWidthStorage.js";
|
|
21
|
+
import { getColumnWidthStyle } from "./getColumnWidthStyle.js";
|
|
22
|
+
import {
|
|
23
|
+
getTableHeaderFlexClassName,
|
|
24
|
+
isTableColumnRightAligned,
|
|
25
|
+
} from "./getTableHeaderFlexClassName.js";
|
|
26
|
+
import type { DataTableProps, TableColumn } from "./types.js";
|
|
27
|
+
|
|
28
|
+
const headerAriaSort = <T,>(
|
|
29
|
+
column: TableColumn<T>,
|
|
30
|
+
sort: DataTableProps<T>["sort"],
|
|
31
|
+
): "ascending" | "descending" | "none" | undefined => {
|
|
32
|
+
if (!column.sortable) return undefined;
|
|
33
|
+
if (sort?.field !== column.key) return "none";
|
|
34
|
+
return sort.order === SortOrder.Desc ? "descending" : "ascending";
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
interface TableHeaderProps<T> {
|
|
38
|
+
columns: TableColumn<T>[];
|
|
39
|
+
sort?: DataTableProps<T>["sort"];
|
|
40
|
+
selection?: DataTableProps<T>["selection"];
|
|
41
|
+
draggable?: DataTableProps<T>["draggable"];
|
|
42
|
+
expandableRows?: DataTableProps<T>["expandableRows"];
|
|
43
|
+
allSelected: boolean;
|
|
44
|
+
headerCheckboxDisabled?: boolean;
|
|
45
|
+
|
|
46
|
+
columnResize?: ColumnResizeApi | null;
|
|
47
|
+
onSort: (column: TableColumn<T>) => void;
|
|
48
|
+
onSelectAll: () => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const DataTableHeader = <T,>({
|
|
52
|
+
columns,
|
|
53
|
+
sort,
|
|
54
|
+
selection,
|
|
55
|
+
draggable,
|
|
56
|
+
expandableRows,
|
|
57
|
+
allSelected,
|
|
58
|
+
headerCheckboxDisabled = false,
|
|
59
|
+
columnResize,
|
|
60
|
+
onSort,
|
|
61
|
+
onSelectAll,
|
|
62
|
+
}: TableHeaderProps<T>) => {
|
|
63
|
+
const hasAnyTooltip = columns.some((c) => c.headerTooltip);
|
|
64
|
+
const utilityColumnStyle = columnResize
|
|
65
|
+
? getColumnWidthStyle(UTILITY_COLUMN_WIDTH_PX)
|
|
66
|
+
: undefined;
|
|
67
|
+
const headerCells = (
|
|
68
|
+
<>
|
|
69
|
+
{selection && (
|
|
70
|
+
<TableHead className="w-10" style={utilityColumnStyle}>
|
|
71
|
+
<input
|
|
72
|
+
type="checkbox"
|
|
73
|
+
aria-label={selection.selectAllAriaLabel}
|
|
74
|
+
data-testid={selection.selectAllTestId}
|
|
75
|
+
checked={allSelected}
|
|
76
|
+
disabled={headerCheckboxDisabled}
|
|
77
|
+
onChange={onSelectAll}
|
|
78
|
+
className={
|
|
79
|
+
headerCheckboxDisabled
|
|
80
|
+
? "cursor-not-allowed opacity-50"
|
|
81
|
+
: "cursor-pointer"
|
|
82
|
+
}
|
|
83
|
+
/>
|
|
84
|
+
</TableHead>
|
|
85
|
+
)}
|
|
86
|
+
{draggable?.enabled && (
|
|
87
|
+
<TableHead className="w-10" style={utilityColumnStyle}>
|
|
88
|
+
<span className="sr-only">Reorder rows</span>
|
|
89
|
+
</TableHead>
|
|
90
|
+
)}
|
|
91
|
+
{expandableRows && (
|
|
92
|
+
<TableHead className="w-10" style={utilityColumnStyle} />
|
|
93
|
+
)}
|
|
94
|
+
{columns.map((column) => {
|
|
95
|
+
const columnKey = String(column.key);
|
|
96
|
+
const isRightAligned = isTableColumnRightAligned(column.className);
|
|
97
|
+
const isResizable =
|
|
98
|
+
Boolean(columnResize?.isFixedLayout) && column.resizable !== false;
|
|
99
|
+
const currentWidth = columnResize
|
|
100
|
+
? readColumnWidth(columnResize.widths, columnKey)
|
|
101
|
+
: undefined;
|
|
102
|
+
const pinnedWidth = columnResize?.isFixedLayout
|
|
103
|
+
? currentWidth
|
|
104
|
+
: undefined;
|
|
105
|
+
const styleWidth = pinnedWidth ?? column.width;
|
|
106
|
+
const sortIcon = column.sortable ? (
|
|
107
|
+
<span className="inline-block h-4 w-4 flex-shrink-0">
|
|
108
|
+
{sort?.field === column.key ? (
|
|
109
|
+
sort.order === SortOrder.Asc ? (
|
|
110
|
+
<ChevronUp className="h-4 w-4" />
|
|
111
|
+
) : (
|
|
112
|
+
<ChevronDown className="h-4 w-4" />
|
|
113
|
+
)
|
|
114
|
+
) : (
|
|
115
|
+
<span className="h-4 w-4" />
|
|
116
|
+
)}
|
|
117
|
+
</span>
|
|
118
|
+
) : null;
|
|
119
|
+
|
|
120
|
+
const headerLabel = columnResize ? (
|
|
121
|
+
<span className="truncate">{column.header}</span>
|
|
122
|
+
) : (
|
|
123
|
+
column.header
|
|
124
|
+
);
|
|
125
|
+
const headerContent = column.sortable ? (
|
|
126
|
+
<button
|
|
127
|
+
type="button"
|
|
128
|
+
aria-label={column.sortAriaLabel}
|
|
129
|
+
className="inline-flex min-w-0 items-center gap-1 rounded-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
|
|
130
|
+
>
|
|
131
|
+
{isRightAligned ? sortIcon : null}
|
|
132
|
+
{headerLabel}
|
|
133
|
+
{isRightAligned ? null : sortIcon}
|
|
134
|
+
</button>
|
|
135
|
+
) : (
|
|
136
|
+
headerLabel
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<TableHead
|
|
141
|
+
key={columnKey}
|
|
142
|
+
ref={columnResize?.registerHeaderRef(columnKey)}
|
|
143
|
+
aria-label={columnResize ? column.header : undefined}
|
|
144
|
+
aria-sort={headerAriaSort(column, sort)}
|
|
145
|
+
className={cn(
|
|
146
|
+
"text-gray-900 dark:text-white",
|
|
147
|
+
column.sortable && "cursor-pointer",
|
|
148
|
+
columnResize && "relative",
|
|
149
|
+
column.className,
|
|
150
|
+
column.headerClassName,
|
|
151
|
+
)}
|
|
152
|
+
style={getColumnWidthStyle(styleWidth)}
|
|
153
|
+
onClick={column.sortable ? () => onSort(column) : undefined}
|
|
154
|
+
>
|
|
155
|
+
<div
|
|
156
|
+
className={cn(
|
|
157
|
+
getTableHeaderFlexClassName(column.className),
|
|
158
|
+
columnResize && "min-w-0",
|
|
159
|
+
)}
|
|
160
|
+
>
|
|
161
|
+
{headerContent}
|
|
162
|
+
{column.headerTooltip && (
|
|
163
|
+
<Tooltip>
|
|
164
|
+
<TooltipTrigger asChild>
|
|
165
|
+
<button
|
|
166
|
+
type="button"
|
|
167
|
+
onClick={(e) => e.stopPropagation()}
|
|
168
|
+
className="inline-flex shrink-0 rounded p-0.5 text-muted-foreground hover:text-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-1"
|
|
169
|
+
aria-label="More information"
|
|
170
|
+
>
|
|
171
|
+
<HelpCircle className="h-3.5 w-3.5" />
|
|
172
|
+
</button>
|
|
173
|
+
</TooltipTrigger>
|
|
174
|
+
<TooltipContent side="top">
|
|
175
|
+
{column.headerTooltip}
|
|
176
|
+
</TooltipContent>
|
|
177
|
+
</Tooltip>
|
|
178
|
+
)}
|
|
179
|
+
</div>
|
|
180
|
+
{isResizable && columnResize && (
|
|
181
|
+
<ColumnResizeHandle
|
|
182
|
+
columnKey={columnKey}
|
|
183
|
+
columnHeader={column.header}
|
|
184
|
+
width={currentWidth}
|
|
185
|
+
columnResize={columnResize}
|
|
186
|
+
/>
|
|
187
|
+
)}
|
|
188
|
+
</TableHead>
|
|
189
|
+
);
|
|
190
|
+
})}
|
|
191
|
+
</>
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<UITableHeader>
|
|
196
|
+
<TableRow className="hover:bg-zinc-50 dark:hover:bg-zinc-800">
|
|
197
|
+
{hasAnyTooltip ? (
|
|
198
|
+
<TooltipProvider>{headerCells}</TooltipProvider>
|
|
199
|
+
) : (
|
|
200
|
+
headerCells
|
|
201
|
+
)}
|
|
202
|
+
</TableRow>
|
|
203
|
+
</UITableHeader>
|
|
204
|
+
);
|
|
205
|
+
};
|
package/src/TableRow.tsx
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { DraggableProvided } from "@hello-pangea/dnd";
|
|
2
|
+
import { DragHandleDots2Icon } from "@radix-ui/react-icons";
|
|
3
|
+
import { ChevronDown, ChevronRight } from "lucide-react";
|
|
4
|
+
import { TableCell, TableRow as UITableRow } from "./ui/table.js";
|
|
5
|
+
import { cn } from "./cn.js";
|
|
6
|
+
import { isRowClickSelectTarget } from "./isRowClickSelectTarget.js";
|
|
7
|
+
import { DataTableCell } from "./TableCell.js";
|
|
8
|
+
import type { DataTableProps, TableColumn } from "./types.js";
|
|
9
|
+
|
|
10
|
+
interface TableRowProps<T> {
|
|
11
|
+
row: T;
|
|
12
|
+
rowId: string;
|
|
13
|
+
columns: TableColumn<T>[];
|
|
14
|
+
selection?: DataTableProps<T>["selection"];
|
|
15
|
+
draggable?: DataTableProps<T>["draggable"];
|
|
16
|
+
expandableRows?: {
|
|
17
|
+
isRowExpandable: boolean;
|
|
18
|
+
isExpanded: boolean;
|
|
19
|
+
expandedContentId: string;
|
|
20
|
+
onToggleExpand: () => void;
|
|
21
|
+
toggleAriaLabel?: string;
|
|
22
|
+
toggleDataTestId?: string;
|
|
23
|
+
};
|
|
24
|
+
provided?: DraggableProvided;
|
|
25
|
+
rowClassName?: (row: T) => string;
|
|
26
|
+
getRowDataTestId?: (row: T) => string | undefined;
|
|
27
|
+
onRowClick?: DataTableProps<T>["onRowClick"];
|
|
28
|
+
onRowMouseEnter?: (row: T) => void;
|
|
29
|
+
onRowMouseLeave?: () => void;
|
|
30
|
+
onRowSelect: (rowId: string) => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const DataTableRow = <T,>({
|
|
34
|
+
row,
|
|
35
|
+
rowId,
|
|
36
|
+
columns,
|
|
37
|
+
selection,
|
|
38
|
+
draggable,
|
|
39
|
+
expandableRows,
|
|
40
|
+
provided,
|
|
41
|
+
rowClassName,
|
|
42
|
+
getRowDataTestId,
|
|
43
|
+
onRowClick,
|
|
44
|
+
onRowMouseEnter,
|
|
45
|
+
onRowMouseLeave,
|
|
46
|
+
onRowSelect,
|
|
47
|
+
}: TableRowProps<T>) => {
|
|
48
|
+
const rowSelectable =
|
|
49
|
+
!selection?.isRowSelectable || selection.isRowSelectable(row as T);
|
|
50
|
+
const clickTogglesSelection = Boolean(
|
|
51
|
+
selection?.selectOnRowClick && rowSelectable,
|
|
52
|
+
);
|
|
53
|
+
const clickOpensRow = Boolean(onRowClick) && !clickTogglesSelection;
|
|
54
|
+
|
|
55
|
+
const handleRowClick = (event: React.MouseEvent<HTMLTableRowElement>) => {
|
|
56
|
+
if (!isRowClickSelectTarget(event.target)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (clickTogglesSelection) {
|
|
60
|
+
onRowSelect((row as { id: string }).id);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
onRowClick?.(row);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<UITableRow
|
|
68
|
+
ref={provided?.innerRef}
|
|
69
|
+
{...provided?.draggableProps}
|
|
70
|
+
data-testid={getRowDataTestId?.(row as T)}
|
|
71
|
+
className={cn(
|
|
72
|
+
(clickTogglesSelection || clickOpensRow) && "cursor-pointer",
|
|
73
|
+
rowClassName?.(row),
|
|
74
|
+
)}
|
|
75
|
+
onClick={
|
|
76
|
+
clickTogglesSelection || clickOpensRow ? handleRowClick : undefined
|
|
77
|
+
}
|
|
78
|
+
onMouseEnter={() => {
|
|
79
|
+
onRowMouseEnter?.(row);
|
|
80
|
+
}}
|
|
81
|
+
onMouseLeave={() => {
|
|
82
|
+
onRowMouseLeave?.();
|
|
83
|
+
}}
|
|
84
|
+
>
|
|
85
|
+
{selection && (
|
|
86
|
+
<TableCell className="w-10">
|
|
87
|
+
<input
|
|
88
|
+
type="checkbox"
|
|
89
|
+
aria-label={selection.getRowCheckboxAriaLabel?.(row as T)}
|
|
90
|
+
data-testid={selection.getRowCheckboxTestId?.(row as T)}
|
|
91
|
+
checked={
|
|
92
|
+
rowSelectable &&
|
|
93
|
+
selection.selectedIds.includes((row as { id: string }).id)
|
|
94
|
+
}
|
|
95
|
+
disabled={!rowSelectable}
|
|
96
|
+
onChange={() => onRowSelect((row as { id: string }).id)}
|
|
97
|
+
className={
|
|
98
|
+
rowSelectable ? "cursor-pointer" : "cursor-not-allowed opacity-50"
|
|
99
|
+
}
|
|
100
|
+
/>
|
|
101
|
+
</TableCell>
|
|
102
|
+
)}
|
|
103
|
+
{draggable?.enabled && (
|
|
104
|
+
<TableCell className="w-11">
|
|
105
|
+
{}
|
|
106
|
+
<div
|
|
107
|
+
{...(draggable?.enabled ? provided?.dragHandleProps : {})}
|
|
108
|
+
aria-label={
|
|
109
|
+
draggable.getRowDragHandleLabel?.(row as T) ?? "Reorder row"
|
|
110
|
+
}
|
|
111
|
+
className={cn(
|
|
112
|
+
"flex min-h-11 min-w-11 items-center justify-center text-muted-foreground hover:text-foreground",
|
|
113
|
+
draggable?.dragHandleClassName,
|
|
114
|
+
)}
|
|
115
|
+
>
|
|
116
|
+
<DragHandleDots2Icon className="h-4 w-4" aria-hidden />
|
|
117
|
+
</div>
|
|
118
|
+
</TableCell>
|
|
119
|
+
)}
|
|
120
|
+
{expandableRows && (
|
|
121
|
+
<TableCell className="w-10">
|
|
122
|
+
{expandableRows.isRowExpandable && (
|
|
123
|
+
<button
|
|
124
|
+
type="button"
|
|
125
|
+
data-testid={expandableRows.toggleDataTestId}
|
|
126
|
+
aria-expanded={expandableRows.isExpanded}
|
|
127
|
+
aria-controls={expandableRows.expandedContentId}
|
|
128
|
+
onClick={expandableRows.onToggleExpand}
|
|
129
|
+
className="inline-flex h-8 w-8 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-1"
|
|
130
|
+
>
|
|
131
|
+
<span className="sr-only">
|
|
132
|
+
{expandableRows.toggleAriaLabel ??
|
|
133
|
+
(expandableRows.isExpanded
|
|
134
|
+
? `Collapse row ${rowId}`
|
|
135
|
+
: `Expand row ${rowId}`)}
|
|
136
|
+
</span>
|
|
137
|
+
{expandableRows.isExpanded ? (
|
|
138
|
+
<ChevronDown className="h-4 w-4" aria-hidden="true" />
|
|
139
|
+
) : (
|
|
140
|
+
<ChevronRight className="h-4 w-4" aria-hidden="true" />
|
|
141
|
+
)}
|
|
142
|
+
</button>
|
|
143
|
+
)}
|
|
144
|
+
</TableCell>
|
|
145
|
+
)}
|
|
146
|
+
{columns.map((column) => (
|
|
147
|
+
<DataTableCell key={String(column.key)} column={column} row={row} />
|
|
148
|
+
))}
|
|
149
|
+
</UITableRow>
|
|
150
|
+
);
|
|
151
|
+
};
|
package/src/cn.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PersistedColumnWidths } from "./columnWidthStorage.js";
|
|
2
|
+
export type ColumnResizeApi = {
|
|
3
|
+
/** Host-defined bounds announced by resize controls. */
|
|
4
|
+
getColumnBounds?: (columnKey: string) => { min: number; max: number };
|
|
5
|
+
widths: PersistedColumnWidths;
|
|
6
|
+
|
|
7
|
+
totalWidth: number | null;
|
|
8
|
+
|
|
9
|
+
isFixedLayout: boolean;
|
|
10
|
+
|
|
11
|
+
resizingColumnKey: string | null;
|
|
12
|
+
|
|
13
|
+
registerHeaderRef: (
|
|
14
|
+
columnKey: string,
|
|
15
|
+
) => (element: HTMLTableCellElement | null) => void;
|
|
16
|
+
|
|
17
|
+
startResize: (columnKey: string, clientX: number) => void;
|
|
18
|
+
|
|
19
|
+
updateResize: (clientX: number) => void;
|
|
20
|
+
|
|
21
|
+
endResize: () => void;
|
|
22
|
+
|
|
23
|
+
nudgeWidth: (columnKey: string, deltaX: number) => void;
|
|
24
|
+
|
|
25
|
+
resetWidth: (columnKey: string) => void;
|
|
26
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type NestedKeyOf<T> = T extends object
|
|
2
|
+
? {
|
|
3
|
+
[K in keyof T]: K extends string
|
|
4
|
+
? T[K] extends object
|
|
5
|
+
? K | `${K}.${NestedKeyOf<T[K]>}`
|
|
6
|
+
: K
|
|
7
|
+
: never;
|
|
8
|
+
}[keyof T]
|
|
9
|
+
: never;
|
|
10
|
+
|
|
11
|
+
export type ExportCellValue = string | number | boolean | null | undefined;
|
|
12
|
+
|
|
13
|
+
export type SortCellValue = string | number | boolean | null | undefined;
|
|
14
|
+
|
|
15
|
+
export interface TableColumn<T> {
|
|
16
|
+
key: NestedKeyOf<T> | string;
|
|
17
|
+
header: string;
|
|
18
|
+
|
|
19
|
+
headerTooltip?: string;
|
|
20
|
+
/** Dynamic nested cell values preserve the host column renderer contract. */
|
|
21
|
+
render?: (value: any, row: T) => React.ReactNode;
|
|
22
|
+
sortable?: boolean;
|
|
23
|
+
|
|
24
|
+
sortValue?: (row: T) => SortCellValue;
|
|
25
|
+
width?: string | number;
|
|
26
|
+
|
|
27
|
+
resizable?: boolean;
|
|
28
|
+
className?: string;
|
|
29
|
+
/** Additional header-only classes (for sticky headers or theme overrides). */
|
|
30
|
+
headerClassName?: string;
|
|
31
|
+
/** Accessible sort-button name; defaults to the visible header. */
|
|
32
|
+
sortAriaLabel?: string;
|
|
33
|
+
|
|
34
|
+
skeletonClassName?: string;
|
|
35
|
+
/** Metadata consumed by host exporters; this package never exports data. */
|
|
36
|
+
exportValue?: (value: any, row: T) => ExportCellValue;
|
|
37
|
+
|
|
38
|
+
excludeFromExport?: boolean;
|
|
39
|
+
}
|