import { Checkbox } from "@/components/ui/checkbox" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Eye, Edit, Trash2 } from "lucide-react" import { type ColumnDef, TableMeta } from "@tanstack/react-table" import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header" import { EditableCell } from "@/components/data-table/editable-cell" import { AvlListItem } from "../types" interface GetColumnsProps { selectedRows?: number[] onRowSelect?: (id: number, selected: boolean) => void } // 수정 여부 확인 헬퍼 함수 const getIsModified = (table: any, rowId: string, fieldName: string) => { const pendingChanges = table.options.meta?.getPendingChanges?.() || {} return String(rowId) in pendingChanges && fieldName in pendingChanges[String(rowId)] } // 테이블 메타 타입 확장 declare module "@tanstack/react-table" { interface TableMeta { onCellUpdate?: (id: string, field: keyof TData, newValue: any) => Promise onCellCancel?: (id: string, field: keyof TData) => void onAction?: (action: string, data?: any) => void onSaveEmptyRow?: (tempId: string) => Promise onCancelEmptyRow?: (tempId: string) => void isEmptyRow?: (id: string) => boolean } } // 테이블 컬럼 정의 함수 export function getColumns({ selectedRows = [], onRowSelect }: GetColumnsProps): ColumnDef[] { const columns: ColumnDef[] = [ // 기본 정보 그룹 { header: "기본 정보", columns: [ { id: "select", header: () =>
선택
, cell: ({ row }) => (
{ onRowSelect?.(row.original.id, !!checked) }} aria-label="행 선택" className="translate-y-[2px]" />
), enableSorting: false, enableHiding: false, size: 40, }, { accessorKey: "no", header: ({ column }) => ( ), cell: ({ getValue }) =>
{getValue() as number}
, size: 60, }, { accessorKey: "isTemplate", header: ({ column }) => ( ), cell: ({ getValue, row, table }) => { const value = getValue() as boolean const isModified = getIsModified(table, row.id, "isTemplate") return ( { table.options.meta?.onCellUpdate?.(row.id, "isTemplate", newValue === "true") }} onCancel={() => { table.options.meta?.onCellCancel?.(row.id, "isTemplate") }} /> ) }, size: 120, }, { accessorKey: "constructionSector", header: ({ column }) => ( ), cell: ({ getValue, row, table }) => { const value = getValue() as string const isModified = getIsModified(table, row.id, "constructionSector") return ( { table.options.meta?.onCellUpdate?.(row.id, "constructionSector", newValue) }} onCancel={() => { table.options.meta?.onCellCancel?.(row.id, "constructionSector") }} /> ) }, size: 100, }, { accessorKey: "projectCode", header: ({ column }) => ( ), cell: ({ getValue, row, table }) => { const value = getValue() as string const isModified = getIsModified(table, row.id, "projectCode") return ( { table.options.meta?.onCellUpdate?.(row.id, "projectCode", newValue) }} onCancel={() => { table.options.meta?.onCellCancel?.(row.id, "projectCode") }} /> ) }, size: 140, }, { accessorKey: "shipType", header: ({ column }) => ( ), cell: ({ getValue, row, table }) => { const value = getValue() as string const isModified = getIsModified(table, row.id, "shipType") return ( { table.options.meta?.onCellUpdate?.(row.id, "shipType", newValue) }} onCancel={() => { table.options.meta?.onCellCancel?.(row.id, "shipType") }} /> ) }, size: 100, }, { accessorKey: "avlKind", header: ({ column }) => ( ), cell: ({ getValue, row, table }) => { const value = getValue() as string const isModified = getIsModified(table, row.id, "avlKind") return ( { table.options.meta?.onCellUpdate?.(row.id, "avlKind", newValue) }} onCancel={() => { table.options.meta?.onCellCancel?.(row.id, "avlKind") }} /> ) }, size: 120, }, { accessorKey: "htDivision", header: ({ column }) => ( ), cell: ({ getValue, row, table }) => { const value = getValue() as string const isModified = getIsModified(table, row.id, "htDivision") return ( { table.options.meta?.onCellUpdate?.(row.id, "htDivision", newValue) }} onCancel={() => { table.options.meta?.onCellCancel?.(row.id, "htDivision") }} /> ) }, size: 80, }, { accessorKey: "rev", header: ({ column }) => ( ), cell: ({ getValue, row, table }) => { const value = getValue() as number const isModified = getIsModified(table, row.id, "rev") return ( { table.options.meta?.onCellUpdate?.(row.id, "rev", parseInt(newValue)) }} onCancel={() => { table.options.meta?.onCellCancel?.(row.id, "rev") }} /> ) }, size: 80, }, ], }, // 등록 정보 그룹 { header: "등록 정보", columns: [ { accessorKey: "createdAt", header: ({ column }) => ( ), cell: ({ getValue }) => { const date = getValue() as string return
{date}
}, size: 100, }, { accessorKey: "updatedAt", header: ({ column }) => ( ), cell: ({ getValue }) => { const date = getValue() as string return
{date}
}, size: 100, }, ], }, // 액션 그룹 { id: "actions", header: "액션", columns: [ { id: "actions", header: () =>
액션
, cell: ({ row, table }) => { const isEmptyRow = table.options.meta?.isEmptyRow?.(row.id) || false if (isEmptyRow) { return (
) } return (
) }, enableSorting: false, enableHiding: false, size: 120, }, ], }, ] return columns }