Skip to content

Commit eae13a2

Browse files
committed
Make mobile friendly
1 parent ef7f59d commit eae13a2

File tree

1 file changed

+72
-26
lines changed

1 file changed

+72
-26
lines changed

site/src/components/PaginationWidget/PaginationWidget.tsx

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
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"
34
import KeyboardArrowLeft from "@material-ui/icons/KeyboardArrowLeft"
45
import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight"
6+
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"
7+
import { Maybe } from "components/Conditionals/Maybe"
58
import { CSSProperties } from "react"
69

710
export type PaginationWidgetProps = {
@@ -74,6 +77,39 @@ 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+
key={`Page${page}`}
106+
onClick={() => onPageClick && onPageClick(page)}
107+
>
108+
<div>{page}</div>
109+
</Button>
110+
)
111+
}
112+
77113
export const PaginationWidget = ({
78114
prevLabel,
79115
nextLabel,
@@ -88,7 +124,8 @@ export const PaginationWidget = ({
88124
const numPages = numRecords ? Math.ceil(numRecords / numRecordsPerPage) : 0
89125
const firstPageActive = activePage === 1 && numPages !== 0
90126
const lastPageActive = activePage === numPages && numPages !== 0
91-
127+
const theme = useTheme()
128+
const isMobile = useMediaQuery(theme.breakpoints.down('sm'))
92129
const styles = useStyles()
93130

94131
// No need to display any pagination if we know the number of pages is 1
@@ -107,30 +144,39 @@ export const PaginationWidget = ({
107144
<KeyboardArrowLeft />
108145
<div>{prevLabel}</div>
109146
</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>
134180
<Button
135181
aria-label="Next page"
136182
disabled={lastPageActive}

0 commit comments

Comments
 (0)