import { useListSearch } from "@/hooks/useListSearch"; import { ColumnDef, flexRender, getCoreRowModel, getPaginationRowModel, getSortedRowModel, PaginationState, useReactTable, } from "@tanstack/react-table"; import clsx from "clsx"; import { useState } from "react"; import { BsArrowDown, BsArrowUp } from "react-icons/bs"; import Button from "../Low/Button"; interface Props { data: T[]; columns: ColumnDef[]; searchFields: string[][]; size?: number; onDownload?: (rows: T[]) => void; isDownloadLoading?: boolean; searchPlaceholder?: string; isLoading?: boolean; } export default function Table({ data, columns, searchFields, size = 16, onDownload, isDownloadLoading, searchPlaceholder, isLoading, }: Props) { const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: size, }); const { rows, renderSearch } = useListSearch( searchFields, data, searchPlaceholder ); const table = useReactTable({ data: rows, columns, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getPaginationRowModel: getPaginationRowModel(), onPaginationChange: setPagination, state: { pagination, }, }); return (
{renderSearch()} {onDownload && ( )}
Page
{table.getState().pagination.pageIndex + 1} of{" "} {table.getPageCount().toLocaleString()}
| Total: {table.getRowCount().toLocaleString()}
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( ))} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( ))} ))}
{flexRender( header.column.columnDef.header, header.getContext() )} {{ asc: , desc: , }[header.column.getIsSorted() as string] ?? null}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
{isLoading ? (
) : ( rows.length === 0 && (
No data found...
) )}
); }