medkit/src/kit/pagination.tsx

115 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"
import { cn } from "@/lib/utils"
const DEFAULT_SIZES = [15, 30, 50] as const
/** Номера страниц с многоточиями: 1 … 4 5 6 … 12 */
function pageList(current: number, pages: number): (number | "gap")[] {
if (pages <= 7) return Array.from({ length: pages }, (_, i) => i)
const set = new Set([0, pages - 1, current - 1, current, current + 1])
const sorted = [...set].filter((p) => p >= 0 && p < pages).sort((a, b) => a - b)
return sorted.flatMap((p, i) => (i > 0 && p - sorted[i - 1] > 1 ? ["gap" as const, p] : [p]))
}
/**
* Пагинация под таблицей: «1 – 15 из 42», страницы, размер страницы.
* `page` — с нуля.
*/
export function Pagination({
total,
page,
size,
onPageChange,
onSizeChange,
sizeOptions = DEFAULT_SIZES,
className,
}: {
total: number
page: number
size: number
onPageChange: (page: number) => void
onSizeChange?: (size: number) => void
sizeOptions?: readonly number[]
className?: string
}) {
const pages = Math.max(1, Math.ceil(total / size))
const current = Math.min(page, pages - 1)
const from = total ? current * size + 1 : 0
const to = Math.min((current + 1) * size, total)
const pageButton = "grid h-7 min-w-7 place-items-center rounded-md px-1.5 text-xs font-medium tabular transition"
return (
<div className={cn("flex flex-wrap items-center justify-between gap-3 px-4 py-2.5", className)}>
<span className="rounded-md bg-muted px-2 py-1 text-xs text-muted-foreground tabular">
{from} – {to} из {total}
</span>
<div className="flex items-center gap-0.5">
<button
type="button"
aria-label="Предыдущая страница"
disabled={current === 0}
onClick={() => onPageChange(current - 1)}
className={cn(pageButton, "text-muted-foreground hover:bg-muted disabled:opacity-30")}
>
<ChevronLeftIcon className="size-4" />
</button>
{pageList(current, pages).map((p, i) =>
p === "gap" ? (
<span key={`gap-${i}`} className="px-1 text-xs text-muted-foreground">
…
</span>
) : (
<button
key={p}
type="button"
aria-current={p === current ? "page" : undefined}
onClick={() => onPageChange(p)}
className={cn(
pageButton,
p === current ? "bg-accent text-accent-foreground" : "text-muted-foreground hover:bg-muted",
)}
>
{p + 1}
</button>
),
)}
<button
type="button"
aria-label="Следующая страница"
disabled={current >= pages - 1}
onClick={() => onPageChange(current + 1)}
className={cn(pageButton, "text-muted-foreground hover:bg-muted disabled:opacity-30")}
>
<ChevronRightIcon className="size-4" />
</button>
</div>
{onSizeChange ? (
<div className="flex items-center gap-0.5 rounded-lg bg-muted p-0.5">
{sizeOptions.map((s) => (
<button
key={s}
type="button"
onClick={() => {
onSizeChange(s)
onPageChange(0)
}}
className={cn(
"h-6 rounded-md px-2 text-xs font-medium tabular transition",
s === size ? "bg-card text-foreground shadow-xs" : "text-muted-foreground hover:text-foreground",
)}
>
{s}
</button>
))}
</div>
) : (
<span />
)}
</div>
)
}