feat(SW-360): Added checkbox component
This commit is contained in:
committed by
Pontus Dreij
parent
2886f537ee
commit
a4483b7d71
@@ -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;
|
||||||
|
}
|
||||||
7
components/TempDesignSystem/Form/Checkbox/checkbox.ts
Normal file
7
components/TempDesignSystem/Form/Checkbox/checkbox.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { RegisterOptions } from "react-hook-form"
|
||||||
|
|
||||||
|
export interface CheckboxProps
|
||||||
|
extends React.InputHTMLAttributes<HTMLInputElement> {
|
||||||
|
name: string
|
||||||
|
registerOptions?: RegisterOptions
|
||||||
|
}
|
||||||
48
components/TempDesignSystem/Form/Checkbox/index.tsx
Normal file
48
components/TempDesignSystem/Form/Checkbox/index.tsx
Normal file
@@ -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<CheckboxProps>) {
|
||||||
|
const { control } = useFormContext()
|
||||||
|
const { field, fieldState } = useController({
|
||||||
|
control,
|
||||||
|
name,
|
||||||
|
rules: registerOptions,
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AriaCheckbox
|
||||||
|
className={styles.container}
|
||||||
|
isSelected={field.value}
|
||||||
|
onChange={field.onChange}
|
||||||
|
>
|
||||||
|
{({ isSelected }) => (
|
||||||
|
<>
|
||||||
|
<div className={styles.checkboxContainer}>
|
||||||
|
<div className={styles.checkbox}>
|
||||||
|
{isSelected && <CheckIcon color="white" />}
|
||||||
|
</div>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
{fieldState.error ? (
|
||||||
|
<Caption className={styles.error} fontOnly>
|
||||||
|
<InfoCircleIcon color="red" />
|
||||||
|
{fieldState.error.message}
|
||||||
|
</Caption>
|
||||||
|
) : null}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</AriaCheckbox>
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user