99 lines
4.5 KiB
TypeScript
99 lines
4.5 KiB
TypeScript
'use client'
|
|
|
|
import type { Table } from '@tanstack/react-table'
|
|
import { ChevronLeftIcon, ChevronRightIcon, ChevronsLeftIcon, ChevronsRightIcon } from 'lucide-react'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Pagination, PaginationContent, PaginationEllipsis, PaginationItem } from '@/components/ui/pagination'
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
|
import { usePagination } from './hooks/usePagination'
|
|
|
|
interface DataTablePaginationProps<TData> {
|
|
table: Table<TData>
|
|
isClient: boolean
|
|
totalCount: number
|
|
pageSizeOptions: number[]
|
|
paginationItemsToDisplay: number
|
|
}
|
|
|
|
export function DataTablePagination<TData>({
|
|
table,
|
|
isClient,
|
|
totalCount,
|
|
pageSizeOptions,
|
|
paginationItemsToDisplay,
|
|
}: DataTablePaginationProps<TData>) {
|
|
'use no memo'
|
|
|
|
const currentPage = table.getState().pagination.pageIndex + 1
|
|
const pageCount = table.getPageCount()
|
|
const { pages, showLeftEllipsis, showRightEllipsis } = usePagination({
|
|
currentPage,
|
|
totalPages: pageCount,
|
|
paginationItemsToDisplay,
|
|
})
|
|
|
|
return (
|
|
<div className="flex items-center justify-between gap-3 p-2 pt-0 max-sm:flex-col">
|
|
<p className="text-muted-foreground flex-1 whitespace-nowrap text-sm" aria-live="polite">
|
|
Page <span className="text-foreground">{currentPage}</span> of{' '}
|
|
<span className="text-foreground">{pageCount}</span>
|
|
{!isClient && totalCount > 0 ? (
|
|
<span className="ml-2">(총 <span className="text-foreground">{totalCount}</span>건)</span>
|
|
) : null}
|
|
</p>
|
|
|
|
<div className="grow">
|
|
<Pagination>
|
|
<PaginationContent>
|
|
<PaginationItem>
|
|
<Button size="icon" variant="outline" onClick={() => table.firstPage()} disabled={!table.getCanPreviousPage()} aria-label="첫 페이지">
|
|
<ChevronsLeftIcon aria-hidden="true" />
|
|
</Button>
|
|
</PaginationItem>
|
|
<PaginationItem>
|
|
<Button size="icon" variant="outline" onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()} aria-label="이전 페이지">
|
|
<ChevronLeftIcon aria-hidden="true" />
|
|
</Button>
|
|
</PaginationItem>
|
|
{showLeftEllipsis ? <PaginationItem><PaginationEllipsis /></PaginationItem> : null}
|
|
{pages.map((page) => (
|
|
<PaginationItem key={page}>
|
|
<Button
|
|
size="icon"
|
|
variant={page === currentPage ? 'default' : 'ghost'}
|
|
onClick={() => table.setPageIndex(page - 1)}
|
|
aria-current={page === currentPage ? 'page' : undefined}
|
|
>
|
|
{page}
|
|
</Button>
|
|
</PaginationItem>
|
|
))}
|
|
{showRightEllipsis ? <PaginationItem><PaginationEllipsis /></PaginationItem> : null}
|
|
<PaginationItem>
|
|
<Button size="icon" variant="outline" onClick={() => table.nextPage()} disabled={!table.getCanNextPage()} aria-label="다음 페이지">
|
|
<ChevronRightIcon aria-hidden="true" />
|
|
</Button>
|
|
</PaginationItem>
|
|
<PaginationItem>
|
|
<Button size="icon" variant="outline" onClick={() => table.lastPage()} disabled={!table.getCanNextPage()} aria-label="마지막 페이지">
|
|
<ChevronsRightIcon aria-hidden="true" />
|
|
</Button>
|
|
</PaginationItem>
|
|
</PaginationContent>
|
|
</Pagination>
|
|
</div>
|
|
|
|
<div className="flex flex-1 justify-end">
|
|
<Select value={String(table.getState().pagination.pageSize)} onValueChange={(value) => table.setPageSize(Number(value))}>
|
|
<SelectTrigger className="w-fit whitespace-nowrap" aria-label="페이지당 개수">
|
|
<SelectValue placeholder="페이지당 개수" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{pageSizeOptions.map((size) => <SelectItem key={size} value={String(size)}>{size} / page</SelectItem>)}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|