From d506cab978204028c1f2bf7de183a94791cff29d Mon Sep 17 00:00:00 2001 From: Romain Le Cellier Date: Wed, 26 Jul 2023 16:52:49 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(react)=20react-hook-form=20Checkbox?= =?UTF-8?q?=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Our form elements needs to be usable with react-hook-form --- .../src/components/Forms/Checkbox/index.mdx | 9 ++ .../Forms/Checkbox/index.stories.tsx | 54 ++++++++- .../src/components/Forms/Checkbox/index.tsx | 104 ++++++++++-------- 3 files changed, 121 insertions(+), 46 deletions(-) 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 ( +
+ + + + ); +}; 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 ( -