Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 'use client'; import { Table } from '@tanstack/react-table'; import { Icon } from '@/components/ui/icons'; import { cn } from '@/lib/core'; interface DataTablePaginationProps<TData> { /** Table instance from useReactTable */ table: Table<TData>; /** Available page size options */ pageSizeOptions?: number[]; } /** * Pagination controls for DataTable. * * Features: * - First/Previous/Next/Last navigation * - Page size selector * - Current page indicator * - Total rows count */ export function DataTablePagination<TData>({ table, pageSizeOptions = [10, 20, 30, 50, 100], }: DataTablePaginationProps<TData>) { const pageIndex = table.getState().pagination.pageIndex; const pageSize = table.getState().pagination.pageSize; const pageCount = table.getPageCount(); const totalRows = table.getFilteredRowModel().rows.length; return ( <div className="flex flex-col sm:flex-row items-center justify-between gap-4 px-2"> {/* Row count and page size */} <div className="flex items-center gap-4 text-sm text-gray-600 dark:text-gray-400"> <span> {totalRows} {totalRows === 1 ? 'row' : 'rows'} </span> <div className="flex items-center gap-2"> <label htmlFor="page-size" className="sr-only"> Rows per page </label> <span>Rows per page:</span> <select id="page-size" value={pageSize} onChange={(e) => table.setPageSize(Number(e.target.value))} className={cn( 'px-2 py-1 border rounded-md text-sm', 'bg-white dark:bg-gray-800', 'border-gray-300 dark:border-gray-600', 'text-gray-900 dark:text-white', 'focus:outline-none focus:ring-2 focus:ring-primary-500' )} > {pageSizeOptions.map((size) => ( <option key={size} value={size}> {size} </option> ))} </select> </div> </div> {/* Page navigation */} <div className="flex items-center gap-2"> <span className="text-sm text-gray-600 dark:text-gray-400"> Page {pageIndex + 1} of {pageCount || 1} </span> <div className="flex items-center gap-1"> {/* First page */} <button onClick={() => table.setPageIndex(0)} disabled={!table.getCanPreviousPage()} className={cn( 'p-1.5 rounded-md transition-colors', 'hover:bg-gray-100 dark:hover:bg-gray-800', 'disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent' )} aria-label="Go to first page" > <Icon name="chevron-left" className="h-4 w-4" /> </button> {/* Previous page */} <button onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()} className={cn( 'p-1.5 rounded-md transition-colors', 'hover:bg-gray-100 dark:hover:bg-gray-800', 'disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent' )} aria-label="Go to previous page" > <Icon name="chevron-left" className="h-4 w-4" /> </button> {/* Next page */} <button onClick={() => table.nextPage()} disabled={!table.getCanNextPage()} className={cn( 'p-1.5 rounded-md transition-colors', 'hover:bg-gray-100 dark:hover:bg-gray-800', 'disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent' )} aria-label="Go to next page" > <Icon name="chevron-right" className="h-4 w-4" /> </button> {/* Last page */} <button onClick={() => table.setPageIndex(pageCount - 1)} disabled={!table.getCanNextPage()} className={cn( 'p-1.5 rounded-md transition-colors', 'hover:bg-gray-100 dark:hover:bg-gray-800', 'disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent' )} aria-label="Go to last page" > <Icon name="chevron-right" className="h-4 w-4" /> </button> </div> </div> </div> ); } export default DataTablePagination; |