@graphcommerce/react-hook-form 3.1.3 → 3.2.2

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,23 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1490](https://github.com/graphcommerce-org/graphcommerce/pull/1490) [`d311ef48b`](https://github.com/graphcommerce-org/graphcommerce/commit/d311ef48bb3e97806d992af5516d6b7f183ec9cb) Thanks [@paales](https://github.com/paales)! - upgraded packages
8
+
9
+ ## 3.2.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#1495](https://github.com/graphcommerce-org/graphcommerce/pull/1495) [`ffec8800a`](https://github.com/graphcommerce-org/graphcommerce/commit/ffec8800a50ff2fe9b9fc5feeb5a0a878b573f0e) Thanks [@paales](https://github.com/paales)! - solve issue where useLazyQueryPromise didn’t always resolve
14
+
15
+ ## 3.2.0
16
+
17
+ ### Minor Changes
18
+
19
+ - [#1496](https://github.com/graphcommerce-org/graphcommerce/pull/1496) [`858a3b3a3`](https://github.com/graphcommerce-org/graphcommerce/commit/858a3b3a3601cd00491219daf45557c2f1cc804b) Thanks [@FrankHarland](https://github.com/FrankHarland)! - Only submit dirty forms, allow passing false to onSubmit
20
+
3
21
  ## 3.1.3
4
22
 
5
23
  ### Patch Changes
package/index.ts CHANGED
@@ -5,7 +5,6 @@ export * from './src/useFormAutoSubmit'
5
5
  export * from './src/useFormPersist'
6
6
  export * from './src/useFormMuiRegister'
7
7
  export * from './src/useFormValidFields'
8
- export * from './src/useLazyQueryPromise'
9
8
  export * from './src/useFormGql'
10
9
  export * from './src/validationPatterns'
11
10
  export * from './src/ComposedForm'
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": "3.1.3",
5
+ "version": "3.2.2",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,19 +12,19 @@
12
12
  }
13
13
  },
14
14
  "devDependencies": {
15
- "@graphcommerce/eslint-config-pwa": "^4.1.7",
15
+ "@graphcommerce/eslint-config-pwa": "^4.1.8",
16
16
  "@graphcommerce/prettier-config-pwa": "^4.0.6",
17
17
  "@graphcommerce/typescript-config-pwa": "^4.0.3",
18
18
  "@playwright/test": "^1.21.1",
19
19
  "type-fest": "^2.12.2"
20
20
  },
21
21
  "dependencies": {
22
- "@apollo/client": "^3.6.0",
23
- "graphql": "16.4.0",
24
- "react-hook-form": "7.30.0"
22
+ "@apollo/client": "^3.6.6",
23
+ "graphql": "16.5.0",
24
+ "react-hook-form": "7.32.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "react": "^17.0.2",
28
- "react-dom": "^17.0.2"
27
+ "react": "^18.0.0",
28
+ "react-dom": "^18.0.0"
29
29
  }
30
30
  }
@@ -5,11 +5,11 @@ import {
5
5
  useApolloClient,
6
6
  MutationTuple,
7
7
  ApolloError,
8
+ LazyQueryResultTuple,
8
9
  } from '@apollo/client'
9
10
  import { UseFormProps, UseFormReturn, UnpackNestedValue, DeepPartial } from 'react-hook-form'
10
11
  import diff from './diff'
11
12
  import { useGqlDocumentHandler, UseGqlDocumentHandler } from './useGqlDocumentHandler'
12
- import { LazyQueryTuple } from './useLazyQueryPromise'
13
13
 
14
14
  export type OnCompleteFn<Q> = (
15
15
  data: FetchResult<Q>,
@@ -17,7 +17,11 @@ export type OnCompleteFn<Q> = (
17
17
  ) => void | Promise<void>
18
18
 
19
19
  type UseFormGraphQLCallbacks<Q, V> = {
20
- onBeforeSubmit?: (variables: V) => V | Promise<V>
20
+ /**
21
+ * Allows you to modify the variablels computed by the form to make it compatible with the GraphQL
22
+ * Mutation. Also allows you to send false to skip submission.
23
+ */
24
+ onBeforeSubmit?: (variables: V) => V | false | Promise<V | false>
21
25
  onComplete?: OnCompleteFn<Q>
22
26
  }
23
27
 
@@ -27,7 +31,7 @@ export type UseFormGqlMethods<Q, V> = Omit<UseGqlDocumentHandler<V>, 'encode' |
27
31
  Pick<UseFormReturn<V>, 'handleSubmit'> & { data?: Q | null; error?: ApolloError }
28
32
 
29
33
  /**
30
- * Combines useMutation/useLazyQueryPromise with react-hook-form's useForm:
34
+ * Combines useMutation/useLazyQuery with react-hook-form's useForm:
31
35
  *
32
36
  * - Automatically extracts all required arguments for a query
33
37
  * - Casts Float/Int mutation input variables to a Number
@@ -38,7 +42,7 @@ export function useFormGql<Q, V>(
38
42
  options: {
39
43
  document: TypedDocumentNode<Q, V>
40
44
  form: UseFormReturn<V>
41
- tuple: MutationTuple<Q, V> | LazyQueryTuple<Q, V>
45
+ tuple: MutationTuple<Q, V> | LazyQueryResultTuple<Q, V>
42
46
  defaultValues?: UseFormProps<V>['defaultValues']
43
47
  } & UseFormGraphQLCallbacks<Q, V>,
44
48
  ): UseFormGqlMethods<Q, V> {
@@ -56,7 +60,12 @@ export function useFormGql<Q, V>(
56
60
  })
57
61
 
58
62
  // Wait for the onBeforeSubmit to complete
59
- if (onBeforeSubmit) variables = await onBeforeSubmit(variables)
63
+ if (onBeforeSubmit) {
64
+ const res = await onBeforeSubmit(variables)
65
+ if (res === false) return
66
+ variables = res
67
+ }
68
+ // if (variables === false) onInvalid?.(formValues, event)
60
69
 
61
70
  const result = await execute({ variables })
62
71
  if (onComplete && result.data) await onComplete(result, client)
@@ -1,10 +1,9 @@
1
- import { LazyQueryHookOptions, TypedDocumentNode } from '@apollo/client'
1
+ import { LazyQueryHookOptions, TypedDocumentNode, useLazyQuery } from '@apollo/client'
2
2
  import { 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
- import { useLazyQueryPromise } from './useLazyQueryPromise'
8
7
 
9
8
  export type UseFormGqlQueryReturn<Q, V> = UseFormGqlMutationReturn<Q, V>
10
9
 
@@ -14,7 +13,7 @@ export function useFormGqlQuery<Q, V>(
14
13
  operationOptions?: LazyQueryHookOptions<Q, V>,
15
14
  ): UseFormGqlQueryReturn<Q, V> {
16
15
  const form = useForm<V>(options)
17
- const tuple = useLazyQueryPromise(document, operationOptions)
16
+ const tuple = useLazyQuery(document, operationOptions)
18
17
  const operation = useFormGql({ document, form, tuple, ...options })
19
18
  const muiRegister = useFormMuiRegister(form)
20
19
  const valid = useFormValidFields(form, operation.required)
@@ -34,6 +34,8 @@ export type UseFormPersistOptions<
34
34
  /**
35
35
  * Will persist any dirty fields and store it in the sessionStorage/localStorage Will restory any
36
36
  * dirty fields when the form is initialized
37
+ *
38
+ * Todo: Use wath callback so it won't trigger a rerender
37
39
  */
38
40
  export function useFormPersist<V>(options: UseFormPersistOptions<V>) {
39
41
  const { form, name, storage = 'sessionStorage', exclude = [], persist = [] } = options
@@ -1,43 +0,0 @@
1
- import {
2
- DocumentNode,
3
- LazyQueryHookOptions,
4
- LazyQueryResult,
5
- QueryLazyOptions,
6
- TypedDocumentNode,
7
- useLazyQuery,
8
- } from '@apollo/client'
9
- import { useEffect, useRef } from 'react'
10
- import type { Promisable } from 'type-fest'
11
-
12
- export type LazyQueryTuple<Q, V> = [
13
- (options?: QueryLazyOptions<V>) => Promisable<LazyQueryResult<Q, V>>,
14
- LazyQueryResult<Q, V>,
15
- ]
16
-
17
- /**
18
- * Same API as useLazyQuery, except:
19
- *
20
- * - The execute method is a promise that will return the eventual result
21
- */
22
- export function useLazyQueryPromise<Q, V>(
23
- query: DocumentNode | TypedDocumentNode<Q, V>,
24
- options?: LazyQueryHookOptions<Q, V>,
25
- ): LazyQueryTuple<Q, V> {
26
- const [execute, result] = useLazyQuery<Q, V>(query, options)
27
- const ref = useRef<(value: Promisable<LazyQueryResult<Q, V>>) => void>()
28
-
29
- useEffect(() => {
30
- if (!result.called || result.loading || !ref.current) return
31
- ref.current(result)
32
- ref.current = undefined
33
- }, [result])
34
-
35
- const queryLazily = (executeOptions?: QueryLazyOptions<V>) => {
36
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
37
- execute(executeOptions)
38
- return new Promise<LazyQueryResult<Q, V>>((resolve) => {
39
- ref.current = resolve
40
- })
41
- }
42
- return [queryLazily, result]
43
- }