2025-10-16 11:18:55 +00:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
ColumnDef,
|
|
|
|
|
ColumnFiltersState,
|
|
|
|
|
ColumnSizingState,
|
2025-10-16 17:59:13 +00:00
|
|
|
Row,
|
2025-10-16 11:18:55 +00:00
|
|
|
SortingState,
|
2025-10-16 17:59:13 +00:00
|
|
|
Table,
|
|
|
|
|
TableMeta,
|
2025-10-16 11:18:55 +00:00
|
|
|
VisibilityState,
|
|
|
|
|
flexRender,
|
|
|
|
|
getCoreRowModel,
|
|
|
|
|
getFacetedRowModel,
|
|
|
|
|
getFacetedUniqueValues,
|
|
|
|
|
getFilteredRowModel,
|
|
|
|
|
getPaginationRowModel,
|
|
|
|
|
getSortedRowModel,
|
|
|
|
|
useReactTable
|
|
|
|
|
} from "@tanstack/react-table"
|
|
|
|
|
import * as React from "react"
|
|
|
|
|
|
|
|
|
|
import {
|
2025-10-16 17:59:13 +00:00
|
|
|
Button,
|
2025-10-16 11:18:55 +00:00
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
2025-10-16 17:59:13 +00:00
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
TableBody,
|
2025-10-16 11:18:55 +00:00
|
|
|
TableCell,
|
2025-10-16 17:59:13 +00:00
|
|
|
Table as TableComp,
|
|
|
|
|
TableFooter,
|
2025-10-16 11:18:55 +00:00
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
2025-10-16 17:59:13 +00:00
|
|
|
TableRow
|
2025-10-16 11:18:55 +00:00
|
|
|
} from '@repo/shadcn-ui/components'
|
|
|
|
|
import { DataTablePagination } from './data-table-pagination.tsx'
|
|
|
|
|
import { DataTableToolbar } from "./data-table-toolbar.tsx"
|
|
|
|
|
|
|
|
|
|
import { useTranslation } from "../../locales/i18n.ts"
|
|
|
|
|
|
2025-10-16 17:59:13 +00:00
|
|
|
export type DataTableOps<TData> = {
|
|
|
|
|
onAdd?: (table: Table<TData>) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type DataTableRowOps<TData> = {
|
|
|
|
|
duplicate?(index: number, table: Table<TData>): void;
|
|
|
|
|
remove?(index: number, table: Table<TData>): void;
|
|
|
|
|
move?(from: number, to: number, table: Table<TData>): void;
|
|
|
|
|
canMoveUp?(index: number, table: Table<TData>): boolean;
|
|
|
|
|
canMoveDown?(index: number, lastIndex: number, table: Table<TData>): boolean;
|
|
|
|
|
};
|
2025-10-16 11:18:55 +00:00
|
|
|
|
2025-10-16 17:59:13 +00:00
|
|
|
export type DataTableBulkRowOps<TData> = {
|
|
|
|
|
duplicateSelected?: (indexes: number[], table: Table<TData>) => void;
|
|
|
|
|
removeSelected?: (indexes: number[], table: Table<TData>) => void;
|
|
|
|
|
moveSelectedUp?: (indexes: number[], table: Table<TData>) => void;
|
|
|
|
|
moveSelectedDown?: (indexes: number[], table: Table<TData>) => void;
|
2025-10-16 11:18:55 +00:00
|
|
|
};
|
|
|
|
|
|
2025-10-16 17:59:13 +00:00
|
|
|
export type DataTableMeta<TData> = TableMeta<TData> & {
|
|
|
|
|
tableOps?: DataTableOps<TData>
|
|
|
|
|
rowOps?: DataTableRowOps<TData>
|
|
|
|
|
bulkOps?: DataTableBulkRowOps<TData>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface DataTableProps<TData, TValue> {
|
2025-10-16 11:18:55 +00:00
|
|
|
columns: ColumnDef<TData, TValue>[]
|
2025-10-16 17:59:13 +00:00
|
|
|
data: TData[]
|
|
|
|
|
meta?: DataTableMeta<TData>
|
2025-10-16 11:18:55 +00:00
|
|
|
|
2025-10-16 17:59:13 +00:00
|
|
|
// Configuración
|
|
|
|
|
readOnly?: boolean
|
|
|
|
|
enablePagination?: boolean
|
|
|
|
|
pageSize?: number
|
|
|
|
|
enableRowSelection?: boolean
|
|
|
|
|
EditorComponent?: React.ComponentType<{ row: TData; index: number; onClose: () => void }>
|
2025-10-16 11:18:55 +00:00
|
|
|
|
2025-10-16 17:59:13 +00:00
|
|
|
getRowId?: (row: Row<TData>, index: number) => string;
|
2025-10-16 11:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function DataTable<TData, TValue>({
|
|
|
|
|
columns,
|
|
|
|
|
data,
|
|
|
|
|
meta,
|
2025-10-16 17:59:13 +00:00
|
|
|
readOnly = false,
|
|
|
|
|
enablePagination = true,
|
2025-10-16 11:18:55 +00:00
|
|
|
pageSize = 25,
|
2025-10-16 17:59:13 +00:00
|
|
|
enableRowSelection = false,
|
|
|
|
|
EditorComponent,
|
2025-10-16 11:18:55 +00:00
|
|
|
}: DataTableProps<TData, TValue>) {
|
|
|
|
|
const { t } = useTranslation();
|
2025-10-16 17:59:13 +00:00
|
|
|
const [rowSelection, setRowSelection] = React.useState({})
|
|
|
|
|
const [sorting, setSorting] = React.useState<SortingState>([])
|
|
|
|
|
const [columnVisibility, setColumnVisibility] = React.useState<VisibilityState>({})
|
|
|
|
|
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([])
|
|
|
|
|
const [colSizes, setColSizes] = React.useState<ColumnSizingState>({})
|
|
|
|
|
const [editIndex, setEditIndex] = React.useState<number | null>(null)
|
2025-10-16 11:18:55 +00:00
|
|
|
|
|
|
|
|
const table = useReactTable({
|
|
|
|
|
data,
|
|
|
|
|
columns,
|
|
|
|
|
columnResizeMode: "onChange",
|
|
|
|
|
onColumnSizingChange: setColSizes,
|
2025-10-16 17:59:13 +00:00
|
|
|
getRowId: (row: any, i) => row.id ?? String(i),
|
|
|
|
|
meta,
|
2025-10-16 11:18:55 +00:00
|
|
|
state: {
|
|
|
|
|
columnSizing: colSizes,
|
|
|
|
|
sorting,
|
|
|
|
|
columnVisibility,
|
|
|
|
|
rowSelection,
|
|
|
|
|
columnFilters,
|
|
|
|
|
},
|
2025-10-16 17:59:13 +00:00
|
|
|
initialState: {
|
|
|
|
|
pagination: { pageSize },
|
|
|
|
|
},
|
2025-10-16 11:18:55 +00:00
|
|
|
enableRowSelection,
|
|
|
|
|
onRowSelectionChange: setRowSelection,
|
|
|
|
|
onSortingChange: setSorting,
|
|
|
|
|
onColumnFiltersChange: setColumnFilters,
|
|
|
|
|
onColumnVisibilityChange: setColumnVisibility,
|
2025-10-16 17:59:13 +00:00
|
|
|
getCoreRowModel: getCoreRowModel(),
|
2025-10-16 11:18:55 +00:00
|
|
|
getFilteredRowModel: getFilteredRowModel(),
|
|
|
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
|
|
|
getSortedRowModel: getSortedRowModel(),
|
|
|
|
|
getFacetedRowModel: getFacetedRowModel(),
|
|
|
|
|
getFacetedUniqueValues: getFacetedUniqueValues(),
|
|
|
|
|
})
|
|
|
|
|
|
2025-10-16 17:59:13 +00:00
|
|
|
const handleCloseEditor = React.useCallback(() => setEditIndex(null), [])
|
|
|
|
|
|
2025-10-16 11:18:55 +00:00
|
|
|
return (
|
2025-10-16 17:59:13 +00:00
|
|
|
<div className='flex flex-col gap-0'>
|
|
|
|
|
<DataTableToolbar table={table} showViewOptions={false} />
|
2025-10-16 11:18:55 +00:00
|
|
|
|
|
|
|
|
<div className="overflow-hidden rounded-md border">
|
2025-10-16 17:59:13 +00:00
|
|
|
<TableComp className="w-full text-sm">
|
2025-10-16 11:18:55 +00:00
|
|
|
<TableHeader className="sticky top-0 bg-muted hover:bg-muted z-10">
|
|
|
|
|
{table.getHeaderGroups().map((hg) => (
|
|
|
|
|
<TableRow key={hg.id}>
|
|
|
|
|
{hg.headers.map((h) => {
|
|
|
|
|
const w = h.getSize(); // px
|
|
|
|
|
const minW = h.column.columnDef.minSize;
|
|
|
|
|
const maxW = h.column.columnDef.maxSize;
|
|
|
|
|
return (
|
|
|
|
|
<TableHead
|
|
|
|
|
key={h.id}
|
|
|
|
|
colSpan={h.colSpan}
|
|
|
|
|
style={{
|
|
|
|
|
width: w ? `${w}px` : undefined,
|
|
|
|
|
minWidth: typeof minW === "number" ? `${minW}px` : undefined,
|
|
|
|
|
maxWidth: typeof maxW === "number" ? `${maxW}px` : undefined,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{h.isPlaceholder ? null : flexRender(h.column.columnDef.header, h.getContext())}
|
|
|
|
|
</TableHead>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableHeader>
|
|
|
|
|
|
|
|
|
|
<TableBody>
|
|
|
|
|
{table.getRowModel().rows.length ? (
|
2025-10-16 17:59:13 +00:00
|
|
|
table.getRowModel().rows.map((row, i) => (
|
|
|
|
|
<TableRow key={row.id} data-state={row.getIsSelected() && "selected"} className='group'>
|
2025-10-16 11:18:55 +00:00
|
|
|
{row.getVisibleCells().map((cell) => {
|
|
|
|
|
const w = cell.column.getSize();
|
|
|
|
|
const minW = cell.column.columnDef.minSize;
|
|
|
|
|
const maxW = cell.column.columnDef.maxSize;
|
|
|
|
|
return (
|
|
|
|
|
<TableCell
|
|
|
|
|
key={cell.id}
|
|
|
|
|
className="align-top"
|
|
|
|
|
style={{
|
|
|
|
|
width: w ? `${w}px` : undefined,
|
|
|
|
|
minWidth: typeof minW === "number" ? `${minW}px` : undefined,
|
|
|
|
|
maxWidth: typeof maxW === "number" ? `${maxW}px` : undefined,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
|
|
|
</TableCell>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</TableRow>
|
|
|
|
|
))
|
|
|
|
|
) : (
|
|
|
|
|
<TableRow>
|
2025-10-16 17:59:13 +00:00
|
|
|
<TableCell
|
|
|
|
|
colSpan={columns.length}
|
|
|
|
|
className='h-24 text-center text-muted-foreground'
|
|
|
|
|
>
|
|
|
|
|
{t("components.datatable.empty")}
|
2025-10-16 11:18:55 +00:00
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
)}
|
|
|
|
|
</TableBody>
|
2025-10-16 17:59:13 +00:00
|
|
|
<TableFooter>
|
2025-10-16 11:18:55 +00:00
|
|
|
|
2025-10-16 17:59:13 +00:00
|
|
|
</TableFooter>
|
|
|
|
|
</TableComp>
|
|
|
|
|
</div>
|
2025-10-16 11:18:55 +00:00
|
|
|
|
2025-10-16 17:59:13 +00:00
|
|
|
{enablePagination && <DataTablePagination table={table} />}
|
|
|
|
|
|
|
|
|
|
{EditorComponent && editIndex !== null && (
|
|
|
|
|
<Dialog open onOpenChange={handleCloseEditor}>
|
|
|
|
|
<DialogContent className="max-w-3xl">
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>{t("components.datatable.editor.title")}</DialogTitle>
|
|
|
|
|
<DialogDescription>{t("components.datatable.editor.subtitle")}</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
|
|
|
|
<div className="mt-4">
|
|
|
|
|
<EditorComponent
|
|
|
|
|
row={data[editIndex]}
|
|
|
|
|
index={editIndex}
|
|
|
|
|
onClose={handleCloseEditor}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button type="button" variant="secondary" onClick={handleCloseEditor}>
|
|
|
|
|
{t("common.close")}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
2025-10-16 11:18:55 +00:00
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2025-10-16 17:59:13 +00:00
|
|
|
)
|
2025-10-16 11:18:55 +00:00
|
|
|
}
|