|
1
|
"use client" |
|
2
|
|
|
3
|
import * as React from "react" |
|
4
|
|
|
5
|
import type { DataTableActionColumnDefMeta } from "@/blocks/data-table/types" |
|
6
|
import { DropdownMenu } from "@/components/dropdown-menu" |
|
7
|
import { IconButton } from "@/components/icon-button" |
|
8
|
import { EllipsisHorizontal } from "@medusajs/icons" |
|
9
|
import { CellContext } from "@tanstack/react-table" |
|
10
|
|
|
11
|
interface DataTableActionCellProps<TData> { |
|
12
|
ctx: CellContext<TData, unknown> |
|
13
|
} |
|
14
|
|
|
15
|
const DataTableActionCell = <TData,>({ |
|
16
|
ctx, |
|
17
|
}: DataTableActionCellProps<TData>) => { |
|
18
|
const meta = ctx.column.columnDef.meta as |
|
19
|
| DataTableActionColumnDefMeta<TData> |
|
20
|
| undefined |
|
21
|
const actions = meta?.___actions |
|
22
|
|
|
23
|
if (!actions) { |
|
24
|
return null |
|
25
|
} |
|
26
|
|
|
27
|
const resolvedActions = typeof actions === "function" ? actions(ctx) : actions |
|
28
|
|
|
29
|
if (!Array.isArray(resolvedActions)) { |
|
30
|
return null |
|
31
|
} |
|
32
|
|
|
33
|
return ( |
|
34
|
<DropdownMenu> |
|
35
|
<DropdownMenu.Trigger asChild className="ml-1"> |
|
36
|
<IconButton size="small" variant="transparent"> |
|
37
|
<EllipsisHorizontal /> |
|
38
|
</IconButton> |
|
39
|
</DropdownMenu.Trigger> |
|
40
|
<DropdownMenu.Content side="bottom"> |
|
41
|
{resolvedActions.map((actionOrGroup, idx) => { |
|
42
|
const isArray = Array.isArray(actionOrGroup) |
|
43
|
const isLast = idx === resolvedActions.length - 1 |
|
44
|
|
|
45
|
return isArray ? ( |
|
46
|
<React.Fragment key={idx}> |
|
47
|
{actionOrGroup.map((action) => ( |
|
48
|
<DropdownMenu.Item |
|
49
|
key={action.label} |
|
50
|
onClick={(e) => { |
|
51
|
e.stopPropagation() |
|
52
|
action.onClick(ctx) |
|
53
|
}} |
|
54
|
className="[&>svg]:text-ui-fg-subtle flex items-center gap-2" |
|
55
|
> |
|
56
|
{action.icon} |
|
57
|
{action.label} |
|
58
|
</DropdownMenu.Item> |
|
59
|
))} |
|
60
|
{!isLast && <DropdownMenu.Separator />} |
|
61
|
</React.Fragment> |
|
62
|
) : ( |
|
63
|
<DropdownMenu.Item |
|
64
|
key={actionOrGroup.label} |
|
65
|
onClick={(e) => { |
|
66
|
e.stopPropagation() |
|
67
|
actionOrGroup.onClick(ctx) |
|
68
|
}} |
|
69
|
className="[&>svg]:text-ui-fg-subtle flex items-center gap-2" |
|
70
|
> |
|
71
|
{actionOrGroup.icon} |
|
72
|
{actionOrGroup.label} |
|
73
|
</DropdownMenu.Item> |
|
74
|
) |
|
75
|
})} |
|
76
|
</DropdownMenu.Content> |
|
77
|
</DropdownMenu> |
|
78
|
) |
|
79
|
} |
|
80
|
DataTableActionCell.displayName = "DataTable.ActionCell" |
|
81
|
|
|
82
|
export { DataTableActionCell } |
|
83
|
export type { DataTableActionCellProps } |
|
84
|
|