diff --git a/packages/react/src/components/Forms/Examples/ReactHookForm/Sports.stories.tsx b/packages/react/src/components/Forms/Examples/ReactHookForm/Sports.stories.tsx index 5b47f37..383a49a 100644 --- a/packages/react/src/components/Forms/Examples/ReactHookForm/Sports.stories.tsx +++ b/packages/react/src/components/Forms/Examples/ReactHookForm/Sports.stories.tsx @@ -1,20 +1,149 @@ +import { yupResolver } from "@hookform/resolvers/yup"; import { Meta } from "@storybook/react"; import React from "react"; +import { useForm, Controller, ControllerRenderProps } from "react-hook-form"; +import * as Yup from "yup"; import { Input } from ":/components/Forms/Input"; -import { Checkbox } from ":/components/Forms/Checkbox"; import { Button } from ":/components/Button"; import { Select } from ":/components/Forms/Select"; import { CunninghamProvider } from ":/components/Provider"; -import { FileUploader } from ":/components/Forms/FileUploader"; -import { Switch } from ":/components/Forms/Switch"; -import { Radio } from ":/components/Forms/Radio"; +import { Radio, RadioGroup } from ":/components/Forms/Radio"; +import { + getFieldState, + getFieldErrorMessage, + onSubmit, +} from ":/tests/reactHookFormUtils"; export default { - title: "Components/Forms/Examples", + title: "Components/Forms/Reac-Hook-Form", } as Meta; +enum GenderEnum { + MALE = "male", + FEMALE = "female", + OTHER = "other", +} +enum CompetitionEnum { + ATHLETICS = "Athletics", + SWIMMING = "Swimming", + MARATHON = "Marathon", +} +enum RewardEnum { + BRONZE = "bronze", + SILVER = "silver", + GOLD = "gold", + FLOCON = "flocon", + OURSON = "ourson", + CHAMOIS = "chamois", +} + +interface SportsStoryFormValues { + firstName: string; + lastName: string; + gender: GenderEnum; + competition: CompetitionEnum; + rewards: RewardEnum[]; +} + +const sportsSchema = Yup.object().shape({ + firstName: Yup.string().required(), + lastName: Yup.string().required(), + gender: Yup.string().required(), + competition: Yup.string() + .defined() + .required() + .oneOf(Object.values(CompetitionEnum)), + rewards: Yup.array().of(Yup.string().defined()).defined(), +}); export const Sports = () => { + const { register, handleSubmit, formState, control } = + useForm({ + defaultValues: { + firstName: "", + lastName: "", + rewards: [], + }, + mode: "onChange", + reValidateMode: "onChange", + resolver: yupResolver(sportsSchema), + }); + + const renderCompetitionSelect = ({ + field, + }: { + field: ControllerRenderProps; + }) => { + return ( + + ); + }; + return (
{ gap: "1rem", width: "400px", }} + onSubmit={handleSubmit(onSubmit)} >

{ Register

- - + +
-
- - - -
-