diff --git a/packages/react/src/components/Forms/Checkbox/index.mdx b/packages/react/src/components/Forms/Checkbox/index.mdx index 71c01b5..cba0811 100644 --- a/packages/react/src/components/Forms/Checkbox/index.mdx +++ b/packages/react/src/components/Forms/Checkbox/index.mdx @@ -85,6 +85,15 @@ You can also define `state`, `text` props on the group component +## Usage with react-hook-form + +You can use this input with [react-hook-form](https://react-hook-form.com/docs) + + + + + + ### Props diff --git a/packages/react/src/components/Forms/Checkbox/index.stories.tsx b/packages/react/src/components/Forms/Checkbox/index.stories.tsx index cd71562..9170e1c 100644 --- a/packages/react/src/components/Forms/Checkbox/index.stories.tsx +++ b/packages/react/src/components/Forms/Checkbox/index.stories.tsx @@ -1,6 +1,15 @@ -import { Meta, StoryFn } from "@storybook/react"; import React from "react"; +import { yupResolver } from "@hookform/resolvers/yup"; +import { Meta, StoryFn } from "@storybook/react"; +import { useForm } from "react-hook-form"; +import * as Yup from "yup"; import { Checkbox, CheckboxGroup } from ":/components/Forms/Checkbox/index"; +import { Button } from ":/components/Button"; +import { + getFieldState, + getFieldErrorMessage, + onSubmit, +} from ":/tests/reactHookFormUtils"; export default { title: "Components/Forms/Checkbox", @@ -138,3 +147,46 @@ export const GroupSuccess = () => ( ); + +export const ReactHookForm = () => { + interface CheckboxExampleFormValues { + terms: boolean; + } + + const checkboxExampleSchema = Yup.object().shape({ + terms: Yup.boolean() + .required() + .oneOf([true], "You have to accept the terms of use"), + }); + + const { register, handleSubmit, formState } = + useForm({ + defaultValues: { + terms: false, + }, + mode: "onChange", + reValidateMode: "onChange", + resolver: yupResolver(checkboxExampleSchema), + }); + + return ( + + + Log-in + + ); +}; diff --git a/packages/react/src/components/Forms/Checkbox/index.tsx b/packages/react/src/components/Forms/Checkbox/index.tsx index 97e669e..b4edaa7 100644 --- a/packages/react/src/components/Forms/Checkbox/index.tsx +++ b/packages/react/src/components/Forms/Checkbox/index.tsx @@ -1,6 +1,7 @@ import React, { InputHTMLAttributes, PropsWithChildren, + forwardRef, useEffect, useRef, useState, @@ -14,55 +15,68 @@ type Props = InputHTMLAttributes & label?: string; }; -export const Checkbox = ({ - indeterminate, - className = "", - checked, - label, - text, - rightText, - state, - ...props -}: Props) => { - const inputRef = useRef(null); - const [value, setValue] = useState(!!checked); +export const Checkbox = forwardRef( + ( + { + indeterminate, + className = "", + checked, + label, + text, + rightText, + state, + ...props + }: Props, + ref, + ) => { + const inputRef = useRef(); + const [value, setValue] = useState(!!checked); - useEffect(() => { - setValue(!!checked); - }, [checked]); + useEffect(() => { + setValue(!!checked); + }, [checked]); - useEffect(() => { - if (inputRef.current) { - inputRef.current.indeterminate = !!indeterminate; - } - }, [indeterminate]); + useEffect(() => { + if (inputRef.current) { + inputRef.current.indeterminate = !!indeterminate; + } + }, [indeterminate]); - return ( - - - - - setValue(e.target.checked)} - {...props} - checked={value} - ref={inputRef} - /> - - + return ( + + + + + { + setValue(e.target.checked); + props.onChange?.(e); + }} + checked={value} + ref={(checkboxRef) => { + if (typeof ref === "function") { + ref(checkboxRef); + } + inputRef.current = checkboxRef || undefined; + }} + /> + + + + {label && {label}} - {label && {label}} - - - - ); -}; + + + ); + }, +); export const CheckboxGroup = ({ children,