@graphcommerce/react-hook-form 3.3.4 → 4.30.0-canary.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,13 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 4.30.0-canary.0
|
|
4
|
+
|
|
5
|
+
## 3.3.5
|
|
6
|
+
|
|
7
|
+
### Patch Changes
|
|
8
|
+
|
|
9
|
+
- [#1665](https://github.com/graphcommerce-org/graphcommerce/pull/1665) [`1f2e14ba8`](https://github.com/graphcommerce-org/graphcommerce/commit/1f2e14ba8b674b87257a123e8cb215157890eb22) Thanks [@paales](https://github.com/paales)! - Make sure the onComplete callback also sends the variables
|
|
10
|
+
|
|
3
11
|
## 3.3.4
|
|
4
12
|
|
|
5
13
|
### Patch Changes
|
|
@@ -1,25 +1,35 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import { ApolloClient, InMemoryCache } from '@apollo/client'
|
|
3
|
+
import { renderHook } from '@testing-library/react'
|
|
2
4
|
import { TestShippingAddressFormDocument } from '../__mocks__/TestShippingAddressForm.gql'
|
|
3
5
|
import { useFormGqlMutation } from '../src/useFormGqlMutation'
|
|
4
6
|
|
|
5
7
|
describe('useFormGqlMutation', () => {
|
|
6
|
-
const {
|
|
7
|
-
|
|
8
|
+
const { result } = renderHook(() =>
|
|
9
|
+
useFormGqlMutation(
|
|
10
|
+
TestShippingAddressFormDocument,
|
|
11
|
+
{},
|
|
12
|
+
{
|
|
13
|
+
client: new ApolloClient({
|
|
14
|
+
cache: new InMemoryCache(),
|
|
15
|
+
}),
|
|
16
|
+
},
|
|
17
|
+
),
|
|
8
18
|
)
|
|
9
19
|
|
|
10
20
|
it('can register stuff', () => {
|
|
11
|
-
register('address.telephone')
|
|
12
|
-
register('customerNote')
|
|
13
|
-
register('address.street.0')
|
|
21
|
+
result.current.register('address.telephone')
|
|
22
|
+
result.current.register('customerNote')
|
|
23
|
+
result.current.register('address.street.0')
|
|
14
24
|
|
|
15
25
|
// @ts-expect-error should not be posssible
|
|
16
|
-
register('address.street.hoi')
|
|
26
|
+
result.current.register('address.street.hoi')
|
|
17
27
|
})
|
|
18
28
|
|
|
19
29
|
it('extracts required fields correctly', () => {
|
|
20
|
-
expect(required).toEqual({ address: true, cartId: true, customerNote: false })
|
|
30
|
+
expect(result.current.required).toEqual({ address: true, cartId: true, customerNote: false })
|
|
21
31
|
})
|
|
22
32
|
it('extracts defaults correctly', () => {
|
|
23
|
-
expect(defaultVariables).toEqual({ customerNote: 'joi' })
|
|
33
|
+
expect(result.current.defaultVariables).toEqual({ customerNote: 'joi' })
|
|
24
34
|
})
|
|
25
35
|
})
|
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
|
+
"version": "4.30.0-canary.0",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
8
8
|
"eslintConfig": {
|
package/src/useFormGql.tsx
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
FetchResult,
|
|
3
|
-
ApolloClient,
|
|
4
3
|
TypedDocumentNode,
|
|
5
|
-
useApolloClient,
|
|
6
4
|
MutationTuple,
|
|
7
5
|
ApolloError,
|
|
8
6
|
LazyQueryResultTuple,
|
|
@@ -12,10 +10,7 @@ import { UseFormProps, UseFormReturn, UnpackNestedValue, DeepPartial } from 'rea
|
|
|
12
10
|
import diff from './diff'
|
|
13
11
|
import { useGqlDocumentHandler, UseGqlDocumentHandler } from './useGqlDocumentHandler'
|
|
14
12
|
|
|
15
|
-
export type OnCompleteFn<Q> = (
|
|
16
|
-
data: FetchResult<Q>,
|
|
17
|
-
client: ApolloClient<unknown>,
|
|
18
|
-
) => void | Promise<void>
|
|
13
|
+
export type OnCompleteFn<Q, V> = (data: FetchResult<Q>, variables: V) => void | Promise<void>
|
|
19
14
|
|
|
20
15
|
type UseFormGraphQLCallbacks<Q, V> = {
|
|
21
16
|
/**
|
|
@@ -23,7 +18,7 @@ type UseFormGraphQLCallbacks<Q, V> = {
|
|
|
23
18
|
* Mutation. Also allows you to send false to skip submission.
|
|
24
19
|
*/
|
|
25
20
|
onBeforeSubmit?: (variables: V) => V | false | Promise<V | false>
|
|
26
|
-
onComplete?: OnCompleteFn<Q>
|
|
21
|
+
onComplete?: OnCompleteFn<Q, V>
|
|
27
22
|
}
|
|
28
23
|
|
|
29
24
|
export type UseFormGraphQlOptions<Q, V> = UseFormProps<V> & UseFormGraphQLCallbacks<Q, V>
|
|
@@ -50,7 +45,6 @@ export function useFormGql<Q, V>(
|
|
|
50
45
|
const { onComplete, onBeforeSubmit, document, form, tuple, defaultValues } = options
|
|
51
46
|
const { encode, type, ...gqlDocumentHandler } = useGqlDocumentHandler<Q, V>(document)
|
|
52
47
|
const [execute, { data, error }] = tuple
|
|
53
|
-
const client = useApolloClient()
|
|
54
48
|
|
|
55
49
|
// automatically updates the default values
|
|
56
50
|
const initital = useRef(true)
|
|
@@ -81,7 +75,7 @@ export function useFormGql<Q, V>(
|
|
|
81
75
|
// if (variables === false) onInvalid?.(formValues, event)
|
|
82
76
|
|
|
83
77
|
const result = await execute({ variables })
|
|
84
|
-
if (onComplete && result.data) await onComplete(result,
|
|
78
|
+
if (onComplete && result.data) await onComplete(result, variables)
|
|
85
79
|
|
|
86
80
|
// Reset the state of the form if it is unmodified afterwards
|
|
87
81
|
if (typeof diff(form.getValues(), formValues) === 'undefined')
|