From 6a18369abf298cf41463336a64bb899b21ac3563 Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Sun, 31 Mar 2024 15:46:39 -0700 Subject: [PATCH 1/2] Prettier pagination --- public/assets/icons/arrow-left.svg | 2 +- public/assets/icons/arrow-right.svg | 2 +- .../common/PaginationControls/index.tsx | 66 ++++++++----------- .../PaginationControls/style.module.scss | 47 +++++++++---- .../PaginationControls/style.module.scss.d.ts | 4 +- src/pages/events.tsx | 2 +- 6 files changed, 68 insertions(+), 55 deletions(-) diff --git a/public/assets/icons/arrow-left.svg b/public/assets/icons/arrow-left.svg index 52dc05c6..f8ff17a2 100644 --- a/public/assets/icons/arrow-left.svg +++ b/public/assets/icons/arrow-left.svg @@ -1,3 +1,3 @@ - + diff --git a/public/assets/icons/arrow-right.svg b/public/assets/icons/arrow-right.svg index b25599ae..820e9c21 100644 --- a/public/assets/icons/arrow-right.svg +++ b/public/assets/icons/arrow-right.svg @@ -1,3 +1,3 @@ - + diff --git a/src/components/common/PaginationControls/index.tsx b/src/components/common/PaginationControls/index.tsx index 3e0ca0bf..da2fe727 100644 --- a/src/components/common/PaginationControls/index.tsx +++ b/src/components/common/PaginationControls/index.tsx @@ -1,6 +1,6 @@ -import LeftArrowIcon from '@/public/assets/icons/page-left-icon.svg'; -import RightArrowIcon from '@/public/assets/icons/page-right-icon.svg'; -import { useEffect, useState } from 'react'; +import Typography from '@/components/common/Typography'; +import LeftArrowIcon from '@/public/assets/icons/arrow-left.svg'; +import RightArrowIcon from '@/public/assets/icons/arrow-right.svg'; import style from './style.module.scss'; interface PaginationControlsProps { @@ -9,12 +9,18 @@ interface PaginationControlsProps { pages: number; } -const PaginationControls = ({ page, onPage, pages }: PaginationControlsProps) => { - const [value, setValue] = useState(String(page + 1)); +function calculatePagesToDisplay(page: number, totalPages: number, maxWidth: number): number[] { + const radius = (maxWidth - 1) / 2; + if (page + radius >= totalPages) { + return Array.from({ length: maxWidth }, (_, key) => totalPages - maxWidth + 1 + key); + } + const displayPage = page + 1; + const start = Math.max(1, displayPage - radius); + return Array.from({ length: Math.min(maxWidth, totalPages) }, (_, key) => start + key); +} - useEffect(() => { - setValue(String(page + 1)); - }, [page]); +const PaginationControls = ({ page, onPage, pages }: PaginationControlsProps) => { + const pagesToDisplay = calculatePagesToDisplay(page, pages, 5); return (
@@ -27,35 +33,21 @@ const PaginationControls = ({ page, onPage, pages }: PaginationControlsProps) =>
- { - setValue(e.currentTarget.value); - const page = +e.currentTarget.value - 1; - if (Number.isInteger(page) && page >= 0 && page < pages) { - onPage(page); - } - }} - onBlur={e => { - // Clamp page number between 1 and pages - const inputPage = Math.min( - Math.max(Math.trunc(+e.currentTarget.value - 1), 0), - pages - 1 - ); - if (Number.isNaN(inputPage)) { - setValue(String(page + 1)); - } else { - onPage(inputPage); - setValue(String(inputPage + 1)); - } - }} - /> - of - {pages} + {pagesToDisplay.map(displayPage => ( + + ))}
- {pagesToDisplay.map(displayPage => ( - - ))} + { + setValue(e.currentTarget.value); + const page = +e.currentTarget.value - 1; + if (Number.isInteger(page) && page >= 0 && page < pages) { + onPage(page); + } + }} + onBlur={e => { + // Clamp page number between 1 and pages + const inputPage = Math.min( + Math.max(Math.trunc(+e.currentTarget.value - 1), 0), + pages - 1 + ); + if (Number.isNaN(inputPage)) { + setValue(String(page + 1)); + } else { + onPage(inputPage); + setValue(String(inputPage + 1)); + } + }} + /> + / + {pages}