From a6ff5eee45215effb3344cb5d631a27a7c0369aa Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Fri, 22 Sep 2023 19:34:01 +0200
Subject: refactor(components): rewrite form components

---
 src/components/atoms/forms/fieldset/fieldset.tsx | 68 ++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 src/components/atoms/forms/fieldset/fieldset.tsx

(limited to 'src/components/atoms/forms/fieldset/fieldset.tsx')

diff --git a/src/components/atoms/forms/fieldset/fieldset.tsx b/src/components/atoms/forms/fieldset/fieldset.tsx
new file mode 100644
index 0000000..eb42961
--- /dev/null
+++ b/src/components/atoms/forms/fieldset/fieldset.tsx
@@ -0,0 +1,68 @@
+import {
+  forwardRef,
+  type FieldsetHTMLAttributes,
+  ForwardRefRenderFunction,
+  ReactElement,
+} from 'react';
+import styles from './fieldset.module.scss';
+import { LegendProps } from '../legend';
+
+export type FieldsetProps = Omit<
+  FieldsetHTMLAttributes<HTMLFieldSetElement>,
+  'disabled' | 'hidden'
+> & {
+  /**
+   * Should the fieldset be disabled?
+   *
+   * @default false
+   */
+  isDisabled?: boolean;
+  /**
+   * Should the fieldset contents be inlined?
+   *
+   * @default false
+   */
+  isInline?: boolean;
+  /**
+   * The fieldset legend.
+   */
+  legend?: ReactElement<LegendProps>;
+};
+
+/**
+ * Fieldset component.
+ */
+const FieldsetWithRef: ForwardRefRenderFunction<
+  HTMLFieldSetElement,
+  FieldsetProps
+> = (
+  {
+    children,
+    className = '',
+    isDisabled = false,
+    isInline = false,
+    legend,
+    ...props
+  },
+  ref
+) => {
+  const layoutModifier = isInline
+    ? styles['fieldset--inline']
+    : styles['fieldset--stack'];
+  const legendModifier = legend ? styles['fieldset--has-legend'] : '';
+  const fieldsetClass = `${styles.fieldset} ${legendModifier} ${layoutModifier} ${className}`;
+
+  return (
+    <fieldset
+      {...props}
+      className={fieldsetClass}
+      disabled={isDisabled}
+      ref={ref}
+    >
+      {legend}
+      {children}
+    </fieldset>
+  );
+};
+
+export const Fieldset = forwardRef(FieldsetWithRef);
-- 
cgit v1.2.3

