2025-11-14 14:43:53 +09:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import React, { useState, useEffect, useRef } from "react";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { X, Loader2, ChevronDown } from "lucide-react";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { useEntitySearch } from "../entity-search-input/useEntitySearch";
|
|
|
|
|
import { EntitySearchResult } from "../entity-search-input/types";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2025-11-20 17:47:56 +09:00
|
|
|
import { AutocompleteSearchInputConfig } from "./types";
|
|
|
|
|
import { ComponentRendererProps } from "../../DynamicComponentRenderer";
|
2025-11-14 14:43:53 +09:00
|
|
|
|
2025-11-20 17:47:56 +09:00
|
|
|
export interface AutocompleteSearchInputProps extends ComponentRendererProps {
|
2025-11-14 14:43:53 +09:00
|
|
|
config?: AutocompleteSearchInputConfig;
|
2025-11-20 17:47:56 +09:00
|
|
|
tableName?: string;
|
|
|
|
|
displayField?: string;
|
|
|
|
|
valueField?: string;
|
|
|
|
|
searchFields?: string[];
|
2025-11-14 14:43:53 +09:00
|
|
|
filterCondition?: Record<string, any>;
|
2025-11-20 17:47:56 +09:00
|
|
|
placeholder?: string;
|
|
|
|
|
showAdditionalInfo?: boolean;
|
|
|
|
|
additionalFields?: string[];
|
2025-11-14 14:43:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function AutocompleteSearchInputComponent({
|
2025-11-20 17:47:56 +09:00
|
|
|
component,
|
2025-11-14 14:43:53 +09:00
|
|
|
config,
|
|
|
|
|
tableName: propTableName,
|
|
|
|
|
displayField: propDisplayField,
|
|
|
|
|
valueField: propValueField,
|
|
|
|
|
searchFields: propSearchFields,
|
|
|
|
|
filterCondition = {},
|
|
|
|
|
placeholder: propPlaceholder,
|
|
|
|
|
disabled = false,
|
|
|
|
|
value,
|
|
|
|
|
onChange,
|
|
|
|
|
className,
|
2025-11-20 17:47:56 +09:00
|
|
|
isInteractive = false,
|
|
|
|
|
onFormDataChange,
|
|
|
|
|
formData,
|
2025-11-14 14:43:53 +09:00
|
|
|
}: AutocompleteSearchInputProps) {
|
|
|
|
|
// config prop 우선, 없으면 개별 prop 사용
|
|
|
|
|
const tableName = config?.tableName || propTableName || "";
|
|
|
|
|
const displayField = config?.displayField || propDisplayField || "";
|
|
|
|
|
const valueField = config?.valueField || propValueField || "";
|
|
|
|
|
const searchFields = config?.searchFields || propSearchFields || [displayField];
|
|
|
|
|
const placeholder = config?.placeholder || propPlaceholder || "검색...";
|
2025-11-20 17:47:56 +09:00
|
|
|
|
2025-11-14 14:43:53 +09:00
|
|
|
const [inputValue, setInputValue] = useState("");
|
|
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
|
const [selectedData, setSelectedData] = useState<EntitySearchResult | null>(null);
|
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
const { searchText, setSearchText, results, loading, clearSearch } = useEntitySearch({
|
|
|
|
|
tableName,
|
|
|
|
|
searchFields,
|
|
|
|
|
filterCondition,
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-20 17:47:56 +09:00
|
|
|
// formData에서 현재 값 가져오기 (isInteractive 모드)
|
|
|
|
|
const currentValue = isInteractive && formData && component?.columnName
|
|
|
|
|
? formData[component.columnName]
|
|
|
|
|
: value;
|
|
|
|
|
|
2025-11-14 14:43:53 +09:00
|
|
|
// value가 변경되면 표시값 업데이트
|
|
|
|
|
useEffect(() => {
|
2025-11-20 17:47:56 +09:00
|
|
|
if (currentValue && selectedData) {
|
2025-11-14 14:43:53 +09:00
|
|
|
setInputValue(selectedData[displayField] || "");
|
2025-11-20 17:47:56 +09:00
|
|
|
} else if (!currentValue) {
|
2025-11-14 14:43:53 +09:00
|
|
|
setInputValue("");
|
|
|
|
|
setSelectedData(null);
|
|
|
|
|
}
|
2025-11-20 17:47:56 +09:00
|
|
|
}, [currentValue, displayField, selectedData]);
|
2025-11-14 14:43:53 +09:00
|
|
|
|
|
|
|
|
// 외부 클릭 감지
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleClickOutside = (event: MouseEvent) => {
|
|
|
|
|
if (containerRef.current && !containerRef.current.contains(event.target as Node)) {
|
|
|
|
|
setIsOpen(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.addEventListener("mousedown", handleClickOutside);
|
|
|
|
|
return () => document.removeEventListener("mousedown", handleClickOutside);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
const newValue = e.target.value;
|
|
|
|
|
setInputValue(newValue);
|
|
|
|
|
setSearchText(newValue);
|
|
|
|
|
setIsOpen(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSelect = (item: EntitySearchResult) => {
|
|
|
|
|
setSelectedData(item);
|
|
|
|
|
setInputValue(item[displayField] || "");
|
2025-11-20 13:47:21 +09:00
|
|
|
|
2025-11-20 17:47:56 +09:00
|
|
|
console.log("🔍 AutocompleteSearchInput handleSelect:", {
|
|
|
|
|
item,
|
|
|
|
|
valueField,
|
|
|
|
|
value: item[valueField],
|
|
|
|
|
config,
|
|
|
|
|
isInteractive,
|
|
|
|
|
hasOnFormDataChange: !!onFormDataChange,
|
|
|
|
|
columnName: component?.columnName,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// isInteractive 모드에서만 저장
|
|
|
|
|
if (isInteractive && onFormDataChange) {
|
|
|
|
|
// 필드 매핑 처리
|
|
|
|
|
if (config?.fieldMappings && Array.isArray(config.fieldMappings)) {
|
|
|
|
|
console.log("📋 필드 매핑 처리 시작:", config.fieldMappings);
|
|
|
|
|
|
|
|
|
|
config.fieldMappings.forEach((mapping: any, index: number) => {
|
|
|
|
|
const targetField = mapping.targetField || mapping.targetColumn;
|
|
|
|
|
|
|
|
|
|
console.log(` 매핑 ${index + 1}:`, {
|
|
|
|
|
sourceField: mapping.sourceField,
|
|
|
|
|
targetField,
|
|
|
|
|
label: mapping.label,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (mapping.sourceField && targetField) {
|
|
|
|
|
const sourceValue = item[mapping.sourceField];
|
|
|
|
|
|
|
|
|
|
console.log(` 값: ${mapping.sourceField} = ${sourceValue}`);
|
|
|
|
|
|
|
|
|
|
if (sourceValue !== undefined) {
|
|
|
|
|
console.log(` ✅ 저장: ${targetField} = ${sourceValue}`);
|
|
|
|
|
onFormDataChange(targetField, sourceValue);
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(` ⚠️ sourceField "${mapping.sourceField}"의 값이 undefined입니다`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(` ⚠️ 매핑 불완전: sourceField=${mapping.sourceField}, targetField=${targetField}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 기본 필드 저장 (columnName이 설정된 경우)
|
|
|
|
|
if (component?.columnName) {
|
|
|
|
|
console.log(`💾 기본 필드 저장: ${component.columnName} = ${item[valueField]}`);
|
|
|
|
|
onFormDataChange(component.columnName, item[valueField]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// onChange 콜백 호출 (호환성)
|
|
|
|
|
onChange?.(item[valueField], item);
|
2025-11-20 13:47:21 +09:00
|
|
|
|
2025-11-14 14:43:53 +09:00
|
|
|
setIsOpen(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleClear = () => {
|
|
|
|
|
setInputValue("");
|
|
|
|
|
setSelectedData(null);
|
|
|
|
|
onChange?.(null, null);
|
|
|
|
|
setIsOpen(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleInputFocus = () => {
|
|
|
|
|
// 포커스 시 항상 검색 실행 (빈 값이면 전체 목록)
|
|
|
|
|
if (!selectedData) {
|
|
|
|
|
setSearchText(inputValue || "");
|
|
|
|
|
setIsOpen(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn("relative", className)} ref={containerRef}>
|
|
|
|
|
{/* 입력 필드 */}
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<Input
|
|
|
|
|
value={inputValue}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
onFocus={handleInputFocus}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
disabled={disabled}
|
2025-11-21 17:00:25 +09:00
|
|
|
className="h-8 pr-16 text-xs sm:h-10 sm:text-sm !bg-background"
|
2025-11-14 14:43:53 +09:00
|
|
|
/>
|
2025-11-20 17:47:56 +09:00
|
|
|
<div className="absolute right-1 top-1/2 flex -translate-y-1/2 items-center gap-1">
|
2025-11-14 14:43:53 +09:00
|
|
|
{loading && (
|
|
|
|
|
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
|
|
|
|
|
)}
|
|
|
|
|
{inputValue && !disabled && (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={handleClear}
|
|
|
|
|
className="h-6 w-6 p-0"
|
|
|
|
|
>
|
|
|
|
|
<X className="h-3 w-3" />
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
<ChevronDown className="h-4 w-4 text-muted-foreground" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* 드롭다운 결과 */}
|
|
|
|
|
{isOpen && (results.length > 0 || loading) && (
|
2025-11-21 17:00:25 +09:00
|
|
|
<div className="absolute z-[100] mt-1 max-h-[300px] w-full overflow-y-auto rounded-md border bg-background shadow-lg">
|
2025-11-14 14:43:53 +09:00
|
|
|
{loading && results.length === 0 ? (
|
|
|
|
|
<div className="p-4 text-center text-sm text-muted-foreground">
|
2025-11-20 17:47:56 +09:00
|
|
|
<Loader2 className="mx-auto mb-2 h-4 w-4 animate-spin" />
|
2025-11-14 14:43:53 +09:00
|
|
|
검색 중...
|
|
|
|
|
</div>
|
|
|
|
|
) : results.length === 0 ? (
|
|
|
|
|
<div className="p-4 text-center text-sm text-muted-foreground">
|
|
|
|
|
검색 결과가 없습니다
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="py-1">
|
|
|
|
|
{results.map((item, index) => (
|
|
|
|
|
<button
|
|
|
|
|
key={index}
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => handleSelect(item)}
|
2025-11-20 17:47:56 +09:00
|
|
|
className="w-full px-3 py-2 text-left text-xs transition-colors hover:bg-accent sm:text-sm"
|
2025-11-14 14:43:53 +09:00
|
|
|
>
|
|
|
|
|
<div className="font-medium">{item[displayField]}</div>
|
|
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|