import React, { useRef } from 'react' import { Icon } from '../../Icons/Icon' const PaginationNavComponent = ({ currentPage = 1, totalPages = 1, onPageChange, }: PaginationNavProps) => { const inputEl = useRef(null) const maxTotalPages = Math.max(currentPage, totalPages) const hasNext = currentPage < totalPages const hasPrev = currentPage > 1 const handleChange = (page: number) => { page = page < 1 ? 1 : page > maxTotalPages ? maxTotalPages : page onPageChange && onPageChange(page) } const onPreventDefault = (e: any) => { e.preventDefault() return false } const handleKeyDown = (e: any) => { if (e.which >= 35 && e.which <= 39) { return } if (e.which === 46 || e.which === 8) { return } if (e.which === 13) { const currentValue = inputEl?.current?.innerText if (currentValue) { const page = parseInt(currentValue) handleChange(page) } e.preventDefault() return } if (isNaN(parseInt(String.fromCharCode(e.which)))) { e.preventDefault() } return } return (
{currentPage} {' '} /{' '}
) } export const PaginationNav = PaginationNavComponent export interface PaginationNavProps { /** Define selected page. * @default 1 * */ currentPage?: number /** Define total count of pages. * @default 1 * */ totalPages?: number /** * Function to run when page is changed. * */ onPageChange?: (arg0: number) => any }