|
1
|
import { CommandBar, Table, clx } from "@medusajs/ui" |
|
2
|
import { |
|
3
|
ColumnDef, |
|
4
|
Table as ReactTable, |
|
5
|
Row, |
|
6
|
flexRender, |
|
7
|
} from "@tanstack/react-table" |
|
8
|
import { |
|
9
|
ComponentPropsWithoutRef, |
|
10
|
Fragment, |
|
11
|
UIEvent, |
|
12
|
useEffect, |
|
13
|
useRef, |
|
14
|
useState, |
|
15
|
} from "react" |
|
16
|
import { useTranslation } from "react-i18next" |
|
17
|
import { Link } from "react-router-dom" |
|
18
|
import { NoResults } from "../../../common/empty-table-content" |
|
19
|
|
|
20
|
type BulkCommand = { |
|
21
|
label: string |
|
22
|
shortcut: string |
|
23
|
action: (selection: Record<string, boolean>) => Promise<void> |
|
24
|
} |
|
25
|
|
|
26
|
export interface DataTableRootProps<TData> { |
|
27
|
/** |
|
28
|
* The table instance to render |
|
29
|
*/ |
|
30
|
table: ReactTable<TData> |
|
31
|
/** |
|
32
|
* The columns to render |
|
33
|
*/ |
|
34
|
columns: ColumnDef<TData, any>[] |
|
35
|
/** |
|
36
|
* Function to generate a link to navigate to when clicking on a row |
|
37
|
*/ |
|
38
|
navigateTo?: (row: Row<TData>) => string |
|
39
|
/** |
|
40
|
* Bulk actions to render |
|
41
|
*/ |
|
42
|
commands?: BulkCommand[] |
|
43
|
/** |
|
44
|
* The total number of items in the table |
|
45
|
*/ |
|
46
|
count?: number |
|
47
|
/** |
|
48
|
* Whether to display pagination controls |
|
49
|
*/ |
|
50
|
pagination?: boolean |
|
51
|
/** |
|
52
|
* Whether the table is empty due to no results from the active query |
|
53
|
*/ |
|
54
|
noResults?: boolean |
|
55
|
/** |
|
56
|
* Whether to display the tables header |
|
57
|
*/ |
|
58
|
noHeader?: boolean |
|
59
|
/** |
|
60
|
* The layout of the table |
|
61
|
*/ |
|
62
|
layout?: "fill" | "fit" |
|
63
|
} |
|
64
|
|
|
65
|
/** |
|
66
|
* TODO |
|
67
|
* |
|
68
|
* Add a sticky header to the table that shows the column name when scrolling through the table vertically. |
|
69
|
* |
|
70
|
* This is a bit tricky as we can't support horizontal scrolling and sticky headers at the same time, natively |
|
71
|
* with CSS. We need to implement a custom solution for this. One solution is to render a duplicate table header |
|
72
|
* using a DIV that, but it will require rerendeing the duplicate header every time the window is resized, to keep |
|
73
|
* the columns aligned. |
|
74
|
*/ |
|
75
|
|
|
76
|
/** |
|
77
|
* Table component for rendering a table with pagination, filtering and ordering. |
|
78
|
*/ |
|
79
|
export const DataTableRoot = <TData,>({ |
|
80
|
table, |
|
81
|
columns, |
|
82
|
pagination, |
|
83
|
navigateTo, |
|
84
|
commands, |
|
85
|
count = 0, |
|
86
|
noResults = false, |
|
87
|
noHeader = false, |
|
88
|
layout = "fit", |
|
89
|
}: DataTableRootProps<TData>) => { |
|
90
|
const { t } = useTranslation() |
|
91
|
const [showStickyBorder, setShowStickyBorder] = useState(false) |
|
92
|
|
|
93
|
const scrollableRef = useRef<HTMLDivElement>(null) |
|
94
|
|
|
95
|
const hasSelect = columns.find((c) => c.id === "select") |
|
96
|
const hasActions = columns.find((c) => c.id === "actions") |
|
97
|
const hasCommandBar = commands && commands.length > 0 |
|
98
|
|
|
99
|
const rowSelection = table.getState().rowSelection |
|
100
|
const { pageIndex, pageSize } = table.getState().pagination |
|
101
|
|
|
102
|
const colCount = columns.length - (hasSelect ? 1 : 0) - (hasActions ? 1 : 0) |
|
103
|
const colWidth = 100 / colCount |
|
104
|
|
|
105
|
const handleHorizontalScroll = (e: UIEvent<HTMLDivElement>) => { |
|
106
|
const scrollLeft = e.currentTarget.scrollLeft |
|
107
|
|
|
108
|
if (scrollLeft > 0) { |
|
109
|
setShowStickyBorder(true) |
|
110
|
} else { |
|
111
|
setShowStickyBorder(false) |
|
112
|
} |
|
113
|
} |
|
114
|
|
|
115
|
const handleAction = async (action: BulkCommand["action"]) => { |
|
116
|
await action(rowSelection).then(() => { |
|
117
|
table.resetRowSelection() |
|
118
|
}) |
|
119
|
} |
|
120
|
|
|
121
|
useEffect(() => { |
|
122
|
scrollableRef.current?.scroll({ top: 0, left: 0 }) |
|
123
|
}, [pageIndex]) |
|
124
|
|
|
125
|
return ( |
|
126
|
<div |
|
127
|
className={clx("flex w-full flex-col overflow-hidden", { |
|
128
|
"flex flex-1 flex-col": layout === "fill", |
|
129
|
})} |
|
130
|
> |
|
131
|
<div |
|
132
|
ref={scrollableRef} |
|
133
|
onScroll={handleHorizontalScroll} |
|
134
|
className={clx("w-full", { |
|
135
|
"min-h-0 flex-grow overflow-auto": layout === "fill", |
|
136
|
"overflow-x-auto": layout === "fit", |
|
137
|
})} |
|
138
|
> |
|
139
|
{!noResults ? ( |
|
140
|
<Table className="relative w-full"> |
|
141
|
{!noHeader && ( |
|
142
|
<Table.Header className="border-t-0"> |
|
143
|
{table.getHeaderGroups().map((headerGroup) => { |
|
144
|
return ( |
|
145
|
<Table.Row |
|
146
|
key={headerGroup.id} |
|
147
|
className={clx({ |
|
148
|
"relative border-b-0 [&_th:last-of-type]:w-[1%] [&_th:last-of-type]:whitespace-nowrap": |
|
149
|
hasActions, |
|
150
|
"[&_th:first-of-type]:w-[1%] [&_th:first-of-type]:whitespace-nowrap": |
|
151
|
hasSelect, |
|
152
|
})} |
|
153
|
> |
|
154
|
{headerGroup.headers.map((header, index) => { |
|
155
|
const isActionHeader = header.id === "actions" |
|
156
|
const isSelectHeader = header.id === "select" |
|
157
|
const isSpecialHeader = isActionHeader || isSelectHeader |
|
158
|
|
|
159
|
const firstHeader = headerGroup.headers.findIndex( |
|
160
|
(h) => h.id !== "select" |
|
161
|
) |
|
162
|
const isFirstHeader = |
|
163
|
firstHeader !== -1 |
|
164
|
? header.id === headerGroup.headers[firstHeader].id |
|
165
|
: index === 0 |
|
166
|
|
|
167
|
const isStickyHeader = isSelectHeader || isFirstHeader |
|
168
|
|
|
169
|
return ( |
|
170
|
<Table.HeaderCell |
|
171
|
data-table-header-id={header.id} |
|
172
|
key={header.id} |
|
173
|
style={{ |
|
174
|
width: !isSpecialHeader |
|
175
|
? `${colWidth}%` |
|
176
|
: undefined, |
|
177
|
}} |
|
178
|
className={clx({ |
|
179
|
"bg-ui-bg-subtle sticky start-0 after:absolute after:inset-y-0 after:right-0 after:h-full after:w-px after:bg-transparent after:content-['']": |
|
180
|
isStickyHeader, |
|
181
|
"start-[68px]": |
|
182
|
isStickyHeader && hasSelect && !isSelectHeader, |
|
183
|
"after:bg-ui-border-base": |
|
184
|
showStickyBorder && |
|
185
|
isStickyHeader && |
|
186
|
!isSpecialHeader, |
|
187
|
})} |
|
188
|
> |
|
189
|
{flexRender( |
|
190
|
header.column.columnDef.header, |
|
191
|
header.getContext() |
|
192
|
)} |
|
193
|
</Table.HeaderCell> |
|
194
|
) |
|
195
|
})} |
|
196
|
</Table.Row> |
|
197
|
) |
|
198
|
})} |
|
199
|
</Table.Header> |
|
200
|
)} |
|
201
|
<Table.Body className="border-b-0"> |
|
202
|
{table.getRowModel().rows.map((row) => { |
|
203
|
const to = navigateTo ? navigateTo(row) : undefined |
|
204
|
const isRowDisabled = hasSelect && !row.getCanSelect() |
|
205
|
|
|
206
|
const isOdd = row.depth % 2 !== 0 |
|
207
|
|
|
208
|
const cells = row.getVisibleCells() |
|
209
|
|
|
210
|
return ( |
|
211
|
<Table.Row |
|
212
|
key={row.id} |
|
213
|
data-selected={row.getIsSelected()} |
|
214
|
className={clx( |
|
215
|
"transition-fg group/row group relative [&_td:last-of-type]:w-[1%] [&_td:last-of-type]:whitespace-nowrap", |
|
216
|
"has-[[data-row-link]:focus-visible]:bg-ui-bg-base-hover", |
|
217
|
{ |
|
218
|
"bg-ui-bg-subtle hover:bg-ui-bg-subtle-hover": isOdd, |
|
219
|
"cursor-pointer": !!to, |
|
220
|
"bg-ui-bg-highlight hover:bg-ui-bg-highlight-hover": |
|
221
|
row.getIsSelected(), |
|
222
|
"!bg-ui-bg-disabled !hover:bg-ui-bg-disabled": |
|
223
|
isRowDisabled, |
|
224
|
} |
|
225
|
)} |
|
226
|
> |
|
227
|
{cells.map((cell, index) => { |
|
228
|
const visibleCells = row.getVisibleCells() |
|
229
|
const isSelectCell = cell.column.id === "select" |
|
230
|
|
|
231
|
const firstCell = visibleCells.findIndex( |
|
232
|
(h) => h.column.id !== "select" |
|
233
|
) |
|
234
|
const isFirstCell = |
|
235
|
firstCell !== -1 |
|
236
|
? cell.column.id === visibleCells[firstCell].column.id |
|
237
|
: index === 0 |
|
238
|
|
|
239
|
const isStickyCell = isSelectCell || isFirstCell |
|
240
|
|
|
241
|
/** |
|
242
|
* If the table has nested rows, we need to offset the cell padding |
|
243
|
* to indicate the depth of the row. |
|
244
|
*/ |
|
245
|
const depthOffset = |
|
246
|
row.depth > 0 && isFirstCell |
|
247
|
? row.depth * 14 + 24 |
|
248
|
: undefined |
|
249
|
|
|
250
|
const hasLeftOffset = |
|
251
|
isStickyCell && hasSelect && !isSelectCell |
|
252
|
|
|
253
|
const Inner = flexRender( |
|
254
|
cell.column.columnDef.cell, |
|
255
|
cell.getContext() |
|
256
|
) |
|
257
|
|
|
258
|
const isTabableLink = isFirstCell && !!to |
|
259
|
const shouldRenderAsLink = !!to && !isSelectCell |
|
260
|
|
|
261
|
return ( |
|
262
|
<Table.Cell |
|
263
|
key={cell.id} |
|
264
|
className={clx({ |
|
265
|
"!pe-0 !ps-0": shouldRenderAsLink, |
|
266
|
"bg-ui-bg-base group-data-[selected=true]/row:bg-ui-bg-highlight group-data-[selected=true]/row:group-hover/row:bg-ui-bg-highlight-hover group-hover/row:bg-ui-bg-base-hover transition-fg group-has-[[data-row-link]:focus-visible]:bg-ui-bg-base-hover sticky start-0 after:absolute after:inset-y-0 after:right-0 after:h-full after:w-px after:bg-transparent after:content-['']": |
|
267
|
isStickyCell, |
|
268
|
"bg-ui-bg-subtle group-hover/row:bg-ui-bg-subtle-hover": |
|
269
|
isOdd && isStickyCell, |
|
270
|
"start-[68px]": hasLeftOffset, |
|
271
|
"after:bg-ui-border-base": |
|
272
|
showStickyBorder && isStickyCell && !isSelectCell, |
|
273
|
"!bg-ui-bg-disabled !hover:bg-ui-bg-disabled": |
|
274
|
isRowDisabled, |
|
275
|
})} |
|
276
|
style={{ |
|
277
|
paddingLeft: depthOffset |
|
278
|
? `${depthOffset}px` |
|
279
|
: undefined, |
|
280
|
}} |
|
281
|
> |
|
282
|
{shouldRenderAsLink ? ( |
|
283
|
<Link |
|
284
|
to={to} |
|
285
|
className="size-full outline-none" |
|
286
|
data-row-link |
|
287
|
tabIndex={isTabableLink ? 0 : -1} |
|
288
|
> |
|
289
|
<div |
|
290
|
className={clx( |
|
291
|
"flex size-full items-center pe-6", |
|
292
|
{ |
|
293
|
"ps-6": isTabableLink && !hasLeftOffset, |
|
294
|
} |
|
295
|
)} |
|
296
|
> |
|
297
|
{Inner} |
|
298
|
</div> |
|
299
|
</Link> |
|
300
|
) : ( |
|
301
|
Inner |
|
302
|
)} |
|
303
|
</Table.Cell> |
|
304
|
) |
|
305
|
})} |
|
306
|
</Table.Row> |
|
307
|
) |
|
308
|
})} |
|
309
|
</Table.Body> |
|
310
|
</Table> |
|
311
|
) : ( |
|
312
|
<div className={clx({ "border-b": layout === "fit" })}> |
|
313
|
<NoResults /> |
|
314
|
</div> |
|
315
|
)} |
|
316
|
</div> |
|
317
|
{pagination && ( |
|
318
|
<div className={clx({ "border-t": layout === "fill" })}> |
|
319
|
<Pagination |
|
320
|
canNextPage={table.getCanNextPage()} |
|
321
|
canPreviousPage={table.getCanPreviousPage()} |
|
322
|
nextPage={table.nextPage} |
|
323
|
previousPage={table.previousPage} |
|
324
|
count={count} |
|
325
|
pageIndex={pageIndex} |
|
326
|
pageCount={table.getPageCount()} |
|
327
|
pageSize={pageSize} |
|
328
|
/> |
|
329
|
</div> |
|
330
|
)} |
|
331
|
{hasCommandBar && ( |
|
332
|
<CommandBar open={!!Object.keys(rowSelection).length}> |
|
333
|
<CommandBar.Bar> |
|
334
|
<CommandBar.Value> |
|
335
|
{t("general.countSelected", { |
|
336
|
count: Object.keys(rowSelection).length, |
|
337
|
})} |
|
338
|
</CommandBar.Value> |
|
339
|
<CommandBar.Seperator /> |
|
340
|
{commands?.map((command, index) => { |
|
341
|
return ( |
|
342
|
<Fragment key={index}> |
|
343
|
<CommandBar.Command |
|
344
|
label={command.label} |
|
345
|
shortcut={command.shortcut} |
|
346
|
action={() => handleAction(command.action)} |
|
347
|
/> |
|
348
|
{index < commands.length - 1 && <CommandBar.Seperator />} |
|
349
|
</Fragment> |
|
350
|
) |
|
351
|
})} |
|
352
|
</CommandBar.Bar> |
|
353
|
</CommandBar> |
|
354
|
)} |
|
355
|
</div> |
|
356
|
) |
|
357
|
} |
|
358
|
|
|
359
|
type PaginationProps = Omit< |
|
360
|
ComponentPropsWithoutRef<typeof Table.Pagination>, |
|
361
|
"translations" |
|
362
|
> |
|
363
|
|
|
364
|
const Pagination = (props: PaginationProps) => { |
|
365
|
const { t } = useTranslation() |
|
366
|
|
|
367
|
const translations = { |
|
368
|
of: t("general.of"), |
|
369
|
results: t("general.results"), |
|
370
|
pages: t("general.pages"), |
|
371
|
prev: t("general.prev"), |
|
372
|
next: t("general.next"), |
|
373
|
} |
|
374
|
|
|
375
|
return ( |
|
376
|
<Table.Pagination |
|
377
|
className="flex-shrink-0" |
|
378
|
{...props} |
|
379
|
translations={translations} |
|
380
|
/> |
|
381
|
) |
|
382
|
} |