diff --git a/packages/react/src/components/Forms/Radio/index.mdx b/packages/react/src/components/Forms/Radio/index.mdx index 7235527..66257c6 100644 --- a/packages/react/src/components/Forms/Radio/index.mdx +++ b/packages/react/src/components/Forms/Radio/index.mdx @@ -84,6 +84,15 @@ You can also define `state`, `text` props on the group component +## Usage with react-hook-form + +You can use this radio with [react-hook-form](https://react-hook-form.com/docs) + + + + + + ### Props diff --git a/packages/react/src/components/Forms/Radio/index.stories.tsx b/packages/react/src/components/Forms/Radio/index.stories.tsx index d35b092..83b8818 100644 --- a/packages/react/src/components/Forms/Radio/index.stories.tsx +++ b/packages/react/src/components/Forms/Radio/index.stories.tsx @@ -1,6 +1,15 @@ -import { Meta, StoryFn } from "@storybook/react"; import React from "react"; +import { useForm } from "react-hook-form"; +import * as Yup from "yup"; +import { yupResolver } from "@hookform/resolvers/yup"; +import { Meta, StoryFn } from "@storybook/react"; import { Radio, RadioGroup } from ":/components/Forms/Radio/index"; +import { Button } from ":/components/Button"; +import { + getFieldState, + getFieldErrorMessage, + onSubmit, +} from ":/tests/reactHookFormUtils"; export default { title: "Components/Forms/Radio", @@ -178,3 +187,79 @@ export const GroupSuccess = () => { ); }; + +export const ReactHookForm = () => { + interface RadioExampleFormValues { + joTown: string; + } + + const radioExampleSchema = Yup.object().shape({ + joTown: Yup.string() + .required() + .oneOf(["paris"], "That's not the right town!"), + }); + + const { register, handleSubmit, formState } = useForm( + { + defaultValues: { + joTown: "", + }, + mode: "onChange", + reValidateMode: "onChange", + resolver: yupResolver(radioExampleSchema), + }, + ); + + return ( +
+
+ Where will the 2024 Olympics take place? +
+ + + + + + + + +
+ ); +}; diff --git a/packages/react/src/components/Forms/Radio/index.tsx b/packages/react/src/components/Forms/Radio/index.tsx index fa39b1c..9072189 100644 --- a/packages/react/src/components/Forms/Radio/index.tsx +++ b/packages/react/src/components/Forms/Radio/index.tsx @@ -1,4 +1,8 @@ -import React, { InputHTMLAttributes, PropsWithChildren } from "react"; +import React, { + InputHTMLAttributes, + PropsWithChildren, + forwardRef, +} from "react"; import classNames from "classnames"; import { Field, FieldProps } from ":/components/Forms/Field"; @@ -7,22 +11,24 @@ type Props = InputHTMLAttributes & label?: string; }; -export const Radio = ({ label, text, state, ...props }: Props) => { - return ( - - ); -}; +export const Radio = forwardRef( + ({ label, text, state, ...props }: Props, ref) => { + return ( + + ); + }, +); export const RadioGroup = ({ children,