@@ -20,6 +20,8 @@ import { useSuspenseGetQueryMetadata } from '../QueryWrapper/useGetQueryMetadata
2020
2121import { usePrefetchTableRows } from './usePrefetchTableData'
2222
23+ const PAGE_SIZE_OPTIONS = [ 10 , 25 , 100 , 500 ]
24+
2325export const TablePagination = ( ) : React . ReactNode => {
2426 const { goToPage, pageSize, setPageSize, currentPage, currentQueryRequest } =
2527 useQueryContext ( )
@@ -31,29 +33,38 @@ export const TablePagination = (): React.ReactNode => {
3133 } = useSuspenseGetQueryMetadata ( )
3234
3335 const currentLimit = currentQueryRequest . query . limit
34- const maxPageSize = maxRowsPerPage ?? pageSize
36+ const resolvedPageSize = currentLimit ?? pageSize
3537
36- const pageSizeOptions = [ 10 , 25 , 100 , 500 ]
37- if ( currentLimit && ! pageSizeOptions . includes ( currentLimit ) ) {
38- pageSizeOptions . push ( currentLimit )
39- pageSizeOptions . sort ( ( a , b ) => a - b )
40- }
41- const pageSizeOptionsBasedOnData = pageSizeOptions . filter (
42- value => value < maxPageSize ,
43- )
44- if ( pageSizeOptionsBasedOnData . length == 0 ) {
45- pageSizeOptionsBasedOnData . push ( maxPageSize )
46- if ( maxRowsPerPage && pageSize > maxRowsPerPage ) {
47- setPageSize ( maxRowsPerPage )
38+ // Filter by backend limit maxRowsPerPage
39+ let pageSizeOptions = maxRowsPerPage
40+ ? PAGE_SIZE_OPTIONS . filter ( option => option < maxRowsPerPage )
41+ : [ ...PAGE_SIZE_OPTIONS ]
42+
43+ if ( queryCount ) {
44+ // first option that is >= queryCount (show all rows)
45+ const firstSufficientOptionIndex = pageSizeOptions . findIndex (
46+ option => option >= queryCount ,
47+ )
48+ // If found, slice to only include options up to and including that one
49+ if ( firstSufficientOptionIndex !== - 1 ) {
50+ pageSizeOptions = pageSizeOptions . slice ( 0 , firstSufficientOptionIndex + 1 )
4851 }
4952 }
53+
54+ // If resolvedPageSize isn't in the options, use the smallest available option
55+ const displayedPageSize = pageSizeOptions . includes ( resolvedPageSize )
56+ ? resolvedPageSize
57+ : pageSizeOptions [ 0 ]
58+
5059 const handlePage = ( _event : ChangeEvent < unknown > , value : number ) => {
5160 goToPage ( value )
5261 }
5362
5463 const handlePageSize = ( event : SelectChangeEvent < number > ) => {
5564 const value = event . target . value
65+
5666 setPageSize ( value )
67+ goToPage ( 1 )
5768 }
5869
5970 // A custom `renderItem` implementation for the MUI Pagination component that prefetches a page's data when the page number button is hovered over
@@ -91,7 +102,8 @@ export const TablePagination = (): React.ReactNode => {
91102 // Also hide pagination if the query count is unavailable.
92103 if (
93104 ( currentPage == 1 && queryCount == 1 && pageSize != 1 ) ||
94- queryCount == undefined
105+ queryCount == undefined ||
106+ ( maxRowsPerPage && maxRowsPerPage < 5 )
95107 ) {
96108 return null
97109 }
@@ -100,7 +112,7 @@ export const TablePagination = (): React.ReactNode => {
100112 < div >
101113 < Pagination
102114 page = { currentPage }
103- count = { Math . ceil ( queryCount / pageSize ) }
115+ count = { Math . ceil ( queryCount / displayedPageSize ) }
104116 color = "secondary"
105117 onChange = { handlePage }
106118 shape = { 'rounded' }
@@ -116,12 +128,12 @@ export const TablePagination = (): React.ReactNode => {
116128 </ Typography >
117129 < Select
118130 name = "page size"
119- value = { pageSize }
131+ value = { displayedPageSize }
120132 size = "small"
121133 onChange = { handlePageSize }
122134 sx = { { ml : 0.5 } }
123135 >
124- { pageSizeOptionsBasedOnData . map ( pageSize => {
136+ { pageSizeOptions . map ( pageSize => {
125137 return (
126138 < MenuItem key = { pageSize } value = { pageSize } >
127139 { pageSize } per page
0 commit comments