✨(react) react-hook-form Radio example
Our form elements needs to be usable with react-hook-form
This commit is contained in:
@@ -84,6 +84,15 @@ You can also define `state`, `text` props on the group component
|
|||||||
<Story id="components-forms-radio--group-success"/>
|
<Story id="components-forms-radio--group-success"/>
|
||||||
</Canvas>
|
</Canvas>
|
||||||
|
|
||||||
|
## Usage with react-hook-form
|
||||||
|
|
||||||
|
You can use this radio with [react-hook-form](https://react-hook-form.com/docs)
|
||||||
|
|
||||||
|
<Canvas sourceState="shown">
|
||||||
|
<Story id="components-forms-radio--react-hook-form"/>
|
||||||
|
</Canvas>
|
||||||
|
|
||||||
|
|
||||||
### Props
|
### Props
|
||||||
|
|
||||||
<ArgTypes of={Radio} />
|
<ArgTypes of={Radio} />
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
import { Meta, StoryFn } from "@storybook/react";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { useForm } from "react-hook-form";
|
||||||
|
import * as Yup from "yup";
|
||||||
|
import { yupResolver } from "@hookform/resolvers/yup";
|
||||||
|
import { Meta, StoryFn } from "@storybook/react";
|
||||||
import { Radio, RadioGroup } from ":/components/Forms/Radio/index";
|
import { Radio, RadioGroup } from ":/components/Forms/Radio/index";
|
||||||
|
import { Button } from ":/components/Button";
|
||||||
|
import {
|
||||||
|
getFieldState,
|
||||||
|
getFieldErrorMessage,
|
||||||
|
onSubmit,
|
||||||
|
} from ":/tests/reactHookFormUtils";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
title: "Components/Forms/Radio",
|
title: "Components/Forms/Radio",
|
||||||
@@ -178,3 +187,79 @@ export const GroupSuccess = () => {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const ReactHookForm = () => {
|
||||||
|
interface RadioExampleFormValues {
|
||||||
|
joTown: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const radioExampleSchema = Yup.object().shape({
|
||||||
|
joTown: Yup.string()
|
||||||
|
.required()
|
||||||
|
.oneOf(["paris"], "That's not the right town!"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const { register, handleSubmit, formState } = useForm<RadioExampleFormValues>(
|
||||||
|
{
|
||||||
|
defaultValues: {
|
||||||
|
joTown: "",
|
||||||
|
},
|
||||||
|
mode: "onChange",
|
||||||
|
reValidateMode: "onChange",
|
||||||
|
resolver: yupResolver(radioExampleSchema),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<form
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
gap: "1rem",
|
||||||
|
width: "400px",
|
||||||
|
}}
|
||||||
|
onSubmit={handleSubmit(onSubmit)}
|
||||||
|
>
|
||||||
|
<div className="fs-l fw-bold mb-t">
|
||||||
|
Where will the 2024 Olympics take place?
|
||||||
|
</div>
|
||||||
|
<RadioGroup
|
||||||
|
state={getFieldState("joTown", formState)}
|
||||||
|
text={getFieldErrorMessage("joTown", formState)}
|
||||||
|
>
|
||||||
|
<Radio
|
||||||
|
label="Dijon"
|
||||||
|
value="dijon"
|
||||||
|
state={getFieldState("joTown", formState)}
|
||||||
|
{...register("joTown")}
|
||||||
|
/>
|
||||||
|
<Radio
|
||||||
|
label="Paris"
|
||||||
|
value="paris"
|
||||||
|
state={getFieldState("joTown", formState)}
|
||||||
|
{...register("joTown")}
|
||||||
|
/>
|
||||||
|
<Radio
|
||||||
|
label="Düsseldorf"
|
||||||
|
value="düsseldorf"
|
||||||
|
state={getFieldState("joTown", formState)}
|
||||||
|
{...register("joTown")}
|
||||||
|
/>
|
||||||
|
<Radio
|
||||||
|
label="Lubumbashi"
|
||||||
|
value="Lubumbashi"
|
||||||
|
state={getFieldState("joTown", formState)}
|
||||||
|
{...register("joTown")}
|
||||||
|
/>
|
||||||
|
<Radio
|
||||||
|
label="Tokyo"
|
||||||
|
value="tokyo"
|
||||||
|
text="This was the town of the 2020 Olympics!"
|
||||||
|
disabled={true}
|
||||||
|
{...register("joTown")}
|
||||||
|
/>
|
||||||
|
</RadioGroup>
|
||||||
|
<Button fullWidth={true}>Check it!</Button>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import React, { InputHTMLAttributes, PropsWithChildren } from "react";
|
import React, {
|
||||||
|
InputHTMLAttributes,
|
||||||
|
PropsWithChildren,
|
||||||
|
forwardRef,
|
||||||
|
} from "react";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import { Field, FieldProps } from ":/components/Forms/Field";
|
import { Field, FieldProps } from ":/components/Forms/Field";
|
||||||
|
|
||||||
@@ -7,22 +11,24 @@ type Props = InputHTMLAttributes<HTMLInputElement> &
|
|||||||
label?: string;
|
label?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Radio = ({ label, text, state, ...props }: Props) => {
|
export const Radio = forwardRef<HTMLInputElement, Props>(
|
||||||
return (
|
({ label, text, state, ...props }: Props, ref) => {
|
||||||
<label
|
return (
|
||||||
className={classNames("c__checkbox", "c__radio", {
|
<label
|
||||||
"c__checkbox--disabled": props.disabled,
|
className={classNames("c__checkbox", "c__radio", {
|
||||||
})}
|
"c__checkbox--disabled": props.disabled,
|
||||||
>
|
})}
|
||||||
<Field text={text} compact={true} state={state}>
|
>
|
||||||
<div className="c__checkbox__container">
|
<Field text={text} compact={true} state={state}>
|
||||||
<input type="radio" {...props} />
|
<div className="c__checkbox__container">
|
||||||
{label && <div className="c__checkbox__label">{label}</div>}
|
<input type="radio" {...props} ref={ref} />
|
||||||
</div>
|
{label && <div className="c__checkbox__label">{label}</div>}
|
||||||
</Field>
|
</div>
|
||||||
</label>
|
</Field>
|
||||||
);
|
</label>
|
||||||
};
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
export const RadioGroup = ({
|
export const RadioGroup = ({
|
||||||
children,
|
children,
|
||||||
|
|||||||
Reference in New Issue
Block a user