import React, { ReactNode, useEffect, useRef, useState } from 'react'; import { ListBulletIcon, CheckIcon } from '@heroicons/react/24/outline'; import { SortOption } from './SortFilterButton'; interface IconSortDropdownProps { options: SortOption[]; value: string; onChange: (value: string) => void; ariaLabel?: string; title?: string; className?: string; buttonClassName?: string; dropdownLabel?: string; align?: 'left' | 'right'; extraContent?: ReactNode; footerContent?: ReactNode; } const IconSortDropdown: React.FC = ({ options, value, onChange, ariaLabel = 'Sort items', title = 'Sort items', className = '', buttonClassName = '', dropdownLabel = 'Sort by', align = 'right', extraContent, footerContent, }) => { const [isOpen, setIsOpen] = useState(false); const dropdownRef = useRef(null); useEffect(() => { if (!isOpen) { return; } const handleClickOutside = (event: MouseEvent) => { if ( dropdownRef.current && !dropdownRef.current.contains(event.target as Node) ) { setIsOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, [isOpen]); return (
{isOpen && (
{dropdownLabel && (
{dropdownLabel}
)}
{options.map((option) => ( ))}
{extraContent && (
{extraContent}
)} {footerContent && (
{footerContent}
)}
)}
); }; export default IconSortDropdown;