import React, { FC } from 'react' import SVGLeftSmall from '../../svg/left-small.svg' import SVGRightSmall from '../../svg/right-small.svg' import { Flex } from '../flex' import classNames from 'classnames' import { PaginationProps, PaginationPaging } from './types' import { getIndex } from './util' function getInfo(paging: PaginationPaging) { const index = getIndex(paging) // 一共多少页 const all = Math.ceil((paging.count || 0) / paging.limit) // 要左右额外展示多少个页码,比如当前 index=5 diff=2,那么展示页码是 3 4 5 6 7 const diff = 2 // 展示出来的页码数组 let pages = [] // 开始页码 let begin = Math.max(index - diff, 1) // 结束页码 let end = Math.min(index + diff, all) if (all > diff * 2 + 1) { if (begin === 1) { end = diff * 2 + 1 } else if (end === all) { begin = Math.max(end - 2 * diff, 1) } } for (let i = begin; i <= end; i++) { pages.push(i) } // 如果总数为0,还是要给个页码1 if (paging.count === 0) { pages = [1] } return { index, all, begin, end, pages, } } const PageWithCount: FC = ({ paging, onChange }) => { const { index, all, begin, end, pages } = getInfo(paging) const handlePage = (_index: number) => { // 不处理 if (index === _index || _index < 1 || _index > all) { return } onChange({ ...paging, offset: (_index - 1) * paging.limit, }) } return (
handlePage(index - 1)} >
{begin >= 2 && (
handlePage(1)}> 1
)} {begin >= 3 && ···} {pages.map((page, i) => (
handlePage(page)} > {page}
))} {end <= all - 2 && ···} {end <= all - 1 && (
handlePage(all)}> {all}
)}
handlePage(index + 1)} >
) } export default PageWithCount