import React, { useState, useEffect } from 'react'; import { Form } from '../basic/Form'; import { Input } from '../basic/Input'; import { Button } from '../basic/Button'; import { Icon } from '../basic/Icon'; import { IconName } from '../basic/IconTypes'; import { Div } from '../basic/Div'; import { Span } from '../basic/Span'; // G7Core.t() 번역 함수 참조 const t = (key: string, params?: Record) => (window as any).G7Core?.t?.(key, params) ?? key; export interface SearchSuggestion { id: string | number; text: string; } export interface SearchBarProps { name?: string; placeholder?: string; value?: string; onChange?: (e: React.ChangeEvent) => void; onSubmit?: (e: React.FormEvent) => void; showButton?: boolean; // true: 검색 버튼 표시, false: 버튼 숨김 (기본값: false) suggestions?: SearchSuggestion[]; onSuggestionClick?: (suggestion: SearchSuggestion) => void; showSuggestions?: boolean; className?: string; style?: React.CSSProperties; } /** * SearchBar 집합 컴포넌트 * * 검색 입력 필드와 선택적 버튼을 제공하는 검색 바 컴포넌트입니다. * Enter 키를 누르면 항상 검색이 실행됩니다. * * 기본 컴포넌트 조합: Form + Input + Button + Icon + Div + Span * * @example * // 레이아웃 JSON 사용 예시 (버튼 없음) * { * "name": "SearchBar", * "props": { * "placeholder": "검색어를 입력하세요", * "value": "{{query.search}}", * "showButton": false * } * } * * @example * // 레이아웃 JSON 사용 예시 (버튼 있음) * { * "name": "SearchBar", * "props": { * "placeholder": "검색어를 입력하세요", * "value": "{{query.search}}", * "showButton": true * } * } */ export const SearchBar: React.FC = ({ name = 'search', placeholder, value: controlledValue, onChange, onSubmit, showButton = false, suggestions = [], onSuggestionClick, showSuggestions = false, className = '', style, }) => { // props로 전달된 값이 없으면 다국어 키 사용 const resolvedPlaceholder = placeholder ?? t('common.search_placeholder'); const [internalValue, setInternalValue] = useState(''); const [isFocused, setIsFocused] = useState(false); const containerRef = React.useRef(null); const previousValueRef = React.useRef(''); const value = controlledValue !== undefined ? controlledValue : internalValue; const shouldShowSuggestions = showSuggestions && isFocused && suggestions.length > 0 && value.length > 0; // controlledValue가 변경될 때 previousValueRef 동기화 useEffect(() => { if (controlledValue !== undefined) { previousValueRef.current = controlledValue; } }, [controlledValue]); const handleChange = (e: React.ChangeEvent) => { const newValue = e.target.value; setInternalValue(newValue); onChange?.(e); // X 버튼으로 검색어를 지운 경우 감지 // 이전 값이 있었는데 새 값이 빈 문자열이고, nativeEvent가 없는 경우 = X 버튼 클릭 if (previousValueRef.current !== '' && newValue === '' && !(e.nativeEvent as any).inputType) { // X 버튼 클릭: 자동으로 검색 초기화 setTimeout(() => { const formElement = containerRef.current?.querySelector('form'); if (formElement) { formElement.requestSubmit(); } }, 0); } previousValueRef.current = newValue; }; const handleSuggestionClick = (suggestion: SearchSuggestion) => { setInternalValue(suggestion.text); const syntheticEvent = { target: { value: suggestion.text, name }, } as React.ChangeEvent; onChange?.(syntheticEvent); onSuggestionClick?.(suggestion); setIsFocused(false); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); onSubmit?.(e); }; return (
{/* Input wrapper */}
{/* 검색 아이콘 */}
{/* 검색 입력 */} setIsFocused(true)} onBlur={() => setTimeout(() => setIsFocused(false), 200)} placeholder={resolvedPlaceholder} className="w-full pl-10 pr-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white text-sm" />
{/* 검색 버튼 (showButton=true일 때만 표시) */} {showButton && ( )}
{/* 자동완성 제안 목록 */} {shouldShowSuggestions && (
{suggestions.map((suggestion) => (
handleSuggestionClick(suggestion)} >
{suggestion.text}
))}
)}
); };