@tanstack/preact-form 1.30.3 → 1.33.3

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 (116) hide show
  1. package/README.md +15 -1
  2. package/dist/AppForm/Components.lib.js +90 -0
  3. package/dist/AppForm/PreactAppFormApi.public.d.ts +15 -0
  4. package/dist/AppForm/appFormOptions.public.d.ts +15 -0
  5. package/dist/AppForm/componentMap.public.d.ts +11 -0
  6. package/dist/AppForm/contexts.lib.js +18 -0
  7. package/dist/AppForm/createFormHook.public.d.ts +7 -0
  8. package/dist/AppForm/createFormHook.public.js +26 -0
  9. package/dist/AppForm/createFormHookTypes.public.d.ts +16 -0
  10. package/dist/AppForm/fieldComponentHelpers.lib.js +21 -0
  11. package/dist/AppForm/getFormHookHelpers.public.d.ts +35 -0
  12. package/dist/AppForm/getFormHookHelpers.public.js +18 -0
  13. package/dist/AppForm/initializeAppForm.lib.js +12 -0
  14. package/dist/FieldGroup/FieldGroupApi.public.d.ts +25 -0
  15. package/dist/FieldGroup/withFields.lib.js +98 -0
  16. package/dist/FieldGroup/withFields.public.d.ts +53 -0
  17. package/dist/FieldGroup/withFields.public.js +19 -0
  18. package/dist/PreactForm/Components.lib.js +97 -0
  19. package/dist/PreactForm/Components.public.d.ts +67 -0
  20. package/dist/PreactForm/PreactFormApi.lib.js +27 -0
  21. package/dist/PreactForm/fieldSubscriptions.lib.js +27 -0
  22. package/dist/PreactForm/formApiTypes.public.d.ts +11 -0
  23. package/dist/PreactForm/formType.public.d.ts +10 -0
  24. package/dist/PreactForm/useField.lib.js +31 -0
  25. package/dist/PreactForm/useForm.public.d.ts +9 -0
  26. package/dist/PreactForm/useForm.public.js +10 -0
  27. package/dist/Subscribe.public.d.ts +30 -0
  28. package/dist/Subscribe.public.js +16 -0
  29. package/dist/index.d.ts +18 -0
  30. package/dist/index.js +11 -0
  31. package/dist/preactTypes.public.d.ts +10 -0
  32. package/dist/types.public.d.ts +2 -0
  33. package/package.json +15 -16
  34. package/src/AppForm/Components.lib.tsx +160 -0
  35. package/src/AppForm/PreactAppFormApi.public.tsx +17 -0
  36. package/src/AppForm/appFormOptions.public.ts +59 -0
  37. package/src/AppForm/componentMap.public.ts +19 -0
  38. package/src/AppForm/contexts.lib.ts +28 -0
  39. package/src/AppForm/createFormHook.public.ts +37 -0
  40. package/src/AppForm/createFormHookTypes.public.ts +34 -0
  41. package/src/AppForm/fieldComponentHelpers.lib.tsx +30 -0
  42. package/src/AppForm/getFormHookHelpers.public.ts +114 -0
  43. package/src/AppForm/initializeAppForm.lib.ts +22 -0
  44. package/src/FieldGroup/FieldGroupApi.public.ts +91 -0
  45. package/src/FieldGroup/withFields.lib.ts +191 -0
  46. package/src/FieldGroup/withFields.public.ts +221 -0
  47. package/src/PreactForm/Components.lib.tsx +166 -0
  48. package/src/PreactForm/Components.public.ts +383 -0
  49. package/src/PreactForm/PreactFormApi.lib.tsx +48 -0
  50. package/src/PreactForm/fieldSubscriptions.lib.ts +32 -0
  51. package/src/PreactForm/formApiTypes.public.ts +29 -0
  52. package/src/PreactForm/formType.public.ts +52 -0
  53. package/src/PreactForm/useField.lib.ts +52 -0
  54. package/src/PreactForm/useForm.public.ts +34 -0
  55. package/src/Subscribe.public.tsx +56 -0
  56. package/src/index.ts +16 -9
  57. package/src/preactTypes.public.ts +6 -0
  58. package/src/types.public.ts +2 -0
  59. package/dist/cjs/createFormHook.cjs +0 -101
  60. package/dist/cjs/createFormHook.cjs.map +0 -1
  61. package/dist/cjs/createFormHook.d.cts +0 -80
  62. package/dist/cjs/index.cjs +0 -34
  63. package/dist/cjs/index.cjs.map +0 -1
  64. package/dist/cjs/index.d.cts +0 -9
  65. package/dist/cjs/types.d.cts +0 -17
  66. package/dist/cjs/useField.cjs +0 -108
  67. package/dist/cjs/useField.cjs.map +0 -1
  68. package/dist/cjs/useField.d.cts +0 -52
  69. package/dist/cjs/useFieldGroup.cjs +0 -48
  70. package/dist/cjs/useFieldGroup.cjs.map +0 -1
  71. package/dist/cjs/useFieldGroup.d.cts +0 -30
  72. package/dist/cjs/useForm.cjs +0 -78
  73. package/dist/cjs/useForm.cjs.map +0 -1
  74. package/dist/cjs/useForm.d.cts +0 -31
  75. package/dist/cjs/useFormGroup.cjs +0 -163
  76. package/dist/cjs/useFormGroup.cjs.map +0 -1
  77. package/dist/cjs/useFormGroup.d.cts +0 -13
  78. package/dist/cjs/useFormId.cjs +0 -6
  79. package/dist/cjs/useFormId.cjs.map +0 -1
  80. package/dist/cjs/useFormId.d.cts +0 -2
  81. package/dist/cjs/useIsomorphicLayoutEffect.cjs +0 -6
  82. package/dist/cjs/useIsomorphicLayoutEffect.cjs.map +0 -1
  83. package/dist/cjs/useIsomorphicLayoutEffect.d.cts +0 -2
  84. package/dist/esm/createFormHook.d.ts +0 -80
  85. package/dist/esm/createFormHook.js +0 -101
  86. package/dist/esm/createFormHook.js.map +0 -1
  87. package/dist/esm/index.d.ts +0 -9
  88. package/dist/esm/index.js +0 -22
  89. package/dist/esm/index.js.map +0 -1
  90. package/dist/esm/types.d.ts +0 -17
  91. package/dist/esm/useField.d.ts +0 -52
  92. package/dist/esm/useField.js +0 -108
  93. package/dist/esm/useField.js.map +0 -1
  94. package/dist/esm/useFieldGroup.d.ts +0 -30
  95. package/dist/esm/useFieldGroup.js +0 -48
  96. package/dist/esm/useFieldGroup.js.map +0 -1
  97. package/dist/esm/useForm.d.ts +0 -31
  98. package/dist/esm/useForm.js +0 -78
  99. package/dist/esm/useForm.js.map +0 -1
  100. package/dist/esm/useFormGroup.d.ts +0 -13
  101. package/dist/esm/useFormGroup.js +0 -163
  102. package/dist/esm/useFormGroup.js.map +0 -1
  103. package/dist/esm/useFormId.d.ts +0 -2
  104. package/dist/esm/useFormId.js +0 -6
  105. package/dist/esm/useFormId.js.map +0 -1
  106. package/dist/esm/useIsomorphicLayoutEffect.d.ts +0 -2
  107. package/dist/esm/useIsomorphicLayoutEffect.js +0 -6
  108. package/dist/esm/useIsomorphicLayoutEffect.js.map +0 -1
  109. package/src/createFormHook.tsx +0 -603
  110. package/src/types.ts +0 -143
  111. package/src/useField.tsx +0 -730
  112. package/src/useFieldGroup.tsx +0 -259
  113. package/src/useForm.tsx +0 -300
  114. package/src/useFormGroup.tsx +0 -741
  115. package/src/useFormId.ts +0 -3
  116. package/src/useIsomorphicLayoutEffect.ts +0 -4
@@ -0,0 +1,221 @@
1
+ import {
2
+ defineFieldsRuntime,
3
+ helperRuntime,
4
+ withFieldsRuntime,
5
+ } from './withFields.lib'
6
+ import type {
7
+ DeepKeys,
8
+ DeepKeysWhereValueIncludes,
9
+ DeepValue,
10
+ FormApi,
11
+ } from '@tanstack/form-core'
12
+ import type { FunctionComponent } from 'preact/compat'
13
+ import type { CrossVersionPreactNode } from '../preactTypes.public'
14
+ import type { PreactTanStackFormComponents } from '../PreactForm/Components.public'
15
+ import type { FieldGroupApi } from './FieldGroupApi.public'
16
+
17
+ declare const fieldGroupFieldSlotValueSymbol: unique symbol
18
+ declare const fieldGroupFieldsSymbol: unique symbol
19
+
20
+ export type FieldGroupFieldSlotMode = 'strict' | 'loose'
21
+
22
+ export interface FieldGroupFieldSlot<
23
+ out TValue,
24
+ out TMode extends FieldGroupFieldSlotMode = FieldGroupFieldSlotMode,
25
+ > {
26
+ readonly mode: TMode
27
+ readonly [fieldGroupFieldSlotValueSymbol]: TValue
28
+ }
29
+
30
+ export type AnyFieldGroupFieldSlot = FieldGroupFieldSlot<any>
31
+
32
+ export type StrictFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<
33
+ TValue,
34
+ 'strict'
35
+ >
36
+
37
+ export type LooseFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<
38
+ TValue,
39
+ 'loose'
40
+ >
41
+
42
+ export type FieldGroupFieldSlotValue<TSlot> =
43
+ TSlot extends FieldGroupFieldSlot<infer TValue> ? TValue : never
44
+
45
+ export type FieldGroupFieldSlotModeOf<TSlot> =
46
+ TSlot extends FieldGroupFieldSlot<any, infer TMode> ? TMode : never
47
+
48
+ type IsSame<TTypeA, TTypeB> = [TTypeA] extends [TTypeB]
49
+ ? [TTypeB] extends [TTypeA]
50
+ ? true
51
+ : false
52
+ : false
53
+
54
+ export type FieldGroupFieldSlotAllows<TSlot, TValue> =
55
+ TSlot extends FieldGroupFieldSlot<infer TAcceptedValue, infer TMode>
56
+ ? TMode extends 'strict'
57
+ ? IsSame<TValue, TAcceptedValue>
58
+ : [TValue] extends [TAcceptedValue]
59
+ ? true
60
+ : false
61
+ : false
62
+
63
+ export type FieldGroupFieldNameForSlot<
64
+ TFieldData,
65
+ TSlot extends AnyFieldGroupFieldSlot,
66
+ > = {
67
+ [TFieldName in DeepKeys<TFieldData>]: FieldGroupFieldSlotAllows<
68
+ TSlot,
69
+ DeepValue<TFieldData, TFieldName>
70
+ > extends true
71
+ ? TFieldName
72
+ : never
73
+ }[DeepKeys<TFieldData>]
74
+
75
+ export type FieldGroupFields = Record<string, AnyFieldGroupFieldSlot>
76
+
77
+ export type FieldGroupFieldNames<
78
+ TFieldData,
79
+ TFields extends FieldGroupFields,
80
+ > = {
81
+ [TFieldName in keyof TFields]: FieldGroupFieldNameForSlot<
82
+ TFieldData,
83
+ TFields[TFieldName]
84
+ >
85
+ }
86
+
87
+ export type FieldGroupFieldData<TFields extends FieldGroupFields> = {
88
+ [
89
+ TFieldName in keyof TFields
90
+ ]: TFields[TFieldName] extends FieldGroupFieldSlot<infer TValue, any>
91
+ ? TValue
92
+ : never
93
+ }
94
+
95
+ export type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
96
+ readonly [fieldGroupFieldsSymbol]: infer TFields
97
+ }
98
+ ? TFields
99
+ : never
100
+
101
+ export type FieldGroupDefinition<
102
+ TFields extends FieldGroupFields,
103
+ TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
104
+ never,
105
+ never
106
+ >,
107
+ > = FieldGroupApi<FieldGroupFieldData<TFields>, TFieldComponents> & {
108
+ readonly [fieldGroupFieldsSymbol]: TFields
109
+ }
110
+
111
+ export type FieldGroupFieldComponentsOf<TFieldGroup> =
112
+ TFieldGroup extends FieldGroupDefinition<any, infer TFieldComponents>
113
+ ? TFieldComponents
114
+ : never
115
+
116
+ export type FieldGroupForm<
117
+ TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
118
+ never,
119
+ never
120
+ >,
121
+ TFormData = any,
122
+ > = FormApi<TFormData, any> &
123
+ PreactTanStackFormComponents<TFormData, any, TFieldComponents>
124
+
125
+ export type FieldGroupFieldBindingForSlot<
126
+ TFormData,
127
+ TSlot extends AnyFieldGroupFieldSlot,
128
+ > =
129
+ TSlot extends FieldGroupFieldSlot<infer TValue, infer TMode>
130
+ ? TMode extends 'strict'
131
+ ? FieldGroupFieldNameForSlot<TFormData, TSlot>
132
+ : DeepKeysWhereValueIncludes<TFormData, TValue>
133
+ : never
134
+
135
+ export type FieldGroupFieldBindings<
136
+ TFields extends FieldGroupFields,
137
+ TFormData = any,
138
+ > = {
139
+ [TFieldName in keyof TFields]: FieldGroupFieldBindingForSlot<
140
+ TFormData,
141
+ TFields[TFieldName]
142
+ >
143
+ }
144
+
145
+ export type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> =
146
+ FieldGroupFieldsOf<TFieldGroup> extends FieldGroupFields
147
+ ? FieldGroupFieldBindings<FieldGroupFieldsOf<TFieldGroup>, TFormData>
148
+ : never
149
+
150
+ export type FieldGroupFieldsPropName<
151
+ TProps,
152
+ TFieldGroup extends FieldGroupDefinition<any, any>,
153
+ > = {
154
+ [TPropName in keyof TProps]-?: IsSame<
155
+ TProps[TPropName],
156
+ TFieldGroup
157
+ > extends true
158
+ ? TPropName
159
+ : never
160
+ }[keyof TProps]
161
+
162
+ export type FieldGroupWithFieldsFn = <
163
+ TFieldGroup extends FieldGroupDefinition<any, any>,
164
+ TProps extends object,
165
+ TFieldsPropName extends FieldGroupFieldsPropName<TProps, TFieldGroup>,
166
+ >(
167
+ fields: TFieldGroup,
168
+ Component: (props: TProps) => CrossVersionPreactNode,
169
+ fieldsPropName: TFieldsPropName,
170
+ ) => <TFormData>(
171
+ props: Omit<TProps, TFieldsPropName | 'form'> & {
172
+ form: FieldGroupForm<FieldGroupFieldComponentsOf<TFieldGroup>, TFormData>
173
+ } & {
174
+ [TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<
175
+ TFieldGroup,
176
+ TFormData
177
+ >
178
+ },
179
+ ) => CrossVersionPreactNode
180
+
181
+ export interface FieldGroupHelper {
182
+ strict: <TValue>() => StrictFieldGroupFieldSlot<TValue>
183
+ loose: <TValue>() => LooseFieldGroupFieldSlot<TValue>
184
+ }
185
+
186
+ const helper: FieldGroupHelper = helperRuntime as never
187
+
188
+ export type DefineFieldsFn<
189
+ TFieldComponents extends Record<string, FunctionComponent<any>>,
190
+ > = <const TFields extends FieldGroupFields>(
191
+ fields: TFields,
192
+ ) => FieldGroupDefinition<TFields, TFieldComponents>
193
+
194
+ const defineFields: DefineFieldsFn<Record<never, never>> =
195
+ defineFieldsRuntime as never
196
+
197
+ const withFields: FieldGroupWithFieldsFn = withFieldsRuntime as never
198
+
199
+ export interface FieldGroupHelpers<
200
+ TFieldComponents extends Record<string, FunctionComponent<any>>,
201
+ > {
202
+ helper: FieldGroupHelper
203
+ defineFields: DefineFieldsFn<TFieldComponents>
204
+ withFields: FieldGroupWithFieldsFn
205
+ }
206
+
207
+ function createFieldGroupHelpers<
208
+ TFieldComponents extends Record<string, FunctionComponent<any>>,
209
+ >(): FieldGroupHelpers<TFieldComponents> {
210
+ return {
211
+ helper,
212
+ defineFields: defineFields as never,
213
+ withFields,
214
+ }
215
+ }
216
+
217
+ export function getFieldGroupHelpers(): FieldGroupHelpers<
218
+ Record<never, never>
219
+ > {
220
+ return createFieldGroupHelpers()
221
+ }
@@ -0,0 +1,166 @@
1
+ import Preact, { useEffect } from 'preact/compat'
2
+ import { InternalFormGroupApi } from '@tanstack/form-core/internals'
3
+ import { Subscribe } from '../Subscribe.public'
4
+ import {
5
+ useArrayFieldSubscription,
6
+ useValueFieldSubscription,
7
+ } from './fieldSubscriptions.lib'
8
+ import { useField } from './useField.lib'
9
+ import type { AnyInternalFormApi } from '@tanstack/form-core/internals'
10
+ import type { InternalPreactFormApi } from './PreactFormApi.lib'
11
+ import type { FunctionComponent } from 'preact/compat'
12
+ import type {
13
+ PreactFormFieldProps,
14
+ PreactFormGroupProps,
15
+ PreactFormSubscribeProps,
16
+ } from './Components.public'
17
+
18
+ export function attachPreactFormComponents(
19
+ form: AnyInternalFormApi,
20
+ fieldComponents: Record<string, FunctionComponent<any>> | null,
21
+ ): InternalPreactFormApi {
22
+ const resultForm = form as InternalPreactFormApi
23
+ resultForm.Field = createFieldComponent(
24
+ form,
25
+ fieldComponents,
26
+ ) as InternalPreactFormApi['Field']
27
+ resultForm.ArrayField = createArrayFieldComponent(form, fieldComponents)
28
+ resultForm.Subscribe = createSubscribeComponent(form)
29
+ resultForm.FormGroup = createFormGroupComponent(
30
+ resultForm,
31
+ ) as InternalPreactFormApi['FormGroup']
32
+
33
+ return resultForm
34
+ }
35
+
36
+ type AnyFieldComponent = FunctionComponent<
37
+ PreactFormFieldProps<any, any, any, any, never, any, any, any>
38
+ >
39
+
40
+ function createFieldComponent(
41
+ form: AnyInternalFormApi,
42
+ fieldComponents: Record<string, FunctionComponent<any>> | null,
43
+ ): AnyFieldComponent {
44
+ const TanStackFormField: AnyFieldComponent = (props) => {
45
+ const fieldApi = useField({ ...props, form }, fieldComponents)
46
+ const field = useValueFieldSubscription(fieldApi)
47
+
48
+ return props.children(field)
49
+ }
50
+
51
+ TanStackFormField.displayName = 'TanStackForm.Field'
52
+
53
+ return TanStackFormField
54
+ }
55
+
56
+ type AnyArrayFieldComponent = FunctionComponent<any>
57
+
58
+ function createArrayFieldComponent(
59
+ form: AnyInternalFormApi,
60
+ fieldComponents: Record<string, FunctionComponent<any>> | null,
61
+ ): AnyArrayFieldComponent {
62
+ const TanStackFormArrayField: AnyArrayFieldComponent = (props) => {
63
+ const fieldApi = useField({ ...props, form }, fieldComponents)
64
+ const field = useArrayFieldSubscription(fieldApi)
65
+
66
+ return props.children(field)
67
+ }
68
+
69
+ TanStackFormArrayField.displayName = 'TanStackForm.ArrayField'
70
+
71
+ return TanStackFormArrayField
72
+ }
73
+
74
+ type AnySubscribeComponent = FunctionComponent<
75
+ PreactFormSubscribeProps<any, any, any>
76
+ >
77
+
78
+ function createSubscribeComponent(
79
+ form: AnyInternalFormApi,
80
+ ): AnySubscribeComponent {
81
+ const TanStackFormSubscribe: AnySubscribeComponent = (props) => {
82
+ return <Subscribe source={form.atom} {...props} />
83
+ }
84
+
85
+ TanStackFormSubscribe.displayName = 'TanStackForm.Subscribe'
86
+
87
+ return TanStackFormSubscribe
88
+ }
89
+
90
+ type AnyFormGroupComponent = FunctionComponent<
91
+ PreactFormGroupProps<any, any, any, any, any, any>
92
+ >
93
+
94
+ function createFormGroupComponent(
95
+ form: InternalPreactFormApi,
96
+ ): AnyFormGroupComponent {
97
+ const TanStackFormGroup: AnyFormGroupComponent = (props) => {
98
+ const groupRef =
99
+ Preact.useRef<InternalFormGroupApi<any, any, any, any, any>>(null)
100
+
101
+ if (!groupRef.current) {
102
+ groupRef.current = attachPreactFormGroupComponents(
103
+ new InternalFormGroupApi({ ...props, form } as never),
104
+ form,
105
+ )
106
+ }
107
+
108
+ useEffect(() => groupRef.current?.update({ ...props, form }))
109
+
110
+ Preact.useEffect(() => {
111
+ const group = groupRef.current!
112
+ group.mount()
113
+ return () => group._cleanup()
114
+ }, [])
115
+
116
+ return props.children(groupRef.current as never)
117
+ }
118
+
119
+ TanStackFormGroup.displayName = 'TanStackForm.FormGroup'
120
+
121
+ return TanStackFormGroup
122
+ }
123
+
124
+ function attachPreactFormGroupComponents(
125
+ group: InternalFormGroupApi<any, any, any, any, any>,
126
+ form: InternalPreactFormApi,
127
+ ) {
128
+ type FormGroupComponents = InternalFormGroupApi<any, any, any, any, any> & {
129
+ Field: FunctionComponent<any>
130
+ ArrayField: FunctionComponent<any>
131
+ Subscribe: FunctionComponent<any>
132
+ }
133
+
134
+ const resultGroup: FormGroupComponents = group as never
135
+
136
+ resultGroup.Field = function Field(props) {
137
+ return (
138
+ <form.Field
139
+ {...(group._getFormFieldOptions(props, (base, overrides) => ({
140
+ ...base,
141
+ ...overrides,
142
+ })) as any)}
143
+ />
144
+ )
145
+ }
146
+ resultGroup.Field.displayName = 'TanStackForm.FormGroup.Field'
147
+
148
+ resultGroup.ArrayField = function ArrayField(props) {
149
+ return (
150
+ <form.ArrayField
151
+ {...(group._getFormFieldOptions(props, (base, overrides) => ({
152
+ ...base,
153
+ ...overrides,
154
+ })) as any)}
155
+ />
156
+ )
157
+ }
158
+ resultGroup.ArrayField.displayName = 'TanStackForm.FormGroup.ArrayField'
159
+
160
+ resultGroup.Subscribe = (props) => {
161
+ return <Subscribe source={group.atom} {...props} />
162
+ }
163
+ resultGroup.Subscribe.displayName = 'TanStackForm.FormGroup.Subscribe'
164
+
165
+ return resultGroup
166
+ }