@graphcommerce/react-hook-form 2.102.1
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 +99 -0
- package/README.md +157 -0
- package/__mocks__/TestShippingAddressForm.gql.ts +14 -0
- package/__mocks__/TestShippingAddressForm.graphql +16 -0
- package/__tests__/useFormGqlMutation.tsx +41 -0
- package/__tests__/useGqlDocumentHandler.tsx +50 -0
- package/index.ts +11 -0
- package/package.json +34 -0
- package/src/ComposedForm/ComposedForm.tsx +32 -0
- package/src/ComposedForm/ComposedSubmit.tsx +84 -0
- package/src/ComposedForm/context.ts +6 -0
- package/src/ComposedForm/index.ts +8 -0
- package/src/ComposedForm/reducer.ts +78 -0
- package/src/ComposedForm/types.ts +85 -0
- package/src/ComposedForm/useFormCompose.ts +36 -0
- package/src/diff.ts +39 -0
- package/src/useFormAutoSubmit.tsx +82 -0
- package/src/useFormGql.tsx +73 -0
- package/src/useFormGqlMutation.tsx +39 -0
- package/src/useFormGqlQuery.tsx +23 -0
- package/src/useFormMuiRegister.tsx +22 -0
- package/src/useFormPersist.tsx +71 -0
- package/src/useFormValidFields.tsx +23 -0
- package/src/useGqlDocumentHandler.tsx +151 -0
- package/src/useLazyQueryPromise.tsx +41 -0
- package/src/validationPatterns.tsx +4 -0
- package/tsconfig.json +5 -0
|
@@ -0,0 +1,41 @@
|
|
|
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 { 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
|
+
* - The execute method is a promise that will return the eventual result
|
|
20
|
+
*/
|
|
21
|
+
export function useLazyQueryPromise<Q, V>(
|
|
22
|
+
query: DocumentNode | TypedDocumentNode<Q, V>,
|
|
23
|
+
options?: LazyQueryHookOptions<Q, V>,
|
|
24
|
+
): LazyQueryTuple<Q, V> {
|
|
25
|
+
const [execute, result] = useLazyQuery<Q, V>(query, options)
|
|
26
|
+
const ref = useRef<(value: Promisable<LazyQueryResult<Q, V>>) => void>()
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!result.called || result.loading || !ref.current) return
|
|
30
|
+
ref.current(result)
|
|
31
|
+
ref.current = undefined
|
|
32
|
+
}, [result])
|
|
33
|
+
|
|
34
|
+
const queryLazily = (executeOptions?: QueryLazyOptions<V>) => {
|
|
35
|
+
execute(executeOptions)
|
|
36
|
+
return new Promise<LazyQueryResult<Q, V>>((resolve) => {
|
|
37
|
+
ref.current = resolve
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
return [queryLazily, result]
|
|
41
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export const emailPattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
2
|
+
export const phonePattern = /^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$/
|
|
3
|
+
|
|
4
|
+
export const houseNumberPattern = /^\d+([-\\/]\d+)*$/
|