diff --git a/frontend/lib/registry/components/entity-search-input/EntitySearchInputComponent.tsx b/frontend/lib/registry/components/entity-search-input/EntitySearchInputComponent.tsx index df087536..8bdd5758 100644 --- a/frontend/lib/registry/components/entity-search-input/EntitySearchInputComponent.tsx +++ b/frontend/lib/registry/components/entity-search-input/EntitySearchInputComponent.tsx @@ -172,19 +172,38 @@ export function EntitySearchInputComponent({ // value가 변경되면 표시값 업데이트 (외래키 값으로 데이터 조회) useEffect(() => { const loadDisplayValue = async () => { - if (value && selectedData) { - // 이미 selectedData가 있으면 표시값만 업데이트 + // value가 없으면 초기화 + if (!value) { + setDisplayValue(""); + setSelectedData(null); + return; + } + + // 이미 selectedData가 있고 value와 일치하면 표시값만 업데이트 + if (selectedData && String(selectedData[valueField]) === String(value)) { setDisplayValue(selectedData[displayField] || ""); - } else if (value && mode === "select" && effectiveOptions.length > 0) { - // select 모드에서 value가 있고 options가 로드된 경우 - const found = effectiveOptions.find((opt) => opt[valueField] === value); + return; + } + + // select 모드에서 options가 로드된 경우 먼저 옵션에서 찾기 + if (mode === "select" && effectiveOptions.length > 0) { + // 타입 변환하여 비교 (숫자 vs 문자열 문제 해결) + const found = effectiveOptions.find((opt) => String(opt[valueField]) === String(value)); if (found) { setSelectedData(found); setDisplayValue(found[displayField] || ""); + console.log("✅ [EntitySearchInput] 옵션에서 초기값 찾음:", { value, found }); + return; } - } else if (value && !selectedData && tableName) { - // value는 있지만 selectedData가 없는 경우 (초기 로드 시) - // API로 해당 데이터 조회 + // 옵션에서 찾지 못한 경우 API로 조회 진행 + console.log("⚠️ [EntitySearchInput] 옵션에서 찾지 못함, API로 조회:", { + value, + optionsCount: effectiveOptions.length, + }); + } + + // API로 해당 데이터 조회 + if (tableName) { try { console.log("🔍 [EntitySearchInput] 초기값 조회:", { value, tableName, valueField }); const response = await dynamicFormApi.getTableData(tableName, { @@ -222,9 +241,6 @@ export function EntitySearchInputComponent({ // 에러 시 value 자체를 표시 setDisplayValue(String(value)); } - } else if (!value) { - setDisplayValue(""); - setSelectedData(null); } };