@graphcommerce/react-hook-form 3.3.0 → 3.3.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,23 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.3.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1629](https://github.com/graphcommerce-org/graphcommerce/pull/1629) [`662f510c2`](https://github.com/graphcommerce-org/graphcommerce/commit/662f510c21fc44a63036e5c7a0726ccb33c31600) Thanks [@paales](https://github.com/paales)! - The submit button would remain in a loading state when all composed forms were invalid
8
+
9
+ ## 3.3.2
10
+
11
+ ### Patch Changes
12
+
13
+ - [#1573](https://github.com/graphcommerce-org/graphcommerce/pull/1573) [`02023d8d8`](https://github.com/graphcommerce-org/graphcommerce/commit/02023d8d89c8138144243edce67290bd79ff49a7) Thanks [@paales](https://github.com/paales)! - useGqlDocumentHandler would not encode array items correctly
14
+
15
+ ## 3.3.1
16
+
17
+ ### Patch Changes
18
+
19
+ - [#1553](https://github.com/graphcommerce-org/graphcommerce/pull/1553) [`4a4579bb2`](https://github.com/graphcommerce-org/graphcommerce/commit/4a4579bb2f7da378f3fcc504405caf2560dc10f6) Thanks [@NickdeK](https://github.com/NickdeK)! - When using a useFormGqlMutation or useFormGqlQuery and defaultValues change, the form values will update for fields that aren't dirty.
20
+
3
21
  ## 3.3.0
4
22
 
5
23
  ### Minor Changes
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.3.0",
5
+ "version": "3.3.3",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -104,7 +104,12 @@ export function ComposedSubmit(props: ComposedSubmitProps) {
104
104
  throw e
105
105
  }
106
106
  }
107
- dispatch({ type: 'SUBMITTING' })
107
+
108
+ dispatch(
109
+ invalidKeys.length === 0
110
+ ? { type: 'SUBMITTING' }
111
+ : { type: 'SUBMITTED', isSubmitSuccessful: false },
112
+ )
108
113
  } catch (error) {
109
114
  dispatch({ type: 'SUBMITTED', isSubmitSuccessful: false })
110
115
  }
@@ -7,6 +7,7 @@ import {
7
7
  ApolloError,
8
8
  LazyQueryResultTuple,
9
9
  } from '@apollo/client'
10
+ import { useEffect, useRef } from 'react'
10
11
  import { UseFormProps, UseFormReturn, UnpackNestedValue, DeepPartial } from 'react-hook-form'
11
12
  import diff from './diff'
12
13
  import { useGqlDocumentHandler, UseGqlDocumentHandler } from './useGqlDocumentHandler'
@@ -51,6 +52,18 @@ export function useFormGql<Q, V>(
51
52
  const [execute, { data, error }] = tuple
52
53
  const client = useApolloClient()
53
54
 
55
+ // automatically updates the default values
56
+ const initital = useRef(true)
57
+ const valuesString = JSON.stringify(defaultValues)
58
+ useEffect(() => {
59
+ if (initital.current) {
60
+ initital.current = false
61
+ return
62
+ }
63
+ form.reset(defaultValues, { keepDirtyValues: true })
64
+ // eslint-disable-next-line react-hooks/exhaustive-deps
65
+ }, [valuesString, form])
66
+
54
67
  const handleSubmit: UseFormReturn<V>['handleSubmit'] = (onValid, onInvalid) =>
55
68
  form.handleSubmit(async (formValues, event) => {
56
69
  // Combine defaults with the formValues and encode
@@ -1,5 +1,5 @@
1
1
  import type { TypedDocumentNode } from '@apollo/client'
2
- import {
2
+ import type {
3
3
  DefinitionNode,
4
4
  OperationDefinitionNode,
5
5
  ValueNode,
@@ -124,7 +124,7 @@ export function handlerFactory<Q, V>(document: TypedDocumentNode<Q, V>): UseGqlD
124
124
  }
125
125
 
126
126
  function encodeItem(enc: FieldTypes, val: unknown) {
127
- if (Array.isArray(enc)) return [encodeItem(enc[0], val)]
127
+ if (Array.isArray(val)) return val.map((v, i) => encodeItem(enc[i], v))
128
128
  if (val && typeof val === 'object') {
129
129
  return Object.fromEntries(
130
130
  Object.entries(val).map(([key, v]) => [key, heuristicEncode(v as string)]),