import React, { useEffect, useState } from 'react'; import { GlobalSearchConfig } from '../../types'; import { cn } from '../../utils/cn'; interface TableSearchProps { /** * Value of the search input */ value: string; /** * Callback when search input changes */ onChange: (value: string) => void; /** * Global search configuration */ config?: GlobalSearchConfig; /** * Custom CSS class */ className?: string; /** * Whether search is enabled */ enabled?: boolean; } const TableSearch: React.FC = ({ value, onChange, config, className, enabled = true, }) => { // Local state for debounced input const [inputValue, setInputValue] = useState(value); // For debounced search const debounceMs = config?.debounceMs || 300; // Update local state when external value changes useEffect(() => { setInputValue(value); }, [value]); // Debounce input changes useEffect(() => { if (!enabled) return; const timeoutId = setTimeout(() => { if (inputValue !== value) { onChange(inputValue); } }, debounceMs); return () => clearTimeout(timeoutId); }, [inputValue, value, onChange, debounceMs, enabled]); // Handle input change const handleChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); }; // Handle clear search const handleClear = () => { setInputValue(''); onChange(''); }; // If search is disabled, return null if (!enabled) { return null; } return (
{/* Search icon */} {/* Search input */} {/* Clear button (only shown when there's a value) */} {inputValue && ( )}
); }; export default TableSearch;