"use client" import React, { ReactNode } from "react" import { useDataGrid } from "@/components/reui/data-grid/data-grid" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Skeleton } from "@/components/ui/skeleton" import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react" interface DataGridPaginationProps { sizes?: number[] sizesInfo?: string sizesLabel?: string sizesDescription?: string sizesSkeleton?: ReactNode more?: boolean moreLimit?: number info?: string infoSkeleton?: ReactNode className?: string rowsPerPageLabel?: string previousPageLabel?: string nextPageLabel?: string ellipsisText?: string } function DataGridPagination(props: DataGridPaginationProps): React.JSX.Element { const { table, recordCount, isLoading } = useDataGrid() const defaultProps: Partial = { sizes: [5, 10, 25, 50, 100], sizesLabel: "Show", sizesDescription: "per page", sizesSkeleton: , moreLimit: 5, more: false, info: "{from} - {to} of {count}", infoSkeleton: , rowsPerPageLabel: "Rows per page", previousPageLabel: "Go to previous page", nextPageLabel: "Go to next page", ellipsisText: "...", } const mergedProps: DataGridPaginationProps = { ...defaultProps, ...props } const btnBaseClasses = "size-7 p-0 text-sm" const btnArrowClasses = btnBaseClasses + " rtl:transform rtl:rotate-180" const pageIndex = table.getState().pagination.pageIndex const pageSize = table.getState().pagination.pageSize const from = pageIndex * pageSize + 1 const to = Math.min((pageIndex + 1) * pageSize, recordCount) const pageCount = table.getPageCount() // Replace placeholders in paginationInfo const paginationInfo = mergedProps?.info ? mergedProps.info .replace("{from}", from.toString()) .replace("{to}", to.toString()) .replace("{count}", recordCount.toString()) : `${from} - ${to} of ${recordCount}` // Pagination limit logic const paginationMoreLimit = mergedProps?.moreLimit || 5 // Determine the start and end of the pagination group const currentGroupStart = Math.floor(pageIndex / paginationMoreLimit) * paginationMoreLimit const currentGroupEnd = Math.min( currentGroupStart + paginationMoreLimit, pageCount ) // Render page buttons based on the current group const renderPageButtons = () => { const buttons = [] for (let i = currentGroupStart; i < currentGroupEnd; i++) { buttons.push( ) } return buttons } // Render a "previous" ellipsis button if there are previous pages to show const renderEllipsisPrevButton = () => { if (currentGroupStart > 0) { return ( ) } return null } // Render a "next" ellipsis button if there are more pages to show after the current group const renderEllipsisNextButton = () => { if (currentGroupEnd < pageCount) { return ( ) } return null } return (
{isLoading ? ( mergedProps?.sizesSkeleton ) : ( <>
{mergedProps.rowsPerPageLabel}
)}
{isLoading ? ( mergedProps?.infoSkeleton ) : ( <>
{paginationInfo}
{pageCount > 1 && (
{renderEllipsisPrevButton()} {renderPageButtons()} {renderEllipsisNextButton()}
)} )}
) } export { DataGridPagination, type DataGridPaginationProps }