@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.
- package/dist/AppForm/Components.lib.js +91 -0
- package/dist/AppForm/PreactAppFormApi.public.d.ts +14 -0
- package/dist/AppForm/appFormOptions.public.d.ts +14 -0
- package/dist/AppForm/componentMap.public.d.ts +10 -0
- package/dist/AppForm/contexts.lib.js +18 -0
- package/dist/AppForm/createFormHook.public.d.ts +6 -0
- package/dist/AppForm/createFormHook.public.js +26 -0
- package/dist/AppForm/createFormHookTypes.public.d.ts +15 -0
- package/dist/AppForm/fieldComponentHelpers.lib.js +22 -0
- package/dist/AppForm/getFormHookHelpers.public.d.ts +34 -0
- package/dist/AppForm/getFormHookHelpers.public.js +18 -0
- package/dist/AppForm/initializeAppForm.lib.js +13 -0
- package/dist/FieldGroup/FieldGroupApi.public.d.ts +24 -0
- package/dist/FieldGroup/withFields.lib.js +62 -0
- package/dist/FieldGroup/withFields.public.d.ts +55 -0
- package/dist/FieldGroup/withFields.public.js +8 -0
- package/dist/PreactForm/Components.lib.js +99 -0
- package/dist/PreactForm/Components.public.d.ts +66 -0
- package/dist/PreactForm/PreactFormApi.lib.js +28 -0
- package/dist/PreactForm/fieldSubscriptions.lib.js +29 -0
- package/dist/PreactForm/formApiTypes.public.d.ts +10 -0
- package/dist/PreactForm/formType.public.d.ts +9 -0
- package/dist/PreactForm/useField.lib.js +31 -0
- package/dist/PreactForm/useForm.public.d.ts +8 -0
- package/dist/PreactForm/useForm.public.js +10 -0
- package/dist/Subscribe.public.d.ts +29 -0
- package/dist/Subscribe.public.js +16 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +11 -0
- package/dist/preactTypes.public.d.ts +8 -0
- package/dist/types.public.d.ts +2 -0
- package/package.json +17 -18
- package/src/AppForm/Components.lib.tsx +160 -0
- package/src/AppForm/PreactAppFormApi.public.tsx +17 -0
- package/src/AppForm/appFormOptions.public.ts +59 -0
- package/src/AppForm/componentMap.public.ts +19 -0
- package/src/AppForm/contexts.lib.ts +28 -0
- package/src/AppForm/createFormHook.public.ts +37 -0
- package/src/AppForm/createFormHookTypes.public.ts +32 -0
- package/src/AppForm/fieldComponentHelpers.lib.tsx +30 -0
- package/src/AppForm/getFormHookHelpers.public.ts +114 -0
- package/src/AppForm/initializeAppForm.lib.ts +22 -0
- package/src/FieldGroup/FieldGroupApi.public.ts +91 -0
- package/src/FieldGroup/withFields.lib.ts +109 -0
- package/src/FieldGroup/withFields.public.ts +203 -0
- package/src/PreactForm/Components.lib.tsx +166 -0
- package/src/PreactForm/Components.public.ts +383 -0
- package/src/PreactForm/PreactFormApi.lib.tsx +48 -0
- package/src/PreactForm/fieldSubscriptions.lib.ts +32 -0
- package/src/PreactForm/formApiTypes.public.ts +29 -0
- package/src/PreactForm/formType.public.ts +52 -0
- package/src/PreactForm/useField.lib.ts +52 -0
- package/src/PreactForm/useForm.public.ts +34 -0
- package/src/Subscribe.public.tsx +56 -0
- package/src/index.ts +16 -9
- package/src/preactTypes.public.ts +6 -0
- package/src/types.public.ts +2 -0
- package/dist/cjs/createFormHook.cjs +0 -101
- package/dist/cjs/createFormHook.cjs.map +0 -1
- package/dist/cjs/createFormHook.d.cts +0 -80
- package/dist/cjs/index.cjs +0 -34
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -9
- package/dist/cjs/types.d.cts +0 -17
- package/dist/cjs/useField.cjs +0 -108
- package/dist/cjs/useField.cjs.map +0 -1
- package/dist/cjs/useField.d.cts +0 -52
- package/dist/cjs/useFieldGroup.cjs +0 -48
- package/dist/cjs/useFieldGroup.cjs.map +0 -1
- package/dist/cjs/useFieldGroup.d.cts +0 -30
- package/dist/cjs/useForm.cjs +0 -78
- package/dist/cjs/useForm.cjs.map +0 -1
- package/dist/cjs/useForm.d.cts +0 -31
- package/dist/cjs/useFormGroup.cjs +0 -163
- package/dist/cjs/useFormGroup.cjs.map +0 -1
- package/dist/cjs/useFormGroup.d.cts +0 -13
- package/dist/cjs/useFormId.cjs +0 -6
- package/dist/cjs/useFormId.cjs.map +0 -1
- package/dist/cjs/useFormId.d.cts +0 -2
- package/dist/cjs/useIsomorphicLayoutEffect.cjs +0 -6
- package/dist/cjs/useIsomorphicLayoutEffect.cjs.map +0 -1
- package/dist/cjs/useIsomorphicLayoutEffect.d.cts +0 -2
- package/dist/esm/createFormHook.d.ts +0 -80
- package/dist/esm/createFormHook.js +0 -101
- package/dist/esm/createFormHook.js.map +0 -1
- package/dist/esm/index.d.ts +0 -9
- package/dist/esm/index.js +0 -22
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/types.d.ts +0 -17
- package/dist/esm/useField.d.ts +0 -52
- package/dist/esm/useField.js +0 -108
- package/dist/esm/useField.js.map +0 -1
- package/dist/esm/useFieldGroup.d.ts +0 -30
- package/dist/esm/useFieldGroup.js +0 -48
- package/dist/esm/useFieldGroup.js.map +0 -1
- package/dist/esm/useForm.d.ts +0 -31
- package/dist/esm/useForm.js +0 -78
- package/dist/esm/useForm.js.map +0 -1
- package/dist/esm/useFormGroup.d.ts +0 -13
- package/dist/esm/useFormGroup.js +0 -163
- package/dist/esm/useFormGroup.js.map +0 -1
- package/dist/esm/useFormId.d.ts +0 -2
- package/dist/esm/useFormId.js +0 -6
- package/dist/esm/useFormId.js.map +0 -1
- package/dist/esm/useIsomorphicLayoutEffect.d.ts +0 -2
- package/dist/esm/useIsomorphicLayoutEffect.js +0 -6
- package/dist/esm/useIsomorphicLayoutEffect.js.map +0 -1
- package/src/createFormHook.tsx +0 -603
- package/src/types.ts +0 -143
- package/src/useField.tsx +0 -730
- package/src/useFieldGroup.tsx +0 -259
- package/src/useForm.tsx +0 -300
- package/src/useFormGroup.tsx +0 -741
- package/src/useFormId.ts +0 -3
- package/src/useIsomorphicLayoutEffect.ts +0 -4
package/src/createFormHook.tsx
DELETED
|
@@ -1,603 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @eslint-react/no-context-provider */
|
|
2
|
-
import { createContext } from 'preact'
|
|
3
|
-
import { useContext, useMemo } from 'preact/hooks'
|
|
4
|
-
import { useForm } from './useForm'
|
|
5
|
-
import { useFieldGroup } from './useFieldGroup'
|
|
6
|
-
import type {
|
|
7
|
-
AnyFieldApi,
|
|
8
|
-
AnyFormApi,
|
|
9
|
-
BaseFormOptions,
|
|
10
|
-
DeepKeysOfType,
|
|
11
|
-
FieldApi,
|
|
12
|
-
FieldsMap,
|
|
13
|
-
FormAsyncValidateOrFn,
|
|
14
|
-
FormOptions,
|
|
15
|
-
FormValidateOrFn,
|
|
16
|
-
} from '@tanstack/form-core'
|
|
17
|
-
import type { ComponentType, Context, FunctionComponent } from 'preact'
|
|
18
|
-
import type { PropsWithChildren } from './types'
|
|
19
|
-
import type { FieldComponent } from './useField'
|
|
20
|
-
import type { PreactFormExtendedApi } from './useForm'
|
|
21
|
-
import type { AppFieldExtendedPreactFieldGroupApi } from './useFieldGroup'
|
|
22
|
-
|
|
23
|
-
// We should never hit the `null` case here
|
|
24
|
-
const FieldContext = createContext<AnyFieldApi>(null as never)
|
|
25
|
-
const FormContext = createContext<AnyFormApi>(null as never)
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* TypeScript inferencing is weird.
|
|
29
|
-
*
|
|
30
|
-
* If you have:
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
*
|
|
34
|
-
* interface Args<T> {
|
|
35
|
-
* arg?: T
|
|
36
|
-
* }
|
|
37
|
-
*
|
|
38
|
-
* function test<T>(arg?: Partial<Args<T>>): T {
|
|
39
|
-
* return 0 as any;
|
|
40
|
-
* }
|
|
41
|
-
*
|
|
42
|
-
* const a = test({});
|
|
43
|
-
*
|
|
44
|
-
* Then `T` will default to `unknown`.
|
|
45
|
-
*
|
|
46
|
-
* However, if we change `test` to be:
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
*
|
|
50
|
-
* function test<T extends undefined>(arg?: Partial<Args<T>>): T;
|
|
51
|
-
*
|
|
52
|
-
* Then `T` becomes `undefined`.
|
|
53
|
-
*
|
|
54
|
-
* Here, we are checking if the passed type `T` extends `DefaultT` and **only**
|
|
55
|
-
* `DefaultT`, as if that's the case we assume that inferencing has not occurred.
|
|
56
|
-
*/
|
|
57
|
-
type UnwrapOrAny<T> = [unknown] extends [T] ? any : T
|
|
58
|
-
type UnwrapDefaultOrAny<DefaultT, T> = [DefaultT] extends [T]
|
|
59
|
-
? [T] extends [DefaultT]
|
|
60
|
-
? any
|
|
61
|
-
: T
|
|
62
|
-
: T
|
|
63
|
-
|
|
64
|
-
function useFormContext() {
|
|
65
|
-
const form = useContext(FormContext)
|
|
66
|
-
|
|
67
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
68
|
-
if (!form) {
|
|
69
|
-
throw new Error(
|
|
70
|
-
'`formContext` only works when within a `formComponent` passed to `createFormHook`',
|
|
71
|
-
)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return form as PreactFormExtendedApi<
|
|
75
|
-
// If you need access to the form data, you need to use `withForm` instead
|
|
76
|
-
Record<string, never>,
|
|
77
|
-
any,
|
|
78
|
-
any,
|
|
79
|
-
any,
|
|
80
|
-
any,
|
|
81
|
-
any,
|
|
82
|
-
any,
|
|
83
|
-
any,
|
|
84
|
-
any,
|
|
85
|
-
any,
|
|
86
|
-
any,
|
|
87
|
-
any
|
|
88
|
-
>
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function createFormHookContexts() {
|
|
92
|
-
function useFieldContext<TData>() {
|
|
93
|
-
const field = useContext(FieldContext)
|
|
94
|
-
|
|
95
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
96
|
-
if (!field) {
|
|
97
|
-
throw new Error(
|
|
98
|
-
'`fieldContext` only works when within a `fieldComponent` passed to `createFormHook`',
|
|
99
|
-
)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return field as FieldApi<
|
|
103
|
-
any,
|
|
104
|
-
string,
|
|
105
|
-
TData,
|
|
106
|
-
any,
|
|
107
|
-
any,
|
|
108
|
-
any,
|
|
109
|
-
any,
|
|
110
|
-
any,
|
|
111
|
-
any,
|
|
112
|
-
any,
|
|
113
|
-
any,
|
|
114
|
-
any,
|
|
115
|
-
any,
|
|
116
|
-
any,
|
|
117
|
-
any,
|
|
118
|
-
any,
|
|
119
|
-
any,
|
|
120
|
-
any,
|
|
121
|
-
any,
|
|
122
|
-
any,
|
|
123
|
-
any,
|
|
124
|
-
any,
|
|
125
|
-
any
|
|
126
|
-
>
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
fieldContext: FieldContext,
|
|
131
|
-
useFieldContext,
|
|
132
|
-
useFormContext,
|
|
133
|
-
formContext: FormContext,
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
interface CreateFormHookProps<
|
|
138
|
-
TFieldComponents extends Record<string, ComponentType<any>>,
|
|
139
|
-
TFormComponents extends Record<string, ComponentType<any>>,
|
|
140
|
-
> {
|
|
141
|
-
fieldComponents: TFieldComponents
|
|
142
|
-
fieldContext: Context<AnyFieldApi>
|
|
143
|
-
formComponents: TFormComponents
|
|
144
|
-
formContext: Context<AnyFormApi>
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* @private
|
|
149
|
-
*/
|
|
150
|
-
export type AppFieldExtendedPreactFormApi<
|
|
151
|
-
TFormData,
|
|
152
|
-
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
153
|
-
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
154
|
-
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
155
|
-
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
156
|
-
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
157
|
-
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
158
|
-
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
159
|
-
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
160
|
-
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
161
|
-
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
162
|
-
TSubmitMeta,
|
|
163
|
-
TFieldComponents extends Record<string, ComponentType<any>>,
|
|
164
|
-
TFormComponents extends Record<string, ComponentType<any>>,
|
|
165
|
-
> = PreactFormExtendedApi<
|
|
166
|
-
TFormData,
|
|
167
|
-
TOnMount,
|
|
168
|
-
TOnChange,
|
|
169
|
-
TOnChangeAsync,
|
|
170
|
-
TOnBlur,
|
|
171
|
-
TOnBlurAsync,
|
|
172
|
-
TOnSubmit,
|
|
173
|
-
TOnSubmitAsync,
|
|
174
|
-
TOnDynamic,
|
|
175
|
-
TOnDynamicAsync,
|
|
176
|
-
TOnServer,
|
|
177
|
-
TSubmitMeta
|
|
178
|
-
> &
|
|
179
|
-
NoInfer<TFormComponents> & {
|
|
180
|
-
AppField: FieldComponent<
|
|
181
|
-
TFormData,
|
|
182
|
-
TOnMount,
|
|
183
|
-
TOnChange,
|
|
184
|
-
TOnChangeAsync,
|
|
185
|
-
TOnBlur,
|
|
186
|
-
TOnBlurAsync,
|
|
187
|
-
TOnSubmit,
|
|
188
|
-
TOnSubmitAsync,
|
|
189
|
-
TOnDynamic,
|
|
190
|
-
TOnDynamicAsync,
|
|
191
|
-
TOnServer,
|
|
192
|
-
TSubmitMeta,
|
|
193
|
-
NoInfer<TFieldComponents>
|
|
194
|
-
>
|
|
195
|
-
AppForm: ComponentType<
|
|
196
|
-
// PropsWithChildren<P> is not optional in React 17
|
|
197
|
-
PropsWithChildren<{}>
|
|
198
|
-
>
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
export interface WithFormProps<
|
|
202
|
-
TFormData,
|
|
203
|
-
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
204
|
-
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
205
|
-
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
206
|
-
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
207
|
-
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
208
|
-
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
209
|
-
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
210
|
-
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
211
|
-
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
212
|
-
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
213
|
-
TSubmitMeta,
|
|
214
|
-
TFieldComponents extends Record<string, ComponentType<any>>,
|
|
215
|
-
TFormComponents extends Record<string, ComponentType<any>>,
|
|
216
|
-
TRenderProps extends object = Record<string, never>,
|
|
217
|
-
> extends FormOptions<
|
|
218
|
-
TFormData,
|
|
219
|
-
TOnMount,
|
|
220
|
-
TOnChange,
|
|
221
|
-
TOnChangeAsync,
|
|
222
|
-
TOnBlur,
|
|
223
|
-
TOnBlurAsync,
|
|
224
|
-
TOnSubmit,
|
|
225
|
-
TOnSubmitAsync,
|
|
226
|
-
TOnDynamic,
|
|
227
|
-
TOnDynamicAsync,
|
|
228
|
-
TOnServer,
|
|
229
|
-
TSubmitMeta
|
|
230
|
-
> {
|
|
231
|
-
// Optional, but adds props to the `render` function outside of `form`
|
|
232
|
-
props?: TRenderProps
|
|
233
|
-
render: FunctionComponent<
|
|
234
|
-
PropsWithChildren<
|
|
235
|
-
NoInfer<TRenderProps> & {
|
|
236
|
-
form: AppFieldExtendedPreactFormApi<
|
|
237
|
-
TFormData,
|
|
238
|
-
TOnMount,
|
|
239
|
-
TOnChange,
|
|
240
|
-
TOnChangeAsync,
|
|
241
|
-
TOnBlur,
|
|
242
|
-
TOnBlurAsync,
|
|
243
|
-
TOnSubmit,
|
|
244
|
-
TOnSubmitAsync,
|
|
245
|
-
TOnDynamic,
|
|
246
|
-
TOnDynamicAsync,
|
|
247
|
-
TOnServer,
|
|
248
|
-
TSubmitMeta,
|
|
249
|
-
TFieldComponents,
|
|
250
|
-
TFormComponents
|
|
251
|
-
>
|
|
252
|
-
}
|
|
253
|
-
>
|
|
254
|
-
>
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export interface WithFieldGroupProps<
|
|
258
|
-
TFieldGroupData,
|
|
259
|
-
TFieldComponents extends Record<string, ComponentType<any>>,
|
|
260
|
-
TFormComponents extends Record<string, ComponentType<any>>,
|
|
261
|
-
TSubmitMeta,
|
|
262
|
-
TRenderProps extends object = Record<string, never>,
|
|
263
|
-
> extends BaseFormOptions<TFieldGroupData, TSubmitMeta> {
|
|
264
|
-
// Optional, but adds props to the `render` function outside of `form`
|
|
265
|
-
props?: TRenderProps
|
|
266
|
-
render: FunctionComponent<
|
|
267
|
-
PropsWithChildren<
|
|
268
|
-
NoInfer<TRenderProps> & {
|
|
269
|
-
group: AppFieldExtendedPreactFieldGroupApi<
|
|
270
|
-
unknown,
|
|
271
|
-
TFieldGroupData,
|
|
272
|
-
string | FieldsMap<unknown, TFieldGroupData>,
|
|
273
|
-
undefined | FormValidateOrFn<unknown>,
|
|
274
|
-
undefined | FormValidateOrFn<unknown>,
|
|
275
|
-
undefined | FormAsyncValidateOrFn<unknown>,
|
|
276
|
-
undefined | FormValidateOrFn<unknown>,
|
|
277
|
-
undefined | FormAsyncValidateOrFn<unknown>,
|
|
278
|
-
undefined | FormValidateOrFn<unknown>,
|
|
279
|
-
undefined | FormAsyncValidateOrFn<unknown>,
|
|
280
|
-
undefined | FormValidateOrFn<unknown>,
|
|
281
|
-
undefined | FormAsyncValidateOrFn<unknown>,
|
|
282
|
-
undefined | FormAsyncValidateOrFn<unknown>,
|
|
283
|
-
// this types it as 'never' in the render prop. It should prevent any
|
|
284
|
-
// untyped meta passed to the handleSubmit by accident.
|
|
285
|
-
unknown extends TSubmitMeta ? never : TSubmitMeta,
|
|
286
|
-
TFieldComponents,
|
|
287
|
-
TFormComponents
|
|
288
|
-
>
|
|
289
|
-
}
|
|
290
|
-
>
|
|
291
|
-
>
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export function createFormHook<
|
|
295
|
-
const TComponents extends Record<string, ComponentType<any>>,
|
|
296
|
-
const TFormComponents extends Record<string, ComponentType<any>>,
|
|
297
|
-
>({
|
|
298
|
-
fieldComponents,
|
|
299
|
-
fieldContext,
|
|
300
|
-
formContext,
|
|
301
|
-
formComponents,
|
|
302
|
-
}: CreateFormHookProps<TComponents, TFormComponents>) {
|
|
303
|
-
function useAppForm<
|
|
304
|
-
TFormData,
|
|
305
|
-
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
306
|
-
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
307
|
-
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
308
|
-
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
309
|
-
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
310
|
-
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
311
|
-
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
312
|
-
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
313
|
-
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
314
|
-
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
315
|
-
TSubmitMeta,
|
|
316
|
-
>(
|
|
317
|
-
props: FormOptions<
|
|
318
|
-
TFormData,
|
|
319
|
-
TOnMount,
|
|
320
|
-
TOnChange,
|
|
321
|
-
TOnChangeAsync,
|
|
322
|
-
TOnBlur,
|
|
323
|
-
TOnBlurAsync,
|
|
324
|
-
TOnSubmit,
|
|
325
|
-
TOnSubmitAsync,
|
|
326
|
-
TOnDynamic,
|
|
327
|
-
TOnDynamicAsync,
|
|
328
|
-
TOnServer,
|
|
329
|
-
TSubmitMeta
|
|
330
|
-
>,
|
|
331
|
-
): AppFieldExtendedPreactFormApi<
|
|
332
|
-
TFormData,
|
|
333
|
-
TOnMount,
|
|
334
|
-
TOnChange,
|
|
335
|
-
TOnChangeAsync,
|
|
336
|
-
TOnBlur,
|
|
337
|
-
TOnBlurAsync,
|
|
338
|
-
TOnSubmit,
|
|
339
|
-
TOnSubmitAsync,
|
|
340
|
-
TOnDynamic,
|
|
341
|
-
TOnDynamicAsync,
|
|
342
|
-
TOnServer,
|
|
343
|
-
TSubmitMeta,
|
|
344
|
-
TComponents,
|
|
345
|
-
TFormComponents
|
|
346
|
-
> {
|
|
347
|
-
const form = useForm(props)
|
|
348
|
-
|
|
349
|
-
// PropsWithChildren<P> is not optional in React 17
|
|
350
|
-
const AppForm = useMemo<ComponentType<PropsWithChildren<{}>>>(() => {
|
|
351
|
-
return ({ children }) => {
|
|
352
|
-
return (
|
|
353
|
-
<formContext.Provider value={form}>{children}</formContext.Provider>
|
|
354
|
-
)
|
|
355
|
-
}
|
|
356
|
-
}, [form])
|
|
357
|
-
|
|
358
|
-
const AppField = useMemo(() => {
|
|
359
|
-
const AppFieldComponent = (({ children, ...restProps }) => {
|
|
360
|
-
return (
|
|
361
|
-
<form.Field {...restProps}>
|
|
362
|
-
{(field) => (
|
|
363
|
-
<fieldContext.Provider value={field}>
|
|
364
|
-
{children(Object.assign(field, fieldComponents))}
|
|
365
|
-
</fieldContext.Provider>
|
|
366
|
-
)}
|
|
367
|
-
</form.Field>
|
|
368
|
-
)
|
|
369
|
-
}) as FieldComponent<
|
|
370
|
-
TFormData,
|
|
371
|
-
TOnMount,
|
|
372
|
-
TOnChange,
|
|
373
|
-
TOnChangeAsync,
|
|
374
|
-
TOnBlur,
|
|
375
|
-
TOnBlurAsync,
|
|
376
|
-
TOnSubmit,
|
|
377
|
-
TOnSubmitAsync,
|
|
378
|
-
TOnDynamic,
|
|
379
|
-
TOnDynamicAsync,
|
|
380
|
-
TOnServer,
|
|
381
|
-
TSubmitMeta,
|
|
382
|
-
TComponents
|
|
383
|
-
>
|
|
384
|
-
return AppFieldComponent
|
|
385
|
-
}, [form])
|
|
386
|
-
|
|
387
|
-
const extendedForm = useMemo(() => {
|
|
388
|
-
return Object.assign(form, {
|
|
389
|
-
AppField,
|
|
390
|
-
AppForm,
|
|
391
|
-
...formComponents,
|
|
392
|
-
})
|
|
393
|
-
}, [form, AppField, AppForm])
|
|
394
|
-
|
|
395
|
-
return extendedForm
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
function withForm<
|
|
399
|
-
TFormData,
|
|
400
|
-
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
401
|
-
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
402
|
-
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
403
|
-
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
404
|
-
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
405
|
-
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
406
|
-
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
407
|
-
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
408
|
-
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
409
|
-
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
410
|
-
TSubmitMeta,
|
|
411
|
-
TRenderProps extends object = {},
|
|
412
|
-
>({
|
|
413
|
-
render,
|
|
414
|
-
props,
|
|
415
|
-
}: WithFormProps<
|
|
416
|
-
TFormData,
|
|
417
|
-
TOnMount,
|
|
418
|
-
TOnChange,
|
|
419
|
-
TOnChangeAsync,
|
|
420
|
-
TOnBlur,
|
|
421
|
-
TOnBlurAsync,
|
|
422
|
-
TOnSubmit,
|
|
423
|
-
TOnSubmitAsync,
|
|
424
|
-
TOnDynamic,
|
|
425
|
-
TOnDynamicAsync,
|
|
426
|
-
TOnServer,
|
|
427
|
-
TSubmitMeta,
|
|
428
|
-
TComponents,
|
|
429
|
-
TFormComponents,
|
|
430
|
-
TRenderProps
|
|
431
|
-
>): WithFormProps<
|
|
432
|
-
UnwrapOrAny<TFormData>,
|
|
433
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnMount>,
|
|
434
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnChange>,
|
|
435
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnChangeAsync>,
|
|
436
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnBlur>,
|
|
437
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnBlurAsync>,
|
|
438
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnSubmit>,
|
|
439
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnSubmitAsync>,
|
|
440
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnDynamic>,
|
|
441
|
-
UnwrapDefaultOrAny<
|
|
442
|
-
undefined | FormValidateOrFn<TFormData>,
|
|
443
|
-
TOnDynamicAsync
|
|
444
|
-
>,
|
|
445
|
-
UnwrapDefaultOrAny<undefined | FormValidateOrFn<TFormData>, TOnServer>,
|
|
446
|
-
UnwrapOrAny<TSubmitMeta>,
|
|
447
|
-
UnwrapOrAny<TComponents>,
|
|
448
|
-
UnwrapOrAny<TFormComponents>,
|
|
449
|
-
UnwrapOrAny<TRenderProps>
|
|
450
|
-
>['render'] {
|
|
451
|
-
return (innerProps) => render({ ...props, ...innerProps })
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
function withFieldGroup<
|
|
455
|
-
TFieldGroupData,
|
|
456
|
-
TSubmitMeta,
|
|
457
|
-
TRenderProps extends object = {},
|
|
458
|
-
>({
|
|
459
|
-
render,
|
|
460
|
-
props,
|
|
461
|
-
defaultValues,
|
|
462
|
-
}: WithFieldGroupProps<
|
|
463
|
-
TFieldGroupData,
|
|
464
|
-
TComponents,
|
|
465
|
-
TFormComponents,
|
|
466
|
-
TSubmitMeta,
|
|
467
|
-
TRenderProps
|
|
468
|
-
>): <
|
|
469
|
-
TFormData,
|
|
470
|
-
TFields extends
|
|
471
|
-
| DeepKeysOfType<TFormData, TFieldGroupData | null | undefined>
|
|
472
|
-
| FieldsMap<TFormData, TFieldGroupData>,
|
|
473
|
-
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
474
|
-
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
475
|
-
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
476
|
-
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
477
|
-
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
478
|
-
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
479
|
-
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
480
|
-
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
481
|
-
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
482
|
-
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
483
|
-
TFormSubmitMeta,
|
|
484
|
-
>(
|
|
485
|
-
params: PropsWithChildren<
|
|
486
|
-
NoInfer<TRenderProps> & {
|
|
487
|
-
form:
|
|
488
|
-
| AppFieldExtendedPreactFormApi<
|
|
489
|
-
TFormData,
|
|
490
|
-
TOnMount,
|
|
491
|
-
TOnChange,
|
|
492
|
-
TOnChangeAsync,
|
|
493
|
-
TOnBlur,
|
|
494
|
-
TOnBlurAsync,
|
|
495
|
-
TOnSubmit,
|
|
496
|
-
TOnSubmitAsync,
|
|
497
|
-
TOnDynamic,
|
|
498
|
-
TOnDynamicAsync,
|
|
499
|
-
TOnServer,
|
|
500
|
-
unknown extends TSubmitMeta ? TFormSubmitMeta : TSubmitMeta,
|
|
501
|
-
TComponents,
|
|
502
|
-
TFormComponents
|
|
503
|
-
>
|
|
504
|
-
| AppFieldExtendedPreactFieldGroupApi<
|
|
505
|
-
// Since this only occurs if you nest it within other field groups, it can be more
|
|
506
|
-
// lenient with the types.
|
|
507
|
-
unknown,
|
|
508
|
-
TFormData,
|
|
509
|
-
string | FieldsMap<unknown, TFormData>,
|
|
510
|
-
any,
|
|
511
|
-
any,
|
|
512
|
-
any,
|
|
513
|
-
any,
|
|
514
|
-
any,
|
|
515
|
-
any,
|
|
516
|
-
any,
|
|
517
|
-
any,
|
|
518
|
-
any,
|
|
519
|
-
any,
|
|
520
|
-
unknown extends TSubmitMeta ? TFormSubmitMeta : TSubmitMeta,
|
|
521
|
-
TComponents,
|
|
522
|
-
TFormComponents
|
|
523
|
-
>
|
|
524
|
-
fields: TFields
|
|
525
|
-
}
|
|
526
|
-
>,
|
|
527
|
-
) => ReturnType<FunctionComponent> {
|
|
528
|
-
return function Render(innerProps) {
|
|
529
|
-
const fieldGroupProps = useMemo(() => {
|
|
530
|
-
return {
|
|
531
|
-
form: innerProps.form,
|
|
532
|
-
fields: innerProps.fields,
|
|
533
|
-
defaultValues,
|
|
534
|
-
formComponents,
|
|
535
|
-
}
|
|
536
|
-
}, [innerProps.form, innerProps.fields])
|
|
537
|
-
const fieldGroupApi = useFieldGroup(fieldGroupProps as any)
|
|
538
|
-
|
|
539
|
-
return render({ ...props, ...innerProps, group: fieldGroupApi as any })
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
/**
|
|
544
|
-
* ⚠️ **Use withForm whenever possible.**
|
|
545
|
-
*
|
|
546
|
-
* Gets a typed form from the `<form.AppForm />` context.
|
|
547
|
-
*/
|
|
548
|
-
function useTypedAppFormContext<
|
|
549
|
-
TFormData,
|
|
550
|
-
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
551
|
-
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
552
|
-
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
553
|
-
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
554
|
-
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
555
|
-
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
556
|
-
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
557
|
-
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
558
|
-
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
559
|
-
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
560
|
-
TSubmitMeta,
|
|
561
|
-
>(
|
|
562
|
-
_props: FormOptions<
|
|
563
|
-
TFormData,
|
|
564
|
-
TOnMount,
|
|
565
|
-
TOnChange,
|
|
566
|
-
TOnChangeAsync,
|
|
567
|
-
TOnBlur,
|
|
568
|
-
TOnBlurAsync,
|
|
569
|
-
TOnSubmit,
|
|
570
|
-
TOnSubmitAsync,
|
|
571
|
-
TOnDynamic,
|
|
572
|
-
TOnDynamicAsync,
|
|
573
|
-
TOnServer,
|
|
574
|
-
TSubmitMeta
|
|
575
|
-
>,
|
|
576
|
-
): AppFieldExtendedPreactFormApi<
|
|
577
|
-
TFormData,
|
|
578
|
-
TOnMount,
|
|
579
|
-
TOnChange,
|
|
580
|
-
TOnChangeAsync,
|
|
581
|
-
TOnBlur,
|
|
582
|
-
TOnBlurAsync,
|
|
583
|
-
TOnSubmit,
|
|
584
|
-
TOnSubmitAsync,
|
|
585
|
-
TOnDynamic,
|
|
586
|
-
TOnDynamicAsync,
|
|
587
|
-
TOnServer,
|
|
588
|
-
TSubmitMeta,
|
|
589
|
-
TComponents,
|
|
590
|
-
TFormComponents
|
|
591
|
-
> {
|
|
592
|
-
const form = useFormContext()
|
|
593
|
-
|
|
594
|
-
return form as never
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
return {
|
|
598
|
-
useAppForm,
|
|
599
|
-
withForm,
|
|
600
|
-
withFieldGroup,
|
|
601
|
-
useTypedAppFormContext,
|
|
602
|
-
}
|
|
603
|
-
}
|