Skip to content
This repository was archived by the owner on Apr 25, 2022. It is now read-only.

Commit 2815f5f

Browse files
committed
Improve paging
1 parent c3b5121 commit 2815f5f

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/components/Pagination.module.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
font-size: 1.2rem;
1212
align-items: center;
1313

14+
div,
1415
a:not(:last-child) {
1516
&.pageLink {
1617
border-right: 1px solid var(--border);
@@ -33,7 +34,7 @@
3334
transition: 0.5s;
3435
line-height: 42px;
3536

36-
&:hover {
37+
&:hover:not(.ellipsis) {
3738
cursor: pointer;
3839
background-color: var(--main-color);
3940
text-decoration: none;

src/components/Pagination.tsx

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,39 @@ const Pagination: FunctionComponent<Props> = ({ currentPage, pages }) => {
1414
return <></>
1515
}
1616

17+
1718
return (
1819
<div className={styles.pagination}>
19-
{pages.map((p, index) => {
20-
const isActive = p === currentPage
21-
const className = isActive ? `${styles.pageActive}` : ''
20+
{pages
21+
.filter(page => (currentPage - 3 < page && currentPage + 3 > page)
22+
|| pages[0] === page
23+
|| pages.length === page)
24+
.map((curr, index, array) => {
25+
const isActive = curr === currentPage
26+
const className = isActive ? `${styles.pageActive}` : ''
2227

23-
return (
24-
<a
25-
onClick={() => {
26-
router.push({
27-
pathname: router.pathname,
28-
query: {
29-
...router.query,
30-
page: p.toString(),
31-
},
32-
})
33-
}}
34-
key={`pagination-${index}`}
35-
className={`${className} ${styles.pageLink}`}
36-
>
37-
{p}
38-
</a>
39-
)
40-
})}
41-
</div>
28+
return (
29+
<>
30+
{index > 0 && array[index - 1] + 1 !== curr && <div className={`${styles.pageLink} ${styles.ellipsis}`}>...</div>}
31+
<a
32+
onClick={() => {
33+
router.push({
34+
pathname: router.pathname,
35+
query: {
36+
...router.query,
37+
page: curr.toString(),
38+
},
39+
})
40+
}}
41+
key={`pagination-${index}`}
42+
className={`${className} ${styles.pageLink}`}
43+
>
44+
{curr}
45+
</a>
46+
</>
47+
)
48+
})}
49+
</div >
4250
)
4351
}
4452
export default Pagination

0 commit comments

Comments
 (0)