@tanstack/preact-form 1.33.3 → 2.0.0-alpha.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.
Files changed (51) hide show
  1. package/dist/AppForm/Components.lib.js +3 -2
  2. package/dist/AppForm/PreactAppFormApi.public.d.ts +0 -1
  3. package/dist/AppForm/appFormOptions.public.d.ts +0 -1
  4. package/dist/AppForm/componentMap.public.d.ts +0 -1
  5. package/dist/AppForm/createFormHook.public.d.ts +0 -1
  6. package/dist/AppForm/createFormHook.public.js +2 -2
  7. package/dist/AppForm/createFormHookTypes.public.d.ts +110 -3
  8. package/dist/AppForm/fieldComponentHelpers.lib.js +3 -2
  9. package/dist/AppForm/getFormHookHelpers.public.d.ts +1 -2
  10. package/dist/AppForm/initializeAppForm.lib.js +2 -1
  11. package/dist/FieldGroup/FieldGroupApi.public.d.ts +0 -1
  12. package/dist/FieldGroup/withFields.lib.js +27 -63
  13. package/dist/FieldGroup/withFields.public.d.ts +26 -24
  14. package/dist/FieldGroup/withFields.public.js +4 -15
  15. package/dist/PreactForm/Components.lib.js +6 -4
  16. package/dist/PreactForm/Components.public.d.ts +2 -3
  17. package/dist/PreactForm/PreactFormApi.lib.js +2 -1
  18. package/dist/PreactForm/fieldSubscriptions.lib.js +4 -2
  19. package/dist/PreactForm/formApiTypes.public.d.ts +25 -2
  20. package/dist/PreactForm/formType.public.d.ts +30 -2
  21. package/dist/PreactForm/useForm.public.d.ts +0 -1
  22. package/dist/Subscribe.public.d.ts +0 -1
  23. package/dist/index.d.ts +2 -2
  24. package/dist/index.js +2 -2
  25. package/dist/preactTypes.public.d.ts +0 -2
  26. package/package.json +5 -6
  27. package/src/AppForm/Components.lib.tsx +0 -160
  28. package/src/AppForm/PreactAppFormApi.public.tsx +0 -17
  29. package/src/AppForm/appFormOptions.public.ts +0 -59
  30. package/src/AppForm/componentMap.public.ts +0 -19
  31. package/src/AppForm/contexts.lib.ts +0 -28
  32. package/src/AppForm/createFormHook.public.ts +0 -37
  33. package/src/AppForm/createFormHookTypes.public.ts +0 -34
  34. package/src/AppForm/fieldComponentHelpers.lib.tsx +0 -30
  35. package/src/AppForm/getFormHookHelpers.public.ts +0 -114
  36. package/src/AppForm/initializeAppForm.lib.ts +0 -22
  37. package/src/FieldGroup/FieldGroupApi.public.ts +0 -91
  38. package/src/FieldGroup/withFields.lib.ts +0 -191
  39. package/src/FieldGroup/withFields.public.ts +0 -221
  40. package/src/PreactForm/Components.lib.tsx +0 -166
  41. package/src/PreactForm/Components.public.ts +0 -383
  42. package/src/PreactForm/PreactFormApi.lib.tsx +0 -48
  43. package/src/PreactForm/fieldSubscriptions.lib.ts +0 -32
  44. package/src/PreactForm/formApiTypes.public.ts +0 -29
  45. package/src/PreactForm/formType.public.ts +0 -52
  46. package/src/PreactForm/useField.lib.ts +0 -52
  47. package/src/PreactForm/useForm.public.ts +0 -34
  48. package/src/Subscribe.public.tsx +0 -56
  49. package/src/index.ts +0 -18
  50. package/src/preactTypes.public.ts +0 -6
  51. package/src/types.public.ts +0 -2
@@ -1,383 +0,0 @@
1
- import type {
2
- DeepKeys,
3
- DeepKeysWhereValueIncludes,
4
- DeepValue,
5
- FieldApi,
6
- FieldApiOptions,
7
- FieldValidators,
8
- FormErrorTypes,
9
- FormGroupApi,
10
- FormGroupOptions,
11
- FormGroupState,
12
- FormGroupValidators,
13
- FormState,
14
- ToFieldError,
15
- ToFormGroupErrorTypes,
16
- } from '@tanstack/form-core'
17
- import type { CrossVersionPreactNode } from '../preactTypes.public'
18
- import type { FunctionComponent, MemoExoticComponent } from 'preact/compat'
19
-
20
- type ExactFieldBrand<out TValue> = {
21
- readonly __tanstackFieldExactType: TValue
22
- }
23
-
24
- type AcceptsFieldBrand<out TAcceptedValue> = {
25
- readonly __tanstackFieldAcceptsType: TAcceptedValue
26
- }
27
-
28
- type CompatibleFieldKey<TKey, TComponent, TTargetValue> = [TComponent] extends [
29
- ExactFieldBrand<infer TExact>,
30
- ]
31
- ? [TExact] extends [TTargetValue]
32
- ? [TTargetValue] extends [TExact]
33
- ? TKey
34
- : never
35
- : never
36
- : [TComponent] extends [AcceptsFieldBrand<infer TLoose>]
37
- ? [TTargetValue] extends [TLoose]
38
- ? TKey
39
- : never
40
- : TKey
41
-
42
- type UnwrapComponent<TComponent> =
43
- TComponent extends MemoExoticComponent<infer TInner>
44
- ? UnwrapComponent<TInner>
45
- : TComponent
46
-
47
- type UnwrappedFieldComponents<
48
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
49
- > = {
50
- [K in keyof TFieldComponents]: UnwrapComponent<TFieldComponents[K]>
51
- }
52
-
53
- type FilteredFieldComponents<
54
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
55
- in out TTargetValue,
56
- in out TUnwrappedFieldComponents extends {
57
- [K in keyof TFieldComponents]: any
58
- } = UnwrappedFieldComponents<TFieldComponents>,
59
- > = {
60
- [
61
- K in keyof TFieldComponents as CompatibleFieldKey<
62
- K,
63
- TUnwrappedFieldComponents[K],
64
- TTargetValue
65
- >
66
- ]: TFieldComponents[K]
67
- }
68
-
69
- type FieldComponentsMatchingType<
70
- TFieldComponents extends Record<string, FunctionComponent<any>>,
71
- TTargetValue,
72
- > = unknown extends TTargetValue
73
- ? TFieldComponents
74
- : string extends keyof TFieldComponents
75
- ? TFieldComponents
76
- : FilteredFieldComponents<TFieldComponents, TTargetValue>
77
-
78
- export type PreactFieldApi<
79
- TFieldName,
80
- TFieldValue,
81
- TFieldError,
82
- TFormData,
83
- TFormErrorTypes extends FormErrorTypes,
84
- TFieldComponents extends Record<string, FunctionComponent<any>>,
85
- > = FieldApi<TFieldName, TFieldValue, TFieldError, TFormData, TFormErrorTypes> &
86
- FieldComponentsMatchingType<TFieldComponents, TFieldValue>
87
-
88
- /**
89
- * Subscribe to `form.atom` (full form state). The selector receives the full
90
- * {@link FormState}.
91
- */
92
- interface PreactSubscribeProps<in out TSourceData, in out TSelected> {
93
- selector: (state: TSourceData) => TSelected
94
- when?: (selected: NoInfer<TSelected>) => boolean
95
- children:
96
- | ((state: NoInfer<TSelected>) => CrossVersionPreactNode)
97
- | CrossVersionPreactNode
98
- }
99
-
100
- export type PreactFormSubscribeProps<
101
- TFormData,
102
- TFormErrorTypes extends FormErrorTypes,
103
- TSelected,
104
- > = PreactSubscribeProps<FormState<TFormData, TFormErrorTypes>, TSelected>
105
-
106
- export type PreactFormSubscribeComponent<
107
- in out TFormData,
108
- in out TFormErrorTypes extends FormErrorTypes,
109
- > = <TSelected>(
110
- props: PreactFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>,
111
- ) => CrossVersionPreactNode
112
-
113
- export interface PreactFormFieldProps<
114
- in out TFieldData,
115
- in out TFieldName,
116
- in out TFieldValue,
117
- in out TFieldValidators extends FieldValidators<
118
- TFieldData,
119
- TFieldName,
120
- TFieldValue
121
- >,
122
- in out TGroupFieldError,
123
- in out TFormData,
124
- in out TFormErrorTypes extends FormErrorTypes,
125
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
126
- > extends FieldApiOptions<
127
- TFieldData,
128
- TFieldName,
129
- TFieldValue,
130
- TFieldValidators,
131
- TGroupFieldError,
132
- TFormData,
133
- TFormErrorTypes
134
- > {
135
- children: (
136
- fieldApi: PreactFieldApi<
137
- TFieldName,
138
- TFieldValue,
139
- ToFieldError<
140
- NoInfer<TFieldValidators>,
141
- TGroupFieldError,
142
- TFormErrorTypes
143
- >,
144
- TFormData,
145
- TFormErrorTypes,
146
- TFieldComponents
147
- >,
148
- ) => CrossVersionPreactNode
149
- }
150
-
151
- export type PreactFormFieldComponent<
152
- in out TFormData,
153
- in out TFormErrorTypes extends FormErrorTypes,
154
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
155
- > = {
156
- <
157
- TFieldName extends DeepKeys<TFormData>,
158
- const TFieldValidators extends FieldValidators<
159
- TFormData,
160
- TFieldName,
161
- DeepValue<TFormData, TFieldName>
162
- >,
163
- >(
164
- props: PreactFormFieldProps<
165
- TFormData,
166
- TFieldName,
167
- DeepValue<TFormData, TFieldName>,
168
- TFieldValidators,
169
- never,
170
- TFormData,
171
- TFormErrorTypes,
172
- TFieldComponents
173
- >,
174
- ): CrossVersionPreactNode
175
- }
176
-
177
- export type PreactFormArrayFieldComponent<
178
- in out TFormData,
179
- in out TFormErrorTypes extends FormErrorTypes,
180
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
181
- > = {
182
- <
183
- TFieldName extends DeepKeysWhereValueIncludes<TFormData, Array<any>>,
184
- const TFieldValidators extends FieldValidators<
185
- TFormData,
186
- TFieldName,
187
- DeepValue<TFormData, TFieldName>
188
- >,
189
- >(
190
- props: PreactFormFieldProps<
191
- TFormData,
192
- TFieldName,
193
- DeepValue<TFormData, TFieldName>,
194
- TFieldValidators,
195
- never,
196
- TFormData,
197
- TFormErrorTypes,
198
- TFieldComponents
199
- >,
200
- ): CrossVersionPreactNode
201
- }
202
-
203
- export type PreactFormGroupSubscribeProps<
204
- TGroupValue,
205
- TGroupErrorTypes extends FormErrorTypes,
206
- TSelected,
207
- > = PreactSubscribeProps<
208
- FormGroupState<TGroupValue, TGroupErrorTypes>,
209
- TSelected
210
- >
211
-
212
- export type PreactFormGroupSubscribeComponent<
213
- in out TGroupValue,
214
- in out TGroupErrorTypes extends FormErrorTypes,
215
- > = <TSelected>(
216
- props: PreactFormGroupSubscribeProps<
217
- TGroupValue,
218
- TGroupErrorTypes,
219
- TSelected
220
- >,
221
- ) => CrossVersionPreactNode
222
-
223
- export interface PreactFormGroupFieldComponent<
224
- in out TFormData,
225
- in out TGroupValue,
226
- in out TGroupErrorTypes extends FormErrorTypes,
227
- in out TFormErrorTypes extends FormErrorTypes,
228
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
229
- > {
230
- <
231
- TFieldName extends DeepKeys<TGroupValue>,
232
- const TFieldValidators extends FieldValidators<
233
- TGroupValue,
234
- TFieldName,
235
- DeepValue<TGroupValue, TFieldName>
236
- >,
237
- >(
238
- props: PreactFormFieldProps<
239
- TGroupValue,
240
- TFieldName,
241
- DeepValue<TGroupValue, TFieldName>,
242
- TFieldValidators,
243
- TGroupErrorTypes['fieldError'],
244
- TFormData,
245
- TFormErrorTypes,
246
- TFieldComponents
247
- >,
248
- ): CrossVersionPreactNode
249
- }
250
-
251
- export type PreactFormGroupArrayFieldComponent<
252
- in out TFormData,
253
- in out TGroupValue,
254
- in out TGroupErrorTypes extends FormErrorTypes,
255
- in out TFormErrorTypes extends FormErrorTypes,
256
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
257
- > = {
258
- <
259
- TFieldName extends DeepKeysWhereValueIncludes<TGroupValue, Array<any>>,
260
- const TFieldValidators extends FieldValidators<
261
- TGroupValue,
262
- TFieldName,
263
- DeepValue<TGroupValue, TFieldName>
264
- >,
265
- >(
266
- props: PreactFormFieldProps<
267
- TGroupValue,
268
- TFieldName,
269
- DeepValue<TGroupValue, TFieldName>,
270
- TFieldValidators,
271
- TGroupErrorTypes['fieldError'],
272
- TFormData,
273
- TFormErrorTypes,
274
- TFieldComponents
275
- >,
276
- ): CrossVersionPreactNode
277
- }
278
-
279
- export interface PreactFormGroupApi<
280
- in out TFormData,
281
- in out TGroupName,
282
- in out TGroupValue,
283
- in out TGroupErrorTypes extends FormErrorTypes,
284
- in out TFormErrorTypes extends FormErrorTypes,
285
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
286
- > extends FormGroupApi<
287
- TFormData,
288
- TGroupName,
289
- TGroupValue,
290
- TGroupErrorTypes,
291
- TFormErrorTypes
292
- > {
293
- Field: PreactFormGroupFieldComponent<
294
- TFormData,
295
- TGroupValue,
296
- TGroupErrorTypes,
297
- TFormErrorTypes,
298
- TFieldComponents
299
- >
300
- ArrayField: PreactFormGroupArrayFieldComponent<
301
- TFormData,
302
- TGroupValue,
303
- TGroupErrorTypes,
304
- TFormErrorTypes,
305
- TFieldComponents
306
- >
307
- Subscribe: PreactFormGroupSubscribeComponent<TGroupValue, TGroupErrorTypes>
308
- }
309
-
310
- export interface PreactFormGroupProps<
311
- in out TFormData,
312
- in out TGroupName,
313
- in out TGroupValue,
314
- in out TGroupValidators extends FormGroupValidators<TGroupValue>,
315
- in out TFormErrorTypes extends FormErrorTypes,
316
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
317
- > extends Omit<
318
- FormGroupOptions<
319
- TFormData,
320
- TGroupName,
321
- TGroupValue,
322
- TGroupValidators,
323
- TFormErrorTypes
324
- >,
325
- 'form'
326
- > {
327
- children: (
328
- groupApi: PreactFormGroupApi<
329
- TFormData,
330
- TGroupName,
331
- TGroupValue,
332
- ToFormGroupErrorTypes<NoInfer<TGroupValidators>>,
333
- TFormErrorTypes,
334
- TFieldComponents
335
- >,
336
- ) => CrossVersionPreactNode
337
- }
338
-
339
- export interface PreactFormGroupComponent<
340
- in out TFormData,
341
- in out TFormErrorTypes extends FormErrorTypes,
342
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
343
- > {
344
- <
345
- TGroupName extends DeepKeys<TFormData>,
346
- TGroupValue extends DeepValue<TFormData, TGroupName>,
347
- const TGroupValidators extends FormGroupValidators<TGroupValue>,
348
- >(
349
- props: PreactFormGroupProps<
350
- TFormData,
351
- TGroupName,
352
- TGroupValue,
353
- TGroupValidators,
354
- TFormErrorTypes,
355
- TFieldComponents
356
- >,
357
- ): CrossVersionPreactNode
358
- }
359
-
360
- export interface PreactTanStackFormComponents<
361
- in out TFormData,
362
- in out TFormErrorTypes extends FormErrorTypes,
363
- in out TFieldComponents extends Record<string, FunctionComponent<any>>,
364
- > {
365
- /**
366
- * TODO docs
367
- */
368
- Field: PreactFormFieldComponent<TFormData, TFormErrorTypes, TFieldComponents>
369
-
370
- ArrayField: PreactFormArrayFieldComponent<
371
- TFormData,
372
- TFormErrorTypes,
373
- TFieldComponents
374
- >
375
-
376
- Subscribe: PreactFormSubscribeComponent<TFormData, TFormErrorTypes>
377
-
378
- FormGroup: PreactFormGroupComponent<
379
- TFormData,
380
- TFormErrorTypes,
381
- TFieldComponents
382
- >
383
- }
@@ -1,48 +0,0 @@
1
- import { InternalFormApi } from '@tanstack/form-core/internals'
2
- import * as Preact from 'preact/compat'
3
- import { useEffect, useRef } from 'preact/compat'
4
- import { attachPreactFormComponents } from './Components.lib'
5
- import type { AnyInternalFormApi } from '@tanstack/form-core/internals'
6
- import type { FormOptions } from '@tanstack/form-core'
7
- import type { PreactTanStackFormComponents } from './Components.public'
8
-
9
- const usePreactId =
10
- (Preact as typeof Preact & { useId?: () => string }).useId ??
11
- (() => undefined)
12
-
13
- export interface InternalPreactFormApi
14
- extends AnyInternalFormApi, PreactTanStackFormComponents<any, any, any> {}
15
-
16
- export function initializeForm(
17
- options: FormOptions<any, any, any>,
18
- ): InternalPreactFormApi {
19
- const form = new InternalFormApi(options)
20
-
21
- const preactFormApi = attachPreactFormComponents(form, null)
22
-
23
- return preactFormApi
24
- }
25
-
26
- export function useInternalForm(
27
- options: FormOptions<any, any, any>,
28
- initializeFn: (options: FormOptions<any, any, any>) => InternalPreactFormApi,
29
- ) {
30
- const preactFormId = usePreactId()
31
- const resolvedOptions =
32
- options.formId === undefined
33
- ? { ...options, formId: preactFormId }
34
- : options
35
- const formRef = useRef<InternalPreactFormApi>(null)
36
-
37
- if (!formRef.current) {
38
- formRef.current = initializeFn(resolvedOptions)
39
- }
40
-
41
- useEffect(() => formRef.current!._update(resolvedOptions))
42
- useEffect(() => {
43
- const unmount = formRef.current!.mount()
44
- return unmount
45
- }, [])
46
-
47
- return formRef.current
48
- }
@@ -1,32 +0,0 @@
1
- import { useSelector } from '@tanstack/preact-store'
2
- import { useMemo } from 'preact/compat'
3
- import type { AnyInternalFieldApi } from '@tanstack/form-core/internals'
4
-
5
- export function useValueFieldSubscription(
6
- fieldApi: AnyInternalFieldApi,
7
- ): AnyInternalFieldApi {
8
- const value = useSelector(fieldApi.atom, (state) => state.value)
9
- const meta = useSelector(fieldApi.atom, (state) => state.meta)
10
-
11
- return useMemo(() => {
12
- void meta
13
- void value
14
- return Object.create(fieldApi)
15
- }, [fieldApi, meta, value])
16
- }
17
-
18
- export function useArrayFieldSubscription(
19
- fieldApi: AnyInternalFieldApi,
20
- ): AnyInternalFieldApi {
21
- const length = useSelector(fieldApi.atom, (state) => state.value.length)
22
- const arrayVersion = useSelector(
23
- fieldApi.atom,
24
- (state) => state.meta._arrayVersion,
25
- )
26
-
27
- return useMemo(() => {
28
- void length
29
- void arrayVersion
30
- return Object.create(fieldApi)
31
- }, [fieldApi, arrayVersion, length])
32
- }
@@ -1,29 +0,0 @@
1
- import type { AnyFormApi, FormApi, FormErrorTypes } from '@tanstack/form-core'
2
- import type { FunctionComponent } from 'preact/compat'
3
- import type { PreactTanStackFormComponents } from './Components.public'
4
- import type { AnyPreactFormComponentMap } from '../AppForm/componentMap.public'
5
-
6
- type ExtendedFormApi<
7
- TFormData,
8
- TFormErrorTypes extends FormErrorTypes,
9
- TFieldComponents extends Record<string, FunctionComponent<any>>,
10
- > = FormApi<TFormData, TFormErrorTypes> &
11
- PreactTanStackFormComponents<TFormData, TFormErrorTypes, TFieldComponents>
12
-
13
- export type PreactFormApi<
14
- TFormData,
15
- TFormErrorTypes extends FormErrorTypes,
16
- TComponents extends AnyPreactFormComponentMap,
17
- > = unknown extends TComponents['formComponents']
18
- ? ExtendedFormApi<TFormData, TFormErrorTypes, TComponents['fieldComponents']>
19
- : ExtendedFormApi<
20
- TFormData,
21
- TFormErrorTypes,
22
- TComponents['fieldComponents']
23
- > &
24
- TComponents['formComponents']
25
-
26
- export type AnyPreactFormApi = AnyFormApi &
27
- PreactTanStackFormComponents<any, any, any>
28
-
29
- export type { PreactFormComponentMap } from '../AppForm/componentMap.public'
@@ -1,52 +0,0 @@
1
- import type {
2
- AnyFormOptions,
3
- FormOptions,
4
- FormValidators,
5
- ToFormErrorTypes,
6
- } from '@tanstack/form-core'
7
- import type { AppFormOptions } from '../AppForm/appFormOptions.public'
8
- import type {
9
- AnyPreactFormComponentMap,
10
- DefaultPreactFormComponentMap,
11
- } from '../AppForm/componentMap.public'
12
- import type { PreactFormApi } from './formApiTypes.public'
13
-
14
- // When defining formOptions, `onSubmit` is most likely not going to be present in it. Users
15
- // prefer to define them in components.
16
- // That's an issue for us because the inferred form error types are covariant, so resolving them
17
- // early can break passing forms as props to child components.
18
- // This must remain `any` so that `form={form}` remains working. There are type tests for it, so feel
19
- // free to tinker.
20
- type PreactFormTypeErrorTypes<
21
- TFormValidators extends FormValidators<any>,
22
- TSubmitReturn,
23
- > = unknown extends TSubmitReturn
24
- ? any
25
- : ToFormErrorTypes<TFormValidators, TSubmitReturn>
26
-
27
- export type PreactFormType<
28
- TOptions extends
29
- AnyFormOptions | AppFormOptions<any, any, any, AnyPreactFormComponentMap>,
30
- > =
31
- TOptions extends AppFormOptions<
32
- infer TFormData,
33
- infer TFormValidators,
34
- infer TSubmitReturn,
35
- infer TComponents
36
- >
37
- ? PreactFormApi<
38
- TFormData,
39
- PreactFormTypeErrorTypes<TFormValidators, TSubmitReturn>,
40
- TComponents
41
- >
42
- : TOptions extends FormOptions<
43
- infer TFormData,
44
- infer TFormValidators,
45
- infer TSubmitReturn
46
- >
47
- ? PreactFormApi<
48
- TFormData,
49
- PreactFormTypeErrorTypes<TFormValidators, TSubmitReturn>,
50
- DefaultPreactFormComponentMap
51
- >
52
- : never
@@ -1,52 +0,0 @@
1
- import { useEffect, useMemo, useRef } from 'preact/compat'
2
- import { useSelector } from '@tanstack/preact-store'
3
- import type { FunctionComponent } from 'preact/compat'
4
- import type {
5
- AnyInternalFieldApi,
6
- InternalFormApi,
7
- } from '@tanstack/form-core/internals'
8
- import type { PreactFormFieldProps } from './Components.public'
9
-
10
- interface InternalFieldProps extends PreactFormFieldProps<
11
- any,
12
- any,
13
- any,
14
- any,
15
- any,
16
- any,
17
- any,
18
- any
19
- > {
20
- form: InternalFormApi<any, any, any>
21
- }
22
-
23
- export function useField(
24
- options: InternalFieldProps,
25
- fieldComponents: Record<string, FunctionComponent<any>> | null,
26
- ): AnyInternalFieldApi {
27
- const optionsRef = useRef(options)
28
- optionsRef.current = options
29
-
30
- const resetVersion = useSelector(options.form._atoms.resetVersion)
31
-
32
- const fieldApi = useMemo(() => {
33
- void resetVersion
34
- const field = options.form._getOrCreateFieldApi({
35
- ...optionsRef.current,
36
- name: options.name,
37
- })
38
- if (fieldComponents === null) return field
39
- Object.assign(field, fieldComponents)
40
-
41
- return field
42
- }, [options.name, options.form, resetVersion, fieldComponents])
43
-
44
- useEffect(() => fieldApi._update(options))
45
-
46
- useEffect(() => {
47
- const cleanup = fieldApi._register()
48
- return cleanup
49
- }, [fieldApi])
50
-
51
- return fieldApi
52
- }
@@ -1,34 +0,0 @@
1
- import { initializeForm, useInternalForm } from './PreactFormApi.lib'
2
- import type {
3
- FormOptions,
4
- FormValidators,
5
- ToFormErrorTypes,
6
- } from '@tanstack/form-core'
7
- import type {
8
- AnyPreactFormComponentMap,
9
- DefaultPreactFormComponentMap,
10
- } from '../AppForm/componentMap.public'
11
- import type { PreactFormApi } from './formApiTypes.public'
12
-
13
- export type UseFormHook<in out TComponents extends AnyPreactFormComponentMap> =
14
- <
15
- TFormData,
16
- const TFormValidators extends FormValidators<TFormData>,
17
- TSubmitReturn,
18
- >(
19
- options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
20
- ) => PreactFormApi<
21
- TFormData,
22
- ToFormErrorTypes<TFormValidators, TSubmitReturn>,
23
- TComponents
24
- >
25
-
26
- function useFormHook(options: FormOptions<any, any, any>) {
27
- const form = useInternalForm(options, initializeForm)
28
- return form
29
- }
30
-
31
- const useForm =
32
- useFormHook as never as UseFormHook<DefaultPreactFormComponentMap>
33
-
34
- export { useForm }
@@ -1,56 +0,0 @@
1
- import { shallow, useSelector } from '@tanstack/preact-store'
2
- import type {
3
- Atom,
4
- ReadonlyAtom,
5
- ReadonlyStore,
6
- Store,
7
- } from '@tanstack/preact-store'
8
- import type { CrossVersionPreactNode } from './preactTypes.public'
9
-
10
- export type SubscribeSource<TValue> =
11
- Atom<TValue> | ReadonlyAtom<TValue> | Store<TValue> | ReadonlyStore<TValue>
12
-
13
- /**
14
- * Subscribe to `form.atom` (full form state). The selector receives the full
15
- * {@link FormState}.
16
- */
17
- export interface SubscribeProps<in out TSourceData, in out TSelected> {
18
- source: SubscribeSource<TSourceData>
19
- /**
20
- * Select from full form state. Re-renders when the selected value changes
21
- * (shallow compare).
22
- */
23
- selector: (state: TSourceData) => TSelected
24
- /**
25
- * Optional. If provided, the component will only render when the `when` function returns `true`.
26
- */
27
- when?: (selected: NoInfer<TSelected>) => boolean
28
- children:
29
- | ((state: NoInfer<TSelected>) => CrossVersionPreactNode)
30
- | CrossVersionPreactNode
31
- }
32
-
33
- /**
34
- * A Preact component that allows you to subscribe to source state.
35
- *
36
- * This is useful for opting into state re-renders for specific parts of the state.
37
- */
38
- export function Subscribe<TSourceData, const TSelected>(
39
- props: SubscribeProps<TSourceData, TSelected>,
40
- ): CrossVersionPreactNode {
41
- const selected = useSelector(
42
- // Atom and store share the same selection protocol; union args need a widen for TS.
43
- props.source,
44
- props.selector as Parameters<typeof useSelector>[1],
45
- {
46
- compare: shallow,
47
- },
48
- ) as TSelected
49
-
50
- if (props.when?.(selected) === false) {
51
- return null
52
- }
53
- return typeof props.children === 'function'
54
- ? props.children(selected)
55
- : props.children
56
- }