diff --git a/packages/forms/jest.config.js b/packages/forms/jest.config.js index 7ae80c2b1c03..a60438886936 100644 --- a/packages/forms/jest.config.js +++ b/packages/forms/jest.config.js @@ -6,6 +6,6 @@ module.exports = { * that now requires this. */ 'react-hook-form': - '/../../node_modules/react-hook-form/dist/index.cjs.development', + '/../../node_modules/react-hook-form/dist/index.cjs', }, } diff --git a/packages/forms/package.json b/packages/forms/package.json index d91158fdbb2e..74e681f24511 100644 --- a/packages/forms/package.json +++ b/packages/forms/package.json @@ -12,7 +12,7 @@ "@types/pascalcase": "^1.0.0", "core-js": "3.12.1", "pascalcase": "1.0.0", - "react-hook-form": "^6.9.5" + "react-hook-form": "^7.6.1" }, "scripts": { "build": "yarn build:js", diff --git a/packages/forms/src/index.tsx b/packages/forms/src/index.tsx index 0b63b7fc99bc..38e9707a9459 100644 --- a/packages/forms/src/index.tsx +++ b/packages/forms/src/index.tsx @@ -6,8 +6,8 @@ import { FormProvider, useFormContext, RegisterOptions, - UseFormMethods, - UseFormOptions, + UseFormReturn, + UseFormProps, } from 'react-hook-form' import { @@ -27,29 +27,31 @@ const DEFAULT_MESSAGES = { validate: 'is not valid', } -enum INPUT_TYPES { - BUTTON = 'button', - COLOR = 'color', - DATE = 'date', - DATETIME_LOCAL = 'datetime-local', - EMAIL = 'email', - FILE = 'file', - HIDDEN = 'hidden', - IMAGE = 'image', - MONTH = 'month', - NUMBER = 'number', - PASSWORD = 'password', - RADIO = 'radio', - RANGE = 'range', - RESET = 'reset', - SEARCH = 'search', - SUBMIT = 'submit', - TEL = 'tel', - TEXT = 'text', - TIME = 'time', - URL = 'url', - WEEK = 'week', -} +const inputTypes = [ + 'button', + 'color', + 'date', + 'datetime-local', + 'email', + 'file', + 'hidden', + 'image', + 'month', + 'number', + 'password', + 'radio', + 'range', + 'reset', + 'search', + 'submit', + 'tel', + 'text', + 'time', + 'url', + 'week', +] + +type INPUT_TYPES = typeof inputTypes[number] // Massages a hash of props depending on whether the given named field has // any errors on it @@ -72,8 +74,11 @@ interface ValidatableFieldProps extends InputTagProps { const inputTagProps = ( props: T ): Omit => { - // eslint-disable-next-line react-hooks/rules-of-hooks - const { errors, setError } = useFormContext() + const { + setError, + formState: { errors }, + // eslint-disable-next-line react-hooks/rules-of-hooks + } = useFormContext() // Check for errors from server and set on field if present @@ -134,8 +139,8 @@ const coerceValues = ( interface FormWithCoercionContext extends Omit, 'onSubmit'> { error?: any - formMethods?: UseFormMethods - validation?: UseFormOptions + formMethods?: UseFormReturn + validation?: UseFormProps onSubmit?: ( values: Record, event?: React.BaseSyntheticEvent @@ -151,8 +156,8 @@ const FormWithCoercionContext: React.FC = (props) => { onSubmit, ...formProps } = props - const useFormMethods = useForm(props.validation) - const formMethods = propFormMethods || useFormMethods + const UseFormReturn = useForm(props.validation) + const formMethods = propFormMethods || UseFormReturn const { coerce } = useCoercion() return ( @@ -212,7 +217,9 @@ interface FieldErrorProps extends React.HTMLProps { } const FieldError = (props: FieldErrorProps) => { - const { errors } = useFormContext() + const { + formState: { errors }, + } = useFormContext() const validationError = errors[props.name] const errorMessage = validationError && @@ -264,13 +271,15 @@ const TextAreaField = forwardRef< validation.validate = jsonValidation } + const { onChange, onBlur } = register(props.name, validation) + return (