import useClickOutside from '@utils/hooks/use-click-outside'; import { FC, useRef, useState } from 'react'; import HelpButton from '../buttons/help-button'; import Tooltip, { type TooltipProps } from '../modals/tooltip'; import LabelledSelect, { type LabelledSelectProps } from './labelled-select'; import styles from './select-with-tooltip.module.scss'; export type SelectWithTooltipProps = Omit< LabelledSelectProps, 'labelPosition' > & Pick & { /** * Set additional classnames to the select wrapper. */ className?: string; /** * Set additional classnames to the tooltip wrapper. */ tooltipClassName?: TooltipProps['className']; }; /** * SelectWithTooltip component * * Render a select with a button to display a tooltip about options. */ const SelectWithTooltip: FC = ({ className = '', content, id, title, tooltipClassName = '', ...props }) => { const [isTooltipOpened, setIsTooltipOpened] = useState(false); const buttonRef = useRef(null); const tooltipRef = useRef(null); const buttonModifier = isTooltipOpened ? styles['btn--activated'] : ''; const tooltipModifier = isTooltipOpened ? styles['tooltip--visible'] : styles['tooltip--hidden']; const closeTooltip = (target: EventTarget) => { if (buttonRef.current && !buttonRef.current.contains(target as Node)) setIsTooltipOpened(false); }; useClickOutside( tooltipRef, (target) => isTooltipOpened && closeTooltip(target) ); return (
setIsTooltipOpened(!isTooltipOpened)} ref={buttonRef} />
); }; export default SelectWithTooltip;