import { ChevronLeftIcon, ChevronRightIcon } from '@chakra-ui/icons' import { Button, Flex, HStack, Input, Text } from '@chakra-ui/react' import React, { useEffect, useRef, useState } from 'react' import { LIMIT_PAGE_OPTIONS, PAGE_CHANGE_TYPE } from '../../../constants' interface IPaginationProps { onPageChange?: any total?: number pagination: any onLimitPerPageChange?: any isShowLimitList?: boolean } const getPageValue = (page, totalPage) => { if (page >= totalPage) { return totalPage } if (page <= 1) { return 1 } return page } const Pagination: React.FunctionComponent = (props) => { const { total = 0, onPageChange = () => {}, pagination, onLimitPerPageChange, isShowLimitList = false } = props const { limit = 12, page: defaultCurrent = 1 } = pagination const [limitPerPage, setLimitPerPage] = useState(LIMIT_PAGE_OPTIONS[0]) const totalPage = Math.ceil(total / limit) const page = getPageValue(defaultCurrent, totalPage) const inputRef = useRef(null) const handleOnChangePage = (e) => { let value = inputRef.current.value let key = e.keyCode if (!value) { return } if (value > totalPage) { value = totalPage } if (value < 1) { value = 1 } if (key === 13) { onPageChange(value) } } const handleOnChangeListingsPerPage = (selectedOption) => { const { value: newLimit } = selectedOption const newTotalPage = Math.ceil(total / newLimit) const newPage = page > newTotalPage ? newTotalPage : page inputRef.current.value = newPage onLimitPerPageChange(newPage, newLimit) setLimitPerPage(selectedOption) } const handleOnClickChangePage = (direction: PAGE_CHANGE_TYPE) => { const newPage = getPageValue(page + direction, totalPage) onPageChange(newPage) } useEffect(() => { if (inputRef.current) { inputRef.current.value = page } }, [page]) if (total === 0) { return null } return ( {/* {isShowLimitList && ( handleOnChangeListingsPerPage(selectedOption)} /> )} */} Page of {totalPage} ) } export default Pagination