summaryrefslogtreecommitdiffstats
path: root/src/components/Tooltip/Tooltip.tsx
blob: 5929c9668bddc64c3e4975ec503de7de5a5aa33b (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
import { ButtonHelp } from '@components/Buttons';
import { useState } from 'react';
import { useIntl } from 'react-intl';
import styles from './Tooltip.module.scss';

const Tooltip = ({
  message,
  title,
}: {
  message: string | string[];
  title: string;
}) => {
  const intl = useIntl();
  const [isOpen, setIsOpen] = useState<boolean>(false);

  const getMessageFromArray = (strings: string[]) => {
    let keyIndex = 0;
    return (
      <ul>
        {strings.map((string) => {
          keyIndex = keyIndex + 1;
          return <li key={`message-${keyIndex}`}>{string}</li>;
        })}
      </ul>
    );
  };

  const buttonTitle = isOpen
    ? intl.formatMessage({
        defaultMessage: 'Close help',
        description: 'Tooltip: button title',
      })
    : intl.formatMessage({
        defaultMessage: 'Show help',
        description: 'Tooltip: button title',
      });

  const wrapperModifier = isOpen ? styles.visible : styles.hidden;

  return (
    <div>
      <ButtonHelp
        showHelp={isOpen}
        setShowHelp={setIsOpen}
        title={buttonTitle}
      />
      <div className={`${styles.wrapper} ${wrapperModifier}`}>
        <div className={styles.title}>{title}</div>
        <div className={styles.message}>
          {Array.isArray(message) ? getMessageFromArray(message) : message}
        </div>
      </div>
    </div>
  );
};

export default Tooltip;