99 useReactTable ,
1010 getPaginationRowModel ,
1111 getFacetedMinMaxValues ,
12+ PaginationState ,
13+ OnChangeFn ,
1214} from "@tanstack/react-table"
1315import React from "react"
1416
@@ -131,6 +133,11 @@ type TableProps<TData> = {
131133 enableGlobalSearch ?: boolean
132134 globalSearchPlaceholder ?: string
133135 addPagination ?: boolean
136+ manualPagination ?: boolean
137+ paginationState ?: PaginationState
138+ onPaginationChange ?: OnChangeFn < PaginationState >
139+ pageCount ?: number
140+ pageSizeOptions ?: number [ ]
134141 classNames ?: {
135142 table ?: string
136143 thead ?: string
@@ -179,9 +186,26 @@ const Table = <TData,>({
179186 enableGlobalSearch = true ,
180187 globalSearchPlaceholder = "Search..." ,
181188 addPagination = false ,
189+ manualPagination = false ,
190+ paginationState,
191+ onPaginationChange,
192+ pageCount : controlledPageCount ,
193+ pageSizeOptions = [ 5 , 10 , 20 , 30 , 40 , 50 ] ,
182194} : TableProps < TData > ) => {
183195 const [ sorting , setSorting ] = React . useState ( [ ] )
184196 const [ globalFilter , setGlobalFilter ] = React . useState ( "" )
197+ const [ internalPagination , setInternalPagination ] = React . useState < PaginationState > ( {
198+ pageIndex : 0 ,
199+ pageSize : 5 ,
200+ } )
201+
202+ const resolvedPaginationState = manualPagination
203+ ? paginationState ?? { pageIndex : 0 , pageSize : 5 }
204+ : internalPagination
205+
206+ const handlePaginationChange : OnChangeFn < PaginationState > = manualPagination
207+ ? onPaginationChange ?? ( ( ) => { } )
208+ : setInternalPagination
185209
186210 const table = useReactTable ( {
187211 data,
@@ -192,19 +216,18 @@ const Table = <TData,>({
192216 getSortedRowModel : getSortedRowModel ( ) ,
193217 getFilteredRowModel : getFilteredRowModel ( ) ,
194218 getFacetedUniqueValues : getFacetedUniqueValues ( ) ,
195- getPaginationRowModel : getPaginationRowModel ( ) ,
219+ ... ( manualPagination ? { } : { getPaginationRowModel : getPaginationRowModel ( ) } ) ,
196220 getFacetedMinMaxValues : getFacetedMinMaxValues ( ) ,
221+ manualPagination,
222+ pageCount : manualPagination ? controlledPageCount : undefined ,
197223 state : {
198224 sorting : sorting ,
199225 globalFilter : globalFilter ,
200- } ,
201- initialState : {
202- pagination : {
203- pageSize : 5 ,
204- } ,
226+ pagination : resolvedPaginationState ,
205227 } ,
206228 onSortingChange : setSorting ,
207229 onGlobalFilterChange : setGlobalFilter ,
230+ onPaginationChange : handlePaginationChange ,
208231 globalFilterFn : defaultGlobalFilterFn ,
209232 autoResetPageIndex : false ,
210233 } )
@@ -220,10 +243,10 @@ const Table = <TData,>({
220243 return
221244 }
222245
223- if ( pageCount > 0 && pageIndex >= pageCount ) {
246+ if ( ! manualPagination && pageCount > 0 && pageIndex >= pageCount ) {
224247 table . setPageIndex ( 0 )
225248 }
226- } , [ addPagination , pageCount , pageIndex , table ] )
249+ } , [ addPagination , pageCount , pageIndex , table , manualPagination ] )
227250
228251 return (
229252 < >
@@ -386,7 +409,7 @@ const Table = <TData,>({
386409 classNames ?. pageSizeSelect || ""
387410 } `}
388411 >
389- { [ 5 , 10 , 20 , 30 , 40 , 50 ] . map ( ( pageSize ) => (
412+ { pageSizeOptions . map ( ( pageSize ) => (
390413 < option key = { pageSize } value = { pageSize } >
391414 Show { pageSize }
392415 </ option >
0 commit comments