"use client"; import React, { useState, useEffect } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Search, Loader2 } from "lucide-react"; import { useEntitySearch } from "./useEntitySearch"; import { EntitySearchResult } from "./types"; interface EntitySearchModalProps { open: boolean; onOpenChange: (open: boolean) => void; tableName: string; displayField: string; valueField: string; searchFields?: string[]; filterCondition?: Record; modalTitle?: string; modalColumns?: string[]; onSelect: (value: any, fullData: EntitySearchResult) => void; } export function EntitySearchModal({ open, onOpenChange, tableName, displayField, valueField, searchFields = [displayField], filterCondition = {}, modalTitle = "검색", modalColumns = [], onSelect, }: EntitySearchModalProps) { const [localSearchText, setLocalSearchText] = useState(""); const { results, loading, error, pagination, search, clearSearch, loadMore, } = useEntitySearch({ tableName, searchFields, filterCondition, }); // 모달 열릴 때 초기 검색 useEffect(() => { if (open) { search("", 1); // 빈 검색어로 전체 목록 조회 } else { clearSearch(); setLocalSearchText(""); } }, [open]); const handleSearch = () => { search(localSearchText, 1); }; const handleSelect = (item: EntitySearchResult) => { onSelect(item[valueField], item); onOpenChange(false); }; // 표시할 컬럼 결정 const displayColumns = modalColumns.length > 0 ? modalColumns : [displayField]; return ( {modalTitle} 항목을 검색하고 선택하세요 {/* 검색 입력 */}
setLocalSearchText(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleSearch(); } }} className="h-8 text-xs sm:h-10 sm:text-sm" />
{/* 오류 메시지 */} {error && (
{error}
)} {/* 검색 결과 테이블 */}
{displayColumns.map((col) => ( ))} {loading && results.length === 0 ? ( ) : results.length === 0 ? ( ) : ( results.map((item, index) => { const uniqueKey = item[valueField] !== undefined ? `${item[valueField]}` : `row-${index}`; return ( handleSelect(item)} > {displayColumns.map((col) => ( ))} ); }) )}
{col} 선택

검색 중...

검색 결과가 없습니다
{item[col] || "-"}
{/* 페이지네이션 정보 */} {results.length > 0 && (
전체 {pagination.total}개 중 {results.length}개 표시 {pagination.page * pagination.limit < pagination.total && ( )}
)}
); }