@graphcommerce/react-hook-form 5.1.0-canary.2 → 5.1.0-canary.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Change Log
2
2
 
3
+ ## 5.1.0-canary.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1752](https://github.com/graphcommerce-org/graphcommerce/pull/1752) [`2a6a4d9ec`](https://github.com/graphcommerce-org/graphcommerce/commit/2a6a4d9ecfa1b58a66ba9b9d00016d6feda9aa95) - Updated dependencies to latest versions, except for nextjs; Solve tons of peer dependency issues.
8
+
9
+ - Updated the @mui/material package
10
+ - Removed dependencies on react-hook-form-mui and @playwright/test
11
+ - Upgraded dependencies including type-fest and graphql-mesh
12
+ - Solved peer dependency issues ([@paales](https://github.com/paales))
13
+
3
14
  ## 5.1.0-canary.2
4
15
 
5
16
  ## 5.1.0-canary.1
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/react-hook-form",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "5.1.0-canary.2",
5
+ "version": "5.1.0-canary.3",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -11,20 +11,19 @@
11
11
  "project": "./tsconfig.json"
12
12
  }
13
13
  },
14
- "devDependencies": {
15
- "@graphcommerce/eslint-config-pwa": "^5.1.0-canary.2",
16
- "@graphcommerce/prettier-config-pwa": "^5.1.0-canary.2",
17
- "@graphcommerce/typescript-config-pwa": "^5.1.0-canary.2",
18
- "@playwright/test": "^1.21.1",
19
- "type-fest": "^2.12.2"
20
- },
21
14
  "dependencies": {
22
- "@apollo/client": "^3.6.9",
23
- "graphql": "16.5.0",
24
- "react-hook-form": "7.32.0"
15
+ "@apollo/client": "^3.7.1",
16
+ "graphql": "16.6.0",
17
+ "react-hook-form": "7.40.0"
18
+ },
19
+ "devDependencies": {
20
+ "@graphcommerce/eslint-config-pwa": "5.1.0-canary.3",
21
+ "@graphcommerce/prettier-config-pwa": "5.1.0-canary.3",
22
+ "@graphcommerce/typescript-config-pwa": "5.1.0-canary.3",
23
+ "@testing-library/react": "^13.4.0"
25
24
  },
26
25
  "peerDependencies": {
27
- "react": "^18.0.0",
28
- "react-dom": "^18.0.0"
26
+ "react": "^18.2.0",
27
+ "react-dom": "^18.2.0"
29
28
  }
30
29
  }
@@ -4,7 +4,9 @@ import { isFormGqlOperation } from '../useFormGqlMutation'
4
4
  import { composedFormContext } from './context'
5
5
  import { UseFormComposeOptions } from './types'
6
6
 
7
- export function useFormCompose<V>(fields: UseFormComposeOptions<V>) {
7
+ export function useFormCompose<V extends Record<string, unknown>>(
8
+ fields: UseFormComposeOptions<V>,
9
+ ) {
8
10
  const [state, dispatch] = useContext(composedFormContext)
9
11
  const { form, key, step, submit } = fields
10
12
 
@@ -39,9 +39,10 @@ export type UseFormAutoSubmitOptions<TForm extends UseFormReturn<V>, V extends F
39
39
  * formState.isDirty should be true after the submission
40
40
  * @see useFormGqlMutation.tsx for an example implementation
41
41
  */
42
- export function useFormAutoSubmit<Form extends UseFormReturn<V>, V = FieldValues>(
43
- options: UseFormAutoSubmitOptions<Form, V>,
44
- ) {
42
+ export function useFormAutoSubmit<
43
+ Form extends UseFormReturn<V>,
44
+ V extends FieldValues = FieldValues,
45
+ >(options: UseFormAutoSubmitOptions<Form, V>) {
45
46
  const { form, submit, wait = 500, fields, forceInitialSubmit } = options
46
47
  const { formState } = form
47
48
 
@@ -6,7 +6,7 @@ import {
6
6
  LazyQueryResultTuple,
7
7
  } from '@apollo/client'
8
8
  import { useEffect, useRef } from 'react'
9
- import { UseFormProps, UseFormReturn, UnpackNestedValue, DeepPartial } from 'react-hook-form'
9
+ import { FieldValues, UseFormProps, UseFormReturn } from 'react-hook-form'
10
10
  import diff from './diff'
11
11
  import { useGqlDocumentHandler, UseGqlDocumentHandler } from './useGqlDocumentHandler'
12
12
 
@@ -21,9 +21,13 @@ type UseFormGraphQLCallbacks<Q, V> = {
21
21
  onComplete?: OnCompleteFn<Q, V>
22
22
  }
23
23
 
24
- export type UseFormGraphQlOptions<Q, V> = UseFormProps<V> & UseFormGraphQLCallbacks<Q, V>
24
+ export type UseFormGraphQlOptions<Q, V extends FieldValues> = UseFormProps<V> &
25
+ UseFormGraphQLCallbacks<Q, V>
25
26
 
26
- export type UseFormGqlMethods<Q, V> = Omit<UseGqlDocumentHandler<V>, 'encode' | 'type'> &
27
+ export type UseFormGqlMethods<Q, V extends FieldValues> = Omit<
28
+ UseGqlDocumentHandler<V>,
29
+ 'encode' | 'type'
30
+ > &
27
31
  Pick<UseFormReturn<V>, 'handleSubmit'> & { data?: Q | null; error?: ApolloError }
28
32
 
29
33
  /**
@@ -34,7 +38,7 @@ export type UseFormGqlMethods<Q, V> = Omit<UseGqlDocumentHandler<V>, 'encode' |
34
38
  * - Updates the form when the query updates
35
39
  * - Resets the form after submitting the form when no modifications are found
36
40
  */
37
- export function useFormGql<Q, V>(
41
+ export function useFormGql<Q, V extends FieldValues>(
38
42
  options: {
39
43
  document: TypedDocumentNode<Q, V>
40
44
  form: UseFormReturn<V>
@@ -61,10 +65,7 @@ export function useFormGql<Q, V>(
61
65
  const handleSubmit: UseFormReturn<V>['handleSubmit'] = (onValid, onInvalid) =>
62
66
  form.handleSubmit(async (formValues, event) => {
63
67
  // Combine defaults with the formValues and encode
64
- let variables = encode({
65
- ...defaultValues,
66
- ...(formValues as Record<string, unknown>),
67
- })
68
+ let variables = encode({ ...defaultValues, ...formValues })
68
69
 
69
70
  // Wait for the onBeforeSubmit to complete
70
71
  if (onBeforeSubmit) {
@@ -78,8 +79,7 @@ export function useFormGql<Q, V>(
78
79
  if (onComplete && result.data) await onComplete(result, variables)
79
80
 
80
81
  // Reset the state of the form if it is unmodified afterwards
81
- if (typeof diff(form.getValues(), formValues) === 'undefined')
82
- form.reset(formValues as UnpackNestedValue<DeepPartial<V>>)
82
+ if (typeof diff(form.getValues(), formValues) === 'undefined') form.reset(formValues)
83
83
 
84
84
  await onValid(formValues, event)
85
85
  }, onInvalid)
@@ -1,5 +1,5 @@
1
1
  import { MutationHookOptions, TypedDocumentNode, useMutation } from '@apollo/client'
2
- import { useForm, UseFormReturn } from 'react-hook-form'
2
+ import { FieldValues, useForm, UseFormReturn } from 'react-hook-form'
3
3
  import { useFormGql, UseFormGqlMethods, UseFormGraphQlOptions } from './useFormGql'
4
4
  import { useFormMuiRegister, UseMuiFormRegister } from './useFormMuiRegister'
5
5
  import { useFormValidFields, UseFormValidReturn } from './useFormValidFields'
@@ -8,26 +8,28 @@ export type UseFormGqlMutationReturn<
8
8
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
9
  Q extends Record<string, any> = Record<string, any>,
10
10
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- V extends Record<string, any> = Record<string, any>,
11
+ V extends FieldValues = FieldValues,
12
12
  > = UseFormGqlMethods<Q, V> &
13
13
  UseFormReturn<V> & { muiRegister: UseMuiFormRegister<V>; valid: UseFormValidReturn<V> }
14
14
 
15
- export function isFormGqlOperation<V, Q = Record<string, unknown>>(
16
- form: UseFormReturn<V>,
17
- ): form is UseFormGqlMutationReturn<Q, V> {
15
+ export function isFormGqlOperation<
16
+ V extends FieldValues,
17
+ Q extends Record<string, unknown> = Record<string, unknown>,
18
+ >(form: UseFormReturn<V>): form is UseFormGqlMutationReturn<Q, V> {
18
19
  return typeof (form as UseFormGqlMutationReturn<Q, V>).muiRegister === 'function'
19
20
  }
20
21
 
21
- export function assertFormGqlOperation<V, Q = Record<string, unknown>>(
22
- form: UseFormReturn<V>,
23
- ): asserts form is UseFormGqlMutationReturn<Q, V> {
22
+ export function assertFormGqlOperation<
23
+ V extends FieldValues,
24
+ Q extends Record<string, unknown> = Record<string, unknown>,
25
+ >(form: UseFormReturn<V>): asserts form is UseFormGqlMutationReturn<Q, V> {
24
26
  if (typeof (form as UseFormGqlMutationReturn<Q, V>).muiRegister !== 'function') {
25
27
  throw Error(`form must be one of 'useFromGqlMutation' or 'useFormGqlQuery'`)
26
28
  }
27
29
  }
28
30
 
29
31
  /** Bindings between react-hook-form's useForm and Apollo Client's useMutation hook. */
30
- export function useFormGqlMutation<Q, V>(
32
+ export function useFormGqlMutation<Q extends Record<string, unknown>, V extends FieldValues>(
31
33
  document: TypedDocumentNode<Q, V>,
32
34
  options: UseFormGraphQlOptions<Q, V> = {},
33
35
  operationOptions?: MutationHookOptions<Q, V>,
@@ -1,13 +1,16 @@
1
1
  import { LazyQueryHookOptions, TypedDocumentNode, useLazyQuery } from '@apollo/client'
2
- import { useForm } from 'react-hook-form'
2
+ import { FieldValues, useForm } from 'react-hook-form'
3
3
  import { useFormGql, UseFormGraphQlOptions } from './useFormGql'
4
4
  import { UseFormGqlMutationReturn } from './useFormGqlMutation'
5
5
  import { useFormMuiRegister } from './useFormMuiRegister'
6
6
  import { useFormValidFields } from './useFormValidFields'
7
7
 
8
- export type UseFormGqlQueryReturn<Q, V> = UseFormGqlMutationReturn<Q, V>
8
+ export type UseFormGqlQueryReturn<
9
+ Q extends Record<string, unknown>,
10
+ V extends FieldValues,
11
+ > = UseFormGqlMutationReturn<Q, V>
9
12
 
10
- export function useFormGqlQuery<Q, V>(
13
+ export function useFormGqlQuery<Q extends Record<string, unknown>, V extends FieldValues>(
11
14
  document: TypedDocumentNode<Q, V>,
12
15
  options: UseFormGraphQlOptions<Q, V> = {},
13
16
  operationOptions?: LazyQueryHookOptions<Q, V>,
@@ -13,7 +13,9 @@ export type UseMuiFormRegister<TFieldValues extends FieldValues> = <
13
13
  options?: RegisterOptions<TFieldValues, TFieldName>,
14
14
  ) => Omit<UseFormRegisterReturn, 'ref'> & { inputRef: UseFormRegisterReturn['ref'] }
15
15
 
16
- export function useFormMuiRegister<V>({ register }: Pick<UseFormReturn<V>, 'register'>) {
16
+ export function useFormMuiRegister<V extends FieldValues>({
17
+ register,
18
+ }: Pick<UseFormReturn<V>, 'register'>) {
17
19
  const muiRegister: UseMuiFormRegister<V> = (name, opts) => {
18
20
  const { ref: inputRef, ...fields } = register(name, opts)
19
21
  return { ...fields, inputRef }
@@ -37,7 +37,7 @@ export type UseFormPersistOptions<
37
37
  *
38
38
  * Todo: Use wath callback so it won't trigger a rerender
39
39
  */
40
- export function useFormPersist<V>(options: UseFormPersistOptions<V>) {
40
+ export function useFormPersist<V extends FieldValues>(options: UseFormPersistOptions<V>) {
41
41
  const { form, name, storage = 'sessionStorage', exclude = [], persist = [] } = options
42
42
  const { setValue, watch, formState } = form
43
43
 
@@ -65,10 +65,7 @@ export function useFormPersist<V>(options: UseFormPersistOptions<V>) {
65
65
 
66
66
  const storedValues = JSON.parse(storedFormStr) as FieldValues
67
67
  if (storedValues) {
68
- const entries = Object.entries(storedValues) as [
69
- Path<V>,
70
- UnpackNestedValue<FieldPathValue<V, Path<V>>>,
71
- ][]
68
+ const entries = Object.entries(storedValues) as [Path<V>, FieldPathValue<V, Path<V>>][]
72
69
  entries.forEach(([entryName, value]) =>
73
70
  setValue(entryName, value, {
74
71
  shouldDirty: true,
@@ -84,7 +84,9 @@ export type UseGqlDocumentHandler<V extends FieldValues> = {
84
84
  ) => V
85
85
  }
86
86
 
87
- export function handlerFactory<Q, V>(document: TypedDocumentNode<Q, V>): UseGqlDocumentHandler<V> {
87
+ export function handlerFactory<Q, V extends FieldValues>(
88
+ document: TypedDocumentNode<Q, V>,
89
+ ): UseGqlDocumentHandler<V> {
88
90
  type Defaults = Partial<Pick<V, OptionalKeys<V>>>
89
91
  type Encoding = { [k in keyof V]: FieldTypes }
90
92
  type Required = IsRequired<V>
@@ -125,14 +127,14 @@ export function handlerFactory<Q, V>(document: TypedDocumentNode<Q, V>): UseGqlD
125
127
 
126
128
  function encode(variables: { [k in keyof V]?: DeepStringify<V[k]> }, enc = encoding) {
127
129
  return Object.fromEntries(
128
- Object.entries(variables).map(([key, val]) => [key, encodeItem(enc[key] as FieldTypes, val)]),
130
+ Object.entries(variables).map(([key, val]) => [key, encodeItem(enc[key], val)]),
129
131
  ) as V
130
132
  }
131
133
 
132
134
  return { type, required, defaultVariables, encode }
133
135
  }
134
136
 
135
- export function useGqlDocumentHandler<Q, V>(
137
+ export function useGqlDocumentHandler<Q, V extends FieldValues>(
136
138
  document: TypedDocumentNode<Q, V>,
137
139
  ): UseGqlDocumentHandler<V> {
138
140
  return useMemo(() => handlerFactory<Q, V>(document), [document])