"use client" import * as React from "react" import { flexRender, type Table as TanstackTable } from "@tanstack/react-table" import { ChevronRight, ChevronUp } from "lucide-react" import { cn } from "@/lib/utils" import { getCommonPinningStyles } from "@/lib/data-table" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { DataTablePagination } from "@/components/data-table/data-table-pagination" import { DataTableResizer } from "@/components/data-table/data-table-resizer" import { useAutoSizeColumns } from "@/hooks/useAutoSizeColumns" interface DataTableProps extends React.HTMLAttributes { table: TanstackTable floatingBar?: React.ReactNode | null autoSizeColumns?: boolean } /** * 멀티 그룹핑 + 그룹 토글 + 그룹 컬럼/헤더 숨김 + Indent + 리사이징 */ export function DataTable({ table, floatingBar = null, autoSizeColumns = true, children, className, ...props }: DataTableProps) { useAutoSizeColumns(table, autoSizeColumns) return (
{children}
{/* ------------------------------- Table Header → 그룹핑된 컬럼의 헤더는 숨김 처리 ------------------------------- */} {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { // 만약 이 컬럼이 현재 "그룹핑" 상태라면 헤더도 표시하지 않음 if (header.column.getIsGrouped()) { return null } return (
{header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} {/* 리사이즈 핸들 - 별도의 컴포넌트로 분리 */} {header.column.getCanResize() && ( )}
) })}
))}
{/* ------------------------------- Table Body ------------------------------- */} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => { // --------------------------------------------------- // 1) "그룹핑 헤더" Row인지 확인 // --------------------------------------------------- if (row.getIsGrouped()) { // row.groupingColumnId로 어떤 컬럼을 기준으로 그룹화 되었는지 알 수 있음 const groupingColumnId = row.groupingColumnId ?? "" const groupingColumn = table.getColumn(groupingColumnId) // 해당 column 객체 // 컬럼 라벨 가져오기 let columnLabel = groupingColumnId if (groupingColumn) { const headerDef = groupingColumn.columnDef.meta?.excelHeader if (typeof headerDef === "string") { columnLabel = headerDef } } return ( {/* 그룹 헤더는 한 줄에 합쳐서 보여주고, 토글 버튼 + 그룹 라벨 + 값 표기 */} {/* 확장/축소 버튼 (아이콘 중앙 정렬 + Indent) */} {row.getCanExpand() && ( )} {/* Group Label + 값 */} {columnLabel}: {row.getValue(groupingColumnId)} ({row.subRows.length} rows) ) } // --------------------------------------------------- // 2) 일반 Row // → "그룹핑된 컬럼"은 숨긴다 // --------------------------------------------------- return ( {row.getVisibleCells().map((cell) => { // 이 셀의 컬럼이 grouped라면 숨긴다 if (cell.column.getIsGrouped()) { return null } return ( {flexRender( cell.column.columnDef.cell, cell.getContext() )} ) })} ) }) ) : ( // --------------------------------------------------- // 3) 데이터가 없을 때 // --------------------------------------------------- No results. )}
{/* Pagination */} {/* Floating Bar (선택된 행 있을 때) */} {table.getFilteredSelectedRowModel().rows.length > 0 && floatingBar}
) }