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/label/label.tsx | 62 ++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 src/components/atoms/forms/label/label.tsx

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

diff --git a/src/components/atoms/forms/label/label.tsx b/src/components/atoms/forms/label/label.tsx
new file mode 100644
index 0000000..5087325
--- /dev/null
+++ b/src/components/atoms/forms/label/label.tsx
@@ -0,0 +1,62 @@
+import { FC, LabelHTMLAttributes, ReactNode } from 'react';
+import styles from './label.module.scss';
+
+export type LabelSize = 'md' | 'sm';
+
+export type LabelProps = Omit<
+  LabelHTMLAttributes<HTMLLabelElement>,
+  'hidden' | 'size'
+> & {
+  /**
+   * The label body.
+   */
+  children: ReactNode;
+  /**
+   * Should the label be hidden?
+   *
+   * @default false
+   */
+  isHidden?: boolean;
+  /**
+   * Is the field required?
+   *
+   * @default false
+   */
+  isRequired?: boolean;
+  /**
+   * The label size.
+   *
+   * @default 'sm'
+   */
+  size?: LabelSize;
+};
+
+/**
+ * Label Component
+ *
+ * Render a HTML label element.
+ */
+export const Label: FC<LabelProps> = ({
+  children,
+  className = '',
+  isHidden = false,
+  isRequired = false,
+  size = 'sm',
+  ...props
+}) => {
+  const visibilityClass = isHidden ? 'screen-reader-text' : '';
+  const sizeClass = styles[`label--${size}`];
+  const labelClass = `${styles.label} ${sizeClass} ${visibilityClass} ${className}`;
+  const requiredSymbol = ' *';
+
+  return (
+    <label {...props} className={labelClass}>
+      {children}
+      {isRequired ? (
+        <span aria-hidden className={styles.required}>
+          {requiredSymbol}
+        </span>
+      ) : null}
+    </label>
+  );
+};
-- 
cgit v1.2.3

