@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,94 @@
|
|
|
1
|
+
export const COLUMN_WIDTH_STORAGE_KEY_PREFIX = "datatable-column-widths:v1";
|
|
2
|
+
|
|
3
|
+
export const MIN_COLUMN_WIDTH_PX = 64;
|
|
4
|
+
|
|
5
|
+
export const MAX_COLUMN_WIDTH_PX = 960;
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_COLUMN_WIDTH_PX = 160;
|
|
8
|
+
|
|
9
|
+
export const UTILITY_COLUMN_WIDTH_PX = 64;
|
|
10
|
+
|
|
11
|
+
export type PersistedColumnWidths = Record<string, number>;
|
|
12
|
+
|
|
13
|
+
export const createColumnWidths = (): PersistedColumnWidths =>
|
|
14
|
+
Object.create(null) as PersistedColumnWidths;
|
|
15
|
+
|
|
16
|
+
export const buildColumnWidthStorageKey = (tableId: string): string =>
|
|
17
|
+
`${COLUMN_WIDTH_STORAGE_KEY_PREFIX}:${tableId}`;
|
|
18
|
+
|
|
19
|
+
export const clampColumnWidth = (width: number): number => {
|
|
20
|
+
if (!Number.isFinite(width)) {
|
|
21
|
+
return MIN_COLUMN_WIDTH_PX;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return Math.min(
|
|
25
|
+
MAX_COLUMN_WIDTH_PX,
|
|
26
|
+
Math.max(MIN_COLUMN_WIDTH_PX, Math.round(width)),
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const clampColumnWidthAround = (
|
|
31
|
+
width: number,
|
|
32
|
+
baselineWidth: number,
|
|
33
|
+
): number => {
|
|
34
|
+
if (!(Number.isFinite(baselineWidth) && Number.isFinite(width))) {
|
|
35
|
+
return clampColumnWidth(width);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const baseline = Math.round(baselineWidth);
|
|
39
|
+
|
|
40
|
+
return Math.min(
|
|
41
|
+
Math.max(MAX_COLUMN_WIDTH_PX, baseline),
|
|
42
|
+
Math.max(Math.min(MIN_COLUMN_WIDTH_PX, baseline), Math.round(width)),
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const resizeColumnWidth = (startWidth: number, deltaX: number): number =>
|
|
47
|
+
clampColumnWidthAround(startWidth + deltaX, startWidth);
|
|
48
|
+
|
|
49
|
+
export const parsePersistedColumnWidths = (
|
|
50
|
+
rawValue: string | null,
|
|
51
|
+
): PersistedColumnWidths | null => {
|
|
52
|
+
if (rawValue === null) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let parsed: unknown;
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(rawValue);
|
|
60
|
+
} catch {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const widths = createColumnWidths();
|
|
69
|
+
|
|
70
|
+
for (const [columnKey, width] of Object.entries(parsed)) {
|
|
71
|
+
if (typeof width !== "number" || !Number.isFinite(width) || width <= 0) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
widths[columnKey] = Math.round(width);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return widths;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const readColumnWidth = (
|
|
82
|
+
widths: PersistedColumnWidths,
|
|
83
|
+
columnKey: string,
|
|
84
|
+
): number | undefined => {
|
|
85
|
+
if (!Object.hasOwn(widths, columnKey)) {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const width = widths[columnKey];
|
|
90
|
+
|
|
91
|
+
return typeof width === "number" && Number.isFinite(width)
|
|
92
|
+
? width
|
|
93
|
+
: undefined;
|
|
94
|
+
};
|
package/src/compat.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
// Compatibility exports for apps migrating existing table modules.
|
|
3
|
+
export * from "./DataTableRows.js";
|
|
4
|
+
export * from "./DataTableDraggableRows.js";
|
|
5
|
+
export * from "./DataTableSkeletonRows.js";
|
|
6
|
+
export * from "./TableHeader.js";
|
|
7
|
+
export * from "./TableRow.js";
|
|
8
|
+
export * from "./TableCell.js";
|
|
9
|
+
export * from "./ColumnResizeHandle.js";
|
|
10
|
+
export * from "./rowIds.js";
|
|
11
|
+
export * from "./isRowClickSelectTarget.js";
|
|
12
|
+
export * from "./stripImportantWidthClasses.js";
|
|
13
|
+
export * from "./getColumnWidthStyle.js";
|
|
14
|
+
export * from "./getTableHeaderFlexClassName.js";
|
|
15
|
+
export * from "./columnWidthStorage.js";
|
|
16
|
+
export * from "./columnTypes.js";
|
|
17
|
+
export * from "./selectionTypes.js";
|
|
18
|
+
export * from "./sortRowsByColumn.js";
|
|
19
|
+
export * from "./utils.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const getColumnWidthStyle = (
|
|
2
|
+
width: string | number | undefined,
|
|
3
|
+
): React.CSSProperties | undefined => {
|
|
4
|
+
if (width === undefined) {
|
|
5
|
+
return undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const cssWidth = typeof width === "number" ? `${width}px` : width;
|
|
9
|
+
|
|
10
|
+
return { width: cssWidth, minWidth: cssWidth, maxWidth: cssWidth };
|
|
11
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { cn } from "./cn.js";
|
|
2
|
+
|
|
3
|
+
export const isTableColumnRightAligned = (columnClassName?: string): boolean =>
|
|
4
|
+
(columnClassName?.split(/\s+/) ?? []).includes("text-right");
|
|
5
|
+
|
|
6
|
+
export const getTableHeaderFlexClassName = (columnClassName?: string) => {
|
|
7
|
+
const classes = columnClassName?.split(/\s+/) ?? [];
|
|
8
|
+
|
|
9
|
+
return cn(
|
|
10
|
+
"flex items-center gap-1",
|
|
11
|
+
classes.includes("text-right") && "justify-end",
|
|
12
|
+
classes.includes("text-center") && "justify-center",
|
|
13
|
+
);
|
|
14
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
export {
|
|
3
|
+
DataTableTableContent,
|
|
4
|
+
DataTableTableContent as DataTableCore,
|
|
5
|
+
} from "./DataTableTableContent.js";
|
|
6
|
+
export type {
|
|
7
|
+
DataTableTableContentProps,
|
|
8
|
+
DataTableTableContentProps as DataTableCoreProps,
|
|
9
|
+
} from "./DataTableTableContent.js";
|
|
10
|
+
export type * from "./types.js";
|
|
11
|
+
export type { ColumnResizeApi } from "./columnResizeTypes.js";
|
|
12
|
+
export * from "./rowPickerOpenState.js";
|
|
13
|
+
export { sortRowsByColumn } from "./sortRowsByColumn.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const INTERACTIVE_SELECTOR = [
|
|
2
|
+
"a[href]",
|
|
3
|
+
"button",
|
|
4
|
+
"input",
|
|
5
|
+
"select",
|
|
6
|
+
"textarea",
|
|
7
|
+
"label",
|
|
8
|
+
"summary",
|
|
9
|
+
"audio[controls]",
|
|
10
|
+
"video[controls]",
|
|
11
|
+
"[contenteditable='true']",
|
|
12
|
+
"[role='button']",
|
|
13
|
+
"[role='checkbox']",
|
|
14
|
+
"[role='combobox']",
|
|
15
|
+
"[role='link']",
|
|
16
|
+
"[role='menuitem']",
|
|
17
|
+
"[role='option']",
|
|
18
|
+
"[role='radio']",
|
|
19
|
+
"[role='switch']",
|
|
20
|
+
"[role='tab']",
|
|
21
|
+
].join(",");
|
|
22
|
+
|
|
23
|
+
export const isRowClickSelectTarget = (target: EventTarget | null): boolean => {
|
|
24
|
+
if (!(target instanceof Element)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (target.closest(INTERACTIVE_SELECTOR)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const selection =
|
|
33
|
+
typeof window === "undefined" ? null : window.getSelection?.();
|
|
34
|
+
if (selection && !selection.isCollapsed && selection.toString().length > 0) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return true;
|
|
39
|
+
};
|
package/src/rowIds.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const getRowId = <T>(row: T, index: number): string =>
|
|
2
|
+
(row as { id?: string }).id || `row-${index}`;
|
|
3
|
+
|
|
4
|
+
export const getExpandedContentRowId = (
|
|
5
|
+
tableInstanceId: string,
|
|
6
|
+
rowId: string,
|
|
7
|
+
) =>
|
|
8
|
+
`${tableInstanceId}-expanded-content-${rowId}`.replace(
|
|
9
|
+
/[^a-zA-Z0-9_-]/g,
|
|
10
|
+
"-",
|
|
11
|
+
);
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
type ReactNode,
|
|
6
|
+
useCallback,
|
|
7
|
+
useContext,
|
|
8
|
+
useMemo,
|
|
9
|
+
useState,
|
|
10
|
+
} from "react";
|
|
11
|
+
|
|
12
|
+
type RowPickerOpenStateValue = {
|
|
13
|
+
openPickerId: string | null;
|
|
14
|
+
setPickerOpen: (pickerId: string, open: boolean) => void;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const RowPickerOpenStateContext = createContext<RowPickerOpenStateValue | null>(
|
|
18
|
+
null,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
const DataTableRowIdContext = createContext<string | null>(null);
|
|
22
|
+
|
|
23
|
+
export const RowPickerOpenStateProvider = ({
|
|
24
|
+
children,
|
|
25
|
+
}: {
|
|
26
|
+
children: ReactNode;
|
|
27
|
+
}) => {
|
|
28
|
+
const [openPickerId, setOpenPickerId] = useState<string | null>(null);
|
|
29
|
+
|
|
30
|
+
const setPickerOpen = useCallback((pickerId: string, open: boolean) => {
|
|
31
|
+
setOpenPickerId((current) => {
|
|
32
|
+
if (open) {
|
|
33
|
+
return pickerId;
|
|
34
|
+
}
|
|
35
|
+
return current === pickerId ? null : current;
|
|
36
|
+
});
|
|
37
|
+
}, []);
|
|
38
|
+
|
|
39
|
+
const value = useMemo(
|
|
40
|
+
() => ({ openPickerId, setPickerOpen }),
|
|
41
|
+
[openPickerId, setPickerOpen],
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<RowPickerOpenStateContext.Provider value={value}>
|
|
46
|
+
{children}
|
|
47
|
+
</RowPickerOpenStateContext.Provider>
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const DataTableRowIdProvider = ({
|
|
52
|
+
rowId,
|
|
53
|
+
children,
|
|
54
|
+
}: {
|
|
55
|
+
rowId: string;
|
|
56
|
+
children: ReactNode;
|
|
57
|
+
}) => (
|
|
58
|
+
<DataTableRowIdContext.Provider value={rowId}>
|
|
59
|
+
{children}
|
|
60
|
+
</DataTableRowIdContext.Provider>
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const isHostDisconnected = (hostRef?: {
|
|
64
|
+
readonly current: HTMLElement | null;
|
|
65
|
+
}) => Boolean(hostRef?.current && !hostRef.current.isConnected);
|
|
66
|
+
|
|
67
|
+
export const useDataTableRowPickerOpen = (
|
|
68
|
+
kind: string,
|
|
69
|
+
hostRef?: { readonly current: HTMLElement | null },
|
|
70
|
+
) => {
|
|
71
|
+
const rowId = useContext(DataTableRowIdContext);
|
|
72
|
+
const tableState = useContext(RowPickerOpenStateContext);
|
|
73
|
+
const [localOpen, setLocalOpen] = useState(false);
|
|
74
|
+
|
|
75
|
+
const pickerId = rowId ? `${kind}:${rowId}` : null;
|
|
76
|
+
const persist = pickerId !== null && tableState !== null;
|
|
77
|
+
|
|
78
|
+
const open =
|
|
79
|
+
persist && pickerId && tableState
|
|
80
|
+
? tableState.openPickerId === pickerId
|
|
81
|
+
: localOpen;
|
|
82
|
+
|
|
83
|
+
const setOpen = useCallback(
|
|
84
|
+
(next: boolean) => {
|
|
85
|
+
if (persist && pickerId && tableState) {
|
|
86
|
+
if (!next && isHostDisconnected(hostRef)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
tableState.setPickerOpen(pickerId, next);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
setLocalOpen(next);
|
|
93
|
+
},
|
|
94
|
+
[hostRef, persist, pickerId, tableState],
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
return [open, setOpen] as const;
|
|
98
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type DataTableSelectionOptions<T> = {
|
|
2
|
+
selectedIds: string[];
|
|
3
|
+
onSelectionChange: (selectedIds: string[]) => void;
|
|
4
|
+
|
|
5
|
+
isRowSelectable?: (row: T) => boolean;
|
|
6
|
+
|
|
7
|
+
selectOnRowClick?: boolean;
|
|
8
|
+
|
|
9
|
+
selectAllAriaLabel?: string;
|
|
10
|
+
|
|
11
|
+
selectAllTestId?: string;
|
|
12
|
+
|
|
13
|
+
getRowCheckboxAriaLabel?: (row: T) => string;
|
|
14
|
+
|
|
15
|
+
getRowCheckboxTestId?: (row: T) => string | undefined;
|
|
16
|
+
};
|
package/src/sortOrder.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { SortOrder } from "./sortOrder.js";
|
|
2
|
+
import type { SortCellValue, TableColumn } from "./types.js";
|
|
3
|
+
import { getNestedValue } from "./utils.js";
|
|
4
|
+
|
|
5
|
+
type ComparableSortValue = string | number;
|
|
6
|
+
|
|
7
|
+
const readSortValue = <T>(
|
|
8
|
+
row: T,
|
|
9
|
+
column: TableColumn<T>,
|
|
10
|
+
): SortCellValue | ComparableSortValue => {
|
|
11
|
+
const value = column.sortValue
|
|
12
|
+
? column.sortValue(row)
|
|
13
|
+
: getNestedValue(row, String(column.key));
|
|
14
|
+
|
|
15
|
+
if (typeof value === "boolean") return value ? 1 : 0;
|
|
16
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
17
|
+
return null;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const isEmptySortValue = (value: SortCellValue): boolean =>
|
|
21
|
+
value === null || value === undefined || value === "";
|
|
22
|
+
|
|
23
|
+
const compareSortValues = (
|
|
24
|
+
left: ComparableSortValue,
|
|
25
|
+
right: ComparableSortValue,
|
|
26
|
+
): number => {
|
|
27
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
28
|
+
return left - right;
|
|
29
|
+
}
|
|
30
|
+
return String(left).localeCompare(String(right), undefined, {
|
|
31
|
+
sensitivity: "accent",
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
interface SortRowsByColumnParams<T> {
|
|
36
|
+
rows: T[];
|
|
37
|
+
|
|
38
|
+
column: TableColumn<T> | undefined;
|
|
39
|
+
order: SortOrder | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const sortRowsByColumn = <T>({
|
|
43
|
+
rows,
|
|
44
|
+
column,
|
|
45
|
+
order,
|
|
46
|
+
}: SortRowsByColumnParams<T>): T[] => {
|
|
47
|
+
if (!column?.sortable || !order) {
|
|
48
|
+
return rows;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const direction = order === SortOrder.Desc ? -1 : 1;
|
|
52
|
+
|
|
53
|
+
return [...rows].sort((leftRow, rightRow) => {
|
|
54
|
+
const left = readSortValue(leftRow, column);
|
|
55
|
+
const right = readSortValue(rightRow, column);
|
|
56
|
+
|
|
57
|
+
const leftEmpty = isEmptySortValue(left);
|
|
58
|
+
const rightEmpty = isEmptySortValue(right);
|
|
59
|
+
if (leftEmpty || rightEmpty) {
|
|
60
|
+
if (leftEmpty && rightEmpty) return 0;
|
|
61
|
+
return leftEmpty ? 1 : -1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
direction *
|
|
66
|
+
compareSortValues(
|
|
67
|
+
left as ComparableSortValue,
|
|
68
|
+
right as ComparableSortValue,
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const stripImportantWidthClasses = (
|
|
2
|
+
className: string | undefined,
|
|
3
|
+
): string | undefined => {
|
|
4
|
+
if (!className) {
|
|
5
|
+
return className;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const kept = className
|
|
9
|
+
.split(/\s+/)
|
|
10
|
+
.filter(
|
|
11
|
+
(token) =>
|
|
12
|
+
token !== "" && !/^(?:[\w-]+:)*!(?:w|min-w|max-w)-/.test(token),
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
return kept.length > 0 ? kept.join(" ") : undefined;
|
|
16
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type * as React from "react";
|
|
2
|
+
import type { DropResult } from "@hello-pangea/dnd";
|
|
3
|
+
import type { TableColumn } from "./columnTypes.js";
|
|
4
|
+
import type { DataTableSelectionOptions } from "./selectionTypes.js";
|
|
5
|
+
export type {
|
|
6
|
+
TableColumn,
|
|
7
|
+
SortCellValue,
|
|
8
|
+
ExportCellValue,
|
|
9
|
+
NestedKeyOf,
|
|
10
|
+
} from "./columnTypes.js";
|
|
11
|
+
export type { DataTableSelectionOptions } from "./selectionTypes.js";
|
|
12
|
+
export type ExpandableRowsOptions<T> = {
|
|
13
|
+
renderExpandedContent: (row: T) => React.ReactNode;
|
|
14
|
+
|
|
15
|
+
isRowExpandable?: (row: T) => boolean;
|
|
16
|
+
|
|
17
|
+
expandedRowClassName?: string;
|
|
18
|
+
|
|
19
|
+
getToggleAriaLabel?: (row: T, isExpanded: boolean) => string;
|
|
20
|
+
|
|
21
|
+
getToggleDataTestId?: (row: T) => string | undefined;
|
|
22
|
+
|
|
23
|
+
expandedRowIds?: Set<string>;
|
|
24
|
+
onToggleExpandedRowId?: (rowId: string) => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export interface DataTableProps<T> {
|
|
28
|
+
columns: TableColumn<T>[];
|
|
29
|
+
data: T[];
|
|
30
|
+
sort?: { field: string | null; order: "asc" | "desc" | null };
|
|
31
|
+
selection?: DataTableSelectionOptions<T>;
|
|
32
|
+
draggable?: {
|
|
33
|
+
enabled: boolean;
|
|
34
|
+
onDragEnd: (result: DropResult) => void;
|
|
35
|
+
dragHandleClassName?: string;
|
|
36
|
+
getRowDragHandleLabel?: (row: T) => string;
|
|
37
|
+
};
|
|
38
|
+
expandableRows?: ExpandableRowsOptions<T>;
|
|
39
|
+
tableClassName?: string;
|
|
40
|
+
scrollAreaLabel?: string;
|
|
41
|
+
rowClassName?: (row: T) => string;
|
|
42
|
+
getRowDataTestId?: (row: T) => string | undefined;
|
|
43
|
+
onRowClick?: (row: T) => void;
|
|
44
|
+
onRowMouseEnter?: (row: T) => void;
|
|
45
|
+
onRowMouseLeave?: () => void;
|
|
46
|
+
footer?: {
|
|
47
|
+
cells: { key: string; content: React.ReactNode; className?: string }[];
|
|
48
|
+
className?: string;
|
|
49
|
+
};
|
|
50
|
+
emptyState?: React.ReactNode;
|
|
51
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { cn } from "../cn.js";
|
|
2
|
+
|
|
3
|
+
function Skeleton({
|
|
4
|
+
className,
|
|
5
|
+
...props
|
|
6
|
+
}: React.HTMLAttributes<HTMLDivElement>) {
|
|
7
|
+
return (
|
|
8
|
+
<div
|
|
9
|
+
className={cn("animate-pulse rounded-md bg-muted", className)}
|
|
10
|
+
{...props}
|
|
11
|
+
/>
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { Skeleton };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { useEffect, useRef, useState } from "react";
|
|
5
|
+
|
|
6
|
+
import { cn } from "../cn.js";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
isHorizontallyScrollable,
|
|
10
|
+
resolveTableScrollRegionAttributes,
|
|
11
|
+
} from "./tableScrollRegion.js";
|
|
12
|
+
|
|
13
|
+
interface TableScrollRegionProps {
|
|
14
|
+
label?: string;
|
|
15
|
+
className?: string;
|
|
16
|
+
children: ReactNode;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const TableScrollRegion = ({
|
|
20
|
+
label,
|
|
21
|
+
className,
|
|
22
|
+
children,
|
|
23
|
+
}: TableScrollRegionProps) => {
|
|
24
|
+
const scrollRef = useRef<HTMLDivElement | null>(null);
|
|
25
|
+
const [isScrollable, setIsScrollable] = useState(false);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const node = scrollRef.current;
|
|
29
|
+
if (!node) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const measure = () => {
|
|
34
|
+
setIsScrollable(isHorizontallyScrollable(node));
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
measure();
|
|
38
|
+
|
|
39
|
+
if (typeof ResizeObserver === "undefined") {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const observer = new ResizeObserver(measure);
|
|
44
|
+
observer.observe(node);
|
|
45
|
+
for (const child of Array.from(node.children)) {
|
|
46
|
+
observer.observe(child);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return () => observer.disconnect();
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<div
|
|
54
|
+
ref={scrollRef}
|
|
55
|
+
className={cn(
|
|
56
|
+
"relative w-full overflow-auto",
|
|
57
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset",
|
|
58
|
+
className,
|
|
59
|
+
)}
|
|
60
|
+
data-testid="table-scroll-region"
|
|
61
|
+
data-scrollable={isScrollable ? "true" : "false"}
|
|
62
|
+
{...resolveTableScrollRegionAttributes({ label, isScrollable })}
|
|
63
|
+
>
|
|
64
|
+
{children}
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
TableScrollRegion.displayName = "TableScrollRegion";
|
package/src/ui/table.tsx
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { cn } from "../cn.js";
|
|
4
|
+
|
|
5
|
+
import { TableScrollRegion } from "./table-scroll-region.js";
|
|
6
|
+
|
|
7
|
+
const Table = ({
|
|
8
|
+
ref,
|
|
9
|
+
className,
|
|
10
|
+
scrollAreaLabel,
|
|
11
|
+
...props
|
|
12
|
+
}: React.HTMLAttributes<HTMLTableElement> & {
|
|
13
|
+
ref?: React.Ref<HTMLTableElement>;
|
|
14
|
+
scrollAreaLabel?: string;
|
|
15
|
+
}) => (
|
|
16
|
+
<TableScrollRegion label={scrollAreaLabel}>
|
|
17
|
+
<table
|
|
18
|
+
ref={ref}
|
|
19
|
+
className={cn("w-full caption-bottom text-sm", className)}
|
|
20
|
+
{...props}
|
|
21
|
+
/>
|
|
22
|
+
</TableScrollRegion>
|
|
23
|
+
);
|
|
24
|
+
Table.displayName = "Table";
|
|
25
|
+
|
|
26
|
+
const TableHeader = ({
|
|
27
|
+
ref,
|
|
28
|
+
className,
|
|
29
|
+
...props
|
|
30
|
+
}: React.HTMLAttributes<HTMLTableSectionElement> & {
|
|
31
|
+
ref?: React.Ref<HTMLTableSectionElement>;
|
|
32
|
+
}) => (
|
|
33
|
+
<thead ref={ref} className={cn("[&_tr]:border-b", className)} {...props} />
|
|
34
|
+
);
|
|
35
|
+
TableHeader.displayName = "TableHeader";
|
|
36
|
+
|
|
37
|
+
const TableBody = ({
|
|
38
|
+
ref,
|
|
39
|
+
className,
|
|
40
|
+
...props
|
|
41
|
+
}: React.HTMLAttributes<HTMLTableSectionElement> & {
|
|
42
|
+
ref?: React.Ref<HTMLTableSectionElement>;
|
|
43
|
+
}) => (
|
|
44
|
+
<tbody
|
|
45
|
+
ref={ref}
|
|
46
|
+
className={cn("[&_tr:last-child]:border-0", className)}
|
|
47
|
+
{...props}
|
|
48
|
+
/>
|
|
49
|
+
);
|
|
50
|
+
TableBody.displayName = "TableBody";
|
|
51
|
+
|
|
52
|
+
const TableFooter = ({
|
|
53
|
+
ref,
|
|
54
|
+
className,
|
|
55
|
+
...props
|
|
56
|
+
}: React.HTMLAttributes<HTMLTableSectionElement> & {
|
|
57
|
+
ref?: React.Ref<HTMLTableSectionElement>;
|
|
58
|
+
}) => (
|
|
59
|
+
<tfoot
|
|
60
|
+
ref={ref}
|
|
61
|
+
className={cn(
|
|
62
|
+
"border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",
|
|
63
|
+
className,
|
|
64
|
+
)}
|
|
65
|
+
{...props}
|
|
66
|
+
/>
|
|
67
|
+
);
|
|
68
|
+
TableFooter.displayName = "TableFooter";
|
|
69
|
+
|
|
70
|
+
const TableRow = ({
|
|
71
|
+
ref,
|
|
72
|
+
className,
|
|
73
|
+
...props
|
|
74
|
+
}: React.HTMLAttributes<HTMLTableRowElement> & {
|
|
75
|
+
ref?: React.Ref<HTMLTableRowElement>;
|
|
76
|
+
}) => (
|
|
77
|
+
<tr
|
|
78
|
+
ref={ref}
|
|
79
|
+
className={cn(
|
|
80
|
+
"border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",
|
|
81
|
+
className,
|
|
82
|
+
)}
|
|
83
|
+
{...props}
|
|
84
|
+
/>
|
|
85
|
+
);
|
|
86
|
+
TableRow.displayName = "TableRow";
|
|
87
|
+
|
|
88
|
+
const TableHead = ({
|
|
89
|
+
ref,
|
|
90
|
+
className,
|
|
91
|
+
scope = "col",
|
|
92
|
+
...props
|
|
93
|
+
}: React.ThHTMLAttributes<HTMLTableCellElement> & {
|
|
94
|
+
ref?: React.Ref<HTMLTableCellElement>;
|
|
95
|
+
}) => (
|
|
96
|
+
<th
|
|
97
|
+
ref={ref}
|
|
98
|
+
scope={scope}
|
|
99
|
+
className={cn(
|
|
100
|
+
"h-12 px-4 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0",
|
|
101
|
+
className,
|
|
102
|
+
)}
|
|
103
|
+
{...props}
|
|
104
|
+
/>
|
|
105
|
+
);
|
|
106
|
+
TableHead.displayName = "TableHead";
|
|
107
|
+
|
|
108
|
+
const TableCell = ({
|
|
109
|
+
ref,
|
|
110
|
+
className,
|
|
111
|
+
...props
|
|
112
|
+
}: React.TdHTMLAttributes<HTMLTableCellElement> & {
|
|
113
|
+
ref?: React.Ref<HTMLTableCellElement>;
|
|
114
|
+
}) => (
|
|
115
|
+
<td
|
|
116
|
+
ref={ref}
|
|
117
|
+
className={cn("p-4 align-middle [&:has([role=checkbox])]:pr-0", className)}
|
|
118
|
+
{...props}
|
|
119
|
+
/>
|
|
120
|
+
);
|
|
121
|
+
TableCell.displayName = "TableCell";
|
|
122
|
+
|
|
123
|
+
const TableCaption = ({
|
|
124
|
+
ref,
|
|
125
|
+
className,
|
|
126
|
+
...props
|
|
127
|
+
}: React.HTMLAttributes<HTMLTableCaptionElement> & {
|
|
128
|
+
ref?: React.Ref<HTMLTableCaptionElement>;
|
|
129
|
+
}) => (
|
|
130
|
+
<caption
|
|
131
|
+
ref={ref}
|
|
132
|
+
className={cn("mt-4 text-sm text-muted-foreground", className)}
|
|
133
|
+
{...props}
|
|
134
|
+
/>
|
|
135
|
+
);
|
|
136
|
+
TableCaption.displayName = "TableCaption";
|
|
137
|
+
|
|
138
|
+
export {
|
|
139
|
+
Table,
|
|
140
|
+
TableHeader,
|
|
141
|
+
TableBody,
|
|
142
|
+
TableFooter,
|
|
143
|
+
TableHead,
|
|
144
|
+
TableRow,
|
|
145
|
+
TableCell,
|
|
146
|
+
TableCaption,
|
|
147
|
+
};
|