import React, { Fragment, useState, useEffect } from 'react'; import { Dialog, Disclosure, Menu, Popover, Transition, } from '@headlessui/react'; import { XMarkIcon } from '@heroicons/react/24/outline'; import { ChevronDownIcon } from '@heroicons/react/20/solid'; import { Link } from '../Link/Link'; const styles = { dropDownTitles: `inline-flex justify-center text-sm font-medium text-cu-black-800 group hover:text-cu-red`, chevron: `flex-shrink-0 w-5 h-5 ml-1 text-cu-black-300 group-hover:text-cu-black-600`, }; export interface FilterProps { sortOptions?: { name: string; href: string; current: boolean; }[]; filters: { id: string; name: string; options: { value: string; label: string; checked: boolean; }[]; }[]; callback: any; } function classNames(...classes: string[]) { return classes.filter(Boolean).join(' '); } export const Filter = ({ sortOptions, filters, callback }: FilterProps) => { const [open, setOpen] = useState(false); const [sortItem, setSortItem] = useState(''); const [selectedItems, setSelectedItems] = useState([]); const isSelected = (name: string) => selectedItems.includes(name); const [activeFilters, setActiveFilters] = useState([]); const handleSelect = (name: string) => { const selected: string = name; if (!selectedItems.includes(selected)) { return setSelectedItems([...selectedItems, selected]); } return setSelectedItems( [...selectedItems].filter(item => item !== selected) ); }; const handleRemove = (name: string) => { const selected: string = name; return setSelectedItems( [...selectedItems].filter(item => item !== selected) ); }; useEffect(() => { setSortItem(sortItem); setActiveFilters(selectedItems); }, [sortItem, selectedItems]); useEffect(() => { callback(selectedItems); }, [selectedItems, callback]); return (
{/* Mobile filter dialog */}

Filters

{/* Filters */}
{filters.map(section => ( {({ open }) => ( <>

{section.name}

{section.options.map((option, optionIdx) => (
handleSelect(option.label)} defaultChecked={isSelected(option.label)} className="w-4 h-4 rounded border-cu-black-200 text-cu-red focus:ring-cu-red-100" />
))}
)}
))}

Filters

{/* Filter selectors */}
{sortOptions && sortOptions?.length > 0 && (
Sort
)} {/* Sort drop down menu */} {sortOptions && sortOptions?.length > 0 && (
{sortOptions.map(option => ( {({ active }) => ( setSortItem(option.name)}> {option.name} )} ))}
)}
{filters.map((section, sectionIdx) => ( {section.name}
{section.options.map((option, optionIdx) => (
handleSelect(option.label)} defaultChecked={isSelected(option.label)} className="w-4 h-4 rounded border-cu-black-200 text-cu-red focus:ring-cu-red-100" />
))}
))}
{/* Active filters */}

Filters , active

); };