import React, {
ButtonHTMLAttributes,
DetailedHTMLProps,
useState,
} from "react";
import Stack, { StackProps } from "./Stack";
import classNames from "./lib/classNames";
import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/solid";
import { arrayFromNumber, chunk } from "./lib/utils";
export interface PaginationProps extends Omit {
onChange?: (page: number) => void;
limit?: number;
pages: number;
page: number;
}
export default function Pagination({
page: currentPage,
pages: allPages,
className,
limit = 3,
onChange,
...rest
}: PaginationProps) {
const [section, setSection] = useState(0);
const prevPage = currentPage - 1;
const nextPage = currentPage + 1;
const sections = chunk(arrayFromNumber(allPages), limit);
const previousSection = sections[section - 1];
const nextSection = sections[section + 1];
const pages = sections[section];
return (
{
if (onChange) {
if (previousSection?.includes(prevPage)) {
setSection(section - 1);
onChange(prevPage);
} else {
onChange(prevPage);
}
}
}}
>
{pages.map((page) => {
const isActive = page === currentPage;
return (
{
if (onChange) {
onChange(page);
}
}}
>
{page + 1}
);
})}
{
if (onChange) {
if (nextSection?.includes(nextPage)) {
setSection(section + 1);
onChange(nextPage);
} else {
onChange(nextPage);
}
}
}}
>
);
}
interface PaginationButtonProps
extends DetailedHTMLProps<
ButtonHTMLAttributes,
HTMLButtonElement
> {
isActive?: boolean;
}
function PaginationButton({
className,
isActive,
children,
...rest
}: PaginationButtonProps) {
return (
);
}