Files
medusa-store/packages/design-system/ui/src/blocks/data-table/components/data-table-action-cell.tsx
Kasper Fabricius Kristensen 147c0e5a35 feat(ui,dashboard): Add DataTable block (#10024)
**What**
- Adds opinionated DataTable block to `@medusajs/ui` 
- Adds new DataTable to `@medusajs/dashboard` that uses the above mentioned block as the primitive.

The PR also replaces the table on /customer-groups and the variants table on /products/:id with the new DataTable, to provide an example of it's usage. The previous DataTable component has been renamed to `_DataTable` and has been deprecated.

**Note**
This PR has a lot of LOC. 5,346 of these changes are the fr.json file, which wasn't formatted correctly before. When adding the new translations needed for this PR the file was formatted which caused each line to change to have the proper indentation.

Resolves CMRC-333
2025-01-20 13:26:12 +00:00

84 lines
2.4 KiB
TypeScript

"use client"
import * as React from "react"
import { EllipsisHorizontal } from "@medusajs/icons"
import { CellContext } from "@tanstack/react-table"
import { DropdownMenu } from "../../../components/dropdown-menu"
import { IconButton } from "../../../components/icon-button"
import { DataTableActionColumnDefMeta } from "../types"
interface DataTableActionCellProps<TData> {
ctx: CellContext<TData, unknown>
}
const DataTableActionCell = <TData,>({
ctx,
}: DataTableActionCellProps<TData>) => {
const meta = ctx.column.columnDef.meta as
| DataTableActionColumnDefMeta<TData>
| undefined
const actions = meta?.___actions
if (!actions) {
return null
}
const resolvedActions = typeof actions === "function" ? actions(ctx) : actions
if (!Array.isArray(resolvedActions)) {
return null
}
return (
<DropdownMenu>
<DropdownMenu.Trigger asChild className="ml-1">
<IconButton size="small" variant="transparent">
<EllipsisHorizontal />
</IconButton>
</DropdownMenu.Trigger>
<DropdownMenu.Content side="bottom">
{resolvedActions.map((actionOrGroup, idx) => {
const isArray = Array.isArray(actionOrGroup)
const isLast = idx === resolvedActions.length - 1
return isArray ? (
<React.Fragment key={idx}>
{actionOrGroup.map((action) => (
<DropdownMenu.Item
key={action.label}
onClick={(e) => {
e.stopPropagation()
action.onClick(ctx)
}}
className="[&>svg]:text-ui-fg-subtle flex items-center gap-2"
>
{action.icon}
{action.label}
</DropdownMenu.Item>
))}
{!isLast && <DropdownMenu.Separator />}
</React.Fragment>
) : (
<DropdownMenu.Item
key={actionOrGroup.label}
onClick={(e) => {
e.stopPropagation()
actionOrGroup.onClick(ctx)
}}
className="[&>svg]:text-ui-fg-subtle flex items-center gap-2"
>
{actionOrGroup.icon}
{actionOrGroup.label}
</DropdownMenu.Item>
)
})}
</DropdownMenu.Content>
</DropdownMenu>
)
}
export { DataTableActionCell }
export type { DataTableActionCellProps }