import { type ColumnDef, flexRender, getCoreRowModel, useReactTable, } from "@tanstack/react-table" import { Loader } from "lucide-react" import { Pagination, type PaginationProps } from "@/components/ui/pagination" import { TableBody, TableCell, TableHead, TableHeader, Table as TableRoot, TableRow, } from "@/components/ui/table" import { cn } from "@/lib/utils" export type DataTableProps = { data: T[] status: "load" | "done" | "fail" columns: ColumnDef[] pagination?: PaginationProps classNames?: { headRow?: string dataRow?: string } } export default function DataTable>( props: DataTableProps, ) { const table = useReactTable({ data: props.data, columns: props.columns, getCoreRowModel: getCoreRowModel(), manualPagination: true, rowCount: props.pagination?.total ?? props.data.length, state: { pagination: { pageIndex: props.pagination?.page ?? 0, pageSize: props.pagination?.size ?? props.data.length, }, columnFilters: [], }, autoResetPageIndex: false, }) console.log("data length:", props.data.length) console.log("pageSize:", props.pagination?.size ?? props.data.length) console.log("rows:", table.getRowModel().rows.length) return ( <> {/* 数据表 */}
{table.getHeaderGroups().map(group => ( {group.headers.map(header => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ))} ))} {props.status === "fail" ? ( 加载失败 ) : !props.data?.length ? ( 暂无数据 ) : ( table.getRowModel().rows.map(row => ( {row.getVisibleCells().map(cell => ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ))} )) )} {props.status === "load" && (
加载中
)}
{/* 分页器 */} {props.pagination && } ) }