// eslint-disable-next-line import/no-duplicates import * as React from 'react'; // eslint-disable-next-line import/no-duplicates import {useEffect, useState, useRef} from 'react'; import classNames from 'classnames'; import Icon, {ICON_COLOR} from '../icons/Icon'; type LinksType = Readonly<{ label: string; url: string; }>; export type DropdownPropsType = Readonly<{ name: string; links: Array; initiallyOpened?: boolean; color?: 'default' | 'white'; fullWidth?: boolean; onItemSelect?: ( e: React.SyntheticEvent, link: LinksType ) => void; }>; // if possible, use Select component instead const Dropdown = ({ name, links, initiallyOpened, color = 'default', fullWidth, onItemSelect, }: DropdownPropsType) => { const [open, setOpen] = useState(initiallyOpened || false); const clickedInside = useRef(false); function handleClickInside() { setOpen(prevOpen => !prevOpen); clickedInside.current = true; } function handleClickOutside() { if (clickedInside.current) { clickedInside.current = false; return; } setOpen(false); } useEffect(() => { document.addEventListener('click', handleClickOutside); return () => document.removeEventListener('click', handleClickOutside); }, []); return (

{name}

{open && (
{links.map((link, index) => ( onItemSelect(e, link) : undefined} > {link.label} ))}
)}
); }; export default Dropdown;