📝(react) add RHF examples
Rework a bit the recent work made on RHF example to make some component more generic, such as RhfSelect and RhfDatepicker, which is based on a design using RHF context hooks to provide a seamless integration. Fixes #144
This commit is contained in:
@@ -3,13 +3,13 @@ import { Meta } from "@storybook/react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import * as Yup from "yup";
|
||||
import React from "react";
|
||||
import { Switch } from ":/components/Forms/Switch/index";
|
||||
import { Button } from ":/components/Button";
|
||||
import {
|
||||
getFieldState,
|
||||
getFieldErrorMessage,
|
||||
onSubmit,
|
||||
} from ":/tests/reactHookFormUtils";
|
||||
} from ":/components/Forms/Examples/ReactHookForm/reactHookFormUtils";
|
||||
import { Switch } from ":/components/Forms/Switch/index";
|
||||
import { Button } from ":/components/Button";
|
||||
|
||||
export default {
|
||||
title: "Components/Forms/Switch",
|
||||
@@ -130,49 +130,6 @@ export const Controlled = {
|
||||
},
|
||||
};
|
||||
|
||||
export const ReactHookForm = () => {
|
||||
interface SwitchExampleFormValues {
|
||||
terms: boolean;
|
||||
}
|
||||
|
||||
const switchExampleSchema = Yup.object().shape({
|
||||
terms: Yup.boolean()
|
||||
.required()
|
||||
.oneOf([true], "You have to accept the terms of use"),
|
||||
});
|
||||
|
||||
const { register, handleSubmit, formState } =
|
||||
useForm<SwitchExampleFormValues>({
|
||||
defaultValues: {
|
||||
terms: false,
|
||||
},
|
||||
mode: "onChange",
|
||||
reValidateMode: "onChange",
|
||||
resolver: yupResolver(switchExampleSchema),
|
||||
});
|
||||
|
||||
return (
|
||||
<form
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: "1rem",
|
||||
width: "400px",
|
||||
}}
|
||||
onSubmit={handleSubmit(onSubmit)}
|
||||
>
|
||||
<Switch
|
||||
label="I accept the terms of use"
|
||||
fullWidth
|
||||
state={getFieldState("terms", formState)}
|
||||
text={getFieldErrorMessage("terms", formState)}
|
||||
{...register("terms")}
|
||||
/>
|
||||
<Button fullWidth={true}>Log-in</Button>
|
||||
</form>
|
||||
);
|
||||
};
|
||||
|
||||
export const FormExample = {
|
||||
render: () => {
|
||||
return (
|
||||
@@ -240,3 +197,46 @@ export const FormExampleRight = {
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
export const ReactHookForm = () => {
|
||||
interface SwitchExampleFormValues {
|
||||
terms: boolean;
|
||||
}
|
||||
|
||||
const switchExampleSchema = Yup.object().shape({
|
||||
terms: Yup.boolean()
|
||||
.required()
|
||||
.oneOf([true], "You have to accept the terms of use"),
|
||||
});
|
||||
|
||||
const { register, handleSubmit, formState } =
|
||||
useForm<SwitchExampleFormValues>({
|
||||
defaultValues: {
|
||||
terms: false,
|
||||
},
|
||||
mode: "onChange",
|
||||
reValidateMode: "onChange",
|
||||
resolver: yupResolver(switchExampleSchema),
|
||||
});
|
||||
|
||||
return (
|
||||
<form
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: "1rem",
|
||||
width: "400px",
|
||||
}}
|
||||
onSubmit={handleSubmit(onSubmit)}
|
||||
>
|
||||
<Switch
|
||||
label="I accept the terms of use"
|
||||
fullWidth
|
||||
state={getFieldState("terms", formState)}
|
||||
text={getFieldErrorMessage("terms", formState)}
|
||||
{...register("terms")}
|
||||
/>
|
||||
<Button fullWidth={true}>Log-in</Button>
|
||||
</form>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user