aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/molecules/forms/select-with-tooltip.tsx
blob: 5e48d62ad11822b3d6dfccd72a72feba28aa601e (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
import Select, { SelectProps } from '@components/atoms/forms/select';
import { FC, useState } from 'react';
import HelpButton from '../buttons/help-button';
import Tooltip, { TooltipProps } from '../modals/tooltip';
import styles from './select-with-tooltip.module.scss';

export type SelectWithTooltipProps = SelectProps &
  Pick<TooltipProps, 'title' | 'content'> & {
    /**
     * The select label.
     */
    label: string;
    /**
     * Set additional classes to the tooltip wrapper.
     */
    tooltipClasses?: string;
  };

/**
 * SelectWithTooltip component
 *
 * Render a select with a button to display a tooltip about options.
 */
const SelectWithTooltip: FC<SelectWithTooltipProps> = ({
  title,
  content,
  id,
  label,
  tooltipClasses = '',
  ...props
}) => {
  const [isTooltipOpened, setIsTooltipOpened] = useState<boolean>(false);
  const buttonModifier = isTooltipOpened ? styles['btn--activated'] : '';
  const tooltipModifier = isTooltipOpened
    ? styles['tooltip--visible']
    : styles['tooltip--hidden'];

  return (
    <div className={styles.wrapper}>
      <label htmlFor={id} className={styles.label}>
        {label}
      </label>
      <Select id={id} {...props} classes={styles.select} />
      <HelpButton
        onClick={() => setIsTooltipOpened(!isTooltipOpened)}
        classes={buttonModifier}
      />
      <Tooltip
        title={title}
        content={content}
        icon="?"
        classes={`${styles.tooltip} ${tooltipModifier} ${tooltipClasses}`}
      />
    </div>
  );
};

export default SelectWithTooltip;