import { clsn } from "~/utils"; interface PaginationProps { size: number; total: number; current: number; onClick: (page: number) => void; } const genArr = (min: number, max: number) => { const arr = []; while (min <= max) { arr.push(min); min++; } return arr; } const calc = (current: number, total: number, size: number) => { // 总页数 const totalPage = Math.ceil(total / size); // 左右分隔多少个出现小点点 const offset = 1; // 中间部分,只展示前后各一个分页数 const prefixEnd = current - 1; const suffixStart = current + 1; const suffixEnd = current + offset > totalPage ? totalPage : current + offset; const prefixs = current > 1 ? genArr(current - offset > 0 ? current - offset : 1, prefixEnd) : []; const suffixs = current < totalPage ? genArr(suffixStart, suffixEnd) : []; const betweens = [ ...(current > offset + 1 ? [1, "..."] : []), ...prefixs, current, ...suffixs, ...(current > totalPage - (offset + 1) ? [] : ["...", totalPage]), ] return betweens; } function Pagination({ current = 3, total, size, onClick }: PaginationProps) { const arr = calc(current, total, size); return (
{arr.map((item, index) => { if (typeof item === "string") { return {item} } return ( ); })}
); } export default Pagination;