From 034e2994078e40b09fae7e17cd5070d52cc7c51c Mon Sep 17 00:00:00 2001 From: Romain Le Cellier Date: Thu, 21 Sep 2023 16:18:01 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(react)=20fix=20rhf=20reset?= =?UTF-8?q?=20with=20Input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit when using reset(), Input field value wasn't updated --- .changeset/fresh-ghosts-lick.md | 5 +++ .../Examples/ReactHookForm/Login.stories.tsx | 6 ++-- .../Examples/ReactHookForm/Sports.stories.tsx | 35 ++++++++++++++++--- .../components/Forms/Input/stories-utils.tsx | 26 ++++++++++++++ 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 .changeset/fresh-ghosts-lick.md create mode 100644 packages/react/src/components/Forms/Input/stories-utils.tsx diff --git a/.changeset/fresh-ghosts-lick.md b/.changeset/fresh-ghosts-lick.md new file mode 100644 index 0000000..5b53ceb --- /dev/null +++ b/.changeset/fresh-ghosts-lick.md @@ -0,0 +1,5 @@ +--- +"@openfun/cunningham-react": patch +--- + +Fix input usage with react hook form diff --git a/packages/react/src/components/Forms/Examples/ReactHookForm/Login.stories.tsx b/packages/react/src/components/Forms/Examples/ReactHookForm/Login.stories.tsx index 3371587..b3fcac6 100644 --- a/packages/react/src/components/Forms/Examples/ReactHookForm/Login.stories.tsx +++ b/packages/react/src/components/Forms/Examples/ReactHookForm/Login.stories.tsx @@ -3,7 +3,7 @@ import { Meta } from "@storybook/react"; import React from "react"; import { useForm } from "react-hook-form"; import * as Yup from "yup"; -import { Input } from ":/components/Forms/Input"; +import { RhfInput } from ":/components/Forms/Input/stories-utils"; import { Checkbox } from ":/components/Forms/Checkbox"; import { Button } from ":/components/Button"; import { @@ -56,14 +56,14 @@ export const Login = () => { > Log in - - { +export interface SportProps { + values?: SportsStoryFormValues; +} + +const SportsBase = ({ values }: SportProps) => { const methods = useForm({ defaultValues: { firstName: "", @@ -46,6 +50,10 @@ export const Sports = () => { resolver: yupResolver(sportsSchema), }); + useEffect(() => { + methods.reset(values); + }, [values]); + return ( @@ -75,28 +83,31 @@ export const Sports = () => {
- - { ); }; + +export const Sports = () => ; + +export const SportsEdit = () => ( + +); diff --git a/packages/react/src/components/Forms/Input/stories-utils.tsx b/packages/react/src/components/Forms/Input/stories-utils.tsx new file mode 100644 index 0000000..8cf480d --- /dev/null +++ b/packages/react/src/components/Forms/Input/stories-utils.tsx @@ -0,0 +1,26 @@ +import { Controller, useFormContext } from "react-hook-form"; +import React from "react"; +import { Input, InputProps } from "."; + +export const RhfInput = (props: InputProps & { name: string }) => { + const { control, setValue } = useFormContext(); + return ( + { + return ( + setValue(field.name, e.target.value)} + value={field.value} + /> + ); + }} + /> + ); +};