"use client"; import { ColumnDef, flexRender, getCoreRowModel, getPaginationRowModel, useReactTable, } from "@tanstack/react-table" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { DataTablePagination } from "../data-table-pagination"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog"; interface PaginationState { pageIndex: number pageSize: number pageCount: number setPageIndex: (page: number) => void setPageSize: (size: number) => void } interface AlertDialogState { isOpen: boolean title?: string description?: string confirmText?: string cancelText?: string setOpen: (isOpen: boolean) => void onConfirm: () => void onCancel: () => void } interface DataTableProps { columns: ColumnDef[] data: TData[] pagination?: PaginationState, alertDialog?: AlertDialogState, } const DataTable = ({ data, columns, pagination, alertDialog }: DataTableProps) => { const table = useReactTable({ data, columns, pageCount: pagination?.pageCount, state: { pagination: { pageIndex: pagination?.pageIndex ?? 0, pageSize: pagination?.pageSize ?? 10, }, }, onPaginationChange: (updater) => { if (!pagination) return; const next = typeof updater === 'function' ? updater({ pageIndex: pagination.pageIndex, pageSize: pagination.pageSize }) : updater; pagination.setPageIndex(next.pageIndex) pagination.setPageSize(next.pageSize) }, manualPagination: true, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), }) return (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header, index) => { const isFirst = index === 0; const isLast = index === headerGroup.headers.length - 1; return ( {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 results. )}
Are you absolutely sure? This action cannot be undone. This will permanently remove your data from our servers. Cancel Continue
) } export default DataTable