From a1e8f1e4426ed3560ce1b76fb73a6969388ed253 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Thu, 7 Apr 2022 22:57:15 +0200
Subject: chore: add a SelectWithTooltip component

---
 .../molecules/forms/select-with-tooltip.tsx        | 58 ++++++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 src/components/molecules/forms/select-with-tooltip.tsx

(limited to 'src/components/molecules/forms/select-with-tooltip.tsx')

diff --git a/src/components/molecules/forms/select-with-tooltip.tsx b/src/components/molecules/forms/select-with-tooltip.tsx
new file mode 100644
index 0000000..5e48d62
--- /dev/null
+++ b/src/components/molecules/forms/select-with-tooltip.tsx
@@ -0,0 +1,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;
-- 
cgit v1.2.3

