|
| 1 | +import { |
| 2 | + Box, |
| 3 | + Typography, |
| 4 | + IconButton, |
| 5 | + Select, |
| 6 | + MenuItem, |
| 7 | + SelectChangeEvent, |
| 8 | +} from "@mui/material"; |
| 9 | +import KeyboardArrowLeft from "@mui/icons-material/KeyboardArrowLeft"; |
| 10 | +import KeyboardArrowRight from "@mui/icons-material/KeyboardArrowRight"; |
| 11 | +import { FC } from "react"; |
| 12 | + |
| 13 | +type Props = { |
| 14 | + page: number; |
| 15 | + total: number; |
| 16 | + pageSize: number; |
| 17 | + onPageChange: (nextPage: number) => void; |
| 18 | + onPageSizeChange: (nextRpp: number) => void; |
| 19 | + pageSizeOptions?: number[]; |
| 20 | +}; |
| 21 | + |
| 22 | +export const PaginationFooter: FC<Props> = ({ |
| 23 | + page, |
| 24 | + total, |
| 25 | + pageSize, |
| 26 | + onPageChange, |
| 27 | + onPageSizeChange, |
| 28 | + pageSizeOptions = [5, 10, 25, 50], |
| 29 | +}) => { |
| 30 | + const pageCount = Math.max(1, Math.ceil((total || 0) / (pageSize || 1))); |
| 31 | + const clampedPage = Math.min(Math.max(page, 1), pageCount); |
| 32 | + |
| 33 | + const start = total === 0 ? 0 : (clampedPage - 1) * pageSize + 1; |
| 34 | + const end = total === 0 ? 0 : Math.min(clampedPage * pageSize, total); |
| 35 | + |
| 36 | + const handlePrev = () => onPageChange(Math.max(clampedPage - 1, 1)); |
| 37 | + const handleNext = () => onPageChange(Math.min(clampedPage + 1, pageCount)); |
| 38 | + |
| 39 | + const handlePageSizeChange = (e: SelectChangeEvent<number>) => { |
| 40 | + const next = Number(e.target.value); |
| 41 | + onPageSizeChange(next); |
| 42 | + |
| 43 | + const nextPageCount = Math.max(1, Math.ceil((total || 0) / next)); |
| 44 | + if (clampedPage > nextPageCount) { |
| 45 | + onPageChange(nextPageCount); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <Box |
| 51 | + sx={{ |
| 52 | + display: "flex", |
| 53 | + alignItems: "center", |
| 54 | + justifyContent: "flex-end", |
| 55 | + gap: 2, |
| 56 | + px: 2, |
| 57 | + py: 1, |
| 58 | + color: "#3B485C", |
| 59 | + }} |
| 60 | + > |
| 61 | + <Box sx={{ display: "inline-flex", alignItems: "baseline", gap: 1.25 }}> |
| 62 | + <Typography variant="body2" sx={{ fontWeight: 500, color: "#3B485C" }}> |
| 63 | + Rows per page: |
| 64 | + </Typography> |
| 65 | + |
| 66 | + <Select |
| 67 | + value={pageSize} |
| 68 | + onChange={handlePageSizeChange} |
| 69 | + variant="standard" |
| 70 | + disableUnderline |
| 71 | + sx={{ |
| 72 | + verticalAlign: "baseline", |
| 73 | + "& .MuiSelect-select": { |
| 74 | + py: 0, |
| 75 | + lineHeight: 1.5, |
| 76 | + display: "inline-flex", |
| 77 | + alignItems: "center", |
| 78 | + }, |
| 79 | + "& .MuiSelect-icon": { |
| 80 | + top: "calc(50% - 12px)", |
| 81 | + }, |
| 82 | + }} |
| 83 | + renderValue={(val) => ( |
| 84 | + <Box |
| 85 | + component="span" |
| 86 | + sx={{ fontWeight: 500, color: "#3B485C", fontSize: 15 }} |
| 87 | + > |
| 88 | + {val as number} |
| 89 | + </Box> |
| 90 | + )} |
| 91 | + MenuProps={{ |
| 92 | + PaperProps: { sx: { mt: 1, borderRadius: 1.5 } }, |
| 93 | + MenuListProps: { dense: true }, |
| 94 | + }} |
| 95 | + > |
| 96 | + {pageSizeOptions.map((n) => ( |
| 97 | + <MenuItem key={n} value={n}> |
| 98 | + {n} |
| 99 | + </MenuItem> |
| 100 | + ))} |
| 101 | + </Select> |
| 102 | + </Box> |
| 103 | + |
| 104 | + <Typography variant="body2" sx={{ minWidth: 110, textAlign: "center" }}> |
| 105 | + {start}-{end} of {total} |
| 106 | + </Typography> |
| 107 | + |
| 108 | + <Box sx={{ display: "flex", alignItems: "center", gap: 0.5 }}> |
| 109 | + <IconButton |
| 110 | + size="small" |
| 111 | + onClick={handlePrev} |
| 112 | + disabled={clampedPage <= 1 || total === 0} |
| 113 | + aria-label="Previous page" |
| 114 | + > |
| 115 | + <KeyboardArrowLeft /> |
| 116 | + </IconButton> |
| 117 | + |
| 118 | + <Typography |
| 119 | + variant="body2" |
| 120 | + sx={{ width: 24, textAlign: "center" }} |
| 121 | + aria-label="Current page" |
| 122 | + > |
| 123 | + {clampedPage} |
| 124 | + </Typography> |
| 125 | + |
| 126 | + <IconButton |
| 127 | + size="small" |
| 128 | + onClick={handleNext} |
| 129 | + disabled={clampedPage >= pageCount || total === 0} |
| 130 | + aria-label="Next page" |
| 131 | + > |
| 132 | + <KeyboardArrowRight /> |
| 133 | + </IconButton> |
| 134 | + </Box> |
| 135 | + </Box> |
| 136 | + ); |
| 137 | +}; |
0 commit comments