|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useMemo } from "react"; |
| 4 | +import { useReactTable, getCoreRowModel, flexRender, ColumnDef } from "@tanstack/react-table"; |
| 5 | +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/Table"; |
| 6 | +import { TablePagination } from "./TablePagination"; |
| 7 | +import { TableToolbar } from "./TableToolbar"; |
| 8 | + |
| 9 | +interface TableDataProps<T> { |
| 10 | + data: T[]; |
| 11 | + columns: ColumnDef<T>[]; |
| 12 | + searchable?: boolean; |
| 13 | + searchPlaceholder?: string; |
| 14 | + itemsPerPage?: number; |
| 15 | + className?: string; |
| 16 | + rightAction?: React.ReactNode; |
| 17 | +} |
| 18 | + |
| 19 | +function TableData<T>({ |
| 20 | + data, |
| 21 | + columns, |
| 22 | + searchable = false, |
| 23 | + searchPlaceholder = "Search...", |
| 24 | + itemsPerPage = 5, |
| 25 | + className, |
| 26 | + rightAction, |
| 27 | +}: TableDataProps<T>) { |
| 28 | + const [globalFilter, setGlobalFilter] = useState(""); |
| 29 | + const [currentPage, setCurrentPage] = useState(1); |
| 30 | + |
| 31 | + const filteredData = useMemo(() => { |
| 32 | + if (!searchable || !globalFilter) return data; |
| 33 | + |
| 34 | + return data.filter((item: T) => |
| 35 | + Object.values(item as Record<string, unknown>).some((value) => |
| 36 | + value?.toString().toLowerCase().includes(globalFilter.toLowerCase()) |
| 37 | + ) |
| 38 | + ); |
| 39 | + }, [data, globalFilter, searchable]); |
| 40 | + |
| 41 | + const totalPages = Math.ceil(filteredData.length / itemsPerPage); |
| 42 | + |
| 43 | + const paginatedData = useMemo(() => { |
| 44 | + const startIndex = (currentPage - 1) * itemsPerPage; |
| 45 | + return filteredData.slice(startIndex, startIndex + itemsPerPage); |
| 46 | + }, [filteredData, currentPage, itemsPerPage]); |
| 47 | + |
| 48 | + const table = useReactTable({ |
| 49 | + data: paginatedData, |
| 50 | + columns, |
| 51 | + getCoreRowModel: getCoreRowModel(), |
| 52 | + manualPagination: true, |
| 53 | + }); |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className={`space-y-4 ${className}`}> |
| 57 | + <TableToolbar |
| 58 | + searchable={searchable} |
| 59 | + searchPlaceholder={searchPlaceholder} |
| 60 | + searchValue={globalFilter} |
| 61 | + onSearchChange={(value) => { |
| 62 | + setGlobalFilter(value); |
| 63 | + setCurrentPage(1); |
| 64 | + }} |
| 65 | + rightAction={rightAction} |
| 66 | + /> |
| 67 | + |
| 68 | + <div className="rounded-md border"> |
| 69 | + <Table> |
| 70 | + <TableHeader className="bg-muted/50"> |
| 71 | + {table.getHeaderGroups().map((headerGroup) => ( |
| 72 | + <TableRow key={headerGroup.id}> |
| 73 | + {headerGroup.headers.map((header) => ( |
| 74 | + <TableHead key={header.id} className="text-foreground font-semibold"> |
| 75 | + {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())} |
| 76 | + </TableHead> |
| 77 | + ))} |
| 78 | + </TableRow> |
| 79 | + ))} |
| 80 | + </TableHeader> |
| 81 | + <TableBody> |
| 82 | + {table.getRowModel().rows.length === 0 ? ( |
| 83 | + <TableRow> |
| 84 | + <TableCell colSpan={columns.length} className="text-muted-foreground h-24 text-center"> |
| 85 | + No data found. |
| 86 | + </TableCell> |
| 87 | + </TableRow> |
| 88 | + ) : ( |
| 89 | + table.getRowModel().rows.map((row) => ( |
| 90 | + <TableRow key={row.id}> |
| 91 | + {row.getVisibleCells().map((cell) => ( |
| 92 | + <TableCell key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</TableCell> |
| 93 | + ))} |
| 94 | + </TableRow> |
| 95 | + )) |
| 96 | + )} |
| 97 | + </TableBody> |
| 98 | + </Table> |
| 99 | + </div> |
| 100 | + |
| 101 | + {totalPages > 1 && ( |
| 102 | + <div className="flex items-center justify-between"> |
| 103 | + <div className="text-muted-foreground text-sm"> |
| 104 | + {currentPage} of {totalPages} pages |
| 105 | + </div> |
| 106 | + |
| 107 | + <TablePagination currentPage={currentPage} totalPages={totalPages} onPageChange={setCurrentPage} /> |
| 108 | + </div> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +export default TableData; |
0 commit comments