|
| 1 | +import Button from "@material-ui/core/Button" |
| 2 | +import { makeStyles } from "@material-ui/core/styles" |
| 3 | +import KeyboardArrowLeft from "@material-ui/icons/KeyboardArrowLeft" |
| 4 | +import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight" |
| 5 | +import { CSSProperties } from "react" |
| 6 | + |
| 7 | +export type PaginationWidgetProps = { |
| 8 | + prevLabel: string |
| 9 | + nextLabel: string |
| 10 | + onPrevClick: () => void |
| 11 | + onNextClick: () => void |
| 12 | + onPageClick?: (page: number) => void |
| 13 | + numRecordsPerPage?: number |
| 14 | + numRecords?: number |
| 15 | + activePage?: number |
| 16 | + containerStyle?: CSSProperties |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Generates a ranged array with an option to step over values. |
| 21 | + * Shamelessly stolen from: |
| 22 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#sequence_generator_range |
| 23 | + */ |
| 24 | +const range = (start: number, stop: number, step = 1) => |
| 25 | + Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step) |
| 26 | + |
| 27 | +const DEFAULT_RECORDS_PER_PAGE = 25 |
| 28 | +// Number of pages to the left or right of the current page selection. |
| 29 | +const PAGE_NEIGHBORS = 1 |
| 30 | +// Number of pages displayed for cases where there are multiple ellipsis showing. This can be |
| 31 | +// thought of as the minimum number of page numbers to display when multiple ellipsis are showing. |
| 32 | +const PAGES_TO_DISPLAY = PAGE_NEIGHBORS * 2 + 3 |
| 33 | +// Total page blocks(page numbers or ellipsis) displayed, including the maximum number of ellipsis (2). |
| 34 | +// This gives us maximum number of 7 page blocks to be displayed when the page neighbors value is 1. |
| 35 | +const NUM_PAGE_BLOCKS = PAGES_TO_DISPLAY + 2 |
| 36 | + |
| 37 | +/** |
| 38 | + * Builds a list of pages based on how many pages exist and where the user is in their navigation of those pages. |
| 39 | + * List result is used to from the buttons that make up the Pagination Widget |
| 40 | + */ |
| 41 | +export const buildPagedList = (numPages: number, activePage: number): (string | number)[] => { |
| 42 | + if (numPages > NUM_PAGE_BLOCKS) { |
| 43 | + let pages = [] |
| 44 | + const leftBound = activePage - PAGE_NEIGHBORS |
| 45 | + const rightBound = activePage + PAGE_NEIGHBORS |
| 46 | + const beforeLastPage = numPages - 1 |
| 47 | + const startPage = leftBound > 2 ? leftBound : 2 |
| 48 | + const endPage = rightBound < beforeLastPage ? rightBound : beforeLastPage |
| 49 | + |
| 50 | + pages = range(startPage, endPage) |
| 51 | + |
| 52 | + const singleSpillOffset = PAGES_TO_DISPLAY - pages.length - 1 |
| 53 | + const hasLeftOverflow = startPage > 2 |
| 54 | + const hasRightOverflow = endPage < beforeLastPage |
| 55 | + const leftOverflowPage = "left" |
| 56 | + const rightOverflowPage = "right" |
| 57 | + |
| 58 | + if (hasLeftOverflow && !hasRightOverflow) { |
| 59 | + const extraPages = range(startPage - singleSpillOffset, startPage - 1) |
| 60 | + pages = [leftOverflowPage, ...extraPages, ...pages] |
| 61 | + } else if (!hasLeftOverflow && hasRightOverflow) { |
| 62 | + const extraPages = range(endPage + 1, endPage + singleSpillOffset) |
| 63 | + pages = [...pages, ...extraPages, rightOverflowPage] |
| 64 | + } else if (hasLeftOverflow && hasRightOverflow) { |
| 65 | + pages = [leftOverflowPage, ...pages, rightOverflowPage] |
| 66 | + } |
| 67 | + |
| 68 | + return [1, ...pages, numPages] |
| 69 | + } |
| 70 | + |
| 71 | + return range(1, numPages) |
| 72 | +} |
| 73 | + |
| 74 | +export const PaginationWidget = ({ |
| 75 | + prevLabel, |
| 76 | + nextLabel, |
| 77 | + onPrevClick, |
| 78 | + onNextClick, |
| 79 | + onPageClick, |
| 80 | + numRecords, |
| 81 | + numRecordsPerPage = DEFAULT_RECORDS_PER_PAGE, |
| 82 | + activePage = 1, |
| 83 | + containerStyle, |
| 84 | +}: PaginationWidgetProps): JSX.Element | null => { |
| 85 | + const numPages = numRecords ? Math.ceil(numRecords / numRecordsPerPage) : 0 |
| 86 | + const firstPageActive = activePage === 1 && numPages !== 0 |
| 87 | + const lastPageActive = activePage === numPages && numPages !== 0 |
| 88 | + |
| 89 | + const styles = useStyles() |
| 90 | + |
| 91 | + // No need to display any pagination if we know the number of pages is 1 |
| 92 | + if (numPages === 1) { |
| 93 | + return null |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <div style={containerStyle} className={styles.defaultContainerStyles}> |
| 98 | + <Button |
| 99 | + className={styles.prevLabelStyles} |
| 100 | + aria-label="Previous page" |
| 101 | + disabled={firstPageActive} |
| 102 | + onClick={onPrevClick} |
| 103 | + > |
| 104 | + <KeyboardArrowLeft /> |
| 105 | + <div>{prevLabel}</div> |
| 106 | + </Button> |
| 107 | + {numPages > 0 && |
| 108 | + buildPagedList(numPages, activePage).map((page) => |
| 109 | + typeof page !== "number" ? ( |
| 110 | + <Button className={styles.pageButton} key={`Page${page}`} disabled> |
| 111 | + <div>...</div> |
| 112 | + </Button> |
| 113 | + ) : ( |
| 114 | + <Button |
| 115 | + className={ |
| 116 | + activePage === page |
| 117 | + ? `${styles.pageButton} ${styles.activePageButton}` |
| 118 | + : styles.pageButton |
| 119 | + } |
| 120 | + aria-label={`${page === activePage ? "Current Page" : ""} ${ |
| 121 | + page === numPages ? "Last Page" : "" |
| 122 | + } Page${page}`} |
| 123 | + name="Page button" |
| 124 | + key={`Page${page}`} |
| 125 | + onClick={() => onPageClick && onPageClick(page)} |
| 126 | + > |
| 127 | + <div>{page}</div> |
| 128 | + </Button> |
| 129 | + ), |
| 130 | + )} |
| 131 | + <Button aria-label="Next page" disabled={lastPageActive} onClick={onNextClick}> |
| 132 | + <div>{nextLabel}</div> |
| 133 | + <KeyboardArrowRight /> |
| 134 | + </Button> |
| 135 | + </div> |
| 136 | + ) |
| 137 | +} |
| 138 | + |
| 139 | +const useStyles = makeStyles((theme) => ({ |
| 140 | + defaultContainerStyles: { |
| 141 | + justifyContent: "center", |
| 142 | + alignItems: "center", |
| 143 | + display: "flex", |
| 144 | + flexDirection: "row", |
| 145 | + padding: "20px", |
| 146 | + }, |
| 147 | + |
| 148 | + prevLabelStyles: { |
| 149 | + marginRight: `${theme.spacing(0.5)}px`, |
| 150 | + }, |
| 151 | + |
| 152 | + pageButton: { |
| 153 | + "&:not(:last-of-type)": { |
| 154 | + marginRight: theme.spacing(0.5), |
| 155 | + }, |
| 156 | + }, |
| 157 | + |
| 158 | + activePageButton: { |
| 159 | + borderColor: `${theme.palette.info.main}`, |
| 160 | + backgroundColor: `${theme.palette.info.dark}`, |
| 161 | + }, |
| 162 | +})) |
0 commit comments