import { useEffect, useRef } from 'react'; import { FormHandles } from '@unform/core'; import { HiOutlineDotsHorizontal } from 'react-icons/hi'; import { Flex } from '../Flex'; import { Form, Select } from '../Form'; import { IconButton } from '../IconButton'; import { Text } from '../Text'; import { PaginationItem } from './PaginationItem'; import { Container } from './styles'; type PaginationProps = { totalCount: number; currentPage: number; onPageChange: (page: number) => void; perPage: number; perPageChange?: (amount: number) => void; }; const siblingsCount = 1; function generatePagesArray(from: number, to: number): number[] { return [...new Array(to - from)] .map((_, index) => { return from + index + 1; }) .filter((page) => page > 0); } export function Pagination({ totalCount, currentPage, onPageChange, perPage, perPageChange, }: PaginationProps): JSX.Element { const formRef = useRef(null); useEffect(() => { if (formRef.current) { formRef.current.setFieldValue('perPage', perPage); } }, [perPage]); const lastPage = Math.ceil(totalCount / perPage); const previousPages = currentPage > 1 ? generatePagesArray(currentPage - 1 - siblingsCount, currentPage - 1) : []; const nextPages = currentPage < lastPage ? generatePagesArray( currentPage, Math.min(currentPage + siblingsCount, lastPage), ) : []; return ( {(currentPage - 1) * perPage} -{' '} {totalCount < currentPage * perPage ? totalCount : currentPage * perPage} {' '} de {totalCount} {!!perPageChange && (
undefined}>