import { flexRender, type RowData, type Table as TableInstance, } from "@tanstack/react-table"; import { ChevronLeftIcon, ChevronRightIcon, DoubleArrowLeftIcon, DoubleArrowRightIcon, } from "@radix-ui/react-icons"; import { cn } from "../../lib"; import { Button } from "../button"; import { Table, TableCell, TableHead, TableHeader, TableRow } from "../table"; import type { HTMLAttributes } from "react"; declare module "@tanstack/react-table" { // eslint-disable-next-line @typescript-eslint/no-unused-vars interface ColumnMeta { align?: "left" | "center" | "right"; rowSpan?: number; } } type DataTableProps = { table: TableInstance; variant?: "default" | "secondary" | "line"; className?: HTMLAttributes["className"]; viewPagination?: boolean; onRowClick?: (row: T) => void; }; function DataTable({ table, variant = "default", className, viewPagination, onRowClick, }: DataTableProps) { return ( <> {table.getRowModel().rows.map((row) => ( onRowClick?.(row.original)} className={cn(onRowClick && "cursor-pointer hover:bg-accent")} > {row.getVisibleCells().map((cell) => { const { meta } = cell.column.columnDef; return ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ); })} ))}
{viewPagination && } ); } function THead({ table }: { table: TableInstance }) { return ( {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { const columnRelativeDepth = header.depth - header.column.depth; if ( !header.isPlaceholder && columnRelativeDepth > 1 && header.id === header.column.id ) { return null; } let rowSpan = 1; if (header.isPlaceholder) { const leafs = header.getLeafHeaders(); rowSpan = (leafs?.[leafs.length - 1]?.depth ?? 0) - header.depth; } const { meta } = header.column.columnDef; return ( {flexRender( header.column.columnDef.header, header.getContext(), )} ); })} ))} ); } function Pagination({ table }: { table: TableInstance }) { const pageCount = table.getPageCount(); const currentPage = table.getState().pagination.pageIndex; const maxVisiblePages = 10; // 한 번에 보여줄 최대 페이지 수 let startPage = Math.max(0, currentPage - maxVisiblePages / 2); // 시작 페이지 const endPage = Math.min(pageCount, startPage + maxVisiblePages); // 끝 페이지 if (endPage - startPage < maxVisiblePages) { startPage = Math.max(0, endPage - maxVisiblePages); } const pageNumbers = Array(endPage - startPage) .fill(0) .map((_, i) => startPage + i); return (
{pageNumbers.map((pageNumber) => ( ))}
); } export { DataTable };