"use client"; /** * Search 메타 컴포넌트 렌더러 * - config.fields 배열로 검색 필드들을 가로로 배치 * - "검색" 버튼 클릭 시 v2EventBus로 TABLE_REFRESH + 필터 전달 * - "초기화" 버튼으로 모든 필터 클리어 * - 디자인 모드: 비활성 상태로 표시 */ import React, { useState } from "react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Search, X } from "lucide-react"; import { v2EventBus } from "@/lib/v2-core/events/EventBus"; import { V2_EVENTS } from "@/lib/v2-core/events/types"; interface SearchField { columnName: string; label: string; searchType?: "text" | "select" | "date"; options?: { value: string; label: string }[]; } interface SearchRendererProps { id: string; config: { fields: SearchField[]; targetTableId?: string; // 연결된 DataView ID (옵션) targetTableName?: string; // 🔍 대상 테이블명 (필터 식별용) }; isDesignMode?: boolean; disabled?: boolean; className?: string; } export function SearchRenderer({ id, config, isDesignMode = false, disabled = false, className, }: SearchRendererProps) { const { fields = [] } = config; const [filters, setFilters] = useState>({}); // 검색 실행 const handleSearch = () => { if (isDesignMode || disabled) return; // 🔍 v2EventBus로 TABLE_REFRESH 이벤트 발행 v2EventBus.emitSync(V2_EVENTS.TABLE_REFRESH, { filters, sourceId: id, targetId: config.targetTableId, tableName: config.targetTableName, // 대상 테이블명 전달 }); }; // 필터 초기화 const handleReset = () => { if (isDesignMode || disabled) return; setFilters({}); // 🔍 빈 필터로 새로고침 v2EventBus.emitSync(V2_EVENTS.TABLE_REFRESH, { filters: {}, sourceId: id, targetId: config.targetTableId, tableName: config.targetTableName, // 대상 테이블명 전달 }); }; // 필드 값 변경 const handleFieldChange = (columnName: string, value: any) => { setFilters((prev) => ({ ...prev, [columnName]: value })); }; // Enter 키 처리 const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { handleSearch(); } }; if (fields.length === 0) { return (
검색 필드를 추가하세요
); } return (
{fields.map((field) => (
{field.searchType === "select" && field.options ? ( ) : ( handleFieldChange(field.columnName, e.target.value)} onKeyDown={handleKeyDown} disabled={isDesignMode || disabled} className="h-8 w-32 sm:h-10 sm:w-40" /> )}
))} {/* 검색 버튼 */} {/* 초기화 버튼 */} {/* 디자인 모드 표시 */} {isDesignMode && ( (디자인 모드: 비활성) )}
); }