1
1
import 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"
3
4
import KeyboardArrowLeft from "@material-ui/icons/KeyboardArrowLeft"
4
5
import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight"
6
+ import { ChooseOne , Cond } from "components/Conditionals/ChooseOne"
7
+ import { Maybe } from "components/Conditionals/Maybe"
5
8
import { CSSProperties } from "react"
6
9
7
10
export type PaginationWidgetProps = {
@@ -24,7 +27,7 @@ export type PaginationWidgetProps = {
24
27
const range = ( start : number , stop : number , step = 1 ) =>
25
28
Array . from ( { length : ( stop - start ) / step + 1 } , ( _ , i ) => start + i * step )
26
29
27
- const DEFAULT_RECORDS_PER_PAGE = 25
30
+ export const DEFAULT_RECORDS_PER_PAGE = 25
28
31
// Number of pages to the left or right of the current page selection.
29
32
const PAGE_NEIGHBORS = 1
30
33
// Number of pages displayed for cases where there are multiple ellipsis showing. This can be
@@ -74,6 +77,38 @@ export const buildPagedList = (
74
77
return range ( 1 , numPages )
75
78
}
76
79
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
+
77
112
export const PaginationWidget = ( {
78
113
prevLabel,
79
114
nextLabel,
@@ -88,11 +123,12 @@ export const PaginationWidget = ({
88
123
const numPages = numRecords ? Math . ceil ( numRecords / numRecordsPerPage ) : 0
89
124
const firstPageActive = activePage === 1 && numPages !== 0
90
125
const lastPageActive = activePage === numPages && numPages !== 0
91
-
126
+ const theme = useTheme ( )
127
+ const isMobile = useMediaQuery ( theme . breakpoints . down ( "sm" ) )
92
128
const styles = useStyles ( )
93
129
94
130
// 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 ) {
96
132
return null
97
133
}
98
134
@@ -107,30 +143,38 @@ export const PaginationWidget = ({
107
143
< KeyboardArrowLeft />
108
144
< div > { prevLabel } </ div >
109
145
</ 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 >
134
178
< Button
135
179
aria-label = "Next page"
136
180
disabled = { lastPageActive }
0 commit comments