From a4483b7d71f3213fc6bfe2a72b167abfc8d6c34d Mon Sep 17 00:00:00 2001 From: Tobias Johansson Date: Fri, 6 Sep 2024 10:56:58 +0200 Subject: [PATCH] feat(SW-360): Added checkbox component --- .../Form/Checkbox/checkbox.module.css | 39 +++++++++++++++ .../Form/Checkbox/checkbox.ts | 7 +++ .../TempDesignSystem/Form/Checkbox/index.tsx | 48 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 components/TempDesignSystem/Form/Checkbox/checkbox.module.css create mode 100644 components/TempDesignSystem/Form/Checkbox/checkbox.ts create mode 100644 components/TempDesignSystem/Form/Checkbox/index.tsx diff --git a/components/TempDesignSystem/Form/Checkbox/checkbox.module.css b/components/TempDesignSystem/Form/Checkbox/checkbox.module.css new file mode 100644 index 000000000..7f7c8f04f --- /dev/null +++ b/components/TempDesignSystem/Form/Checkbox/checkbox.module.css @@ -0,0 +1,39 @@ +.container { + display: flex; + flex-direction: column; + color: var(--text-color); +} + +.container[data-selected] .checkbox { + border: none; + background: var(--UI-Input-Controls-Fill-Selected); +} + +.checkboxContainer { + display: flex; + align-items: flex-start; + gap: var(--Spacing-x-one-and-half); +} + +.checkbox { + flex-grow: 1; + width: 24px; + height: 24px; + min-width: 24px; + border: 2px solid var(--UI-Input-Controls-Border-Normal); + border-radius: 4px; + transition: all 200ms; + display: flex; + align-items: center; + justify-content: center; + transition: all 200ms; + forced-color-adjust: none; +} + +.error { + align-items: center; + color: var(--Scandic-Red-60); + display: flex; + gap: var(--Spacing-x-half); + margin: var(--Spacing-x1) 0 0; +} diff --git a/components/TempDesignSystem/Form/Checkbox/checkbox.ts b/components/TempDesignSystem/Form/Checkbox/checkbox.ts new file mode 100644 index 000000000..8588b7401 --- /dev/null +++ b/components/TempDesignSystem/Form/Checkbox/checkbox.ts @@ -0,0 +1,7 @@ +import { RegisterOptions } from "react-hook-form" + +export interface CheckboxProps + extends React.InputHTMLAttributes { + name: string + registerOptions?: RegisterOptions +} diff --git a/components/TempDesignSystem/Form/Checkbox/index.tsx b/components/TempDesignSystem/Form/Checkbox/index.tsx new file mode 100644 index 000000000..339dff676 --- /dev/null +++ b/components/TempDesignSystem/Form/Checkbox/index.tsx @@ -0,0 +1,48 @@ +import { Checkbox as AriaCheckbox } from "react-aria-components" +import { useController, useFormContext } from "react-hook-form" + +import { InfoCircleIcon } from "@/components/Icons" +import CheckIcon from "@/components/Icons/Check" +import Caption from "@/components/TempDesignSystem/Text/Caption" + +import { CheckboxProps } from "./checkbox" + +import styles from "./checkbox.module.css" + +export default function Checkbox({ + name, + children, + registerOptions, +}: React.PropsWithChildren) { + const { control } = useFormContext() + const { field, fieldState } = useController({ + control, + name, + rules: registerOptions, + }) + + return ( + + {({ isSelected }) => ( + <> +
+
+ {isSelected && } +
+ {children} +
+ {fieldState.error ? ( + + + {fieldState.error.message} + + ) : null} + + )} +
+ ) +}