Skip to content

Update react-hook-form peerDependencies to ^6.15.8 <7.0.0 #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Update `peerDependencies` of `react-hook-form` to `^6.15.8 <7.0.0`
-

## [0.2.0] - 2021-08-03

## [0.2.0-beta.13] - 2020-03-26
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ This component is the top-level component that creates the context with the sche
- `'onblur'`: Validate when an input field is blurred.
- `'onChange'`: Validate when an input field value changes.
- `'onSubmit'`: Validate when the submit is triggered.
- `submitFocusError`: Boolean, when `true` focus on the first field with error after submit validates, if there is any. Defaults to `true`.
- `shouldFocusError`: Boolean, when `true` focus on the first field with error after submit validates, if there is any. Defaults to `true`.
- `onChange`: Callback called when there's a change in the form. It passes the form _data_ formatted by the provided JSON Schema.
- `onSubmit`: If provided `react-hook-form-jsonschema` will call this function as the submit action, it passes an object with the following members:
- `data`: The data that was provided as inputs to the form, correctly formatted as an instance of the JSON Schema provided.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
}
},
"dependencies": {
"react-hook-form": "^4.4.4"
"react-hook-form": "^6.15.8 <7.0.0"
},
"peerDependencies": {
"react": "^16.12.0",
Expand Down
4 changes: 2 additions & 2 deletions src/components/FormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ export const FormContext: FC<FormContextProps> = props => {
onChange,
validationMode = 'onSubmit',
revalidateMode = 'onChange',
submitFocusError = true,
shouldFocusError = true,
defaultValues,
} = props

const methods = useForm({
defaultValues,
mode: validationMode,
reValidateMode: revalidateMode,
submitFocusError: submitFocusError,
shouldFocusError,
})

const isFirstRender = React.useRef(true)
Expand Down
13 changes: 4 additions & 9 deletions src/components/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import React from 'react'
import {
DeepPartial,
FieldValues,
FormContextValues,
Mode,
} from 'react-hook-form'
import { DeepPartial, FieldValues, Mode, UseFormMethods } from 'react-hook-form'

import { JSONSchemaType, IDSchemaPair } from '../../JSONSchema'
import { CustomValidators } from '../../hooks/validators'

export interface JSONFormContextValues<
FormValues extends FieldValues = FieldValues
> extends FormContextValues<FormValues> {
> extends UseFormMethods<FormValues> {
schema: JSONSchemaType
idMap: IDSchemaPair
customValidators?: CustomValidators
Expand All @@ -27,8 +22,8 @@ export type OnSubmitType = (props: OnSubmitParameters) => void | Promise<void>
export type FormContextProps<FormValues extends FieldValues = FieldValues> = {
formProps?: Omit<React.HTMLAttributes<HTMLFormElement>, 'onSubmit'>
validationMode?: Mode
revalidateMode?: Mode
submitFocusError?: boolean
revalidateMode?: Exclude<Mode, 'onTouched' | 'all'>
shouldFocusError?: boolean
onChange?: (data: JSONSchemaType) => void
onSubmit?: OnSubmitType
noNativeValidate?: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/__tests__/useCustomValidator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ test('should use custom validator', async () => {
expect(getByText('__is_not_helena_error__')).toBeDefined()
})

getByText('Submit').click()
fireEvent.change(stringField, {
target: { value: 'Helena' },
})
getByText('Submit').click()

await wait(() => {
expect(queryByText('__is_not_helena_error__')).toBeNull()
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { ValidationOptions, FieldValues } from 'react-hook-form'
import { FieldValues, RegisterOptions } from 'react-hook-form'

import { ErrorMessage } from '../validators'
import { JSONSchemaType } from '../../JSONSchema'
Expand Down Expand Up @@ -34,7 +34,7 @@ export interface BasicInputReturnType {
name: string
type: InputTypes
pointer: string
validator: ValidationOptions
validator: RegisterOptions
}

export interface GenericInputParameters {
Expand Down
9 changes: 3 additions & 6 deletions src/hooks/validators/getError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldError, FormContextValues } from 'react-hook-form'
import { FieldError, UseFormMethods } from 'react-hook-form'

import { ErrorTypes, ErrorMessage } from './types'
import { JSONSchemaType } from '../../JSONSchema'
Expand All @@ -7,7 +7,7 @@ export const getError = (
errors: FieldError | undefined,
currentObject: JSONSchemaType,
isRequired: boolean,
formContext: FormContextValues,
formContext: UseFormMethods,
pointer: string,
minimum?: number,
maximum?: number,
Expand All @@ -16,10 +16,7 @@ export const getError = (
// This is a special element to check errors against
if (currentObject.type === 'array') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const currentValues: any[] | undefined = formContext.getValues({
nest: true,
})[pointer]

const currentValues: any[] | undefined = formContext.getValues()[pointer]
if (currentValues) {
const numberOfSelected =
currentValues.filter(x => x !== false).length || 0
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/validators/getGenericValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValidationOptions } from 'react-hook-form'
import { RegisterOptions } from 'react-hook-form'

import {
ErrorTypes,
Expand Down Expand Up @@ -32,14 +32,14 @@ function getCustomValidator(
export const getValidator = (
context: JSONSubSchemaInfo,
customValidators: CustomValidators
): ValidationOptions => {
): RegisterOptions => {
const { JSONSchema, isRequired } = context

// The use of this variable prevents a strange undocumented behaviour of react-hook-form
// that is it fails to validate if the `validate` field exists but is empty.
const hasValidate =
Object.keys(customValidators).length > 0 || JSONSchema.enum
const validator: ValidationOptions = {
const validator: RegisterOptions = {
...(hasValidate
? {
validate: {
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/validators/getNumberValidator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ValidationOptions } from 'react-hook-form'
import { RegisterOptions } from 'react-hook-form'

import { getNumberMaximum, getNumberMinimum } from './numberUtilities'
import { JSONSchemaType } from '../../JSONSchema'
import { ErrorTypes } from './types'

export const getNumberValidator = (
currentObject: JSONSchemaType,
baseValidator: ValidationOptions
): ValidationOptions => {
baseValidator: RegisterOptions
): RegisterOptions => {
const minimum = getNumberMinimum(currentObject)
const maximum = getNumberMaximum(currentObject)

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/validators/getStringValidator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ValidationOptions } from 'react-hook-form'
import { RegisterOptions } from 'react-hook-form'

import { JSONSchemaType } from '../../JSONSchema'
import { ErrorTypes } from './types'

export const getStringValidator = (
currentObject: JSONSchemaType,
baseValidator: ValidationOptions
): ValidationOptions => {
baseValidator: RegisterOptions
): RegisterOptions => {
if (currentObject.minLength) {
baseValidator.minLength = {
value: currentObject.minLength,
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/validators/numberUtilities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValidationOptions } from 'react-hook-form'
import { RegisterOptions } from 'react-hook-form'

import { JSONSchemaType } from '../../JSONSchema'
import { ErrorTypes } from './types'
Expand Down Expand Up @@ -85,11 +85,11 @@ export const getNumberMaximum = (
export const getNumberValidator = (
currentObject: JSONSchemaType,
required: boolean
): ValidationOptions => {
): RegisterOptions => {
const minimum = getNumberMinimum(currentObject)
const maximum = getNumberMaximum(currentObject)

const validator: ValidationOptions = {
const validator: RegisterOptions = {
validate: {
multipleOf: (value: string) => {
if (currentObject.type === 'integer' && value) {
Expand Down
Loading