@graphcommerce/react-hook-form 8.0.6-canary.3 → 8.0.6
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 +1 -24
- package/README.md +14 -6
- package/package.json +4 -4
- package/src/useFormAutoSubmit.tsx +1 -3
- package/src/useFormGqlMutation.tsx +5 -12
- package/src/useFormGqlQuery.tsx +3 -1
- package/src/useFormMuiRegister.tsx +0 -3
- package/src/useFormPersist.tsx +5 -25
- package/src/useFormValidFields.tsx +0 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,29 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
## 8.0.6
|
|
4
|
-
|
|
5
|
-
## 8.0.6-canary.2
|
|
6
|
-
|
|
7
|
-
### Patch Changes
|
|
8
|
-
|
|
9
|
-
- [#2234](https://github.com/graphcommerce-org/graphcommerce/pull/2234) [`43bd04a`](https://github.com/graphcommerce-org/graphcommerce/commit/43bd04a777c5800cc7e01bee1e123a5aad82f194) - Add deprecation warnings for useFormMuiRegister. Refactor useFormPersist to useWatch and add a separate `<FormPersist/>` component to prevent rerenders.
|
|
10
|
-
([@FrankHarland](https://github.com/FrankHarland))
|
|
11
|
-
|
|
12
|
-
- [#2234](https://github.com/graphcommerce-org/graphcommerce/pull/2234) [`0767bc4`](https://github.com/graphcommerce-org/graphcommerce/commit/0767bc40f7b596209f24ca4e745ff0441f3275c9) - Upgrade input components to no longer use muiRegister, which improves INP scores
|
|
13
|
-
([@FrankHarland](https://github.com/FrankHarland))
|
|
14
|
-
|
|
15
|
-
- [#2234](https://github.com/graphcommerce-org/graphcommerce/pull/2234) [`02da217`](https://github.com/graphcommerce-org/graphcommerce/commit/02da2172ef702133510f6923190efae2801032c5) - Migrate most usages of useFormAutoSubmit to <FormAutoSubmit/> and deprecated useFormAutoSubmit
|
|
16
|
-
([@FrankHarland](https://github.com/FrankHarland))
|
|
17
|
-
|
|
18
|
-
- [#2234](https://github.com/graphcommerce-org/graphcommerce/pull/2234) [`530076e`](https://github.com/graphcommerce-org/graphcommerce/commit/530076e3664703cb8b577b7fcf1998a420819f60) - Moved all usages of useFormPersist to the <FormPersist/> component to prevent rerenders.
|
|
19
|
-
([@FrankHarland](https://github.com/FrankHarland))
|
|
20
|
-
|
|
21
|
-
- [#2234](https://github.com/graphcommerce-org/graphcommerce/pull/2234) [`1a6d0c4`](https://github.com/graphcommerce-org/graphcommerce/commit/1a6d0c4a3584b1e404b444f1ca44c68eaad56cb7) - Mark useFormValidFields as deprecated: Please use TextInputElement, SelectElement, etc. with the showValid prop
|
|
22
|
-
([@FrankHarland](https://github.com/FrankHarland))
|
|
23
|
-
|
|
24
|
-
## 8.0.6-canary.1
|
|
25
|
-
|
|
26
|
-
## 8.0.6-canary.0
|
|
3
|
+
## 8.0.6
|
|
27
4
|
|
|
28
5
|
## 8.0.5
|
|
29
6
|
|
package/README.md
CHANGED
|
@@ -77,10 +77,10 @@ export default function MyComponent() {
|
|
|
77
77
|
}
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
-
## `
|
|
80
|
+
## `useFormAutoSubmit`
|
|
81
81
|
|
|
82
82
|
```tsx
|
|
83
|
-
import {
|
|
83
|
+
import { useFormAutoSubmit } from '@graphcommerce/react-hook-form'
|
|
84
84
|
|
|
85
85
|
export default function MyAutoSubmitForm() {
|
|
86
86
|
// Regular useForm hook, but you can also use useFormGqlMutation
|
|
@@ -90,13 +90,20 @@ export default function MyAutoSubmitForm() {
|
|
|
90
90
|
const submit = handleSubmit(() => {
|
|
91
91
|
console.log('submitted')
|
|
92
92
|
})
|
|
93
|
+
const autoSubmitting = useFormAutoSubmit({
|
|
94
|
+
form,
|
|
95
|
+
submit,
|
|
96
|
+
fields: ['couponCode'], //optional, default: all fields
|
|
97
|
+
wait: 1200, // optional, default: 500ms
|
|
98
|
+
})
|
|
99
|
+
const disableFields = formState.isSubmitting && !autoSubmitting
|
|
93
100
|
|
|
94
101
|
return (
|
|
95
102
|
<form onSubmit={submit} noValidate>
|
|
96
|
-
<FormAutoSubmit control={control} submit={submit} name={['couponCode']} wait={1200}>
|
|
97
103
|
<input
|
|
98
104
|
type='text'
|
|
99
105
|
{...register('couponCode', { required: required.couponCode })}
|
|
106
|
+
disabled={formState.isSubmitting}
|
|
100
107
|
/>
|
|
101
108
|
{errors.couponCode?.message}
|
|
102
109
|
</form>
|
|
@@ -104,10 +111,10 @@ export default function MyAutoSubmitForm() {
|
|
|
104
111
|
}
|
|
105
112
|
```
|
|
106
113
|
|
|
107
|
-
### `
|
|
114
|
+
### `useFormPersist`
|
|
108
115
|
|
|
109
116
|
```tsx
|
|
110
|
-
import {
|
|
117
|
+
import { useFormAutoSubmit } from '@graphcommerce/react-hook-form'
|
|
111
118
|
|
|
112
119
|
export default function MyAutoSubmitForm() {
|
|
113
120
|
// Regular useForm hook, but you can also use useFormGqlMutation
|
|
@@ -117,10 +124,11 @@ export default function MyAutoSubmitForm() {
|
|
|
117
124
|
const submit = handleSubmit(() => {
|
|
118
125
|
console.log('submitted')
|
|
119
126
|
})
|
|
127
|
+
const autoSubmitting = useFormPersist({ form, name: 'MyForm' })
|
|
128
|
+
const disableFields = formState.isSubmitting && !autoSubmitting
|
|
120
129
|
|
|
121
130
|
return (
|
|
122
131
|
<form onSubmit={submit} noValidate>
|
|
123
|
-
<FormPersist form={form} name='MyForm'>
|
|
124
132
|
<input
|
|
125
133
|
type='text'
|
|
126
134
|
{...register('couponCode', { required: required.couponCode })}
|
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": "8.0.6
|
|
5
|
+
"version": "8.0.6",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
8
8
|
"eslintConfig": {
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
18
|
"@apollo/client": "^3",
|
|
19
|
-
"@graphcommerce/eslint-config-pwa": "^8.0.6
|
|
20
|
-
"@graphcommerce/prettier-config-pwa": "^8.0.6
|
|
21
|
-
"@graphcommerce/typescript-config-pwa": "^8.0.6
|
|
19
|
+
"@graphcommerce/eslint-config-pwa": "^8.0.6",
|
|
20
|
+
"@graphcommerce/prettier-config-pwa": "^8.0.6",
|
|
21
|
+
"@graphcommerce/typescript-config-pwa": "^8.0.6",
|
|
22
22
|
"graphql": "^16.6.0",
|
|
23
23
|
"react": "^18.2.0",
|
|
24
24
|
"react-dom": "^18.2.0",
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
} from 'react-hook-form'
|
|
16
16
|
import { DebounceOptions } from './utils/debounce'
|
|
17
17
|
import { useDebouncedCallback } from './utils/useDebounceCallback'
|
|
18
|
+
import { useFormValidFields } from './useFormValidFields'
|
|
18
19
|
|
|
19
20
|
export type UseFormAutoSubmitOptions<TForm extends UseFormReturn<V>, V extends FieldValues> = {
|
|
20
21
|
/** Instance of current form */
|
|
@@ -53,9 +54,6 @@ export type UseFormAutoSubmitOptions<TForm extends UseFormReturn<V>, V extends F
|
|
|
53
54
|
* Q: How to I resubmit if the form is modified during the request?
|
|
54
55
|
* formState.isDirty should be true after the submission
|
|
55
56
|
* @see useFormGqlMutation.tsx for an example implementation
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* @deprecated Please use the <FormAutoSubmit /> component instead. This method causes excessive rerenders.
|
|
59
57
|
*/
|
|
60
58
|
export function useFormAutoSubmit<
|
|
61
59
|
Form extends UseFormReturn<V>,
|
|
@@ -2,7 +2,7 @@ import { MutationHookOptions, TypedDocumentNode, useMutation } from '@apollo/cli
|
|
|
2
2
|
import { FieldValues, useForm, UseFormReturn } from 'react-hook-form'
|
|
3
3
|
import { useFormGql, UseFormGqlMethods, UseFormGraphQlOptions } from './useFormGql'
|
|
4
4
|
import { useFormMuiRegister, UseMuiFormRegister } from './useFormMuiRegister'
|
|
5
|
-
import { UseFormValidReturn } from './useFormValidFields'
|
|
5
|
+
import { useFormValidFields, UseFormValidReturn } from './useFormValidFields'
|
|
6
6
|
|
|
7
7
|
export type UseFormGqlMutationReturn<
|
|
8
8
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -10,16 +10,7 @@ export type UseFormGqlMutationReturn<
|
|
|
10
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
11
|
V extends FieldValues = FieldValues,
|
|
12
12
|
> = UseFormGqlMethods<Q, V> &
|
|
13
|
-
UseFormReturn<V> & {
|
|
14
|
-
/**
|
|
15
|
-
* @deprecated Please use TextFieldElement
|
|
16
|
-
*/
|
|
17
|
-
muiRegister: UseMuiFormRegister<V>
|
|
18
|
-
/**
|
|
19
|
-
* @deprecated Please use TextFieldElement showValid
|
|
20
|
-
*/
|
|
21
|
-
valid: UseFormValidReturn<V>
|
|
22
|
-
}
|
|
13
|
+
UseFormReturn<V> & { muiRegister: UseMuiFormRegister<V>; valid: UseFormValidReturn<V> }
|
|
23
14
|
|
|
24
15
|
export function isFormGqlOperation<
|
|
25
16
|
V extends FieldValues,
|
|
@@ -47,5 +38,7 @@ export function useFormGqlMutation<Q extends Record<string, unknown>, V extends
|
|
|
47
38
|
const tuple = useMutation(document, operationOptions)
|
|
48
39
|
const operation = useFormGql({ document, form, tuple, ...options })
|
|
49
40
|
const muiRegister = useFormMuiRegister(form)
|
|
50
|
-
|
|
41
|
+
const valid = useFormValidFields(form, operation.required)
|
|
42
|
+
|
|
43
|
+
return { ...form, ...operation, valid, muiRegister }
|
|
51
44
|
}
|
package/src/useFormGqlQuery.tsx
CHANGED
|
@@ -3,6 +3,7 @@ import { FieldValues, useForm } from 'react-hook-form'
|
|
|
3
3
|
import { useFormGql, UseFormGraphQlOptions } from './useFormGql'
|
|
4
4
|
import { UseFormGqlMutationReturn } from './useFormGqlMutation'
|
|
5
5
|
import { useFormMuiRegister } from './useFormMuiRegister'
|
|
6
|
+
import { useFormValidFields } from './useFormValidFields'
|
|
6
7
|
|
|
7
8
|
export type UseFormGqlQueryReturn<
|
|
8
9
|
Q extends Record<string, unknown>,
|
|
@@ -18,6 +19,7 @@ export function useFormGqlQuery<Q extends Record<string, unknown>, V extends Fie
|
|
|
18
19
|
const tuple = useLazyQuery(document, operationOptions)
|
|
19
20
|
const operation = useFormGql({ document, form, tuple, ...options })
|
|
20
21
|
const muiRegister = useFormMuiRegister(form)
|
|
22
|
+
const valid = useFormValidFields(form, operation.required)
|
|
21
23
|
|
|
22
|
-
return { ...form, ...operation, valid
|
|
24
|
+
return { ...form, ...operation, valid, muiRegister }
|
|
23
25
|
}
|
|
@@ -13,9 +13,6 @@ export type UseMuiFormRegister<TFieldValues extends FieldValues> = <
|
|
|
13
13
|
options?: RegisterOptions<TFieldValues, TFieldName>,
|
|
14
14
|
) => Omit<UseFormRegisterReturn, 'ref'> & { inputRef: UseFormRegisterReturn['ref'] }
|
|
15
15
|
|
|
16
|
-
/**
|
|
17
|
-
* @deprecated Please use use TextFieldElement, etc.
|
|
18
|
-
*/
|
|
19
16
|
export function useFormMuiRegister<V extends FieldValues>({
|
|
20
17
|
register,
|
|
21
18
|
}: Pick<UseFormReturn<V>, 'register'>) {
|
package/src/useFormPersist.tsx
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
import { useMemoObject } from '@graphcommerce/next-ui/hooks/useMemoObject'
|
|
2
1
|
import { useEffect } from 'react'
|
|
3
|
-
import {
|
|
4
|
-
FieldValues,
|
|
5
|
-
UseFormReturn,
|
|
6
|
-
Path,
|
|
7
|
-
FieldPath,
|
|
8
|
-
PathValue,
|
|
9
|
-
useWatch,
|
|
10
|
-
useFormState,
|
|
11
|
-
} from 'react-hook-form'
|
|
2
|
+
import { FieldValues, UseFormReturn, Path, FieldPath, PathValue } from 'react-hook-form'
|
|
12
3
|
|
|
13
4
|
export type UseFormPersistOptions<
|
|
14
5
|
TFieldValues extends FieldValues = FieldValues,
|
|
@@ -38,30 +29,24 @@ export type UseFormPersistOptions<
|
|
|
38
29
|
* dirty fields when the form is initialized
|
|
39
30
|
*
|
|
40
31
|
* Todo: Use wath callback so it won't trigger a rerender
|
|
41
|
-
*
|
|
42
|
-
* @deprecated Please use <FormPersist /> instead. This method causes INP problems.
|
|
43
32
|
*/
|
|
44
33
|
export function useFormPersist<V extends FieldValues>(options: UseFormPersistOptions<V>) {
|
|
45
34
|
const { form, name, storage = 'sessionStorage', exclude = [], persist = [] } = options
|
|
46
|
-
const { setValue,
|
|
47
|
-
|
|
48
|
-
const formState = useFormState({ control })
|
|
49
|
-
const allFields = useWatch({ control })
|
|
35
|
+
const { setValue, watch, formState } = form
|
|
50
36
|
|
|
51
37
|
const dirtyFieldKeys = Object.keys(formState.dirtyFields) as Path<V>[]
|
|
52
38
|
|
|
53
|
-
//
|
|
39
|
+
// Get all dirty field values and exclude sensitive data
|
|
54
40
|
const newValues = Object.fromEntries(
|
|
55
|
-
dirtyFieldKeys.filter((f) => !exclude.includes(f)).map((field) => [field,
|
|
41
|
+
dirtyFieldKeys.filter((f) => !exclude.includes(f)).map((field) => [field, watch(field)]),
|
|
56
42
|
)
|
|
57
43
|
|
|
58
44
|
// Amend the values with the values that should always be persisted
|
|
59
45
|
persist.forEach((persistKey) => {
|
|
60
|
-
const value =
|
|
46
|
+
const value = watch(persistKey)
|
|
61
47
|
if (value) newValues[persistKey] = value
|
|
62
48
|
})
|
|
63
49
|
|
|
64
|
-
// const valuesJson = useMemoObject(newValues)
|
|
65
50
|
const valuesJson = JSON.stringify(newValues)
|
|
66
51
|
|
|
67
52
|
// Restore changes
|
|
@@ -97,8 +82,3 @@ export function useFormPersist<V extends FieldValues>(options: UseFormPersistOpt
|
|
|
97
82
|
}
|
|
98
83
|
}, [name, storage, valuesJson])
|
|
99
84
|
}
|
|
100
|
-
|
|
101
|
-
export function FormPersist<V extends FieldValues>(props: UseFormPersistOptions<V>) {
|
|
102
|
-
useFormPersist(props)
|
|
103
|
-
return null
|
|
104
|
-
}
|
|
@@ -7,8 +7,6 @@ export type UseFormValidReturn<TFieldValues> = Partial<Record<Path<TFieldValues>
|
|
|
7
7
|
* ### useFormValidFields
|
|
8
8
|
*
|
|
9
9
|
* Record field names as key and boolean as value indicating whether the field is valid
|
|
10
|
-
*
|
|
11
|
-
* @deprecated Please use TextInputElement, SelectElement, etc. with the showValid prop
|
|
12
10
|
*/
|
|
13
11
|
export function useFormValidFields<TFieldValues extends FieldValues>(
|
|
14
12
|
form: Pick<UseFormReturn<TFieldValues>, 'watch' | 'formState'>,
|