Presupuestador_web/client/src/components/DataTable/DataTableRowActions.tsx

77 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-06-11 16:48:09 +00:00
"use client";
2024-07-11 11:08:53 +00:00
import { cn } from "@/lib/utils";
2024-06-11 16:48:09 +00:00
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuTrigger,
} from "@/ui";
2024-07-11 11:08:53 +00:00
import { CellContext } from "@tanstack/react-table";
2024-06-29 19:39:25 +00:00
import { t } from "i18next";
2024-08-25 12:14:21 +00:00
import { MoreVerticalIcon } from "lucide-react";
import { ReactElement } from "react";
2024-06-11 16:48:09 +00:00
2024-07-11 11:08:53 +00:00
export type DataTablaRowActionFunction<TData> = (
props: CellContext<TData, unknown>
) => DataTableRowActionDefinition<TData>[];
2024-06-11 16:48:09 +00:00
2024-07-11 11:08:53 +00:00
export type DataTableRowActionDefinition<TData> = {
2024-06-11 16:48:09 +00:00
label: string | "-";
2024-08-25 12:14:21 +00:00
icon?: ReactElement<any, any>;
2024-06-11 16:48:09 +00:00
shortcut?: string;
2024-07-11 11:08:53 +00:00
onClick?: (props: CellContext<TData, unknown>, e: React.BaseSyntheticEvent) => void;
2024-06-11 16:48:09 +00:00
};
2024-07-11 11:08:53 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type DataTableRowActionsProps<TData, TValue = unknown> = {
className?: string;
actions?: DataTablaRowActionFunction<TData>;
rowContext: CellContext<TData, unknown>;
2024-06-11 16:48:09 +00:00
};
2024-07-11 11:08:53 +00:00
export function DataTableRowActions<TData = any, TValue = unknown>({
2024-06-11 16:48:09 +00:00
actions,
2024-07-11 11:08:53 +00:00
rowContext,
className,
2024-06-11 16:48:09 +00:00
}: DataTableRowActionsProps<TData, TValue>) {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
2024-06-29 19:39:25 +00:00
aria-haspopup='true'
size='icon'
variant='link'
2024-07-11 11:08:53 +00:00
className={cn("w-4 h-4 mt-2 text-ring hover:text-muted-foreground", className)}
2024-06-11 16:48:09 +00:00
>
2024-08-25 12:14:21 +00:00
<MoreVerticalIcon className='w-4 h-4' />
2024-06-29 19:39:25 +00:00
<span className='sr-only'>{t("common.open_menu")}</span>
2024-06-11 16:48:09 +00:00
</Button>
</DropdownMenuTrigger>
2024-06-29 19:39:25 +00:00
<DropdownMenuContent align='end'>
<DropdownMenuLabel>{t("common.actions")} </DropdownMenuLabel>
2024-06-11 16:48:09 +00:00
{actions &&
2024-07-11 11:08:53 +00:00
actions(rowContext).map((action, index) =>
2024-06-11 16:48:09 +00:00
action.label === "-" ? (
<DropdownMenuSeparator key={index} />
) : (
<DropdownMenuItem
key={index}
2024-07-11 11:08:53 +00:00
onClick={(event) => (action.onClick ? action.onClick(rowContext, event) : null)}
2024-06-11 16:48:09 +00:00
>
2024-08-25 12:14:21 +00:00
{action.icon && <>{action.icon}</>}
2024-06-11 16:48:09 +00:00
{action.label}
2024-08-25 12:14:21 +00:00
{action.shortcut && <DropdownMenuShortcut>{action.shortcut}</DropdownMenuShortcut>}
2024-06-11 16:48:09 +00:00
</DropdownMenuItem>
2024-06-29 19:39:25 +00:00
)
2024-06-11 16:48:09 +00:00
)}
</DropdownMenuContent>
</DropdownMenu>
);
}