| 1 |
import React, { useState } from 'react' |
| 2 |
import classnames from 'classnames' |
| 3 |
import { __, _n, sprintf } from '@wordpress/i18n' |
| 4 |
import { updateQueryParams } from '../../../utils/urls' |
| 5 |
import { TablePaginationNavigation } from './TablePaginationNavigation' |
| 6 |
|
| 7 |
interface PaginationControlsProps { |
| 8 |
which: 'top' | 'bottom' |
| 9 |
inputValue: number |
| 10 |
totalPages: number |
| 11 |
totalItems: number |
| 12 |
currentPage: number |
| 13 |
disabled?: boolean |
| 14 |
setInputValue: (value: number) => void |
| 15 |
setCurrentPage: (page: number) => void |
| 16 |
pageSearchParam?: string |
| 17 |
} |
| 18 |
|
| 19 |
const PaginationControls: React.FC<PaginationControlsProps> = ({ |
| 20 |
which, |
| 21 |
disabled, |
| 22 |
totalPages, |
| 23 |
totalItems, |
| 24 |
inputValue, |
| 25 |
currentPage, |
| 26 |
pageSearchParam, |
| 27 |
setCurrentPage, |
| 28 |
setInputValue |
| 29 |
}) => { |
| 30 |
const navLabel = 'top' === which |
| 31 |
? __('Pagination, before the table', 'code-snippets') |
| 32 |
: __('Pagination, after the table', 'code-snippets') |
| 33 |
|
| 34 |
return ( |
| 35 |
<nav className="tablenav-pages-nav" aria-label={navLabel}> |
| 36 |
<form |
| 37 |
className={classnames('tablenav-pages', { |
| 38 |
'one-page': totalPages && 1 === totalPages, |
| 39 |
'no-pages': !totalPages |
| 40 |
})} |
| 41 |
onSubmit={event => { |
| 42 |
event.preventDefault() |
| 43 |
setCurrentPage(inputValue) |
| 44 |
}} |
| 45 |
> |
| 46 |
<span className="displaying-num"> |
| 47 |
{ |
| 48 |
sprintf( |
| 49 |
// translators: %s: Number of items. |
| 50 |
_n('%s item', '%s items', totalItems, 'code-snippets'), |
| 51 |
totalItems |
| 52 |
) |
| 53 |
} |
| 54 |
</span>{'\n'} |
| 55 |
<TablePaginationNavigation |
| 56 |
{...{ |
| 57 |
which, |
| 58 |
disabled, |
| 59 |
totalPages, |
| 60 |
inputValue, |
| 61 |
currentPage, |
| 62 |
pageSearchParam, |
| 63 |
setCurrentPage, |
| 64 |
setInputValue |
| 65 |
}} |
| 66 |
/> |
| 67 |
</form> |
| 68 |
</nav> |
| 69 |
) |
| 70 |
} |
| 71 |
|
| 72 |
export interface TablePaginationProps { |
| 73 |
which: 'top' | 'bottom' |
| 74 |
disabled?: boolean |
| 75 |
totalItems: number |
| 76 |
totalPages: number |
| 77 |
currentPage: number |
| 78 |
pageSearchParam?: string |
| 79 |
setCurrentPage: (page: number) => void |
| 80 |
} |
| 81 |
|
| 82 |
export const TablePagination: React.FC<TablePaginationProps> = ({ |
| 83 |
which, |
| 84 |
disabled, |
| 85 |
totalItems, |
| 86 |
totalPages, |
| 87 |
currentPage, |
| 88 |
setCurrentPage, |
| 89 |
pageSearchParam |
| 90 |
}) => { |
| 91 |
const [inputValue, setInputValue] = useState(currentPage) |
| 92 |
|
| 93 |
const setCurrentPageSafe = (page: number) => { |
| 94 |
if (page) { |
| 95 |
const validPage = Math.max(1, Math.min(page, totalPages)) |
| 96 |
setInputValue(validPage) |
| 97 |
setCurrentPage(validPage) |
| 98 |
|
| 99 |
if (pageSearchParam) { |
| 100 |
updateQueryParams({ [pageSearchParam]: 1 === validPage ? undefined : validPage }) |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return ( |
| 106 |
<PaginationControls |
| 107 |
{...{ which, inputValue, currentPage, totalItems, totalPages, disabled, setInputValue }} |
| 108 |
setCurrentPage={setCurrentPageSafe} |
| 109 |
/> |
| 110 |
) |
| 111 |
} |
| 112 |
|