diff --git a/compass/components/resource/LandingSearchBar.tsx b/compass/components/resource/LandingSearchBar.tsx index fe90fc6..e3f8600 100644 --- a/compass/components/resource/LandingSearchBar.tsx +++ b/compass/components/resource/LandingSearchBar.tsx @@ -1,18 +1,38 @@ import { FunnelIcon as FunnelIconOutline } from "@heroicons/react/24/outline"; import { + ArchiveBoxIcon, + ChevronDownIcon, FunnelIcon, MagnifyingGlassIcon, + TagIcon, XMarkIcon, } from "@heroicons/react/24/solid"; -import React, { useState } from "react"; +import React, { + ReactNode, + SetStateAction, + useState, + useRef, + useEffect, +} from "react"; import Image from "next/image"; import { FilterBox } from "../FilterBox"; export const LandingSearchBar: React.FC = () => { const [searchTerm, setSearchTerm] = useState(""); + const [showFilters, setShowFilters] = useState(false); const [showFilterBox, setShowFilterBox] = useState(false); const toggleFilterBox = () => setShowFilterBox((prev) => !prev); + const collections = ["Resources", "Services"]; + const [selectedCollections, setSelectedCollections] = useState( + new Array(collections.length).fill(false) + ); + + const tags = ["Food Relief", "Period Poverty", "Nutrition Education"]; + const [selectedTags, setSelectedTags] = useState( + new Array(tags.length).fill(false) + ); + const handleSearchChange = (event: React.ChangeEvent) => { setSearchTerm(event.target.value); }; @@ -57,19 +77,39 @@ export const LandingSearchBar: React.FC = () => { + + {/* Search filters */} +
+ + +
+ {/* search results, for now since it's empty this is the default screen */}
{
); }; + +// Closes the filter dropdown when the user clicks outside the dropdown. +const useFilterPillDropdown = ( + ref: React.RefObject, + setShowDropdown: Function +) => { + // Close on outside click + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if (ref.current && !ref.current.contains(event.target as Node)) { + if (setShowDropdown) setShowDropdown(false); + } + }; + + document.addEventListener("mousedown", handleClickOutside); + return () => + // Unbind the event listener on cleanup. + document.removeEventListener("mousedown", handleClickOutside); + }, [ref, setShowDropdown]); +}; + +interface FilterPillProps { + icon: React.ForwardRefExoticComponent< + Omit, "ref"> + >; + name: string; + searchBar?: boolean; + options: string[]; + selectedOptions: boolean[]; + setSelectedOptions: React.Dispatch>; +} + +const FilterPill: React.FC = ({ + icon, + name, + options, + selectedOptions, + setSelectedOptions, + searchBar = false, +}) => { + const Icon = icon; + const [showDropdown, setShowDropdown] = useState(false); + const [isActive, setIsActive] = useState(false); + const dropdownRef = useRef(null); + + const handleCheck = ( + e: React.ChangeEvent, + item: string + ) => { + const selected = selectedOptions.map((o, i) => { + if (i == options.indexOf(item)) { + return e.target.checked; + } else { + return o; + } + }); + + setSelectedOptions(selected); + setIsActive(selected.includes(true)); + }; + + // Closes dropdown when clicked outside + useFilterPillDropdown(dropdownRef, setShowDropdown); + + return ( +
+ {/* The filter pill */} + + + {/* The filter option selection dropdown */} +
+ + {options.map((item, index) => { + return ( + + ); + })} +
+
+ ); +};