-
Notifications
You must be signed in to change notification settings - Fork 21
/
pagination.js
140 lines (123 loc) · 3.81 KB
/
pagination.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React from 'react';
import PropTypes from 'prop-types';
import navigateBefore from 'uswds/img/usa-icons/navigate_before.svg';
import navigateNext from 'uswds/img/usa-icons/navigate_next.svg';
import { Link } from 'gatsby';
function PageLink({ current, pageNumber, path }) {
const isCurrent = current ? 'usa-current' : '';
const normalizedPath = path.endsWith('/')
? path.substring(0, path.length - 1)
: path;
const href =
pageNumber === 1 ? normalizedPath : `${normalizedPath}/${pageNumber}`;
return (
<li className="usa-pagination__item usa-pagination__page-no">
<Link
to={href}
className={`usa-pagination__button ${isCurrent}`}
aria-label={`Page ${pageNumber}`}
>
{pageNumber}
</Link>
</li>
);
}
PageLink.propTypes = {
current: PropTypes.bool.isRequired,
pageNumber: PropTypes.number.isRequired,
path: PropTypes.string.isRequired,
};
function listCenterPages(currentPage, totalPages) {
const listOfPages = [];
for (let x = currentPage - 2; x <= currentPage + 2; x++) {
if (x <= 1) continue;
if (x === totalPages) break;
listOfPages.push(x);
}
return listOfPages;
}
function Pagination({ collectionPath, pageContext }) {
const { humanPageNumber, nextPagePath, numberOfPages, previousPagePath } =
pageContext;
const pagesFromStart = humanPageNumber - 1;
const pagesToEnd = numberOfPages - humanPageNumber;
return (
<nav aria-label="Pagination" className="usa-pagination">
<ul className="usa-pagination__list">
{previousPagePath && (
<li className="usa-pagination__item usa-pagination__arrow">
<Link
to={previousPagePath}
className="usa-pagination__link usa-pagination__previous-page"
aria-label="Previous page"
>
<img
className="usa-icon"
role="img"
src={navigateBefore}
alt="Navigate previous icon"
/>
<span className="usa-pagination__link-text"> Previous </span>
</Link>
</li>
)}
<PageLink
current={humanPageNumber === 1}
pageNumber={1}
path={collectionPath}
/>
{pagesFromStart > 3 && (
<li
className="usa-pagination__item usa-pagination__overflow"
role="presentation"
>
<span> … </span>
</li>
)}
{listCenterPages(humanPageNumber, numberOfPages).map((pageNumber) => (
<PageLink
key={`page-button-${pageNumber}`}
current={humanPageNumber === pageNumber}
pageNumber={pageNumber}
path={collectionPath}
/>
))}
{pagesToEnd > 3 && (
<li
className="usa-pagination__item usa-pagination__overflow"
role="presentation"
>
<span> … </span>
</li>
)}
<PageLink
current={humanPageNumber === numberOfPages}
pageNumber={numberOfPages}
path={collectionPath}
/>
{nextPagePath && (
<li className="usa-pagination__item usa-pagination__arrow">
<Link
to={nextPagePath}
className="usa-pagination__link usa-pagination__next-page"
aria-label="Next page"
>
<span className="usa-pagination__link-text"> Next </span>
<img
className="usa-icon"
role="img"
src={navigateNext}
alt="Navigate next icon"
/>
</Link>
</li>
)}
</ul>
</nav>
);
}
Pagination.propTypes = {
collectionPath: PropTypes.string.isRequired,
pageContext: PropTypes.object.isRequired,
};
export default Pagination;