import React from 'react'; import { SvgIcon, Typography } from '@mui/material'; import { IconColor } from '../types/style.types'; import { TableContext } from '../types/table.type'; import { DOTS, usePagination } from './paginator.hook'; import { PaginatorButton, PaginatorContainer, PaginatorItem } from './style'; export interface DataTablePaginatorProps { context: TableContext, virtualized: boolean, siblingCount: number, onPageChange: (page: number, last: number) => void, } export const DataTablePaginator: React.FC = ({ context, virtualized, siblingCount, onPageChange }) => { const [forceUpdate, setForceUpdate] = React.useState(false); context.forceUpdatePaginator = () => { setForceUpdate(!forceUpdate); } const totalCount = context.data && context.data.rows ? context.data.rows.length : 0; const pageSize = virtualized ? 1000 : context.paginator.pageSize; const currentPage = context.paginator.currentPage; const paginationRange = usePagination({ totalCount, pageSize, currentPage, siblingCount, }); if (context.paginator.currentPage === 0 || totalCount === 0) { return null; } const lastPage = paginationRange && paginationRange[paginationRange.length - 1]; const hasNext = context && context.data && !!context.data.next; const onNext = () => { onPageChange(currentPage + 1, Number(lastPage)); }; const onPrevious = () => { onPageChange(currentPage - 1, Number(lastPage)); }; return ( <> { pageSize && currentPage && 0)} onClick={onPrevious}> 0) ? 'disabled' : 'primary'} /> { paginationRange && paginationRange.map((pageNumber, key) => ( pageNumber === DOTS ? ( ) : ( 0)} onClick={() => onPageChange(Number(pageNumber), Number(lastPage))}> 0) ? 'disabled' : currentPage === pageNumber ? 'primary' : 'textPrimary'}> {pageNumber} ) )) } 0)} onClick={onNext}> 0) ? 'disabled' : 'primary'} /> } ) } const LeftArrowIcon = ({ color }: { color: IconColor }) => ( ) const RightArrowIcon = ({ color }: { color: IconColor }) => ( )