Files
kandev/apps/web/components/kanban/task-search-input.tsx
T
2026-02-13 00:00:04 +00:00

94 lines
2.4 KiB
TypeScript

'use client';
import { useCallback, useEffect, useRef, useState } from 'react';
import { Input } from '@kandev/ui/input';
import { IconSearch, IconX, IconLoader2 } from '@tabler/icons-react';
import { cn } from '@/lib/utils';
interface TaskSearchInputProps {
value: string;
onChange: (value: string) => void;
placeholder?: string;
debounceMs?: number;
isLoading?: boolean;
className?: string;
}
export function TaskSearchInput({
value,
onChange,
placeholder = 'Search tasks...',
debounceMs = 300,
isLoading = false,
className,
}: TaskSearchInputProps) {
const [localValue, setLocalValue] = useState(value);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
// Sync local value when external value changes
useEffect(() => {
setLocalValue(value);
}, [value]);
const handleChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value;
setLocalValue(newValue);
// Clear existing timeout
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
// Debounce the onChange callback
timeoutRef.current = setTimeout(() => {
onChange(newValue);
}, debounceMs);
},
[onChange, debounceMs]
);
const handleClear = useCallback(() => {
setLocalValue('');
onChange('');
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
}, [onChange]);
// Cleanup timeout on unmount
useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, []);
return (
<div className={cn('relative', className)}>
{isLoading ? (
<IconLoader2 className="absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground pointer-events-none animate-spin" />
) : (
<IconSearch className="absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground pointer-events-none" />
)}
<Input
type="text"
value={localValue}
onChange={handleChange}
placeholder={placeholder}
className="pl-8 pr-8 w-64 border border-border"
/>
{localValue && !isLoading && (
<button
type="button"
onClick={handleClear}
className="absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
>
<IconX className="h-4 w-4" />
</button>
)}
</div>
);
}