summaryrefslogtreecommitdiffstats
path: root/src/components/atoms/forms/boolean-field.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-30 19:27:21 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-31 23:15:07 +0200
commit782cc0a31a866519fb7c3e18a523b347d3e40238 (patch)
tree34fb984e8dc356cd013e5e0d6f203a8c8907b9fe /src/components/atoms/forms/boolean-field.tsx
parent519b1439dce3f25dd38cd0d0f82fecd10f28dcc8 (diff)
chore: replace Checkbox component with a BooleanField component
Checkbox and radio buttons are working the same way so I decided to group them in a same component.
Diffstat (limited to 'src/components/atoms/forms/boolean-field.tsx')
-rw-r--r--src/components/atoms/forms/boolean-field.tsx62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/components/atoms/forms/boolean-field.tsx b/src/components/atoms/forms/boolean-field.tsx
new file mode 100644
index 0000000..946e375
--- /dev/null
+++ b/src/components/atoms/forms/boolean-field.tsx
@@ -0,0 +1,62 @@
+import { ChangeEventHandler, FC, MouseEventHandler } from 'react';
+import styles from './boolean-field.module.scss';
+
+export type BooleanFieldProps = {
+ /**
+ * One or more ids that refers to the checkbox name.
+ */
+ 'aria-labelledby'?: string;
+ /**
+ * True if the field should be checked.
+ */
+ checked: boolean;
+ /**
+ * Add classnames to the checkbox.
+ */
+ className?: string;
+ /**
+ * Field id attribute.
+ */
+ id: string;
+ /**
+ * True if the field should be visually hidden. Default: false.
+ */
+ hidden?: boolean;
+ /**
+ * Field name attribute.
+ */
+ name: string;
+ /**
+ * Callback function to handle state change.
+ */
+ onChange: ChangeEventHandler<HTMLInputElement>;
+ /**
+ * A callback function to handle click.
+ */
+ onClick?: MouseEventHandler<HTMLInputElement>;
+ /**
+ * The input type.
+ */
+ type: 'checkbox' | 'radio';
+ /**
+ * Field name attribute.
+ */
+ value: string;
+};
+
+/**
+ * BooleanField component
+ *
+ * Render a checkbox or a radio input type.
+ */
+const BooleanField: FC<BooleanFieldProps> = ({
+ className = '',
+ hidden = false,
+ ...props
+}) => {
+ const modifier = hidden ? 'hidden' : '';
+
+ return <input className={`${styles[modifier]} ${className}`} {...props} />;
+};
+
+export default BooleanField;