@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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,383 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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'
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|