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 = {
@@ -74,6 +77,39 @@ 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
+ key = { `Page${ page } ` }
106
+ onClick = { ( ) => onPageClick && onPageClick ( page ) }
107
+ >
108
+ < div > { page } </ div >
109
+ </ Button >
110
+ )
111
+ }
112
+
77
113
export const PaginationWidget = ( {
78
114
prevLabel,
79
115
nextLabel,
@@ -88,7 +124,8 @@ export const PaginationWidget = ({
88
124
const numPages = numRecords ? Math . ceil ( numRecords / numRecordsPerPage ) : 0
89
125
const firstPageActive = activePage === 1 && numPages !== 0
90
126
const lastPageActive = activePage === numPages && numPages !== 0
91
-
127
+ const theme = useTheme ( )
128
+ const isMobile = useMediaQuery ( theme . breakpoints . down ( 'sm' ) )
92
129
const styles = useStyles ( )
93
130
94
131
// No need to display any pagination if we know the number of pages is 1
@@ -107,30 +144,39 @@ export const PaginationWidget = ({
107
144
< KeyboardArrowLeft />
108
145
< div > { prevLabel } </ div >
109
146
</ 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
- ) }
147
+ < Maybe condition = { numPages > 0 } >
148
+ < ChooseOne >
149
+ < Cond
150
+ condition = { isMobile }
151
+ >
152
+ < PageButton
153
+ activePage = { activePage }
154
+ page = { activePage }
155
+ numPages = { numPages }
156
+ />
157
+ </ Cond >
158
+ < Cond >
159
+ { buildPagedList ( numPages , activePage ) . map ( ( page ) =>
160
+ typeof page !== "number" ? (
161
+ < Button
162
+ className = { styles . pageButton }
163
+ key = { `Page${ page } ` }
164
+ disabled
165
+ >
166
+ < div > ...</ div >
167
+ </ Button >
168
+ ) : (
169
+ < PageButton
170
+ activePage = { activePage }
171
+ page = { page }
172
+ numPages = { numPages }
173
+ onPageClick = { onPageClick }
174
+ />
175
+ ) ,
176
+ ) }
177
+ </ Cond >
178
+ </ ChooseOne >
179
+ </ Maybe >
134
180
< Button
135
181
aria-label = "Next page"
136
182
disabled = { lastPageActive }
0 commit comments