51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { useState, useEffect, useRef } from 'react';
|
|
import { Search } from 'lucide-react';
|
|
|
|
interface SearchInputProps {
|
|
value: string;
|
|
onChange: (value: string) => void;
|
|
placeholder?: string;
|
|
debounceMs?: number;
|
|
}
|
|
|
|
export function SearchInput({
|
|
value,
|
|
onChange,
|
|
placeholder = 'Buscar...',
|
|
debounceMs = 300,
|
|
}: SearchInputProps) {
|
|
const [localValue, setLocalValue] = useState(value);
|
|
const timerRef = useRef<ReturnType<typeof setTimeout>>(undefined);
|
|
|
|
useEffect(() => {
|
|
setLocalValue(value);
|
|
}, [value]);
|
|
|
|
function handleChange(newValue: string) {
|
|
setLocalValue(newValue);
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
timerRef.current = setTimeout(() => {
|
|
onChange(newValue);
|
|
}, debounceMs);
|
|
}
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (timerRef.current) clearTimeout(timerRef.current);
|
|
};
|
|
}, []);
|
|
|
|
return (
|
|
<div className="relative">
|
|
<Search size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-text-muted" />
|
|
<input
|
|
type="text"
|
|
value={localValue}
|
|
onChange={(e) => handleChange(e.target.value)}
|
|
placeholder={placeholder}
|
|
className="w-full rounded border bg-bg pl-9 pr-3 py-2 text-body text-primary placeholder:text-text-muted focus:border-isis-blue focus:outline-none"
|
|
/>
|
|
</div>
|
|
);
|
|
}
|