2022-07-21 05:45:53 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
|
2022-07-10 23:41:33 +00:00
|
|
|
export function Pagination(props: {
|
|
|
|
page: number
|
|
|
|
itemsPerPage: number
|
|
|
|
totalItems: number
|
|
|
|
setPage: (page: number) => void
|
2022-07-10 23:50:59 +00:00
|
|
|
scrollToTop?: boolean
|
2022-07-21 05:45:53 +00:00
|
|
|
className?: string
|
2022-07-19 20:16:20 +00:00
|
|
|
nextTitle?: string
|
|
|
|
prevTitle?: string
|
2022-07-10 23:41:33 +00:00
|
|
|
}) {
|
2022-07-19 20:16:20 +00:00
|
|
|
const {
|
|
|
|
page,
|
|
|
|
itemsPerPage,
|
|
|
|
totalItems,
|
|
|
|
setPage,
|
|
|
|
scrollToTop,
|
|
|
|
nextTitle,
|
|
|
|
prevTitle,
|
2022-07-21 05:45:53 +00:00
|
|
|
className,
|
2022-07-19 20:16:20 +00:00
|
|
|
} = props
|
2022-07-10 23:41:33 +00:00
|
|
|
|
2022-07-13 20:15:03 +00:00
|
|
|
const maxPage = Math.ceil(totalItems / itemsPerPage) - 1
|
|
|
|
|
2022-07-10 23:41:33 +00:00
|
|
|
return (
|
|
|
|
<nav
|
2022-07-21 05:45:53 +00:00
|
|
|
className={clsx(
|
|
|
|
'flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6',
|
|
|
|
className
|
|
|
|
)}
|
2022-07-10 23:41:33 +00:00
|
|
|
aria-label="Pagination"
|
|
|
|
>
|
|
|
|
<div className="hidden sm:block">
|
|
|
|
<p className="text-sm text-gray-700">
|
2022-07-13 20:15:03 +00:00
|
|
|
Showing <span className="font-medium">{page * itemsPerPage + 1}</span>{' '}
|
|
|
|
to{' '}
|
2022-07-10 23:41:33 +00:00
|
|
|
<span className="font-medium">
|
2022-07-13 20:15:03 +00:00
|
|
|
{Math.min(totalItems, (page + 1) * itemsPerPage)}
|
2022-07-10 23:41:33 +00:00
|
|
|
</span>{' '}
|
2022-07-13 20:15:03 +00:00
|
|
|
of <span className="font-medium">{totalItems}</span> results
|
2022-07-10 23:41:33 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-1 justify-between sm:justify-end">
|
2022-07-19 20:16:20 +00:00
|
|
|
{page > 0 && (
|
|
|
|
<a
|
|
|
|
href={scrollToTop ? '#' : undefined}
|
|
|
|
className="relative inline-flex cursor-pointer select-none items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
|
|
|
|
onClick={() => page > 0 && setPage(page - 1)}
|
|
|
|
>
|
|
|
|
{prevTitle ?? 'Previous'}
|
|
|
|
</a>
|
|
|
|
)}
|
2022-07-10 23:41:33 +00:00
|
|
|
<a
|
2022-07-10 23:50:59 +00:00
|
|
|
href={scrollToTop ? '#' : undefined}
|
2022-07-13 20:15:03 +00:00
|
|
|
className="relative ml-3 inline-flex cursor-pointer select-none items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
|
|
|
|
onClick={() => page < maxPage && setPage(page + 1)}
|
2022-07-10 23:41:33 +00:00
|
|
|
>
|
2022-07-19 20:16:20 +00:00
|
|
|
{nextTitle ?? 'Next'}
|
2022-07-10 23:41:33 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
)
|
|
|
|
}
|