"use client"; import React, { useState, useEffect, useRef } from "react"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { ConditionalContainerProps, ConditionalSection } from "./types"; import { ConditionalSectionViewer } from "./ConditionalSectionViewer"; import { cn } from "@/lib/utils"; console.log("πŸš€ ConditionalContainerComponent λͺ¨λ“ˆ λ‘œλ“œλ¨!"); /** * 쑰건뢀 μ»¨ν…Œμ΄λ„ˆ μ»΄ν¬λ„ŒνŠΈ * 상단 μ…€λ ‰νŠΈλ°•μŠ€ 값에 따라 ν•˜λ‹¨μ— λ‹€λ₯Έ UIλ₯Ό ν‘œμ‹œ */ export function ConditionalContainerComponent({ config, controlField: propControlField, controlLabel: propControlLabel, sections: propSections, defaultValue: propDefaultValue, showBorder: propShowBorder, spacing: propSpacing, value, onChange, formData, onFormDataChange, isDesignMode = false, onUpdateComponent, onDeleteComponent, onSelectComponent, selectedComponentId, onHeightChange, componentId, style, className, groupedData, // πŸ†• κ·Έλ£Ή 데이터 }: ConditionalContainerProps) { console.log("🎯 ConditionalContainerComponent λ Œλ”λ§!", { isDesignMode, hasOnHeightChange: !!onHeightChange, componentId, }); // config prop μš°μ„ , μ—†μœΌλ©΄ κ°œλ³„ prop μ‚¬μš© const controlField = config?.controlField || propControlField || "condition"; const controlLabel = config?.controlLabel || propControlLabel || "쑰건 선택"; const sections = config?.sections || propSections || []; const defaultValue = config?.defaultValue || propDefaultValue || sections[0]?.condition; const showBorder = config?.showBorder ?? propShowBorder ?? true; const spacing = config?.spacing || propSpacing || "normal"; // ν˜„μž¬ μ„ νƒλœ κ°’ const [selectedValue, setSelectedValue] = useState( value || formData?.[controlField] || defaultValue || "" ); // formData λ³€κ²½ μ‹œ 동기화 useEffect(() => { if (formData?.[controlField]) { setSelectedValue(formData[controlField]); } }, [formData, controlField]); // κ°’ λ³€κ²½ ν•Έλ“€λŸ¬ const handleValueChange = (newValue: string) => { setSelectedValue(newValue); if (onChange) { onChange(newValue); } if (onFormDataChange) { onFormDataChange(controlField, newValue); } }; // μ»¨ν…Œμ΄λ„ˆ 높이 μΈ‘μ •μš© ref const containerRef = useRef(null); const previousHeightRef = useRef(0); // πŸ” 디버그: props 확인 useEffect(() => { console.log("πŸ” ConditionalContainer props:", { isDesignMode, hasOnHeightChange: !!onHeightChange, componentId, selectedValue, }); }, [isDesignMode, onHeightChange, componentId, selectedValue]); // 높이 λ³€ν™” 감지 및 콜백 호좜 useEffect(() => { console.log("πŸ” ResizeObserver 등둝 쑰건:", { hasContainer: !!containerRef.current, isDesignMode, hasOnHeightChange: !!onHeightChange, }); if (!containerRef.current || isDesignMode || !onHeightChange) return; const resizeObserver = new ResizeObserver((entries) => { for (const entry of entries) { const newHeight = entry.contentRect.height; // 높이가 μ‹€μ œλ‘œ λ³€κ²½λ˜μ—ˆμ„ λ•Œλ§Œ 콜백 호좜 if (Math.abs(newHeight - previousHeightRef.current) > 5) { console.log(`πŸ“ 쑰건뢀 μ»¨ν…Œμ΄λ„ˆ 높이 λ³€ν™”: ${previousHeightRef.current}px β†’ ${newHeight}px`); previousHeightRef.current = newHeight; onHeightChange(newHeight); } } }); resizeObserver.observe(containerRef.current); return () => { resizeObserver.disconnect(); }; }, [isDesignMode, onHeightChange, selectedValue]); // selectedValue λ³€κ²½ μ‹œμ—λ„ 감지 // 간격 μŠ€νƒ€μΌ const spacingClass = { tight: "space-y-2", normal: "space-y-4", loose: "space-y-8", }[spacing]; return (
{/* μ œμ–΄ μ…€λ ‰νŠΈλ°•μŠ€ */}
{/* 쑰건별 μ„Ήμ…˜λ“€ */}
{isDesignMode ? ( // λ””μžμΈ λͺ¨λ“œ: λͺ¨λ“  μ„Ήμ…˜ ν‘œμ‹œ
{sections.map((section) => ( ))}
) : ( // μ‹€ν–‰ λͺ¨λ“œ: ν™œμ„± μ„Ήμ…˜λ§Œ ν‘œμ‹œ sections.map((section) => selectedValue === section.condition ? ( ) : null ) )} {/* μ„Ήμ…˜μ΄ μ—†λŠ” 경우 μ•ˆλ‚΄ */} {sections.length === 0 && isDesignMode && (

μ„€μ • νŒ¨λ„μ—μ„œ 쑰건을 μΆ”κ°€ν•˜μ„Έμš”

)}
); }