11import Button from "@material-ui/core/Button"
2- import { makeStyles } from "@material-ui/core/styles"
2+ import { makeStyles , useTheme } from "@material-ui/core/styles"
3+ import useMediaQuery from "@material-ui/core/useMediaQuery"
34import KeyboardArrowLeft from "@material-ui/icons/KeyboardArrowLeft"
45import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight"
6+ import { ChooseOne , Cond } from "components/Conditionals/ChooseOne"
7+ import { Maybe } from "components/Conditionals/Maybe"
58import { CSSProperties } from "react"
69
710export type PaginationWidgetProps = {
@@ -24,7 +27,7 @@ export type PaginationWidgetProps = {
2427const range = ( start : number , stop : number , step = 1 ) =>
2528 Array . from ( { length : ( stop - start ) / step + 1 } , ( _ , i ) => start + i * step )
2629
27- const DEFAULT_RECORDS_PER_PAGE = 25
30+ export const DEFAULT_RECORDS_PER_PAGE = 25
2831// Number of pages to the left or right of the current page selection.
2932const PAGE_NEIGHBORS = 1
3033// Number of pages displayed for cases where there are multiple ellipsis showing. This can be
@@ -74,6 +77,38 @@ export const buildPagedList = (
7477 return range ( 1 , numPages )
7578}
7679
80+ interface PageButtonProps {
81+ activePage : number
82+ page : number
83+ numPages : number
84+ onPageClick ?: ( page : number ) => void
85+ }
86+
87+ const PageButton = ( {
88+ activePage,
89+ page,
90+ numPages,
91+ onPageClick,
92+ } : PageButtonProps ) : JSX . Element => {
93+ const styles = useStyles ( )
94+ return (
95+ < Button
96+ className = {
97+ activePage === page
98+ ? `${ styles . pageButton } ${ styles . activePageButton } `
99+ : styles . pageButton
100+ }
101+ aria-label = { `${ page === activePage ? "Current Page" : "" } ${
102+ page === numPages ? "Last Page" : ""
103+ } Page${ page } `}
104+ name = "Page button"
105+ onClick = { ( ) => onPageClick && onPageClick ( page ) }
106+ >
107+ < div > { page } </ div >
108+ </ Button >
109+ )
110+ }
111+
77112export const PaginationWidget = ( {
78113 prevLabel,
79114 nextLabel,
@@ -88,11 +123,12 @@ export const PaginationWidget = ({
88123 const numPages = numRecords ? Math . ceil ( numRecords / numRecordsPerPage ) : 0
89124 const firstPageActive = activePage === 1 && numPages !== 0
90125 const lastPageActive = activePage === numPages && numPages !== 0
91-
126+ const theme = useTheme ( )
127+ const isMobile = useMediaQuery ( theme . breakpoints . down ( "sm" ) )
92128 const styles = useStyles ( )
93129
94130 // No need to display any pagination if we know the number of pages is 1
95- if ( numPages === 1 ) {
131+ if ( numPages === 1 || numRecords === 0 ) {
96132 return null
97133 }
98134
@@ -107,30 +143,38 @@ export const PaginationWidget = ({
107143 < KeyboardArrowLeft />
108144 < div > { prevLabel } </ div >
109145 </ Button >
110- { numPages > 0 &&
111- buildPagedList ( numPages , activePage ) . map ( ( page ) =>
112- typeof page !== "number" ? (
113- < Button className = { styles . pageButton } key = { `Page${ page } ` } disabled >
114- < div > ...</ div >
115- </ Button >
116- ) : (
117- < Button
118- className = {
119- activePage === page
120- ? `${ styles . pageButton } ${ styles . activePageButton } `
121- : styles . pageButton
122- }
123- aria-label = { `${ page === activePage ? "Current Page" : "" } ${
124- page === numPages ? "Last Page" : ""
125- } Page${ page } `}
126- name = "Page button"
127- key = { `Page${ page } ` }
128- onClick = { ( ) => onPageClick && onPageClick ( page ) }
129- >
130- < div > { page } </ div >
131- </ Button >
132- ) ,
133- ) }
146+ < Maybe condition = { numPages > 0 } >
147+ < ChooseOne >
148+ < Cond condition = { isMobile } >
149+ < PageButton
150+ activePage = { activePage }
151+ page = { activePage }
152+ numPages = { numPages }
153+ />
154+ </ Cond >
155+ < Cond >
156+ { buildPagedList ( numPages , activePage ) . map ( ( page ) =>
157+ typeof page !== "number" ? (
158+ < Button
159+ className = { styles . pageButton }
160+ key = { `Page${ page } ` }
161+ disabled
162+ >
163+ < div > ...</ div >
164+ </ Button >
165+ ) : (
166+ < PageButton
167+ key = { `Page${ page } ` }
168+ activePage = { activePage }
169+ page = { page }
170+ numPages = { numPages }
171+ onPageClick = { onPageClick }
172+ />
173+ ) ,
174+ ) }
175+ </ Cond >
176+ </ ChooseOne >
177+ </ Maybe >
134178 < Button
135179 aria-label = "Next page"
136180 disabled = { lastPageActive }
0 commit comments