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 function DataTable>( props: DataTableProps, ) { const table = useReactTable({ data: props.data, columns: props.columns, getCoreRowModel: getCoreRowModel(), manualPagination: true, rowCount: props.pagination.total, state: { pagination: { pageIndex: props.pagination.page, pageSize: props.pagination.size, }, columnFilters: [], }, }) 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" && (
加载中
)}
{/* 分页器 */}
) }