2025-11-13 18:57:45 +00:00
|
|
|
"use client";
|
2025-10-16 11:18:55 +00:00
|
|
|
|
|
|
|
|
import {
|
2025-11-13 18:57:45 +00:00
|
|
|
Button,
|
|
|
|
|
DropdownMenu,
|
2025-10-16 11:18:55 +00:00
|
|
|
DropdownMenuCheckboxItem,
|
|
|
|
|
DropdownMenuContent,
|
|
|
|
|
DropdownMenuLabel,
|
|
|
|
|
DropdownMenuSeparator,
|
2025-11-13 18:57:45 +00:00
|
|
|
DropdownMenuTrigger,
|
|
|
|
|
} from "@repo/shadcn-ui/components";
|
2025-11-16 19:29:30 +00:00
|
|
|
import type { Column, Table } from "@tanstack/react-table";
|
2025-11-13 18:57:45 +00:00
|
|
|
import { Settings2 } from "lucide-react";
|
|
|
|
|
|
|
|
|
|
import { useTranslation } from "../../locales/i18n.ts";
|
2025-10-16 11:18:55 +00:00
|
|
|
|
2025-11-13 18:57:45 +00:00
|
|
|
export function DataTableViewOptions<TData>({ table }: { table: Table<TData> }) {
|
|
|
|
|
const { t } = useTranslation();
|
2025-10-16 11:18:55 +00:00
|
|
|
return (
|
|
|
|
|
<DropdownMenu>
|
|
|
|
|
<DropdownMenuTrigger asChild>
|
2025-11-13 18:57:45 +00:00
|
|
|
<Button className="ml-auto hidden h-8 lg:flex" size="sm" type="button" variant="outline">
|
2025-10-16 11:18:55 +00:00
|
|
|
<Settings2 />
|
2025-11-13 18:57:45 +00:00
|
|
|
{t("components.datatable_view_options.view_button")}
|
2025-10-16 11:18:55 +00:00
|
|
|
</Button>
|
|
|
|
|
</DropdownMenuTrigger>
|
2025-11-16 19:29:30 +00:00
|
|
|
<DropdownMenuContent align="end">
|
2025-11-13 18:57:45 +00:00
|
|
|
<DropdownMenuLabel>
|
|
|
|
|
{t("components.datatable_view_options.toggle_columns")}
|
|
|
|
|
</DropdownMenuLabel>
|
2025-10-16 11:18:55 +00:00
|
|
|
<DropdownMenuSeparator />
|
|
|
|
|
{table
|
|
|
|
|
.getAllColumns()
|
2025-11-13 18:57:45 +00:00
|
|
|
.filter((column) => typeof column.accessorFn !== "undefined" && column.getCanHide())
|
2025-10-16 11:18:55 +00:00
|
|
|
.map((column) => {
|
|
|
|
|
return (
|
|
|
|
|
<DropdownMenuCheckboxItem
|
|
|
|
|
checked={column.getIsVisible()}
|
2025-11-13 18:57:45 +00:00
|
|
|
key={column.id}
|
2025-10-16 11:18:55 +00:00
|
|
|
onCheckedChange={(value) => column.toggleVisibility(!!value)}
|
|
|
|
|
>
|
2025-11-16 19:29:30 +00:00
|
|
|
{getColumnLabel(column)}
|
2025-10-16 11:18:55 +00:00
|
|
|
</DropdownMenuCheckboxItem>
|
2025-11-13 18:57:45 +00:00
|
|
|
);
|
2025-10-16 11:18:55 +00:00
|
|
|
})}
|
|
|
|
|
</DropdownMenuContent>
|
|
|
|
|
</DropdownMenu>
|
2025-11-13 18:57:45 +00:00
|
|
|
);
|
2025-10-16 11:18:55 +00:00
|
|
|
}
|
2025-11-16 19:29:30 +00:00
|
|
|
|
|
|
|
|
const getColumnLabel = <TData, TValue>(column: Column<TData, TValue>): string => {
|
|
|
|
|
let label = String(column.id);
|
|
|
|
|
const h = column.columnDef.header;
|
|
|
|
|
|
|
|
|
|
// header como string literal
|
|
|
|
|
if (typeof h === "string") label = h;
|
|
|
|
|
|
|
|
|
|
// header función → intentar obtener title si lo usas en tus componentes
|
|
|
|
|
if (typeof h === "function") {
|
|
|
|
|
// tu DataTableColumnHeader recibe `title` como prop → está en column.columnDef.meta
|
|
|
|
|
const meta = column.columnDef.meta as { title?: string } | undefined;
|
|
|
|
|
label = meta?.title ?? column.id;
|
|
|
|
|
}
|
|
|
|
|
return label;
|
|
|
|
|
};
|