-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathdata-grid-select-column.tsx
More file actions
254 lines (234 loc) · 5.78 KB
/
data-grid-select-column.tsx
File metadata and controls
254 lines (234 loc) · 5.78 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"use client";
import type {
CellContext,
ColumnDef,
HeaderContext,
} from "@tanstack/react-table";
import * as React from "react";
import { Checkbox } from "@/components/ui/checkbox";
import { cn } from "@/lib/utils";
type HitboxSize = "default" | "sm" | "lg";
interface DataGridSelectHitboxProps {
htmlFor: string;
children: React.ReactNode;
size?: HitboxSize;
debug?: boolean;
}
function DataGridSelectHitbox({
htmlFor,
children,
size,
debug,
}: DataGridSelectHitboxProps) {
return (
<div
className={cn(
"group relative -my-1.5 h-[calc(100%+0.75rem)] py-1.5",
size === "default" && "-ms-3 -me-2 ps-3 pe-2",
size === "sm" && "-ms-3 -me-1.5 ps-3 pe-1.5",
size === "lg" && "-mx-3 px-3",
)}
>
{children}
<label
htmlFor={htmlFor}
className={cn(
"absolute inset-0 cursor-pointer",
debug && "border border-red-500 border-dashed bg-red-500/20",
)}
/>
</div>
);
}
interface DataGridSelectCheckboxProps
extends Omit<React.ComponentProps<typeof Checkbox>, "id"> {
rowNumber?: number;
hitboxSize?: HitboxSize;
debug?: boolean;
}
function DataGridSelectCheckbox({
rowNumber,
hitboxSize,
debug,
checked,
className,
...props
}: DataGridSelectCheckboxProps) {
const id = React.useId();
if (rowNumber !== undefined) {
return (
<DataGridSelectHitbox htmlFor={id} size={hitboxSize} debug={debug}>
<div
aria-hidden="true"
className={cn(
"pointer-events-none absolute start-3 top-1.5 flex size-4 items-center justify-center text-muted-foreground text-xs tabular-nums transition-opacity group-hover:opacity-0",
checked && "opacity-0",
)}
>
{rowNumber}
</div>
<Checkbox
id={id}
className={cn(
"relative transition-[shadow,border,opacity] hover:border-primary/40",
"opacity-0 group-hover:opacity-100 data-[state=checked]:opacity-100",
className,
)}
checked={checked}
{...props}
/>
</DataGridSelectHitbox>
);
}
return (
<DataGridSelectHitbox htmlFor={id} size={hitboxSize} debug={debug}>
<Checkbox
id={id}
className={cn(
"relative transition-[shadow,border] hover:border-primary/40",
className,
)}
checked={checked}
{...props}
/>
</DataGridSelectHitbox>
);
}
interface DataGridSelectHeaderProps<TData>
extends Pick<HeaderContext<TData, unknown>, "table"> {
hitboxSize?: HitboxSize;
readOnly?: boolean;
debug?: boolean;
}
function DataGridSelectHeader<TData>({
table,
hitboxSize,
readOnly,
debug,
}: DataGridSelectHeaderProps<TData>) {
const onCheckedChange = React.useCallback(
(value: boolean) => table.toggleAllPageRowsSelected(value),
[table],
);
if (readOnly) {
return (
<div className="mt-1 flex items-center ps-1 text-muted-foreground text-sm">
#
</div>
);
}
return (
<DataGridSelectCheckbox
aria-label="Select all"
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && "indeterminate")
}
onCheckedChange={onCheckedChange}
hitboxSize={hitboxSize}
debug={debug}
/>
);
}
interface DataGridSelectCellProps<TData>
extends Pick<CellContext<TData, unknown>, "row" | "table"> {
hitboxSize?: HitboxSize;
enableRowMarkers?: boolean;
readOnly?: boolean;
debug?: boolean;
}
function DataGridSelectCell<TData>({
row,
table,
hitboxSize,
enableRowMarkers,
readOnly,
debug,
}: DataGridSelectCellProps<TData>) {
const meta = table.options.meta;
const rowNumber = enableRowMarkers
? (meta?.getVisualRowIndex?.(row.id) ?? row.index + 1)
: undefined;
const onCheckedChange = React.useCallback(
(value: boolean) => {
if (meta?.onRowSelect) {
meta.onRowSelect(row.id, value, false);
} else {
row.toggleSelected(value);
}
},
[meta, row],
);
const onClick = React.useCallback(
(event: React.MouseEvent<HTMLButtonElement>) => {
if (event.shiftKey) {
event.preventDefault();
meta?.onRowSelect?.(row.id, !row.getIsSelected(), true);
}
},
[meta, row],
);
if (readOnly) {
return (
<div className="flex items-center ps-1 text-muted-foreground text-xs tabular-nums">
{rowNumber ?? row.index + 1}
</div>
);
}
return (
<DataGridSelectCheckbox
aria-label={rowNumber ? `Select row ${rowNumber}` : "Select row"}
checked={row.getIsSelected()}
onCheckedChange={onCheckedChange}
onClick={onClick}
rowNumber={rowNumber}
hitboxSize={hitboxSize}
debug={debug}
/>
);
}
interface GetDataGridSelectColumnOptions<TData>
extends Omit<Partial<ColumnDef<TData>>, "id" | "header" | "cell"> {
enableRowMarkers?: boolean;
readOnly?: boolean;
hitboxSize?: HitboxSize;
debug?: boolean;
}
export function getDataGridSelectColumn<TData>({
size = 40,
hitboxSize = "default",
enableHiding = false,
enableResizing = false,
enableSorting = false,
enableRowMarkers = false,
readOnly = false,
debug = false,
...props
}: GetDataGridSelectColumnOptions<TData> = {}): ColumnDef<TData> {
return {
id: "select",
header: ({ table }) => (
<DataGridSelectHeader
table={table}
hitboxSize={hitboxSize}
readOnly={readOnly}
debug={debug}
/>
),
cell: ({ row, table }) => (
<DataGridSelectCell
row={row}
table={table}
enableRowMarkers={enableRowMarkers}
readOnly={readOnly}
hitboxSize={hitboxSize}
debug={debug}
/>
),
size,
enableHiding,
enableResizing,
enableSorting,
...props,
};
}