forked from woocommerce/woocommerce-blocks
/
index.js
231 lines (223 loc) · 5.79 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Label } from '@woocommerce/blocks-checkout';
/**
* Internal dependencies
*/
import { getIndexes } from './utils.js';
import './style.scss';
const Pagination = ( {
currentPage,
displayFirstAndLastPages,
displayNextAndPreviousArrows,
pagesToDisplay,
onPageChange,
totalPages,
} ) => {
let { minIndex, maxIndex } = getIndexes(
pagesToDisplay,
currentPage,
totalPages
);
const showFirstPage = displayFirstAndLastPages && Boolean( minIndex !== 1 );
const showLastPage =
displayFirstAndLastPages && Boolean( maxIndex !== totalPages );
const showFirstPageEllipsis =
displayFirstAndLastPages && Boolean( minIndex > 3 );
const showLastPageEllipsis =
displayFirstAndLastPages && Boolean( maxIndex < totalPages - 2 );
// Handle the cases where there would be an ellipsis replacing one single page
if ( showFirstPage && minIndex === 3 ) {
minIndex = minIndex - 1;
}
if ( showLastPage && maxIndex === totalPages - 2 ) {
maxIndex = maxIndex + 1;
}
const pages = [];
if ( minIndex && maxIndex ) {
for ( let i = minIndex; i <= maxIndex; i++ ) {
pages.push( i );
}
}
return (
<div className="wc-block-pagination wc-block-components-pagination">
<Label
screenReaderLabel={ __(
'Navigate to another page',
'woo-gutenberg-products-block'
) }
/>
{ displayNextAndPreviousArrows && (
<button
className="wc-block-pagination-page wc-block-components-pagination__page"
onClick={ () => onPageChange( currentPage - 1 ) }
title={ __(
'Previous page',
'woo-gutenberg-products-block'
) }
disabled={ currentPage <= 1 }
>
<Label
label="<"
screenReaderLabel={ __(
'Previous page',
'woo-gutenberg-products-block'
) }
/>
</button>
) }
{ showFirstPage && (
<button
className={ classNames(
'wc-block-pagination-page',
'wc-block-components-pagination__page',
{
'wc-block-pagination-page--active':
currentPage === 1,
'wc-block-components-pagination__page--active':
currentPage === 1,
}
) }
onClick={ () => onPageChange( 1 ) }
disabled={ currentPage === 1 }
>
<Label
label={ 1 }
screenReaderLabel={ sprintf(
/* translators: %d is the page number (1, 2, 3...). */
__( 'Page %d', 'woo-gutenberg-products-block' ),
1
) }
/>
</button>
) }
{ showFirstPageEllipsis && (
<span
className="wc-block-pagination-ellipsis wc-block-components-pagination__ellipsis"
aria-hidden="true"
>
{ __( '…', 'woo-gutenberg-products-block' ) }
</span>
) }
{ pages.map( ( page ) => {
return (
<button
key={ page }
className={ classNames(
'wc-block-pagination-page',
'wc-block-components-pagination__page',
{
'wc-block-pagination-page--active':
currentPage === page,
'wc-block-components-pagination__page--active':
currentPage === page,
}
) }
onClick={
currentPage === page
? null
: () => onPageChange( page )
}
disabled={ currentPage === page }
>
<Label
label={ page }
screenReaderLabel={ sprintf(
/* translators: %d is the page number (1, 2, 3...). */
__( 'Page %d', 'woo-gutenberg-products-block' ),
page
) }
/>
</button>
);
} ) }
{ showLastPageEllipsis && (
<span
className="wc-block-pagination-ellipsis wc-block-components-pagination__ellipsis"
aria-hidden="true"
>
{ __( '…', 'woo-gutenberg-products-block' ) }
</span>
) }
{ showLastPage && (
<button
className={ classNames(
'wc-block-pagination-page',
'wc-block-components-pagination__page',
{
'wc-block-pagination-page--active':
currentPage === totalPages,
'wc-block-components-pagination__page--active':
currentPage === totalPages,
}
) }
onClick={ () => onPageChange( totalPages ) }
disabled={ currentPage === totalPages }
>
<Label
label={ totalPages }
screenReaderLabel={ sprintf(
/* translators: %d is the page number (1, 2, 3...). */
__( 'Page %d', 'woo-gutenberg-products-block' ),
totalPages
) }
/>
</button>
) }
{ displayNextAndPreviousArrows && (
<button
className="wc-block-pagination-page wc-block-components-pagination__page"
onClick={ () => onPageChange( currentPage + 1 ) }
title={ __( 'Next page', 'woo-gutenberg-products-block' ) }
disabled={ currentPage >= totalPages }
>
<Label
label=">"
screenReaderLabel={ __(
'Next page',
'woo-gutenberg-products-block'
) }
/>
</button>
) }
</div>
);
};
Pagination.propTypes = {
/**
* Number of the page currently being displayed.
*/
currentPage: PropTypes.number.isRequired,
/**
* Total number of pages.
*/
totalPages: PropTypes.number.isRequired,
/**
* Displays first and last pages if they are not in the current range of pages displayed.
*/
displayFirstAndLastPages: PropTypes.bool,
/**
* Displays arrows to navigate to the previous and next pages.
*/
displayNextAndPreviousArrows: PropTypes.bool,
/**
* Callback function called when the user triggers a page change.
*/
onPageChange: PropTypes.func,
/**
* Number of pages to display at the same time, including the active page
* and the pages displayed before and after it. It doesn't include the first
* and last pages.
*/
pagesToDisplay: PropTypes.number,
};
Pagination.defaultProps = {
displayFirstAndLastPages: true,
displayNextAndPreviousArrows: true,
pagesToDisplay: 3,
};
export default Pagination;