@graphcommerce/react-hook-form 7.1.0-canary.20 → 7.1.0-canary.22
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 +12 -0
- package/package.json +4 -4
- package/src/useFormGql.tsx +52 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 7.1.0-canary.22
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#2097](https://github.com/graphcommerce-org/graphcommerce/pull/2097) [`cdc89cedb`](https://github.com/graphcommerce-org/graphcommerce/commit/cdc89cedb9eb4d37a2c37497689e74ff6df21bf6) - Set default value of `experimental_v2` to false in `useFormGql`, since we don't want an experimental feature as default ([@mikekeehnen](https://github.com/mikekeehnen))
|
|
8
|
+
|
|
9
|
+
## 7.1.0-canary.21
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#2093](https://github.com/graphcommerce-org/graphcommerce/pull/2093) [`112b041f0`](https://github.com/graphcommerce-org/graphcommerce/commit/112b041f01a33fbd521ce3eb3955844f96b29917) - Created a new experimental mutation abort feature inside `useFormGql`. This will allow redundant mutations to be canceled. This feature can be enabled via the `experimental_useV2` prop on the `useFormGql` component ([@mikekeehnen](https://github.com/mikekeehnen))
|
|
14
|
+
|
|
3
15
|
## 7.1.0-canary.20
|
|
4
16
|
|
|
5
17
|
## 7.1.0-canary.19
|
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": "7.1.0-canary.
|
|
5
|
+
"version": "7.1.0-canary.22",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
8
8
|
"eslintConfig": {
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"react-hook-form": "7.46.1"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@graphcommerce/eslint-config-pwa": "7.1.0-canary.
|
|
20
|
-
"@graphcommerce/prettier-config-pwa": "7.1.0-canary.
|
|
21
|
-
"@graphcommerce/typescript-config-pwa": "7.1.0-canary.
|
|
19
|
+
"@graphcommerce/eslint-config-pwa": "7.1.0-canary.22",
|
|
20
|
+
"@graphcommerce/prettier-config-pwa": "7.1.0-canary.22",
|
|
21
|
+
"@graphcommerce/typescript-config-pwa": "7.1.0-canary.22",
|
|
22
22
|
"@testing-library/react": "^14.0.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
package/src/useFormGql.tsx
CHANGED
|
@@ -19,6 +19,30 @@ type UseFormGraphQLCallbacks<Q, V> = {
|
|
|
19
19
|
*/
|
|
20
20
|
onBeforeSubmit?: (variables: V) => V | false | Promise<V | false>
|
|
21
21
|
onComplete?: OnCompleteFn<Q, V>
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Changes:
|
|
25
|
+
* - Restores `defaultValues` functionality to original functionality, use `values` instead.
|
|
26
|
+
* - Does not reset the form after submission, use `values` instead.
|
|
27
|
+
* - Does not 'encode' the variables, use onBeforeSubmit instead.
|
|
28
|
+
*
|
|
29
|
+
* Future plans:
|
|
30
|
+
* - Remove the useMutation/useLazyQuery tuple and use a reguler client.mutation() call.
|
|
31
|
+
* - Write graphql errors to setError('root')
|
|
32
|
+
* - Remove onBeforeSubmit, onComplete and the handleSubmit rewrite with a single mutate() callback.
|
|
33
|
+
*
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const { handleSubmit } = useFormGqlMutation();
|
|
37
|
+
*
|
|
38
|
+
* const submit = handleSubmit((formValues, mutate) => {
|
|
39
|
+
* // onBeforeSubmit now simply is code before mutate() where you can return early for example or set errors.
|
|
40
|
+
* const result = mutate() // executes the mutation and automatically sets generic errors with setError('root')
|
|
41
|
+
* // onComplete: now simply use the result after the form, to for example reset the form, or do other things.
|
|
42
|
+
* })
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
experimental_useV2?: boolean
|
|
22
46
|
}
|
|
23
47
|
|
|
24
48
|
export type UseFormGraphQlOptions<Q, V extends FieldValues> = UseFormProps<V> &
|
|
@@ -28,7 +52,11 @@ export type UseFormGqlMethods<Q, V extends FieldValues> = Omit<
|
|
|
28
52
|
UseGqlDocumentHandler<V>,
|
|
29
53
|
'encode' | 'type'
|
|
30
54
|
> &
|
|
31
|
-
Pick<UseFormReturn<V>, 'handleSubmit'> & {
|
|
55
|
+
Pick<UseFormReturn<V>, 'handleSubmit'> & {
|
|
56
|
+
data?: Q | null
|
|
57
|
+
error?: ApolloError
|
|
58
|
+
submittedVariables?: V
|
|
59
|
+
}
|
|
32
60
|
|
|
33
61
|
/**
|
|
34
62
|
* Combines useMutation/useLazyQuery with react-hook-form's useForm:
|
|
@@ -46,16 +74,27 @@ export function useFormGql<Q, V extends FieldValues>(
|
|
|
46
74
|
defaultValues?: UseFormProps<V>['defaultValues']
|
|
47
75
|
} & UseFormGraphQLCallbacks<Q, V>,
|
|
48
76
|
): UseFormGqlMethods<Q, V> {
|
|
49
|
-
const {
|
|
77
|
+
const {
|
|
78
|
+
onComplete,
|
|
79
|
+
onBeforeSubmit,
|
|
80
|
+
document,
|
|
81
|
+
form,
|
|
82
|
+
tuple,
|
|
83
|
+
defaultValues,
|
|
84
|
+
experimental_useV2 = false,
|
|
85
|
+
} = options
|
|
50
86
|
const { encode, type, ...gqlDocumentHandler } = useGqlDocumentHandler<Q, V>(document)
|
|
51
|
-
const [execute, { data, error }] = tuple
|
|
87
|
+
const [execute, { data, error, loading }] = tuple
|
|
52
88
|
|
|
53
89
|
const submittedVariables = useRef<V>()
|
|
54
90
|
|
|
55
91
|
// automatically updates the default values
|
|
56
92
|
const initital = useRef(true)
|
|
93
|
+
const controllerRef = useRef<AbortController | undefined>()
|
|
57
94
|
const valuesString = JSON.stringify(defaultValues)
|
|
58
95
|
useEffect(() => {
|
|
96
|
+
if (experimental_useV2) return
|
|
97
|
+
|
|
59
98
|
if (initital.current) {
|
|
60
99
|
initital.current = false
|
|
61
100
|
return
|
|
@@ -69,7 +108,7 @@ export function useFormGql<Q, V extends FieldValues>(
|
|
|
69
108
|
form.handleSubmit(async (formValues, event) => {
|
|
70
109
|
// Combine defaults with the formValues and encode
|
|
71
110
|
submittedVariables.current = undefined
|
|
72
|
-
let variables = encode({ ...defaultValues, ...formValues })
|
|
111
|
+
let variables = experimental_useV2 ? formValues : encode({ ...defaultValues, ...formValues })
|
|
73
112
|
|
|
74
113
|
// Wait for the onBeforeSubmit to complete
|
|
75
114
|
if (onBeforeSubmit) {
|
|
@@ -80,11 +119,18 @@ export function useFormGql<Q, V extends FieldValues>(
|
|
|
80
119
|
// if (variables === false) onInvalid?.(formValues, event)
|
|
81
120
|
|
|
82
121
|
submittedVariables.current = variables
|
|
83
|
-
|
|
122
|
+
if (loading && experimental_useV2) controllerRef.current?.abort()
|
|
123
|
+
controllerRef.current = new window.AbortController()
|
|
124
|
+
const result = await execute({
|
|
125
|
+
variables,
|
|
126
|
+
context: { fetchOptions: { signal: controllerRef.current.signal } },
|
|
127
|
+
})
|
|
128
|
+
|
|
84
129
|
if (onComplete && result.data) await onComplete(result, variables)
|
|
85
130
|
|
|
86
131
|
// Reset the state of the form if it is unmodified afterwards
|
|
87
|
-
if (typeof diff(form.getValues(), formValues) === 'undefined'
|
|
132
|
+
if (typeof diff(form.getValues(), formValues) === 'undefined' && !experimental_useV2)
|
|
133
|
+
form.reset(formValues)
|
|
88
134
|
|
|
89
135
|
await onValid(formValues, event)
|
|
90
136
|
}, onInvalid)
|