"use client"; import * as React from "react"; import { ColumnDef, ColumnFiltersState, SortingState, VisibilityState, flexRender, getCoreRowModel, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from "lucide-react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; export interface ColumnVisibilityInfo { id: string; isVisible: boolean; canHide: boolean; toggleVisibility: (visible: boolean) => void; } export interface TableInfo { selectedCount: number; getSelectedRows: () => TData[]; getAllVisibleRows: () => TData[]; columns: ColumnVisibilityInfo[]; } interface DataTableProps { columns: ColumnDef[]; data: TData[]; searchKey?: string; searchValue?: string; onTableChange?: (info: TableInfo) => void; } export function DataTable({ columns, data, searchKey, searchValue, onTableChange, }: DataTableProps) { const [sorting, setSorting] = React.useState([]); const [columnFilters, setColumnFilters] = React.useState([]); const [columnVisibility, setColumnVisibility] = React.useState({}); const [rowSelection, setRowSelection] = React.useState({}); const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFilteredRowModel: getFilteredRowModel(), onSortingChange: setSorting, onColumnFiltersChange: setColumnFilters, onColumnVisibilityChange: setColumnVisibility, onRowSelectionChange: setRowSelection, state: { sorting, columnFilters, columnVisibility, rowSelection, }, }); // Notificar cambios al componente padre React.useEffect(() => { if (onTableChange) { const columnsInfo: ColumnVisibilityInfo[] = table .getAllColumns() .filter((column) => typeof column.accessorFn !== "undefined") .map((column) => ({ id: column.id, isVisible: column.getIsVisible(), canHide: column.getCanHide(), toggleVisibility: (visible: boolean) => { setColumnVisibility((prev) => ({ ...prev, [column.id]: visible, })); }, })); onTableChange({ selectedCount: table.getFilteredSelectedRowModel().rows.length, getSelectedRows: () => table.getFilteredSelectedRowModel().rows.map((row) => row.original), getAllVisibleRows: () => table.getFilteredRowModel().rows.map((row) => row.original), columns: columnsInfo, }); } }, [table, onTableChange, columnVisibility, rowSelection]); // Aplicar filtro de búsqueda externo React.useEffect(() => { if (searchKey && searchValue !== undefined) { table.getColumn(searchKey)?.setFilterValue(searchValue); } }, [searchKey, searchValue, table]); return (
{/* Tabla */}
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext() )} ))} )) ) : ( No se encontraron resultados. )}
{/* Paginación */}
{table.getFilteredSelectedRowModel().rows.length} de{" "} {table.getFilteredRowModel().rows.length} fila(s) seleccionada(s).

Filas por página

Página {table.getState().pagination.pageIndex + 1} de{" "} {table.getPageCount()}
); }