import React, { useEffect, useState } from 'react'; import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/20/solid'; import usePagination, { DOTS } from '../../hooks/usePagination'; export interface PaginationProps { hasBorder?: boolean; hasSpacing?: boolean; totalCount: number; siblingCount: number; pageSize: number; callback: any; } export const Pagination = ({ hasBorder, hasSpacing, totalCount, siblingCount, pageSize, callback, }: PaginationProps) => { const [currentPage, setCurrentPage] = useState(1); const paginationRange = usePagination( totalCount, pageSize, siblingCount, currentPage ); const onNext = () => { if (currentPage < lastPage) { setCurrentPage(currentPage + 1); } }; const onPrevious = () => { if (currentPage > 1) { setCurrentPage(currentPage - 1); } }; const onCurrent = (pageNumber: any) => { setCurrentPage(Number(pageNumber)); }; const styles = { border: `py-5 border-t border-cu-black-100`, spacing: `pl-6 pr-5`, mobileButtons: `relative inline-flex items-center px-4 py-2 text-sm font-medium text-cu-black-600 bg-white border border-cu-black-200 rounded-md hover:bg-cu-black-50`, pageListNumbers: `flex items-center bg-white py-2 px-4 text-sm text-cu-black-600 border-r border-cu-black-100 last:border-0 cursor-pointer hover:bg-cu-black-50`, pageListArrows: `text-cu-black-400 px-2.5`, }; const borderStyles = hasBorder ? styles.border : ''; const spacingStyles = hasSpacing ? styles.spacing : ''; const startResult = (currentPage - 1) * pageSize + 1; let endResult = totalCount; let lastPage = 0; useEffect(() => { callback([startResult, endResult]); }, [startResult, endResult, callback]); if (totalCount > currentPage * pageSize) { endResult = currentPage * pageSize; } if (paginationRange !== undefined) { lastPage = Number(paginationRange[paginationRange.length - 1]); } if ( currentPage === 0 || (paginationRange !== undefined && paginationRange.length < 2) ) { return null; } return (
Showing {startResult} to{' '} {endResult} of{' '} {totalCount} results