"use client"; import React, { useState, useEffect } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { Button } from "@/components/ui/button"; import { CheckSquare, Plus, Trash2 } from "lucide-react"; import { WebTypeConfigPanelProps } from "@/lib/registry/types"; import { WidgetComponent, CheckboxTypeConfig } from "@/types/screen"; interface CheckboxOption { label: string; value: string; checked?: boolean; disabled?: boolean; } export const CheckboxConfigPanel: React.FC = ({ component, onUpdateComponent, onUpdateProperty, }) => { const widget = component as WidgetComponent; const config = (widget.webTypeConfig as CheckboxTypeConfig) || {}; // 로컬 상태 const [localConfig, setLocalConfig] = useState({ // 단일 체크박스용 label: config.label || "", checkedValue: config.checkedValue || "Y", uncheckedValue: config.uncheckedValue || "N", defaultChecked: config.defaultChecked || false, // 다중 체크박스용 (체크박스 그룹) options: config.options || [], isGroup: config.isGroup || false, groupLabel: config.groupLabel || "", // 공통 설정 required: config.required || false, readonly: config.readonly || false, inline: config.inline !== false, // 기본값 true }); // 새 옵션 추가용 상태 const [newOptionLabel, setNewOptionLabel] = useState(""); const [newOptionValue, setNewOptionValue] = useState(""); // 컴포넌트 변경 시 로컬 상태 동기화 useEffect(() => { const currentConfig = (widget.webTypeConfig as CheckboxTypeConfig) || {}; setLocalConfig({ label: currentConfig.label || "", checkedValue: currentConfig.checkedValue || "Y", uncheckedValue: currentConfig.uncheckedValue || "N", defaultChecked: currentConfig.defaultChecked || false, options: currentConfig.options || [], isGroup: currentConfig.isGroup || false, groupLabel: currentConfig.groupLabel || "", required: currentConfig.required || false, readonly: currentConfig.readonly || false, inline: currentConfig.inline !== false, }); }, [widget.webTypeConfig]); // 설정 업데이트 핸들러 const updateConfig = (field: keyof CheckboxTypeConfig, value: any) => { const newConfig = { ...localConfig, [field]: value }; setLocalConfig(newConfig); onUpdateProperty("webTypeConfig", newConfig); }; // 체크박스 유형 변경 const toggleCheckboxType = (isGroup: boolean) => { if (isGroup && localConfig.options.length === 0) { // 그룹으로 변경할 때 기본 옵션 추가 const defaultOptions: CheckboxOption[] = [ { label: "옵션 1", value: "option1" }, { label: "옵션 2", value: "option2" }, ]; updateConfig("options", defaultOptions); } updateConfig("isGroup", isGroup); }; // 옵션 추가 const addOption = () => { if (!newOptionLabel.trim() || !newOptionValue.trim()) return; const newOption: CheckboxOption = { label: newOptionLabel.trim(), value: newOptionValue.trim(), checked: false, }; const newOptions = [...localConfig.options, newOption]; updateConfig("options", newOptions); setNewOptionLabel(""); setNewOptionValue(""); }; // 옵션 제거 const removeOption = (index: number) => { const newOptions = localConfig.options.filter((_, i) => i !== index); updateConfig("options", newOptions); }; // 옵션 업데이트 const updateOption = (index: number, field: keyof CheckboxOption, value: any) => { const newOptions = [...localConfig.options]; newOptions[index] = { ...newOptions[index], [field]: value }; updateConfig("options", newOptions); }; return ( 체크박스 설정 체크박스의 라벨, 값, 동작을 설정합니다. {/* 체크박스 유형 선택 */}

체크박스 유형

{!localConfig.isGroup ? ( /* 단일 체크박스 설정 */

단일 체크박스 설정

updateConfig("label", e.target.value)} placeholder="체크박스 라벨" className="text-xs" style={{ fontSize: "12px" }} />
updateConfig("checkedValue", e.target.value)} placeholder="Y" className="text-xs" style={{ fontSize: "12px" }} />
updateConfig("uncheckedValue", e.target.value)} placeholder="N" className="text-xs" style={{ fontSize: "12px" }} />

초기에 체크된 상태로 표시됩니다.

updateConfig("defaultChecked", checked)} />
) : ( /* 체크박스 그룹 설정 */

체크박스 그룹 설정

updateConfig("groupLabel", e.target.value)} placeholder="체크박스 그룹 제목" className="text-xs" style={{ fontSize: "12px" }} />
{/* 옵션 추가 */}
setNewOptionLabel(e.target.value)} placeholder="라벨" className="flex-1 text-xs" style={{ fontSize: "12px" }} /> setNewOptionValue(e.target.value)} placeholder="값" className="flex-1 text-xs" style={{ fontSize: "12px" }} />
{/* 현재 옵션 목록 */}
{localConfig.options.map((option, index) => (
updateOption(index, "checked", checked)} /> updateOption(index, "label", e.target.value)} placeholder="라벨" className="flex-1 text-xs" style={{ fontSize: "12px" }} /> updateOption(index, "value", e.target.value)} placeholder="값" className="flex-1 text-xs" style={{ fontSize: "12px" }} /> updateOption(index, "disabled", !checked)} />
))}
)} {/* 공통 설정 */}

공통 설정

체크박스들을 가로로 배열합니다.

updateConfig("inline", checked)} />

{localConfig.isGroup ? "최소 하나 이상 선택해야 합니다." : "체크박스가 선택되어야 합니다."}

updateConfig("required", checked)} />

체크박스 상태를 변경할 수 없습니다.

updateConfig("readonly", checked)} />
{/* 미리보기 */}

미리보기

{!localConfig.isGroup ? ( /* 단일 체크박스 미리보기 */
) : ( /* 체크박스 그룹 미리보기 */
{localConfig.groupLabel && }
{localConfig.options.map((option, index) => (
))}
)}
{localConfig.isGroup ? `${localConfig.options.length}개 옵션` : `값: ${localConfig.checkedValue}/${localConfig.uncheckedValue}`} {localConfig.inline && " • 가로 배열"} {localConfig.required && " • 필수"}
); }; CheckboxConfigPanel.displayName = "CheckboxConfigPanel";