From 66bd21ee65dd758f1d56349f5b3cf01405225484 Mon Sep 17 00:00:00 2001 From: kjs Date: Thu, 18 Dec 2025 15:24:20 +0900 Subject: [PATCH] =?UTF-8?q?=EC=97=94=ED=8B=B0=ED=8B=B0=ED=83=80=EC=9E=85?= =?UTF-8?q?=20=ED=91=9C=EC=8B=9C=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EntitySearchInputComponent.tsx | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) 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); } };