♻️(react) fix rhf reset with Input
when using reset(), Input field value wasn't updated
This commit is contained in:
@@ -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",
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user