@graphcommerce/react-hook-form 3.1.3 → 3.2.0

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,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#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
8
+
3
9
  ## 3.1.3
4
10
 
5
11
  ### Patch 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.1.3",
5
+ "version": "3.2.0",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -57,13 +57,26 @@ export function ComposedSubmit(props: ComposedSubmitProps) {
57
57
 
58
58
  let formsToProcess = formEntries
59
59
  if (invalidKeys.length === 0) {
60
+ const onlyDirty = formEntries.filter(([, { form }]) => Boolean(form?.formState.isDirty))
61
+
60
62
  if (process.env.NODE_ENV !== 'production') {
61
- // eslint-disable-next-line no-console
62
- console.log(
63
- '[ComposedForm] All forms are valid, submitting...',
64
- formsToProcess.map(([, { key }]) => key),
65
- )
63
+ const all = formsToProcess.map(([, { key }]) => key).join(',')
64
+ const dirtyForms = onlyDirty.map(([, { key }]) => key).join(',')
65
+
66
+ if (dirtyForms.length > 0) {
67
+ // eslint-disable-next-line no-console
68
+ console.log(
69
+ `[ComposedForm] All forms are valid (${all}), following forms are dirty (${dirtyForms}), submitting...`,
70
+ )
71
+ } else {
72
+ // eslint-disable-next-line no-console
73
+ console.log(
74
+ `[ComposedForm] All forms are valid (${all}), no forms to submit, continuing...`,
75
+ )
76
+ }
66
77
  }
78
+
79
+ formsToProcess = onlyDirty
67
80
  } else {
68
81
  formsToProcess = formEntries.filter(([, { key }]) => invalidKeys.includes(key))
69
82
  if (process.env.NODE_ENV !== 'production') {
@@ -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
 
@@ -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)
@@ -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