import React, { useState, useRef, useEffect } from 'react'; interface InColumnStockFilterProps { currentFilters: string[]; onFilterChange: (newFilters: string[]) => void; title?: string; options?: string[]; icon?: React.ReactNode; } const DEFAULT_STOCK_OPTIONS = [ 'Out of Stock (0)', 'In Stock (>0)', 'Low Stock (<10)', ]; export const InColumnStockFilter: React.FC = ({ currentFilters, onFilterChange, title = "Stock Filter", options = DEFAULT_STOCK_OPTIONS, icon }) => { const [isOpen, setIsOpen] = useState(false); const [customInput, setCustomInput] = useState(''); const containerRef = useRef(null); const inputRef = useRef(null); // Close when clicking outside useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (containerRef.current && !containerRef.current.contains(event.target as Node)) { setIsOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, []); const toggleFilter = (option: string) => { let newFilters = [...currentFilters]; if (newFilters.includes(option)) { newFilters = newFilters.filter(f => f !== option); } else { newFilters.push(option); } onFilterChange(newFilters); }; const clearFilters = () => { onFilterChange([]); setIsOpen(false); }; const handleCustomAdd = () => { if (customInput.trim()) { if (!currentFilters.includes(customInput.trim())) { onFilterChange([...currentFilters, customInput.trim()]); } setCustomInput(''); } }; return (
{isOpen && (
{title} {currentFilters.length > 0 && ( )}
{/* Custom Input */}
setCustomInput(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') handleCustomAdd(); e.stopPropagation(); // Prevent grid row clicks if any }} autoFocus />
{options.map(option => { const isSelected = currentFilters.includes(option); return (
{ e.stopPropagation(); toggleFilter(option); }} className={`flex items-center gap-2 px-2 py-1.5 rounded-lg cursor-pointer transition-colors ${isSelected ? 'bg-indigo-500/20 text-indigo-300' : 'text-slate-300 hover:bg-white/5'}`} >
{isSelected && }
{option}
); })}
)}
); };