@tanstack/preact-form 2.0.0-alpha.0 → 2.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/AppForm/createFormHook.public.d.ts +6 -3
  2. package/dist/AppForm/createFormHook.public.js +2 -6
  3. package/dist/AppForm/createFormHookTypes.public.d.ts +190 -6
  4. package/dist/AppForm/initializeAppForm.lib.js +6 -1
  5. package/dist/FieldGroup/FieldGroupApi.public.d.ts +1 -1
  6. package/dist/PreactForm/Components.public.d.ts +2 -2
  7. package/dist/PreactForm/formApiTypes.public.d.ts +24 -0
  8. package/dist/PreactForm/formType.public.d.ts +30 -2
  9. package/dist/PreactForm/useField.lib.js +2 -2
  10. package/dist/PreactForm/useForm.public.d.ts +33 -1
  11. package/dist/PreactForm/useForm.public.js +32 -0
  12. package/dist/index.d.ts +2 -3
  13. package/package.json +3 -4
  14. package/dist/AppForm/appFormOptions.public.d.ts +0 -14
  15. package/src/AppForm/Components.lib.tsx +0 -160
  16. package/src/AppForm/PreactAppFormApi.public.tsx +0 -17
  17. package/src/AppForm/appFormOptions.public.ts +0 -59
  18. package/src/AppForm/componentMap.public.ts +0 -19
  19. package/src/AppForm/contexts.lib.ts +0 -28
  20. package/src/AppForm/createFormHook.public.ts +0 -37
  21. package/src/AppForm/createFormHookTypes.public.ts +0 -32
  22. package/src/AppForm/fieldComponentHelpers.lib.tsx +0 -30
  23. package/src/AppForm/getFormHookHelpers.public.ts +0 -114
  24. package/src/AppForm/initializeAppForm.lib.ts +0 -22
  25. package/src/FieldGroup/FieldGroupApi.public.ts +0 -91
  26. package/src/FieldGroup/withFields.lib.ts +0 -109
  27. package/src/FieldGroup/withFields.public.ts +0 -203
  28. package/src/PreactForm/Components.lib.tsx +0 -166
  29. package/src/PreactForm/Components.public.ts +0 -383
  30. package/src/PreactForm/PreactFormApi.lib.tsx +0 -48
  31. package/src/PreactForm/fieldSubscriptions.lib.ts +0 -32
  32. package/src/PreactForm/formApiTypes.public.ts +0 -29
  33. package/src/PreactForm/formType.public.ts +0 -52
  34. package/src/PreactForm/useField.lib.ts +0 -52
  35. package/src/PreactForm/useForm.public.ts +0 -34
  36. package/src/Subscribe.public.tsx +0 -56
  37. package/src/index.ts +0 -18
  38. package/src/preactTypes.public.ts +0 -6
  39. package/src/types.public.ts +0 -2
@@ -1,109 +0,0 @@
1
- import Preact from 'preact/compat'
2
- import {
3
- InternalFieldGroupApi,
4
- defineFieldGroupFieldsRuntime,
5
- fieldGroupHelperRuntime,
6
- } from '@tanstack/form-core/internals'
7
- import type { InternalFieldGroupBindings } from '@tanstack/form-core/internals'
8
- import type { FunctionComponent } from 'preact/compat'
9
- import type { AnyFieldGroupApi } from './FieldGroupApi.public'
10
-
11
- function attachPreactFieldGroupComponents(
12
- group: InternalFieldGroupApi,
13
- form: any,
14
- ): AnyFieldGroupApi {
15
- const Field: FunctionComponent<any> = (props) => {
16
- const FormField = form.Field
17
- return Preact.createElement(
18
- FormField as FunctionComponent<any>,
19
- group._getFormFieldOptions(props, (base, overrides) => ({
20
- ...base,
21
- ...overrides,
22
- })),
23
- )
24
- }
25
- Field.displayName = 'TanStackForm.FieldGroup.Field'
26
-
27
- const ArrayField: FunctionComponent<any> = (props) => {
28
- const FormArrayField = form.ArrayField
29
- return Preact.createElement(
30
- FormArrayField as FunctionComponent<any>,
31
- group._getFormFieldOptions(props, (base, overrides) => ({
32
- ...base,
33
- ...overrides,
34
- })),
35
- )
36
- }
37
- ArrayField.displayName = 'TanStackForm.FieldGroup.ArrayField'
38
-
39
- const Subscribe: FunctionComponent<any> = (props) => {
40
- const FormSubscribe = form.Subscribe
41
- return Preact.createElement(FormSubscribe as FunctionComponent<any>, props)
42
- }
43
- Subscribe.displayName = 'TanStackForm.FieldGroup.Subscribe'
44
-
45
- return Object.assign(group, {
46
- Field,
47
- ArrayField,
48
- Subscribe,
49
- }) as never
50
- }
51
-
52
- export function defineFieldGroupRuntime<
53
- TFields extends Record<string, unknown>,
54
- >(defineFieldGroupFn: (helperRuntime: any) => TFields) {
55
- const fields = defineFieldGroupFieldsRuntime(
56
- defineFieldGroupFn(fieldGroupHelperRuntime),
57
- )
58
-
59
- return {
60
- fields,
61
- bindComponent: (
62
- Component: (props: any) => unknown,
63
- fieldsPropName: string,
64
- ) => withFieldsRuntime(fields, Component, fieldsPropName),
65
- }
66
- }
67
-
68
- function withFieldsRuntime(
69
- fields: Record<string, unknown>,
70
- Component: (props: any) => unknown,
71
- fieldsPropName: string,
72
- ) {
73
- const fieldNames = Object.keys(fields)
74
-
75
- const FieldGroupComponent = (props: any) => {
76
- const { form, ...restProps } = props
77
- const bindingsRef = Preact.useRef<InternalFieldGroupBindings>(
78
- props[fieldsPropName],
79
- )
80
-
81
- if (!form) {
82
- throw new Error('TanStack Form: Field groups must receive a `form` prop.')
83
- }
84
-
85
- bindingsRef.current = props[fieldsPropName]
86
-
87
- const fieldGroupApi = Preact.useMemo(
88
- () =>
89
- attachPreactFieldGroupComponents(
90
- new InternalFieldGroupApi({
91
- form,
92
- fieldNames,
93
- getBindings: () => bindingsRef.current!,
94
- }),
95
- form,
96
- ),
97
- [form],
98
- )
99
-
100
- return Component({
101
- ...restProps,
102
- [fieldsPropName]: fieldGroupApi,
103
- })
104
- }
105
-
106
- FieldGroupComponent.displayName = 'TanStackForm.FieldGroup'
107
-
108
- return FieldGroupComponent
109
- }
@@ -1,203 +0,0 @@
1
- import { defineFieldGroupRuntime } from './withFields.lib'
2
- import type {
3
- DeepKeys,
4
- DeepKeysWhereValueIncludes,
5
- DeepValue,
6
- FormApi,
7
- } from '@tanstack/form-core'
8
- import type { FunctionComponent } from 'preact/compat'
9
- import type { CrossVersionPreactNode } from '../preactTypes.public'
10
- import type { PreactTanStackFormComponents } from '../PreactForm/Components.public'
11
- import type { FieldGroupApi } from './FieldGroupApi.public'
12
-
13
- declare const fieldGroupFieldSlotValueSymbol: unique symbol
14
- declare const fieldGroupFieldsSymbol: unique symbol
15
-
16
- export type FieldGroupFieldSlotMode = 'strict' | 'loose'
17
-
18
- export interface FieldGroupFieldSlot<
19
- out TValue,
20
- out TMode extends FieldGroupFieldSlotMode = FieldGroupFieldSlotMode,
21
- > {
22
- readonly mode: TMode
23
- readonly [fieldGroupFieldSlotValueSymbol]: TValue
24
- }
25
-
26
- export type AnyFieldGroupFieldSlot = FieldGroupFieldSlot<any>
27
-
28
- export type StrictFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<
29
- TValue,
30
- 'strict'
31
- >
32
-
33
- export type LooseFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<
34
- TValue,
35
- 'loose'
36
- >
37
-
38
- export type FieldGroupFieldSlotValue<TSlot> =
39
- TSlot extends FieldGroupFieldSlot<infer TValue> ? TValue : never
40
-
41
- export type FieldGroupFieldSlotModeOf<TSlot> =
42
- TSlot extends FieldGroupFieldSlot<any, infer TMode> ? TMode : never
43
-
44
- type IsSame<TTypeA, TTypeB> = [TTypeA] extends [TTypeB]
45
- ? [TTypeB] extends [TTypeA]
46
- ? true
47
- : false
48
- : false
49
-
50
- export type FieldGroupFieldSlotAllows<TSlot, TValue> =
51
- TSlot extends FieldGroupFieldSlot<infer TAcceptedValue, infer TMode>
52
- ? TMode extends 'strict'
53
- ? IsSame<TValue, TAcceptedValue>
54
- : [TValue] extends [TAcceptedValue]
55
- ? true
56
- : false
57
- : false
58
-
59
- export type FieldGroupFieldNameForSlot<
60
- TFieldData,
61
- TSlot extends AnyFieldGroupFieldSlot,
62
- > = {
63
- [TFieldName in DeepKeys<TFieldData>]: FieldGroupFieldSlotAllows<
64
- TSlot,
65
- DeepValue<TFieldData, TFieldName>
66
- > extends true
67
- ? TFieldName
68
- : never
69
- }[DeepKeys<TFieldData>]
70
-
71
- export type FieldGroupFields = Record<string, AnyFieldGroupFieldSlot>
72
-
73
- export type FieldGroupFieldNames<
74
- TFieldData,
75
- TFields extends FieldGroupFields,
76
- > = {
77
- [TFieldName in keyof TFields]: FieldGroupFieldNameForSlot<
78
- TFieldData,
79
- TFields[TFieldName]
80
- >
81
- }
82
-
83
- export type FieldGroupFieldData<TFields extends FieldGroupFields> = {
84
- [
85
- TFieldName in keyof TFields
86
- ]: TFields[TFieldName] extends FieldGroupFieldSlot<infer TValue, any>
87
- ? TValue
88
- : never
89
- }
90
-
91
- export type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
92
- readonly [fieldGroupFieldsSymbol]: infer TFields
93
- }
94
- ? TFields
95
- : never
96
-
97
- export type PreactFieldGroup<
98
- TFields extends FieldGroupFields,
99
- TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
100
- never,
101
- never
102
- >,
103
- > = FieldGroupApi<FieldGroupFieldData<TFields>, TFieldComponents> & {
104
- readonly [fieldGroupFieldsSymbol]: TFields
105
- }
106
-
107
- export type FieldGroupFieldComponentsOf<TFieldGroup> =
108
- TFieldGroup extends PreactFieldGroup<any, infer TFieldComponents>
109
- ? TFieldComponents
110
- : never
111
-
112
- export type FieldGroupForm<
113
- TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
114
- never,
115
- never
116
- >,
117
- TFormData = any,
118
- > = FormApi<TFormData, any> &
119
- PreactTanStackFormComponents<TFormData, any, TFieldComponents>
120
-
121
- export type FieldGroupFieldBindingForSlot<
122
- TFormData,
123
- TSlot extends AnyFieldGroupFieldSlot,
124
- > =
125
- TSlot extends FieldGroupFieldSlot<infer TValue, infer TMode>
126
- ? TMode extends 'strict'
127
- ? FieldGroupFieldNameForSlot<TFormData, TSlot>
128
- : DeepKeysWhereValueIncludes<TFormData, TValue>
129
- : never
130
-
131
- export type FieldGroupFieldBindings<
132
- TFields extends FieldGroupFields,
133
- TFormData = any,
134
- > = {
135
- [TFieldName in keyof TFields]: FieldGroupFieldBindingForSlot<
136
- TFormData,
137
- TFields[TFieldName]
138
- >
139
- }
140
-
141
- export type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> =
142
- FieldGroupFieldsOf<TFieldGroup> extends FieldGroupFields
143
- ? FieldGroupFieldBindings<FieldGroupFieldsOf<TFieldGroup>, TFormData>
144
- : never
145
-
146
- export type FieldGroupFieldsPropName<
147
- TProps,
148
- TFieldGroup extends PreactFieldGroup<any, any>,
149
- > = {
150
- [TPropName in keyof TProps]-?: IsSame<
151
- TProps[TPropName],
152
- TFieldGroup
153
- > extends true
154
- ? TPropName
155
- : never
156
- }[keyof TProps]
157
-
158
- export type FieldGroupWithFieldsFn<
159
- TFieldGroup extends PreactFieldGroup<any, any>,
160
- > = <
161
- TProps extends object,
162
- TFieldsPropName extends FieldGroupFieldsPropName<TProps, TFieldGroup>,
163
- >(
164
- Component: (props: TProps) => CrossVersionPreactNode,
165
- fieldsPropName: TFieldsPropName,
166
- ) => <TFormData>(
167
- props: Omit<TProps, TFieldsPropName | 'form'> & {
168
- form: FieldGroupForm<FieldGroupFieldComponentsOf<TFieldGroup>, TFormData>
169
- } & {
170
- [TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<
171
- TFieldGroup,
172
- TFormData
173
- >
174
- },
175
- ) => CrossVersionPreactNode
176
-
177
- export interface FieldGroupHelper {
178
- strict: <TValue>() => StrictFieldGroupFieldSlot<TValue>
179
- loose: <TValue>() => LooseFieldGroupFieldSlot<TValue>
180
- }
181
-
182
- export interface FieldGroupDefinition<
183
- TFields extends FieldGroupFields,
184
- TFieldComponents extends Record<string, FunctionComponent<any>>,
185
- > {
186
- /** The virtual field-group API injected into the bound component. */
187
- fields: PreactFieldGroup<TFields, TFieldComponents>
188
- /** Binds a component's virtual field API to concrete paths in a form. */
189
- bindComponent: FieldGroupWithFieldsFn<
190
- PreactFieldGroup<TFields, TFieldComponents>
191
- >
192
- }
193
-
194
- /** Signature shared by `defineFieldGroup` and app-form field-group definers. */
195
- export type DefineFieldGroupFn<
196
- TFieldComponents extends Record<string, FunctionComponent<any>>,
197
- > = <const TFields extends FieldGroupFields>(
198
- defineFn: (helper: FieldGroupHelper) => TFields,
199
- ) => FieldGroupDefinition<TFields, TFieldComponents>
200
-
201
- /** Defines a reusable group of virtual fields. */
202
- export const defineFieldGroup: DefineFieldGroupFn<Record<never, never>> =
203
- defineFieldGroupRuntime as never
@@ -1,166 +0,0 @@
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
- }