|
| 1 | +import Button from "@material-ui/core/Button" |
| 2 | +import { makeStyles, useTheme } from "@material-ui/core/styles" |
| 3 | +import useMediaQuery from "@material-ui/core/useMediaQuery" |
| 4 | +import KeyboardArrowLeft from "@material-ui/icons/KeyboardArrowLeft" |
| 5 | +import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight" |
| 6 | +import { ChooseOne, Cond } from "components/Conditionals/ChooseOne" |
| 7 | +import { PageButton } from "./PageButton" |
| 8 | +import { buildPagedList } from "./utils" |
| 9 | + |
| 10 | +export type PaginationWidgetBaseProps = { |
| 11 | + count: number |
| 12 | + page: number |
| 13 | + limit: number |
| 14 | + onChange: (page: number) => void |
| 15 | +} |
| 16 | + |
| 17 | +export const PaginationWidgetBase = ({ |
| 18 | + count, |
| 19 | + page, |
| 20 | + limit, |
| 21 | + onChange, |
| 22 | +}: PaginationWidgetBaseProps): JSX.Element | null => { |
| 23 | + const theme = useTheme() |
| 24 | + const isMobile = useMediaQuery(theme.breakpoints.down("sm")) |
| 25 | + const styles = useStyles() |
| 26 | + const numPages = Math.ceil(count / limit) |
| 27 | + const isFirstPage = page === 0 |
| 28 | + const isLastPage = page === numPages - 1 |
| 29 | + |
| 30 | + if (numPages < 2) { |
| 31 | + return null |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <div className={styles.defaultContainerStyles}> |
| 36 | + <Button |
| 37 | + className={styles.prevLabelStyles} |
| 38 | + aria-label="Previous page" |
| 39 | + disabled={isFirstPage} |
| 40 | + onClick={() => { |
| 41 | + if (!isFirstPage) { |
| 42 | + onChange(page - 1) |
| 43 | + } |
| 44 | + }} |
| 45 | + > |
| 46 | + <KeyboardArrowLeft /> |
| 47 | + </Button> |
| 48 | + <ChooseOne> |
| 49 | + <Cond condition={isMobile}> |
| 50 | + <PageButton activePage={page} page={page} numPages={numPages} /> |
| 51 | + </Cond> |
| 52 | + <Cond> |
| 53 | + {buildPagedList(numPages, page).map((pageItem) => { |
| 54 | + if (pageItem === "left" || pageItem === "right") { |
| 55 | + return ( |
| 56 | + <PageButton |
| 57 | + key={pageItem} |
| 58 | + activePage={page} |
| 59 | + placeholder="..." |
| 60 | + disabled |
| 61 | + /> |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <PageButton |
| 67 | + key={pageItem} |
| 68 | + page={pageItem} |
| 69 | + activePage={page} |
| 70 | + numPages={numPages} |
| 71 | + onPageClick={() => onChange(pageItem)} |
| 72 | + /> |
| 73 | + ) |
| 74 | + })} |
| 75 | + </Cond> |
| 76 | + </ChooseOne> |
| 77 | + <Button |
| 78 | + aria-label="Next page" |
| 79 | + disabled={isLastPage} |
| 80 | + onClick={() => { |
| 81 | + if (!isLastPage) { |
| 82 | + onChange(page + 1) |
| 83 | + } |
| 84 | + }} |
| 85 | + > |
| 86 | + <KeyboardArrowRight /> |
| 87 | + </Button> |
| 88 | + </div> |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +const useStyles = makeStyles((theme) => ({ |
| 93 | + defaultContainerStyles: { |
| 94 | + justifyContent: "center", |
| 95 | + alignItems: "center", |
| 96 | + display: "flex", |
| 97 | + flexDirection: "row", |
| 98 | + padding: "20px", |
| 99 | + }, |
| 100 | + |
| 101 | + prevLabelStyles: { |
| 102 | + marginRight: `${theme.spacing(0.5)}px`, |
| 103 | + }, |
| 104 | +})) |
0 commit comments