import {useEffect, useRef, useState} from "react"; import styles from "../styles/select.module.css"; export function Select({multiple, value, onChange, options}) { const [isOpen, setIsOpen] = useState(false); const [highlightedIndex, setHighlightedIndex] = useState(0); const containerRef = useRef(null); /** * Closes the dropdown menu when the user clicks outside the container. */ function clearOptions() { multiple ? onChange([]) : onChange(undefined); } /** * Selects an option from the dropdown menu. * @param option */ function selectOption(option) { if (multiple) { const isOptionIncluded = value.some( (val) => val.label === option.label && val.value === option.value ); if (isOptionIncluded) { // Remove the option object from the value array const newValue = value.filter( (val) => val.label !== option.label || val.value !== option.value ); onChange(newValue); } else { onChange([...value, option]); } } else { if (option !== value) onChange(option); } } /** * Determines if an option is selected. * @param option * @returns {*|boolean} */ function isOptionSelected(option) { if (multiple) { const isOptionIncluded = value.some( (val) => val.label === option.label && val.value === option.value ); return isOptionIncluded; } else { return option === value; } } /** * Closes the dropdown menu when the user clicks outside the container. */ useEffect(() => { if (isOpen) setHighlightedIndex(0); }, [isOpen]); // Close the dropdown menu when the user clicks outside the container return (