♻️(react) fix rhf reset with Input
when using reset(), Input field value wasn't updated
This commit is contained in:
5
.changeset/fresh-ghosts-lick.md
Normal file
5
.changeset/fresh-ghosts-lick.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@openfun/cunningham-react": patch
|
||||
---
|
||||
|
||||
Fix input usage with react hook form
|
||||
@@ -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
|
||||
</h1>
|
||||
<Input
|
||||
<RhfInput
|
||||
label="Email"
|
||||
fullWidth={true}
|
||||
state={getFieldState("email", formState)}
|
||||
text={getFieldErrorMessage("email", formState)}
|
||||
{...register("email")}
|
||||
/>
|
||||
<Input
|
||||
<RhfInput
|
||||
label="Password"
|
||||
state={getFieldState("password", formState)}
|
||||
type="password"
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { yupResolver } from "@hookform/resolvers/yup";
|
||||
import { Meta } from "@storybook/react";
|
||||
import React from "react";
|
||||
import React, { useEffect } from "react";
|
||||
import { useForm, FormProvider } from "react-hook-form";
|
||||
import * as Yup from "yup";
|
||||
import { Input } from ":/components/Forms/Input";
|
||||
import { RhfInput } from ":/components/Forms/Input/stories-utils";
|
||||
import { Button } from ":/components/Button";
|
||||
import { CunninghamProvider } from ":/components/Provider";
|
||||
import { Radio, RadioGroup } from ":/components/Forms/Radio";
|
||||
@@ -34,7 +34,11 @@ const sportsSchema = Yup.object().shape({
|
||||
rewards: Yup.array().of(Yup.string().defined()).defined(),
|
||||
});
|
||||
|
||||
export const Sports = () => {
|
||||
export interface SportProps {
|
||||
values?: SportsStoryFormValues;
|
||||
}
|
||||
|
||||
const SportsBase = ({ values }: SportProps) => {
|
||||
const methods = useForm<SportsStoryFormValues>({
|
||||
defaultValues: {
|
||||
firstName: "",
|
||||
@@ -46,6 +50,10 @@ export const Sports = () => {
|
||||
resolver: yupResolver(sportsSchema),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
methods.reset(values);
|
||||
}, [values]);
|
||||
|
||||
return (
|
||||
<CunninghamProvider>
|
||||
<FormProvider {...methods}>
|
||||
@@ -75,28 +83,31 @@ export const Sports = () => {
|
||||
<Radio
|
||||
label="Male"
|
||||
state={getFieldState("gender", methods.formState)}
|
||||
value="male"
|
||||
{...methods.register("gender")}
|
||||
/>
|
||||
<Radio
|
||||
label="Female"
|
||||
value="female"
|
||||
state={getFieldState("gender", methods.formState)}
|
||||
{...methods.register("gender")}
|
||||
/>
|
||||
<Radio
|
||||
label="Other"
|
||||
value="other"
|
||||
state={getFieldState("gender", methods.formState)}
|
||||
{...methods.register("gender")}
|
||||
/>
|
||||
</RadioGroup>
|
||||
</div>
|
||||
<div style={{ display: "flex", gap: "1rem" }}>
|
||||
<Input
|
||||
<RhfInput
|
||||
label="First name"
|
||||
state={getFieldState("firstName", methods.formState)}
|
||||
text={getFieldErrorMessage("firstName", methods.formState)}
|
||||
{...methods.register("firstName")}
|
||||
/>
|
||||
<Input
|
||||
<RhfInput
|
||||
label="Last name"
|
||||
state={getFieldState("lastName", methods.formState)}
|
||||
text={getFieldErrorMessage("lastName", methods.formState)}
|
||||
@@ -159,3 +170,17 @@ export const Sports = () => {
|
||||
</CunninghamProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export const Sports = () => <SportsBase />;
|
||||
|
||||
export const SportsEdit = () => (
|
||||
<SportsBase
|
||||
values={{
|
||||
firstName: "Evans",
|
||||
lastName: "Chebet",
|
||||
competition: "Marathon",
|
||||
rewards: ["Gold"],
|
||||
gender: "male",
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
26
packages/react/src/components/Forms/Input/stories-utils.tsx
Normal file
26
packages/react/src/components/Forms/Input/stories-utils.tsx
Normal file
@@ -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 (
|
||||
<Controller
|
||||
control={control}
|
||||
name={props.name}
|
||||
render={({ field, fieldState }) => {
|
||||
return (
|
||||
<Input
|
||||
{...props}
|
||||
aria-invalid={!!fieldState.error}
|
||||
state={fieldState.error ? "error" : "default"}
|
||||
text={fieldState.error?.message}
|
||||
onBlur={field.onBlur}
|
||||
onChange={(e) => setValue(field.name, e.target.value)}
|
||||
value={field.value}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user