"use client"; import React, { useState } from "react"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; import { Trash2 } from "lucide-react"; import { RepeaterColumnConfig } from "./types"; import { cn } from "@/lib/utils"; interface RepeaterTableProps { columns: RepeaterColumnConfig[]; data: any[]; onDataChange: (newData: any[]) => void; onRowChange: (index: number, newRow: any) => void; onRowDelete: (index: number) => void; } export function RepeaterTable({ columns, data, onDataChange, onRowChange, onRowDelete, }: RepeaterTableProps) { const [editingCell, setEditingCell] = useState<{ rowIndex: number; field: string; } | null>(null); const handleCellEdit = (rowIndex: number, field: string, value: any) => { const newRow = { ...data[rowIndex], [field]: value }; onRowChange(rowIndex, newRow); }; const renderCell = ( row: any, column: RepeaterColumnConfig, rowIndex: number ) => { const isEditing = editingCell?.rowIndex === rowIndex && editingCell?.field === column.field; const value = row[column.field]; // 계산 필드는 편집 불가 if (column.calculated || !column.editable) { return (
{column.type === "number" ? typeof value === "number" ? value.toLocaleString() : value || "0" : value || "-"}
); } // 편집 가능한 필드 switch (column.type) { case "number": return ( handleCellEdit(rowIndex, column.field, parseFloat(e.target.value) || 0) } className="h-7 text-xs" /> ); case "date": return ( handleCellEdit(rowIndex, column.field, e.target.value)} className="h-7 text-xs" /> ); case "select": return ( ); default: // text return ( handleCellEdit(rowIndex, column.field, e.target.value)} className="h-7 text-xs" /> ); } }; return (
{columns.map((col) => ( ))} {data.length === 0 ? ( ) : ( data.map((row, rowIndex) => ( {columns.map((col) => ( ))} )) )}
# {col.label} {col.required && *} 삭제
추가된 항목이 없습니다
{rowIndex + 1} {renderCell(row, col, rowIndex)}
); }