@graphcommerce/react-hook-form 8.0.6-canary.0 → 8.0.6-canary.2

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