diff --git a/packages/react/src/components/Forms/Select/mono-common.tsx b/packages/react/src/components/Forms/Select/mono-common.tsx index 6037053..37e92a4 100644 --- a/packages/react/src/components/Forms/Select/mono-common.tsx +++ b/packages/react/src/components/Forms/Select/mono-common.tsx @@ -70,6 +70,7 @@ export const SelectMonoAux = ({ value, disabled, clearable = true, + onBlur, }: SelectAuxProps) => { const { t } = useCunningham(); const labelProps = downshiftReturn.getLabelProps(); @@ -101,6 +102,10 @@ export const SelectMonoAux = ({ "c__select--disabled": disabled, }, )} + onBlur={() => + onBlur && + onBlur({ target: { value: downshiftReturn.selectedItem?.value } }) + } > {/* We disabled linting for this specific line because we consider that the onClick props is only used for */} {/* mouse users, so this do not engender any issue for accessibility. */} diff --git a/packages/react/src/components/Forms/Select/mono.mdx b/packages/react/src/components/Forms/Select/mono.mdx index 4991191..fa78ca5 100644 --- a/packages/react/src/components/Forms/Select/mono.mdx +++ b/packages/react/src/components/Forms/Select/mono.mdx @@ -112,6 +112,14 @@ using the component in a controlled way. +## Usage with react-hook-form + +You can use this input with [react-hook-form](https://react-hook-form.com/docs) + + + + + ## Props The props of this component are as close as possible to the native select component. You can see the list of props below. diff --git a/packages/react/src/components/Forms/Select/mono.stories.tsx b/packages/react/src/components/Forms/Select/mono.stories.tsx index 8622d5f..40dd5ec 100644 --- a/packages/react/src/components/Forms/Select/mono.stories.tsx +++ b/packages/react/src/components/Forms/Select/mono.stories.tsx @@ -1,9 +1,17 @@ import { Meta, StoryFn } from "@storybook/react"; import React, { useState } from "react"; +import { useForm, Controller, ControllerRenderProps } from "react-hook-form"; +import * as Yup from "yup"; import { faker } from "@faker-js/faker"; +import { yupResolver } from "@hookform/resolvers/yup"; import { Select } from ":/components/Forms/Select"; import { Button } from ":/components/Button"; import { CunninghamProvider } from ":/components/Provider"; +import { + getFieldState, + getFieldErrorMessage, + onSubmit, +} from ":/tests/reactHookFormUtils"; export default { title: "Components/Forms/Select/Mono", @@ -156,6 +164,87 @@ export const SearchableControlled = () => { ); }; +export const ReactHookForm = () => { + enum CitiesOptionEnum { + NONE = "", + DIJON = "dijon", + PARIS = "paris", + TOKYO = "tokyo", + } + + interface SelectExampleFormValues { + joTown: CitiesOptionEnum; + } + + const selectExampleSchema = Yup.object().shape({ + joTown: Yup.string() + .required() + .oneOf([CitiesOptionEnum.PARIS], "That's not the right town!"), + }); + + const { handleSubmit, formState, control } = useForm( + { + defaultValues: { + joTown: CitiesOptionEnum.NONE, + }, + mode: "onChange", + reValidateMode: "onChange", + resolver: yupResolver(selectExampleSchema), + }, + ); + + const renderSelect = ({ + field, + }: { + field: ControllerRenderProps; + }) => { + return ( + <> +
Where will the 2024 Olympics take place?
+ + + ); + }; + + return ( + +
+ + + +
+ ); +}; + export const FormExample = () => { const handleSubmit = (e: React.FormEvent) => { e.preventDefault();