@tanstack/preact-form 1.30.4 → 2.0.0-alpha.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.
Files changed (115) hide show
  1. package/dist/AppForm/Components.lib.js +91 -0
  2. package/dist/AppForm/PreactAppFormApi.public.d.ts +14 -0
  3. package/dist/AppForm/appFormOptions.public.d.ts +14 -0
  4. package/dist/AppForm/componentMap.public.d.ts +10 -0
  5. package/dist/AppForm/contexts.lib.js +18 -0
  6. package/dist/AppForm/createFormHook.public.d.ts +6 -0
  7. package/dist/AppForm/createFormHook.public.js +26 -0
  8. package/dist/AppForm/createFormHookTypes.public.d.ts +15 -0
  9. package/dist/AppForm/fieldComponentHelpers.lib.js +22 -0
  10. package/dist/AppForm/getFormHookHelpers.public.d.ts +34 -0
  11. package/dist/AppForm/getFormHookHelpers.public.js +18 -0
  12. package/dist/AppForm/initializeAppForm.lib.js +13 -0
  13. package/dist/FieldGroup/FieldGroupApi.public.d.ts +24 -0
  14. package/dist/FieldGroup/withFields.lib.js +62 -0
  15. package/dist/FieldGroup/withFields.public.d.ts +55 -0
  16. package/dist/FieldGroup/withFields.public.js +8 -0
  17. package/dist/PreactForm/Components.lib.js +99 -0
  18. package/dist/PreactForm/Components.public.d.ts +66 -0
  19. package/dist/PreactForm/PreactFormApi.lib.js +28 -0
  20. package/dist/PreactForm/fieldSubscriptions.lib.js +29 -0
  21. package/dist/PreactForm/formApiTypes.public.d.ts +10 -0
  22. package/dist/PreactForm/formType.public.d.ts +9 -0
  23. package/dist/PreactForm/useField.lib.js +31 -0
  24. package/dist/PreactForm/useForm.public.d.ts +8 -0
  25. package/dist/PreactForm/useForm.public.js +10 -0
  26. package/dist/Subscribe.public.d.ts +29 -0
  27. package/dist/Subscribe.public.js +16 -0
  28. package/dist/index.d.ts +18 -0
  29. package/dist/index.js +11 -0
  30. package/dist/preactTypes.public.d.ts +8 -0
  31. package/dist/types.public.d.ts +2 -0
  32. package/package.json +17 -18
  33. package/src/AppForm/Components.lib.tsx +160 -0
  34. package/src/AppForm/PreactAppFormApi.public.tsx +17 -0
  35. package/src/AppForm/appFormOptions.public.ts +59 -0
  36. package/src/AppForm/componentMap.public.ts +19 -0
  37. package/src/AppForm/contexts.lib.ts +28 -0
  38. package/src/AppForm/createFormHook.public.ts +37 -0
  39. package/src/AppForm/createFormHookTypes.public.ts +32 -0
  40. package/src/AppForm/fieldComponentHelpers.lib.tsx +30 -0
  41. package/src/AppForm/getFormHookHelpers.public.ts +114 -0
  42. package/src/AppForm/initializeAppForm.lib.ts +22 -0
  43. package/src/FieldGroup/FieldGroupApi.public.ts +91 -0
  44. package/src/FieldGroup/withFields.lib.ts +109 -0
  45. package/src/FieldGroup/withFields.public.ts +203 -0
  46. package/src/PreactForm/Components.lib.tsx +166 -0
  47. package/src/PreactForm/Components.public.ts +383 -0
  48. package/src/PreactForm/PreactFormApi.lib.tsx +48 -0
  49. package/src/PreactForm/fieldSubscriptions.lib.ts +32 -0
  50. package/src/PreactForm/formApiTypes.public.ts +29 -0
  51. package/src/PreactForm/formType.public.ts +52 -0
  52. package/src/PreactForm/useField.lib.ts +52 -0
  53. package/src/PreactForm/useForm.public.ts +34 -0
  54. package/src/Subscribe.public.tsx +56 -0
  55. package/src/index.ts +16 -9
  56. package/src/preactTypes.public.ts +6 -0
  57. package/src/types.public.ts +2 -0
  58. package/dist/cjs/createFormHook.cjs +0 -101
  59. package/dist/cjs/createFormHook.cjs.map +0 -1
  60. package/dist/cjs/createFormHook.d.cts +0 -80
  61. package/dist/cjs/index.cjs +0 -34
  62. package/dist/cjs/index.cjs.map +0 -1
  63. package/dist/cjs/index.d.cts +0 -9
  64. package/dist/cjs/types.d.cts +0 -17
  65. package/dist/cjs/useField.cjs +0 -108
  66. package/dist/cjs/useField.cjs.map +0 -1
  67. package/dist/cjs/useField.d.cts +0 -52
  68. package/dist/cjs/useFieldGroup.cjs +0 -48
  69. package/dist/cjs/useFieldGroup.cjs.map +0 -1
  70. package/dist/cjs/useFieldGroup.d.cts +0 -30
  71. package/dist/cjs/useForm.cjs +0 -78
  72. package/dist/cjs/useForm.cjs.map +0 -1
  73. package/dist/cjs/useForm.d.cts +0 -31
  74. package/dist/cjs/useFormGroup.cjs +0 -163
  75. package/dist/cjs/useFormGroup.cjs.map +0 -1
  76. package/dist/cjs/useFormGroup.d.cts +0 -13
  77. package/dist/cjs/useFormId.cjs +0 -6
  78. package/dist/cjs/useFormId.cjs.map +0 -1
  79. package/dist/cjs/useFormId.d.cts +0 -2
  80. package/dist/cjs/useIsomorphicLayoutEffect.cjs +0 -6
  81. package/dist/cjs/useIsomorphicLayoutEffect.cjs.map +0 -1
  82. package/dist/cjs/useIsomorphicLayoutEffect.d.cts +0 -2
  83. package/dist/esm/createFormHook.d.ts +0 -80
  84. package/dist/esm/createFormHook.js +0 -101
  85. package/dist/esm/createFormHook.js.map +0 -1
  86. package/dist/esm/index.d.ts +0 -9
  87. package/dist/esm/index.js +0 -22
  88. package/dist/esm/index.js.map +0 -1
  89. package/dist/esm/types.d.ts +0 -17
  90. package/dist/esm/useField.d.ts +0 -52
  91. package/dist/esm/useField.js +0 -108
  92. package/dist/esm/useField.js.map +0 -1
  93. package/dist/esm/useFieldGroup.d.ts +0 -30
  94. package/dist/esm/useFieldGroup.js +0 -48
  95. package/dist/esm/useFieldGroup.js.map +0 -1
  96. package/dist/esm/useForm.d.ts +0 -31
  97. package/dist/esm/useForm.js +0 -78
  98. package/dist/esm/useForm.js.map +0 -1
  99. package/dist/esm/useFormGroup.d.ts +0 -13
  100. package/dist/esm/useFormGroup.js +0 -163
  101. package/dist/esm/useFormGroup.js.map +0 -1
  102. package/dist/esm/useFormId.d.ts +0 -2
  103. package/dist/esm/useFormId.js +0 -6
  104. package/dist/esm/useFormId.js.map +0 -1
  105. package/dist/esm/useIsomorphicLayoutEffect.d.ts +0 -2
  106. package/dist/esm/useIsomorphicLayoutEffect.js +0 -6
  107. package/dist/esm/useIsomorphicLayoutEffect.js.map +0 -1
  108. package/src/createFormHook.tsx +0 -603
  109. package/src/types.ts +0 -143
  110. package/src/useField.tsx +0 -730
  111. package/src/useFieldGroup.tsx +0 -259
  112. package/src/useForm.tsx +0 -300
  113. package/src/useFormGroup.tsx +0 -741
  114. package/src/useFormId.ts +0 -3
  115. package/src/useIsomorphicLayoutEffect.ts +0 -4
@@ -1,259 +0,0 @@
1
- import { useState } from 'preact/hooks'
2
- import { FieldGroupApi, functionalUpdate } from '@tanstack/form-core'
3
- import { useSelector } from '@tanstack/preact-store'
4
- import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
5
- import type {
6
- AnyFieldGroupApi,
7
- DeepKeysOfType,
8
- FieldGroupState,
9
- FieldsMap,
10
- FormAsyncValidateOrFn,
11
- FormValidateOrFn,
12
- } from '@tanstack/form-core'
13
- import type { AppFieldExtendedPreactFormApi } from './createFormHook'
14
- import type { ComponentChild, ComponentType, FunctionComponent } from 'preact'
15
- import type { LensFieldComponent } from './useField'
16
- import type { PropsWithChildren } from './types'
17
-
18
- function LocalSubscribe({
19
- lens,
20
- selector,
21
- children,
22
- }: PropsWithChildren<{
23
- lens: AnyFieldGroupApi
24
- selector: (state: FieldGroupState<any>) => FieldGroupState<any>
25
- }>): ReturnType<FunctionComponent> {
26
- const data = useSelector(lens.store, selector)
27
-
28
- return <>{functionalUpdate(children, data)}</>
29
- }
30
-
31
- /**
32
- * @private
33
- */
34
- export type AppFieldExtendedPreactFieldGroupApi<
35
- TFormData,
36
- TFieldGroupData,
37
- TFields extends
38
- | DeepKeysOfType<TFormData, TFieldGroupData | null | undefined>
39
- | FieldsMap<TFormData, TFieldGroupData>,
40
- TOnMount extends undefined | FormValidateOrFn<TFormData>,
41
- TOnChange extends undefined | FormValidateOrFn<TFormData>,
42
- TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
43
- TOnBlur extends undefined | FormValidateOrFn<TFormData>,
44
- TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
45
- TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
46
- TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
47
- TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
48
- TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
49
- TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
50
- TSubmitMeta,
51
- TFieldComponents extends Record<string, ComponentType<any>>,
52
- TFormComponents extends Record<string, ComponentType<any>>,
53
- > = FieldGroupApi<
54
- TFormData,
55
- TFieldGroupData,
56
- TFields,
57
- TOnMount,
58
- TOnChange,
59
- TOnChangeAsync,
60
- TOnBlur,
61
- TOnBlurAsync,
62
- TOnSubmit,
63
- TOnSubmitAsync,
64
- TOnDynamic,
65
- TOnDynamicAsync,
66
- TOnServer,
67
- TSubmitMeta
68
- > &
69
- NoInfer<TFormComponents> & {
70
- AppField: LensFieldComponent<
71
- TFieldGroupData,
72
- TSubmitMeta,
73
- NoInfer<TFieldComponents>
74
- >
75
- AppForm: ComponentType<PropsWithChildren<{}>>
76
- /**
77
- * A React component to render form fields. With this, you can render and manage individual form fields.
78
- */
79
- Field: LensFieldComponent<TFieldGroupData, TSubmitMeta>
80
-
81
- /**
82
- * A `Subscribe` function that allows you to listen and react to changes in the form's state. It's especially useful when you need to execute side effects or render specific components in response to state updates.
83
- */
84
- Subscribe: <TSelected = NoInfer<FieldGroupState<TFieldGroupData>>>(props: {
85
- selector?: (state: NoInfer<FieldGroupState<TFieldGroupData>>) => TSelected
86
- children: ((state: NoInfer<TSelected>) => ComponentChild) | ComponentChild
87
- }) => ReturnType<FunctionComponent>
88
- }
89
-
90
- export function useFieldGroup<
91
- TFormData,
92
- TFieldGroupData,
93
- TFields extends
94
- | DeepKeysOfType<TFormData, TFieldGroupData | null | undefined>
95
- | FieldsMap<TFormData, TFieldGroupData>,
96
- TOnMount extends undefined | FormValidateOrFn<TFormData>,
97
- TOnChange extends undefined | FormValidateOrFn<TFormData>,
98
- TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
99
- TOnBlur extends undefined | FormValidateOrFn<TFormData>,
100
- TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
101
- TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
102
- TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
103
- TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
104
- TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
105
- TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
106
- TComponents extends Record<string, ComponentType<any>>,
107
- TFormComponents extends Record<string, ComponentType<any>>,
108
- TSubmitMeta = never,
109
- >(opts: {
110
- form:
111
- | AppFieldExtendedPreactFormApi<
112
- TFormData,
113
- TOnMount,
114
- TOnChange,
115
- TOnChangeAsync,
116
- TOnBlur,
117
- TOnBlurAsync,
118
- TOnSubmit,
119
- TOnSubmitAsync,
120
- TOnDynamic,
121
- TOnDynamicAsync,
122
- TOnServer,
123
- TSubmitMeta,
124
- TComponents,
125
- TFormComponents
126
- >
127
- | AppFieldExtendedPreactFieldGroupApi<
128
- // Since this only occurs if you nest it within other form lenses, it can be more
129
- // lenient with the types.
130
- unknown,
131
- TFormData,
132
- string | FieldsMap<unknown, TFormData>,
133
- any,
134
- any,
135
- any,
136
- any,
137
- any,
138
- any,
139
- any,
140
- any,
141
- any,
142
- any,
143
- TSubmitMeta,
144
- TComponents,
145
- TFormComponents
146
- >
147
- fields: TFields
148
- defaultValues?: TFieldGroupData
149
- onSubmitMeta?: TSubmitMeta
150
- formComponents: TFormComponents
151
- }): AppFieldExtendedPreactFieldGroupApi<
152
- TFormData,
153
- TFieldGroupData,
154
- TFields,
155
- TOnMount,
156
- TOnChange,
157
- TOnChangeAsync,
158
- TOnBlur,
159
- TOnBlurAsync,
160
- TOnSubmit,
161
- TOnSubmitAsync,
162
- TOnDynamic,
163
- TOnDynamicAsync,
164
- TOnServer,
165
- TSubmitMeta,
166
- TComponents,
167
- TFormComponents
168
- > {
169
- const [formLensApi] = useState(() => {
170
- const api = new FieldGroupApi(opts)
171
- const form =
172
- opts.form instanceof FieldGroupApi
173
- ? (opts.form.form as AppFieldExtendedPreactFormApi<
174
- TFormData,
175
- TOnMount,
176
- TOnChange,
177
- TOnChangeAsync,
178
- TOnBlur,
179
- TOnBlurAsync,
180
- TOnSubmit,
181
- TOnSubmitAsync,
182
- TOnDynamic,
183
- TOnDynamicAsync,
184
- TOnServer,
185
- TSubmitMeta,
186
- TComponents,
187
- TFormComponents
188
- >)
189
- : opts.form
190
-
191
- const extendedApi: AppFieldExtendedPreactFieldGroupApi<
192
- TFormData,
193
- TFieldGroupData,
194
- TFields,
195
- TOnMount,
196
- TOnChange,
197
- TOnChangeAsync,
198
- TOnBlur,
199
- TOnBlurAsync,
200
- TOnSubmit,
201
- TOnSubmitAsync,
202
- TOnDynamic,
203
- TOnDynamicAsync,
204
- TOnServer,
205
- TSubmitMeta,
206
- TComponents,
207
- TFormComponents
208
- > = api as never
209
-
210
- extendedApi.AppForm = function AppForm(appFormProps) {
211
- return <form.AppForm {...appFormProps} />
212
- }
213
-
214
- extendedApi.AppField = function AppField(props) {
215
- return (
216
- <form.AppField {...(formLensApi.getFormFieldOptions(props) as any)} />
217
- )
218
- }
219
-
220
- extendedApi.Field = function Field(props) {
221
- return <form.Field {...(formLensApi.getFormFieldOptions(props) as any)} />
222
- }
223
-
224
- extendedApi.Subscribe = function Subscribe(props: any) {
225
- return (
226
- <LocalSubscribe
227
- lens={formLensApi}
228
- selector={props.selector}
229
- children={props.children}
230
- />
231
- )
232
- }
233
-
234
- return Object.assign(extendedApi, {
235
- ...opts.formComponents,
236
- }) as AppFieldExtendedPreactFieldGroupApi<
237
- TFormData,
238
- TFieldGroupData,
239
- TFields,
240
- TOnMount,
241
- TOnChange,
242
- TOnChangeAsync,
243
- TOnBlur,
244
- TOnBlurAsync,
245
- TOnSubmit,
246
- TOnSubmitAsync,
247
- TOnDynamic,
248
- TOnDynamicAsync,
249
- TOnServer,
250
- TSubmitMeta,
251
- TComponents,
252
- TFormComponents
253
- >
254
- })
255
-
256
- useIsomorphicLayoutEffect(formLensApi.mount, [formLensApi])
257
-
258
- return formLensApi
259
- }
package/src/useForm.tsx DELETED
@@ -1,300 +0,0 @@
1
- import { FormApi, functionalUpdate, mergeAndUpdate } from '@tanstack/form-core'
2
- import { useMemo, useRef, useState } from 'preact/hooks'
3
- import { useSelector } from '@tanstack/preact-store'
4
- import { Field } from './useField'
5
- import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
6
- import { useFormId } from './useFormId'
7
- import { FormGroup } from './useFormGroup'
8
- import type { FormGroupComponent } from './useFormGroup'
9
- import type {
10
- AnyFormApi,
11
- AnyFormState,
12
- FormAsyncValidateOrFn,
13
- FormOptions,
14
- FormState,
15
- FormValidateOrFn,
16
- } from '@tanstack/form-core'
17
- import type { ComponentChild, FunctionComponent } from 'preact'
18
- import type { FieldComponent } from './useField'
19
- import type { PropsWithChildren } from './types'
20
-
21
- /**
22
- * Fields that are added onto the `FormAPI` from `@tanstack/form-core` and returned from `useForm`
23
- */
24
- export interface PreactFormApi<
25
- in out TFormData,
26
- in out TOnMount extends undefined | FormValidateOrFn<TFormData>,
27
- in out TOnChange extends undefined | FormValidateOrFn<TFormData>,
28
- in out TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
29
- in out TOnBlur extends undefined | FormValidateOrFn<TFormData>,
30
- in out TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
31
- in out TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
32
- in out TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
33
- in out TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
34
- in out TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
35
- in out TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
36
- in out TSubmitMeta,
37
- > {
38
- /**
39
- * A Preact component to render form fields. With this, you can render and manage individual form fields.
40
- */
41
- Field: FieldComponent<
42
- TFormData,
43
- TOnMount,
44
- TOnChange,
45
- TOnChangeAsync,
46
- TOnBlur,
47
- TOnBlurAsync,
48
- TOnSubmit,
49
- TOnSubmitAsync,
50
- TOnDynamic,
51
- TOnDynamicAsync,
52
- TOnServer,
53
- TSubmitMeta
54
- >
55
- FormGroup: FormGroupComponent<
56
- TFormData,
57
- TOnMount,
58
- TOnChange,
59
- TOnChangeAsync,
60
- TOnBlur,
61
- TOnBlurAsync,
62
- TOnSubmit,
63
- TOnSubmitAsync,
64
- TOnDynamic,
65
- TOnDynamicAsync,
66
- TOnServer,
67
- TSubmitMeta
68
- >
69
- /**
70
- * A `Subscribe` function that allows you to listen and react to changes in the form's state. It's especially useful when you need to execute side effects or render specific components in response to state updates.
71
- */
72
- Subscribe: <
73
- TSelected = NoInfer<
74
- FormState<
75
- TFormData,
76
- TOnMount,
77
- TOnChange,
78
- TOnChangeAsync,
79
- TOnBlur,
80
- TOnBlurAsync,
81
- TOnSubmit,
82
- TOnSubmitAsync,
83
- TOnDynamic,
84
- TOnDynamicAsync,
85
- TOnServer
86
- >
87
- >,
88
- >(props: {
89
- selector?: (
90
- state: NoInfer<
91
- FormState<
92
- TFormData,
93
- TOnMount,
94
- TOnChange,
95
- TOnChangeAsync,
96
- TOnBlur,
97
- TOnBlurAsync,
98
- TOnSubmit,
99
- TOnSubmitAsync,
100
- TOnDynamic,
101
- TOnDynamicAsync,
102
- TOnServer
103
- >
104
- >,
105
- ) => TSelected
106
- children: ((state: NoInfer<TSelected>) => ComponentChild) | ComponentChild
107
- }) => ReturnType<FunctionComponent>
108
- }
109
-
110
- /**
111
- * An extended version of the `FormApi` class that includes Preact-specific functionalities from `PreactFormApi`
112
- */
113
- export type PreactFormExtendedApi<
114
- TFormData,
115
- TOnMount extends undefined | FormValidateOrFn<TFormData>,
116
- TOnChange extends undefined | FormValidateOrFn<TFormData>,
117
- TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
118
- TOnBlur extends undefined | FormValidateOrFn<TFormData>,
119
- TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
120
- TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
121
- TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
122
- TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
123
- TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
124
- TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
125
- TSubmitMeta,
126
- > = FormApi<
127
- TFormData,
128
- TOnMount,
129
- TOnChange,
130
- TOnChangeAsync,
131
- TOnBlur,
132
- TOnBlurAsync,
133
- TOnSubmit,
134
- TOnSubmitAsync,
135
- TOnDynamic,
136
- TOnDynamicAsync,
137
- TOnServer,
138
- TSubmitMeta
139
- > &
140
- PreactFormApi<
141
- TFormData,
142
- TOnMount,
143
- TOnChange,
144
- TOnChangeAsync,
145
- TOnBlur,
146
- TOnBlurAsync,
147
- TOnSubmit,
148
- TOnSubmitAsync,
149
- TOnDynamic,
150
- TOnDynamicAsync,
151
- TOnServer,
152
- TSubmitMeta
153
- >
154
-
155
- function LocalSubscribe({
156
- form,
157
- selector,
158
- children,
159
- }: PropsWithChildren<{
160
- form: AnyFormApi
161
- selector: (state: AnyFormState) => AnyFormState
162
- }>): ReturnType<FunctionComponent> {
163
- const data = useSelector(form.store, selector)
164
-
165
- return <>{functionalUpdate(children, data)}</>
166
- }
167
-
168
- /**
169
- * A custom React Hook that returns an extended instance of the `FormApi` class.
170
- *
171
- * This API encapsulates all the necessary functionalities related to the form. It allows you to manage form state, handle submissions, and interact with form fields
172
- */
173
- export function useForm<
174
- TFormData,
175
- TOnMount extends undefined | FormValidateOrFn<TFormData>,
176
- TOnChange extends undefined | FormValidateOrFn<TFormData>,
177
- TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
178
- TOnBlur extends undefined | FormValidateOrFn<TFormData>,
179
- TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
180
- TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
181
- TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
182
- TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
183
- TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
184
- TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
185
- TSubmitMeta,
186
- >(
187
- opts?: FormOptions<
188
- TFormData,
189
- TOnMount,
190
- TOnChange,
191
- TOnChangeAsync,
192
- TOnBlur,
193
- TOnBlurAsync,
194
- TOnSubmit,
195
- TOnSubmitAsync,
196
- TOnDynamic,
197
- TOnDynamicAsync,
198
- TOnServer,
199
- TSubmitMeta
200
- >,
201
- ) {
202
- const fallbackFormId = useFormId()
203
- const [prevFormId, setPrevFormId] = useState<string>(opts?.formId as never)
204
-
205
- const [formApi, setFormApi] = useState(() => {
206
- return new FormApi<
207
- TFormData,
208
- TOnMount,
209
- TOnChange,
210
- TOnChangeAsync,
211
- TOnBlur,
212
- TOnBlurAsync,
213
- TOnSubmit,
214
- TOnSubmitAsync,
215
- TOnDynamic,
216
- TOnDynamicAsync,
217
- TOnServer,
218
- TSubmitMeta
219
- >({ ...opts, formId: opts?.formId ?? fallbackFormId })
220
- })
221
-
222
- if (prevFormId !== opts?.formId) {
223
- const formId = opts?.formId ?? fallbackFormId
224
- setFormApi(new FormApi({ ...opts, formId }))
225
- setPrevFormId(formId)
226
- }
227
-
228
- const extendedFormApi = useMemo(() => {
229
- const extendedApi: PreactFormExtendedApi<
230
- TFormData,
231
- TOnMount,
232
- TOnChange,
233
- TOnChangeAsync,
234
- TOnBlur,
235
- TOnBlurAsync,
236
- TOnSubmit,
237
- TOnSubmitAsync,
238
- TOnDynamic,
239
- TOnDynamicAsync,
240
- TOnServer,
241
- TSubmitMeta
242
- > = {
243
- ...formApi,
244
- handleSubmit: ((...props: never[]) => {
245
- return formApi._handleSubmit(...props)
246
- }) as typeof formApi.handleSubmit,
247
- // We must add all `get`ters from `core`'s `FormApi` here, as otherwise the spread operator won't catch those
248
- get formId(): string {
249
- return formApi._formId
250
- },
251
- get state() {
252
- return formApi.store.state
253
- },
254
- } as never
255
-
256
- extendedApi.Field = function APIField(props) {
257
- return <Field {...props} form={formApi} />
258
- }
259
-
260
- extendedApi.FormGroup = function APIFormGroup(props) {
261
- return <FormGroup {...props} form={formApi} />
262
- }
263
-
264
- extendedApi.Subscribe = function Subscribe(props: any) {
265
- return (
266
- <LocalSubscribe
267
- form={formApi}
268
- selector={props.selector}
269
- children={props.children}
270
- />
271
- )
272
- }
273
-
274
- return extendedApi
275
- }, [formApi])
276
-
277
- useIsomorphicLayoutEffect(formApi.mount, [])
278
-
279
- /**
280
- * formApi.update should not have any side effects. Think of it like a `useRef`
281
- * that we need to keep updated every render with the most up-to-date information.
282
- */
283
- useIsomorphicLayoutEffect(() => {
284
- formApi.update(opts)
285
- })
286
-
287
- const hasRan = useRef(false)
288
-
289
- useIsomorphicLayoutEffect(() => {
290
- if (!hasRan.current) return
291
- if (!opts?.transform) return
292
- mergeAndUpdate(formApi, opts.transform as never)
293
- }, [formApi, opts?.transform])
294
-
295
- useIsomorphicLayoutEffect(() => {
296
- hasRan.current = true
297
- })
298
-
299
- return extendedFormApi
300
- }