import React, { useState, useCallback, useRef } from 'react' import { cn } from '../lib/utils' import { Search, X } from 'lucide-react' interface SearchBarProps { placeholder?: string onSearch?: (query: string) => void debounceMs?: number className?: string value?: string onChange?: (value: string) => void } export function SearchBar({ placeholder = 'Search...', onSearch, debounceMs = 300, className, value: controlledValue, onChange, }: SearchBarProps) { const [internalValue, setInternalValue] = useState('') const timerRef = useRef>(null) const value = controlledValue !== undefined ? controlledValue : internalValue const setValue = onChange || setInternalValue const handleChange = useCallback( (e: React.ChangeEvent) => { const newValue = e.target.value setValue(newValue) if (timerRef.current) clearTimeout(timerRef.current) timerRef.current = setTimeout(() => { onSearch?.(newValue) }, debounceMs) }, [onSearch, debounceMs, setValue] ) const handleClear = useCallback(() => { setValue('') onSearch?.('') }, [onSearch, setValue]) return (
{value && ( )}
) }