ERP-node/frontend/lib/registry/components/repeat-container/RepeatContainerConfigPanel.tsx

804 lines
30 KiB
TypeScript

"use client";
import React, { useState, useEffect, useMemo } from "react";
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command";
import { Database, Table2, ChevronsUpDown, Check, LayoutGrid, LayoutList, Rows3, Plus, X, GripVertical, Trash2, Type } from "lucide-react";
import { cn } from "@/lib/utils";
import { RepeatContainerConfig, SlotComponentConfig } from "./types";
import { tableTypeApi } from "@/lib/api/screen";
import { tableManagementApi } from "@/lib/api/tableManagement";
interface RepeatContainerConfigPanelProps {
config: RepeatContainerConfig;
onChange: (config: Partial<RepeatContainerConfig>) => void;
screenTableName?: string;
}
/**
* 리피터 컨테이너 설정 패널
*/
export function RepeatContainerConfigPanel({
config,
onChange,
screenTableName,
}: RepeatContainerConfigPanelProps) {
const [availableTables, setAvailableTables] = useState<Array<{ tableName: string; displayName?: string }>>([]);
const [loadingTables, setLoadingTables] = useState(false);
const [tableComboboxOpen, setTableComboboxOpen] = useState(false);
// 컬럼 관련 상태
const [availableColumns, setAvailableColumns] = useState<Array<{ columnName: string; displayName?: string }>>([]);
const [loadingColumns, setLoadingColumns] = useState(false);
// 실제 사용할 테이블 이름 계산
const targetTableName = useMemo(() => {
if (config.useCustomTable && config.customTableName) {
return config.customTableName;
}
return config.tableName || screenTableName;
}, [config.useCustomTable, config.customTableName, config.tableName, screenTableName]);
// 화면 테이블명 자동 설정 (초기 한 번만)
useEffect(() => {
if (screenTableName && !config.tableName && !config.customTableName) {
onChange({ tableName: screenTableName });
}
}, [screenTableName, config.tableName, config.customTableName, onChange]);
// 전체 테이블 목록 로드
useEffect(() => {
const fetchTables = async () => {
setLoadingTables(true);
try {
const response = await tableTypeApi.getTables();
setAvailableTables(
response.map((table: any) => ({
tableName: table.tableName,
displayName: table.displayName || table.tableName,
}))
);
} catch (error) {
console.error("테이블 목록 가져오기 실패:", error);
} finally {
setLoadingTables(false);
}
};
fetchTables();
}, []);
// 테이블 컬럼 로드
useEffect(() => {
if (!targetTableName) {
setAvailableColumns([]);
return;
}
const fetchColumns = async () => {
setLoadingColumns(true);
try {
const response = await tableManagementApi.getColumnList(targetTableName);
if (response.success && response.data && Array.isArray(response.data)) {
setAvailableColumns(
response.data.map((col: any) => ({
columnName: col.columnName,
displayName: col.displayName || col.columnLabel || col.columnName,
}))
);
}
} catch (error) {
console.error("컬럼 목록 가져오기 실패:", error);
setAvailableColumns([]);
} finally {
setLoadingColumns(false);
}
};
fetchColumns();
}, [targetTableName]);
return (
<div className="space-y-4">
<div className="text-sm font-medium"> </div>
{/* 데이터 소스 테이블 설정 */}
<div className="space-y-3">
<div>
<h3 className="text-sm font-semibold"> </h3>
<p className="text-muted-foreground text-[10px]"> </p>
</div>
<hr className="border-border" />
{/* 현재 선택된 테이블 표시 (카드 형태) */}
<div className="flex items-center gap-2 rounded-md border bg-slate-50 p-2">
<Database className="h-4 w-4 text-blue-500" />
<div className="flex-1">
<div className="text-xs font-medium">
{config.customTableName || config.tableName || screenTableName || "테이블 미선택"}
</div>
<div className="text-[10px] text-muted-foreground">
{config.useCustomTable ? "커스텀 테이블" : "화면 기본 테이블"}
</div>
</div>
</div>
{/* 테이블 선택 Combobox */}
<Popover open={tableComboboxOpen} onOpenChange={setTableComboboxOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={tableComboboxOpen}
className="h-8 w-full justify-between text-xs"
disabled={loadingTables}
>
...
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<Command>
<CommandInput placeholder="테이블 검색..." className="h-8 text-xs" />
<CommandList>
<CommandEmpty className="py-2 text-xs"> </CommandEmpty>
{/* 그룹 1: 화면 기본 테이블 */}
{screenTableName && (
<CommandGroup heading="기본 (화면 테이블)">
<CommandItem
key={`default-${screenTableName}`}
value={screenTableName}
onSelect={() => {
onChange({
useCustomTable: false,
customTableName: undefined,
tableName: screenTableName,
});
setTableComboboxOpen(false);
}}
className="text-xs cursor-pointer"
>
<Check
className={cn(
"mr-2 h-3 w-3",
!config.useCustomTable ? "opacity-100" : "opacity-0"
)}
/>
<Database className="mr-2 h-3 w-3 text-blue-500" />
{screenTableName}
</CommandItem>
</CommandGroup>
)}
{/* 그룹 2: 전체 테이블 */}
<CommandGroup heading="전체 테이블">
{availableTables
.filter((table) => table.tableName !== screenTableName)
.map((table) => (
<CommandItem
key={table.tableName}
value={`${table.tableName} ${table.displayName || ""}`}
onSelect={() => {
onChange({
useCustomTable: true,
customTableName: table.tableName,
tableName: table.tableName,
});
setTableComboboxOpen(false);
}}
className="text-xs cursor-pointer"
>
<Check
className={cn(
"mr-2 h-3 w-3",
config.customTableName === table.tableName ? "opacity-100" : "opacity-0"
)}
/>
<Table2 className="mr-2 h-3 w-3 text-slate-400" />
{table.displayName || table.tableName}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
{/* 데이터 소스 컴포넌트 연결 */}
<div className="space-y-3">
<div>
<h3 className="text-sm font-semibold"> </h3>
<p className="text-muted-foreground text-[10px]">
</p>
</div>
<hr className="border-border" />
<div className="space-y-2">
<Label className="text-xs"> </Label>
<Select
value={config.dataSourceType || "manual"}
onValueChange={(value) => onChange({ dataSourceType: value as any })}
>
<SelectTrigger className="h-8 text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="manual"> (API에서 )</SelectItem>
<SelectItem value="table-list"> </SelectItem>
</SelectContent>
</Select>
</div>
{config.dataSourceType === "table-list" && (
<div className="space-y-2">
<Label className="text-xs"> ID ()</Label>
<Input
value={config.dataSourceComponentId || ""}
onChange={(e) => onChange({ dataSourceComponentId: e.target.value })}
placeholder="비우면 테이블명으로 자동 매칭"
className="h-8 text-xs"
/>
<p className="text-[10px] text-muted-foreground">
</p>
</div>
)}
</div>
{/* 슬롯 컴포넌트 설정 */}
<SlotChildrenSection
config={config}
onChange={onChange}
availableColumns={availableColumns}
loadingColumns={loadingColumns}
/>
{/* 레이아웃 설정 */}
<div className="space-y-3">
<div>
<h3 className="text-sm font-semibold"></h3>
</div>
<hr className="border-border" />
{/* 레이아웃 타입 선택 */}
<div className="space-y-1">
<Label className="text-xs"> </Label>
<div className="grid grid-cols-3 gap-2">
<Button
variant={config.layout === "vertical" ? "default" : "outline"}
size="sm"
onClick={() => onChange({ layout: "vertical" })}
className="h-9 text-xs flex flex-col gap-1"
>
<Rows3 className="h-4 w-4" />
<span></span>
</Button>
<Button
variant={config.layout === "horizontal" ? "default" : "outline"}
size="sm"
onClick={() => onChange({ layout: "horizontal" })}
className="h-9 text-xs flex flex-col gap-1"
>
<LayoutList className="h-4 w-4 rotate-90" />
<span></span>
</Button>
<Button
variant={config.layout === "grid" ? "default" : "outline"}
size="sm"
onClick={() => onChange({ layout: "grid" })}
className="h-9 text-xs flex flex-col gap-1"
>
<LayoutGrid className="h-4 w-4" />
<span></span>
</Button>
</div>
</div>
{/* 그리드 컬럼 수 (grid 레이아웃일 때만) */}
{config.layout === "grid" && (
<div className="space-y-1">
<Label className="text-xs"> </Label>
<Select
value={String(config.gridColumns || 2)}
onValueChange={(value) => onChange({ gridColumns: Number(value) })}
>
<SelectTrigger className="h-8 text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="2">2</SelectItem>
<SelectItem value="3">3</SelectItem>
<SelectItem value="4">4</SelectItem>
<SelectItem value="5">5</SelectItem>
<SelectItem value="6">6</SelectItem>
</SelectContent>
</Select>
</div>
)}
{/* 간격 */}
<div className="space-y-1">
<Label className="text-xs"> </Label>
<Input
value={config.gap || "16px"}
onChange={(e) => onChange({ gap: e.target.value })}
placeholder="16px"
className="h-8 text-xs"
/>
</div>
</div>
{/* 아이템 카드 설정 */}
<div className="space-y-3">
<div>
<h3 className="text-sm font-semibold"> </h3>
</div>
<hr className="border-border" />
<div className="grid grid-cols-2 gap-2">
<div className="space-y-1">
<Label className="text-xs"></Label>
<Input
type="color"
value={config.backgroundColor || "#ffffff"}
onChange={(e) => onChange({ backgroundColor: e.target.value })}
className="h-8"
/>
</div>
<div className="space-y-1">
<Label className="text-xs"> </Label>
<Input
value={config.borderRadius || "8px"}
onChange={(e) => onChange({ borderRadius: e.target.value })}
placeholder="8px"
className="h-8 text-xs"
/>
</div>
<div className="space-y-1">
<Label className="text-xs"> </Label>
<Input
value={config.padding || "16px"}
onChange={(e) => onChange({ padding: e.target.value })}
placeholder="16px"
className="h-8 text-xs"
/>
</div>
<div className="space-y-1">
<Label className="text-xs"> </Label>
<Input
value={config.itemHeight || "auto"}
onChange={(e) => onChange({ itemHeight: e.target.value })}
placeholder="auto"
className="h-8 text-xs"
/>
</div>
</div>
<div className="flex flex-wrap items-center gap-4">
<div className="flex items-center gap-2">
<Checkbox
id="showBorder"
checked={config.showBorder ?? true}
onCheckedChange={(checked) => onChange({ showBorder: checked as boolean })}
/>
<Label htmlFor="showBorder" className="text-xs">
</Label>
</div>
<div className="flex items-center gap-2">
<Checkbox
id="showShadow"
checked={config.showShadow ?? false}
onCheckedChange={(checked) => onChange({ showShadow: checked as boolean })}
/>
<Label htmlFor="showShadow" className="text-xs">
</Label>
</div>
</div>
</div>
{/* 아이템 제목 설정 */}
<div className="space-y-3">
<div className="flex items-center gap-2">
<Checkbox
id="showItemTitle"
checked={config.showItemTitle ?? false}
onCheckedChange={(checked) => onChange({ showItemTitle: checked as boolean })}
/>
<Label htmlFor="showItemTitle" className="text-sm font-semibold">
</Label>
</div>
<hr className="border-border" />
{config.showItemTitle && (
<div className="space-y-2">
<div className="space-y-1">
<Label className="text-xs"> 릿</Label>
<Input
value={config.itemTitleTemplate || ""}
onChange={(e) => onChange({ itemTitleTemplate: e.target.value })}
placeholder="{order_no} - {item_code}"
className="h-8 text-xs"
/>
<p className="text-[10px] text-muted-foreground">
{"{필드명}"}
</p>
</div>
<div className="grid grid-cols-3 gap-2">
<div className="space-y-1">
<Label className="text-[10px]"> </Label>
<Input
value={config.titleFontSize || "14px"}
onChange={(e) => onChange({ titleFontSize: e.target.value })}
placeholder="14px"
className="h-7 text-xs"
/>
</div>
<div className="space-y-1">
<Label className="text-[10px]"> </Label>
<Input
type="color"
value={config.titleColor || "#374151"}
onChange={(e) => onChange({ titleColor: e.target.value })}
className="h-7"
/>
</div>
<div className="space-y-1">
<Label className="text-[10px]"></Label>
<Select
value={config.titleFontWeight || "600"}
onValueChange={(value) => onChange({ titleFontWeight: value })}
>
<SelectTrigger className="h-7 text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="400"></SelectItem>
<SelectItem value="500"></SelectItem>
<SelectItem value="600"></SelectItem>
<SelectItem value="700"> </SelectItem>
</SelectContent>
</Select>
</div>
</div>
</div>
)}
</div>
{/* 페이징 설정 */}
<div className="space-y-3">
<div className="flex items-center gap-2">
<Checkbox
id="usePaging"
checked={config.usePaging ?? false}
onCheckedChange={(checked) => onChange({ usePaging: checked as boolean })}
/>
<Label htmlFor="usePaging" className="text-sm font-semibold">
</Label>
</div>
<hr className="border-border" />
{config.usePaging && (
<div className="space-y-1">
<Label className="text-xs"> </Label>
<Select
value={String(config.pageSize || 10)}
onValueChange={(value) => onChange({ pageSize: Number(value) })}
>
<SelectTrigger className="h-8 text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="5">5</SelectItem>
<SelectItem value="10">10</SelectItem>
<SelectItem value="20">20</SelectItem>
<SelectItem value="50">50</SelectItem>
</SelectContent>
</Select>
</div>
)}
</div>
{/* 상호작용 설정 */}
<div className="space-y-3">
<div>
<h3 className="text-sm font-semibold"></h3>
</div>
<hr className="border-border" />
<div className="flex flex-wrap items-center gap-4">
<div className="flex items-center gap-2">
<Checkbox
id="clickable"
checked={config.clickable ?? false}
onCheckedChange={(checked) => onChange({ clickable: checked as boolean })}
/>
<Label htmlFor="clickable" className="text-xs">
</Label>
</div>
{config.clickable && (
<>
<div className="flex items-center gap-2">
<Checkbox
id="showSelectedState"
checked={config.showSelectedState ?? true}
onCheckedChange={(checked) => onChange({ showSelectedState: checked as boolean })}
/>
<Label htmlFor="showSelectedState" className="text-xs">
</Label>
</div>
<div className="space-y-1">
<Label className="text-[10px]"> </Label>
<Select
value={config.selectionMode || "single"}
onValueChange={(value) =>
onChange({ selectionMode: value as "single" | "multiple" })
}
>
<SelectTrigger className="h-7 text-xs w-24">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="single"></SelectItem>
<SelectItem value="multiple"></SelectItem>
</SelectContent>
</Select>
</div>
</>
)}
</div>
</div>
{/* 빈 상태 설정 */}
<div className="space-y-3">
<div>
<h3 className="text-sm font-semibold"> </h3>
</div>
<hr className="border-border" />
<div className="space-y-1">
<Label className="text-xs"></Label>
<Input
value={config.emptyMessage || "데이터가 없습니다"}
onChange={(e) => onChange({ emptyMessage: e.target.value })}
placeholder="데이터가 없습니다"
className="h-8 text-xs"
/>
</div>
</div>
</div>
);
}
// ============================================================
// 슬롯 자식 컴포넌트 관리 섹션
// ============================================================
interface SlotChildrenSectionProps {
config: RepeatContainerConfig;
onChange: (config: Partial<RepeatContainerConfig>) => void;
availableColumns: Array<{ columnName: string; displayName?: string }>;
loadingColumns: boolean;
}
function SlotChildrenSection({
config,
onChange,
availableColumns,
loadingColumns,
}: SlotChildrenSectionProps) {
const [selectedColumn, setSelectedColumn] = useState<string>("");
const [columnComboboxOpen, setColumnComboboxOpen] = useState(false);
const children = config.children || [];
// 컴포넌트 추가
const addComponent = (columnName: string, displayName: string) => {
const newChild: SlotComponentConfig = {
id: `slot_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
componentType: "text-display",
label: displayName,
fieldName: columnName,
position: { x: 0, y: children.length * 40 },
size: { width: 200, height: 32 },
componentConfig: {},
};
onChange({
children: [...children, newChild],
});
setSelectedColumn("");
setColumnComboboxOpen(false);
};
// 컴포넌트 삭제
const removeComponent = (id: string) => {
onChange({
children: children.filter((c) => c.id !== id),
});
};
// 컴포넌트 라벨 변경
const updateComponentLabel = (id: string, label: string) => {
onChange({
children: children.map((c) => (c.id === id ? { ...c, label } : c)),
});
};
// 컴포넌트 타입 변경
const updateComponentType = (id: string, componentType: string) => {
onChange({
children: children.map((c) => (c.id === id ? { ...c, componentType } : c)),
});
};
return (
<div className="space-y-3">
<div>
<h3 className="text-sm font-semibold"> </h3>
<p className="text-muted-foreground text-[10px]">
</p>
</div>
<hr className="border-border" />
{/* 추가된 필드 목록 */}
{children.length > 0 ? (
<div className="space-y-2">
{children.map((child, index) => (
<div
key={child.id}
className="flex items-center gap-2 rounded-md border border-green-200 bg-green-50 p-2"
>
<div className="flex h-5 w-5 items-center justify-center rounded bg-green-200 text-xs font-medium text-green-700">
{index + 1}
</div>
<div className="flex-1 space-y-1">
<div className="flex items-center gap-2">
<div className="flex-1">
<div className="text-xs font-medium text-green-700">
{child.label || child.fieldName}
</div>
<div className="text-[10px] text-green-500">
: {child.fieldName}
</div>
</div>
<Select
value={child.componentType}
onValueChange={(value) => updateComponentType(child.id, value)}
>
<SelectTrigger className="h-6 w-20 text-[10px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="text-display"></SelectItem>
<SelectItem value="text-input"></SelectItem>
<SelectItem value="number-display"></SelectItem>
<SelectItem value="date-display"></SelectItem>
</SelectContent>
</Select>
</div>
</div>
<Button
variant="ghost"
size="sm"
className="h-5 w-5 p-0 text-red-400 hover:text-red-600"
onClick={() => removeComponent(child.id)}
>
<X className="h-3 w-3" />
</Button>
</div>
))}
</div>
) : (
<div className="rounded-md border border-dashed border-slate-300 bg-slate-50 p-4 text-center">
<Type className="mx-auto h-6 w-6 text-slate-300" />
<div className="mt-2 text-xs text-slate-500"> </div>
<div className="text-[10px] text-muted-foreground">
</div>
</div>
)}
{/* 컬럼 선택 Combobox */}
<div className="space-y-2">
<Label className="text-xs"> </Label>
<Popover open={columnComboboxOpen} onOpenChange={setColumnComboboxOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={columnComboboxOpen}
className="h-8 w-full justify-between text-xs"
disabled={loadingColumns || availableColumns.length === 0}
>
{loadingColumns
? "로딩 중..."
: availableColumns.length === 0
? "테이블을 먼저 선택하세요"
: "컬럼 선택..."}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<Command>
<CommandInput placeholder="컬럼 검색..." className="h-8 text-xs" />
<CommandList>
<CommandEmpty className="py-2 text-xs"> </CommandEmpty>
<CommandGroup heading="사용 가능한 컬럼">
{availableColumns.map((col) => {
const isAdded = children.some((c) => c.fieldName === col.columnName);
return (
<CommandItem
key={col.columnName}
value={`${col.columnName} ${col.displayName || ""}`}
onSelect={() => {
if (!isAdded) {
addComponent(col.columnName, col.displayName || col.columnName);
}
}}
disabled={isAdded}
className={cn(
"text-xs cursor-pointer",
isAdded && "opacity-50 cursor-not-allowed"
)}
>
<Plus
className={cn(
"mr-2 h-3 w-3",
isAdded ? "text-green-500" : "text-blue-500"
)}
/>
<div className="flex-1">
<div className="font-medium">{col.displayName || col.columnName}</div>
<div className="text-[10px] text-muted-foreground">
{col.columnName}
</div>
</div>
{isAdded && (
<Check className="h-3 w-3 text-green-500" />
)}
</CommandItem>
);
})}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
<div className="rounded-md border border-amber-200 bg-amber-50 p-2">
<p className="text-[10px] text-amber-700">
<strong>:</strong> .<br />
, .
</p>
</div>
</div>
);
}