Skip to content

Commit

Permalink
fix: fix overflow of pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Dec 2, 2023
1 parent 37c736d commit b267606
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/components/Pagination/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Link from 'next/link'
import styles from './index.module.scss'

const INDICATOR_COUNT = 5
const SIBLING_COUNT = (INDICATOR_COUNT - 1) / 2
const SIBLING_COUNT = Math.ceil((INDICATOR_COUNT - 1) / 2)

const Pagination: FC<{ pageCount: number }> = ({ pageCount }) => {
const { query, pathname } = useRouter()
Expand All @@ -14,14 +14,13 @@ const Pagination: FC<{ pageCount: number }> = ({ pageCount }) => {
const next = `${page + 1}`
const getHref = (p: string) => `${pathname}?${new URLSearchParams({ ...query, page: p }).toString()}`

let indicators: Array<number> = []
if (page <= SIBLING_COUNT) {
indicators = Array.from({ length: Math.min(pageCount, INDICATOR_COUNT) }, (_, idx) => idx + 1)
} else if (page >= pageCount - SIBLING_COUNT) {
indicators = Array.from({ length: INDICATOR_COUNT }, (_, idx) => idx + pageCount - INDICATOR_COUNT)
} else {
indicators = Array.from({ length: INDICATOR_COUNT }, (_, idx) => idx + page - 2)
}
const indicators = Array.from({ length: INDICATOR_COUNT })
.map((_, idx) => {
if (page <= SIBLING_COUNT) return idx + 1
if (page >= pageCount - SIBLING_COUNT) return pageCount - INDICATOR_COUNT + idx + 1
return page - SIBLING_COUNT + idx
})
.filter(i => i > 0 && i <= pageCount)

return (
<div className={styles.container}>
Expand Down

1 comment on commit b267606

@vercel
Copy link

@vercel vercel bot commented on b267606 Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.