blob: 29e256340e4868bb514c8ba696dab5a63b02bc8d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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<TooltipProps, 'title' | 'content'> & {
/**
* 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<SelectWithTooltipProps> = ({
title,
content,
id,
tooltipClassName = '',
...props
}) => {
const [isTooltipOpened, setIsTooltipOpened] = useState<boolean>(false);
const buttonRef = useRef<HTMLButtonElement>(null);
const tooltipRef = useRef<HTMLDivElement>(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 (
<div className={styles.wrapper}>
<LabelledSelect
labelPosition="left"
id={id}
labelClassName={styles.label}
{...props}
/>
<HelpButton
className={`${styles.btn} ${buttonModifier}`}
onClick={() => setIsTooltipOpened(!isTooltipOpened)}
ref={buttonRef}
/>
<Tooltip
title={title}
content={content}
icon="?"
className={`${styles.tooltip} ${tooltipModifier} ${tooltipClassName}`}
ref={tooltipRef}
/>
</div>
);
};
export default SelectWithTooltip;
|