2019-09-03 14:41:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-01-30 10:04:39 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2019-09-03 14:41:05 +00:00
|
|
|
import classNames from 'classnames';
|
2021-07-02 09:24:07 +00:00
|
|
|
import Label from '@woocommerce/base-components/label';
|
2019-09-03 14:41:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-02-07 09:34:30 +00:00
|
|
|
import { getIndexes } from './utils';
|
2019-09-03 14:41:05 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2022-02-07 09:34:30 +00:00
|
|
|
interface PaginationProps {
|
|
|
|
/**
|
|
|
|
* Number of the page currently being displayed.
|
|
|
|
*/
|
|
|
|
currentPage: number;
|
|
|
|
/**
|
|
|
|
* Total number of pages.
|
|
|
|
*/
|
|
|
|
totalPages: number;
|
|
|
|
/**
|
|
|
|
* Displays first and last pages if they are not in the current range of pages displayed.
|
|
|
|
*/
|
|
|
|
displayFirstAndLastPages?: boolean;
|
|
|
|
/**
|
|
|
|
* Displays arrows to navigate to the previous and next pages.
|
|
|
|
*/
|
|
|
|
displayNextAndPreviousArrows?: boolean;
|
|
|
|
/**
|
|
|
|
* Callback function called when the user triggers a page change.
|
|
|
|
*/
|
|
|
|
onPageChange: ( currentPage: number ) => void;
|
|
|
|
/**
|
|
|
|
* 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?: number;
|
|
|
|
}
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
const Pagination = ( {
|
|
|
|
currentPage,
|
2022-02-07 09:34:30 +00:00
|
|
|
displayFirstAndLastPages = true,
|
|
|
|
displayNextAndPreviousArrows = true,
|
|
|
|
pagesToDisplay = 3,
|
2019-09-05 15:09:31 +00:00
|
|
|
onPageChange,
|
|
|
|
totalPages,
|
2022-02-07 09:34:30 +00:00
|
|
|
}: PaginationProps ): JSX.Element => {
|
2019-10-28 13:53:09 +00:00
|
|
|
let { minIndex, maxIndex } = getIndexes(
|
2019-09-05 15:09:31 +00:00
|
|
|
pagesToDisplay,
|
|
|
|
currentPage,
|
|
|
|
totalPages
|
|
|
|
);
|
2022-02-07 09:34:30 +00:00
|
|
|
|
2019-09-03 14:41:05 +00:00
|
|
|
const showFirstPage = displayFirstAndLastPages && Boolean( minIndex !== 1 );
|
2019-09-05 15:09:31 +00:00
|
|
|
const showLastPage =
|
|
|
|
displayFirstAndLastPages && Boolean( maxIndex !== totalPages );
|
|
|
|
const showFirstPageEllipsis =
|
2022-02-07 09:34:30 +00:00
|
|
|
displayFirstAndLastPages && Boolean( minIndex && minIndex > 3 );
|
2019-09-05 15:09:31 +00:00
|
|
|
const showLastPageEllipsis =
|
2022-02-07 09:34:30 +00:00
|
|
|
displayFirstAndLastPages &&
|
|
|
|
Boolean( maxIndex && maxIndex < totalPages - 2 );
|
2019-10-28 13:53:09 +00:00
|
|
|
|
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 14:41:05 +00:00
|
|
|
|
|
|
|
return (
|
2020-06-17 09:53:42 +00:00
|
|
|
<div className="wc-block-pagination wc-block-components-pagination">
|
2019-09-03 14:41:05 +00:00
|
|
|
<Label
|
2019-09-05 15:09:31 +00:00
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Navigate to another page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-09-03 14:41:05 +00:00
|
|
|
/>
|
2019-10-28 13:53:09 +00:00
|
|
|
{ displayNextAndPreviousArrows && (
|
2019-09-03 14:41:05 +00:00
|
|
|
<button
|
2021-08-02 11:36:46 +00:00
|
|
|
className="wc-block-pagination-page wc-block-components-pagination__page wc-block-components-pagination-page--arrow"
|
2019-09-03 14:41:05 +00:00
|
|
|
onClick={ () => onPageChange( currentPage - 1 ) }
|
2019-09-09 10:52:48 +00:00
|
|
|
title={ __(
|
|
|
|
'Previous page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage <= 1 }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
|
|
|
<Label
|
2021-07-12 11:32:45 +00:00
|
|
|
label="←"
|
2019-09-05 15:09:31 +00:00
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Previous page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-09-03 14:41:05 +00:00
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
) }
|
|
|
|
{ showFirstPage && (
|
|
|
|
<button
|
2020-06-17 09:53:42 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
) }
|
2019-09-03 14:41:05 +00:00
|
|
|
onClick={ () => onPageChange( 1 ) }
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage === 1 }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
2020-01-30 10:04:39 +00:00
|
|
|
<Label
|
2022-02-07 09:34:30 +00:00
|
|
|
label={ '1' }
|
2020-01-30 10:04:39 +00:00
|
|
|
screenReaderLabel={ sprintf(
|
|
|
|
/* translators: %d is the page number (1, 2, 3...). */
|
|
|
|
__( 'Page %d', 'woo-gutenberg-products-block' ),
|
|
|
|
1
|
|
|
|
) }
|
|
|
|
/>
|
2019-09-03 14:41:05 +00:00
|
|
|
</button>
|
|
|
|
) }
|
|
|
|
{ showFirstPageEllipsis && (
|
2019-09-09 10:52:48 +00:00
|
|
|
<span
|
2020-06-17 09:53:42 +00:00
|
|
|
className="wc-block-pagination-ellipsis wc-block-components-pagination__ellipsis"
|
2019-09-09 10:52:48 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
>
|
2019-09-03 14:41:05 +00:00
|
|
|
{ __( '…', 'woo-gutenberg-products-block' ) }
|
|
|
|
</span>
|
|
|
|
) }
|
|
|
|
{ pages.map( ( page ) => {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
key={ page }
|
2020-06-17 09:53:42 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
) }
|
2019-09-09 10:52:48 +00:00
|
|
|
onClick={
|
|
|
|
currentPage === page
|
2022-02-07 09:34:30 +00:00
|
|
|
? undefined
|
2019-09-09 10:52:48 +00:00
|
|
|
: () => onPageChange( page )
|
|
|
|
}
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage === page }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
2020-01-30 10:04:39 +00:00
|
|
|
<Label
|
2022-02-07 09:34:30 +00:00
|
|
|
label={ page.toString() }
|
2020-01-30 10:04:39 +00:00
|
|
|
screenReaderLabel={ sprintf(
|
|
|
|
/* translators: %d is the page number (1, 2, 3...). */
|
|
|
|
__( 'Page %d', 'woo-gutenberg-products-block' ),
|
|
|
|
page
|
|
|
|
) }
|
|
|
|
/>
|
2019-09-03 14:41:05 +00:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
{ showLastPageEllipsis && (
|
2019-09-09 10:52:48 +00:00
|
|
|
<span
|
2020-06-17 09:53:42 +00:00
|
|
|
className="wc-block-pagination-ellipsis wc-block-components-pagination__ellipsis"
|
2019-09-09 10:52:48 +00:00
|
|
|
aria-hidden="true"
|
|
|
|
>
|
2019-09-03 14:41:05 +00:00
|
|
|
{ __( '…', 'woo-gutenberg-products-block' ) }
|
|
|
|
</span>
|
|
|
|
) }
|
|
|
|
{ showLastPage && (
|
|
|
|
<button
|
2020-06-17 09:53:42 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
) }
|
2019-09-03 14:41:05 +00:00
|
|
|
onClick={ () => onPageChange( totalPages ) }
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage === totalPages }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
2020-01-30 10:04:39 +00:00
|
|
|
<Label
|
2022-02-07 09:34:30 +00:00
|
|
|
label={ totalPages.toString() }
|
2020-01-30 10:04:39 +00:00
|
|
|
screenReaderLabel={ sprintf(
|
|
|
|
/* translators: %d is the page number (1, 2, 3...). */
|
|
|
|
__( 'Page %d', 'woo-gutenberg-products-block' ),
|
|
|
|
totalPages
|
|
|
|
) }
|
|
|
|
/>
|
2019-09-03 14:41:05 +00:00
|
|
|
</button>
|
|
|
|
) }
|
2019-10-28 13:53:09 +00:00
|
|
|
{ displayNextAndPreviousArrows && (
|
2019-09-03 14:41:05 +00:00
|
|
|
<button
|
2021-08-02 11:36:46 +00:00
|
|
|
className="wc-block-pagination-page wc-block-components-pagination__page wc-block-components-pagination-page--arrow"
|
2019-09-03 14:41:05 +00:00
|
|
|
onClick={ () => onPageChange( currentPage + 1 ) }
|
|
|
|
title={ __( 'Next page', 'woo-gutenberg-products-block' ) }
|
2019-10-28 13:53:09 +00:00
|
|
|
disabled={ currentPage >= totalPages }
|
2019-09-03 14:41:05 +00:00
|
|
|
>
|
|
|
|
<Label
|
2021-07-12 11:32:45 +00:00
|
|
|
label="→"
|
2019-09-05 15:09:31 +00:00
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Next page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-09-03 14:41:05 +00:00
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Pagination;
|