@tanstack/preact-form 2.0.0-alpha.0 → 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.
- package/dist/AppForm/createFormHookTypes.public.d.ts +108 -0
- package/dist/PreactForm/formApiTypes.public.d.ts +24 -0
- package/dist/PreactForm/formType.public.d.ts +29 -0
- package/package.json +3 -4
- package/src/AppForm/Components.lib.tsx +0 -160
- package/src/AppForm/PreactAppFormApi.public.tsx +0 -17
- package/src/AppForm/appFormOptions.public.ts +0 -59
- package/src/AppForm/componentMap.public.ts +0 -19
- package/src/AppForm/contexts.lib.ts +0 -28
- package/src/AppForm/createFormHook.public.ts +0 -37
- package/src/AppForm/createFormHookTypes.public.ts +0 -32
- package/src/AppForm/fieldComponentHelpers.lib.tsx +0 -30
- package/src/AppForm/getFormHookHelpers.public.ts +0 -114
- package/src/AppForm/initializeAppForm.lib.ts +0 -22
- package/src/FieldGroup/FieldGroupApi.public.ts +0 -91
- package/src/FieldGroup/withFields.lib.ts +0 -109
- package/src/FieldGroup/withFields.public.ts +0 -203
- package/src/PreactForm/Components.lib.tsx +0 -166
- package/src/PreactForm/Components.public.ts +0 -383
- package/src/PreactForm/PreactFormApi.lib.tsx +0 -48
- package/src/PreactForm/fieldSubscriptions.lib.ts +0 -32
- package/src/PreactForm/formApiTypes.public.ts +0 -29
- package/src/PreactForm/formType.public.ts +0 -52
- package/src/PreactForm/useField.lib.ts +0 -52
- package/src/PreactForm/useForm.public.ts +0 -34
- package/src/Subscribe.public.tsx +0 -56
- package/src/index.ts +0 -18
- package/src/preactTypes.public.ts +0 -6
- package/src/types.public.ts +0 -2
|
@@ -5,10 +5,118 @@ import { PreactAppFormApi } from "./PreactAppFormApi.public.js";
|
|
|
5
5
|
import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
6
6
|
//#region src/AppForm/createFormHookTypes.public.d.ts
|
|
7
7
|
type UseAppFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => PreactAppFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
|
|
8
|
+
/**
|
|
9
|
+
* App Form hooks and helpers bound to the components registered with
|
|
10
|
+
* `createFormHook`.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam TComponents - Library-managed. Do not specify explicitly.
|
|
13
|
+
*/
|
|
8
14
|
interface AppFormHookResult<in out TComponents extends AnyPreactFormComponentMap> {
|
|
15
|
+
/**
|
|
16
|
+
* Defines reusable form options that retain the components registered with
|
|
17
|
+
* `createFormHook`.
|
|
18
|
+
*
|
|
19
|
+
* Use the result with `PreactFormType` when a child component needs the type
|
|
20
|
+
* of one known App Form, including its registered field and form components.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* const profileOptions = appFormOptions({
|
|
25
|
+
* defaultValues: { name: '' },
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* type ProfileForm = PreactFormType<typeof profileOptions>
|
|
29
|
+
*
|
|
30
|
+
* function NameField({ form }: { form: ProfileForm }) {
|
|
31
|
+
* return (
|
|
32
|
+
* <form.Field name="name">
|
|
33
|
+
* {(field) => <field.TextField label="Name" />}
|
|
34
|
+
* </form.Field>
|
|
35
|
+
* )
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
9
39
|
appFormOptions: AppFormOptionsApi<TComponents>;
|
|
40
|
+
/**
|
|
41
|
+
* Defines a reusable field group whose fields expose the field components
|
|
42
|
+
* registered with `createFormHook`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```tsx
|
|
46
|
+
* const { defineAppFieldGroup } = createFormHook({
|
|
47
|
+
* fieldComponents: {
|
|
48
|
+
* TextField,
|
|
49
|
+
* },
|
|
50
|
+
* formComponents: {},
|
|
51
|
+
* })
|
|
52
|
+
*
|
|
53
|
+
* const contactFields = defineAppFieldGroup(({ strict }) => ({
|
|
54
|
+
* name: strict<string>(),
|
|
55
|
+
* email: strict<string>(),
|
|
56
|
+
* }))
|
|
57
|
+
*
|
|
58
|
+
* function ContactFields({
|
|
59
|
+
* fields,
|
|
60
|
+
* }: {
|
|
61
|
+
* fields: typeof contactFields.fields
|
|
62
|
+
* }) {
|
|
63
|
+
* return (
|
|
64
|
+
* <fields.Field name="name">
|
|
65
|
+
* {(field) => <field.TextField label="Name" />}
|
|
66
|
+
* </fields.Field>
|
|
67
|
+
* )
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
10
71
|
defineAppFieldGroup: DefineFieldGroupFn<TComponents['fieldComponents']>;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a Preact form API extended with the field and form components
|
|
74
|
+
* registered with `createFormHook`.
|
|
75
|
+
*
|
|
76
|
+
* Form data, validators, and submission results are inferred from the passed
|
|
77
|
+
* options.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```tsx
|
|
81
|
+
* function ProfileForm() {
|
|
82
|
+
* const form = useAppForm({
|
|
83
|
+
* defaultValues: { name: '' },
|
|
84
|
+
* })
|
|
85
|
+
*
|
|
86
|
+
* return (
|
|
87
|
+
* <form.AppForm>
|
|
88
|
+
* <form.Field name="name">
|
|
89
|
+
* {(field) => <field.TextField label="Name" />}
|
|
90
|
+
* </form.Field>
|
|
91
|
+
* </form.AppForm>
|
|
92
|
+
* )
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
11
96
|
useAppForm: UseAppFormHook<TComponents>;
|
|
97
|
+
/**
|
|
98
|
+
* Reads the current App Form API from the nearest `form.AppForm` provider.
|
|
99
|
+
*
|
|
100
|
+
* Use this hook inside form components registered with `createFormHook`. It
|
|
101
|
+
* throws when called outside a `form.AppForm` subtree.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```tsx
|
|
105
|
+
* function SubmitButton({ label }: { label: string }) {
|
|
106
|
+
* const form = useFormContext()
|
|
107
|
+
*
|
|
108
|
+
* return (
|
|
109
|
+
* <form.Subscribe selector={(state) => state.isSubmitting}>
|
|
110
|
+
* {(isSubmitting) => (
|
|
111
|
+
* <button type="submit" disabled={isSubmitting}>
|
|
112
|
+
* {label}
|
|
113
|
+
* </button>
|
|
114
|
+
* )}
|
|
115
|
+
* </form.Subscribe>
|
|
116
|
+
* )
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
12
120
|
useFormContext: () => PreactAppFormApi<any, any, TComponents>;
|
|
13
121
|
}
|
|
14
122
|
//#endregion
|
|
@@ -5,6 +5,30 @@ import { FunctionComponent } from "preact/compat";
|
|
|
5
5
|
//#region src/PreactForm/formApiTypes.public.d.ts
|
|
6
6
|
type ExtendedFormApi<TFormData, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, FunctionComponent<any>>> = FormApi<TFormData, TFormErrorTypes> & PreactTanStackFormComponents<TFormData, TFormErrorTypes, TFieldComponents>;
|
|
7
7
|
type PreactFormApi<TFormData, TFormErrorTypes extends FormErrorTypes, TComponents extends AnyPreactFormComponentMap> = unknown extends TComponents['formComponents'] ? ExtendedFormApi<TFormData, TFormErrorTypes, TComponents['fieldComponents']> : ExtendedFormApi<TFormData, TFormErrorTypes, TComponents['fieldComponents']> & TComponents['formComponents'];
|
|
8
|
+
/**
|
|
9
|
+
* A Preact form API whose form data and error types are erased.
|
|
10
|
+
*
|
|
11
|
+
* Use it for reusable Preact components that only need core form operations
|
|
12
|
+
* and the `Field`, `ArrayField`, `Subscribe`, or `FormGroup` components common
|
|
13
|
+
* to every Preact form. Field paths and values are not checked against a
|
|
14
|
+
* particular form shape; use `PreactFormType` when a component depends on one
|
|
15
|
+
* known form.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* function FormSubmitButton({ form }: { form: AnyPreactFormApi }) {
|
|
20
|
+
* return (
|
|
21
|
+
* <form.Subscribe selector={(state) => state.isSubmitting}>
|
|
22
|
+
* {(isSubmitting) => (
|
|
23
|
+
* <button type="submit" disabled={isSubmitting}>
|
|
24
|
+
* {isSubmitting ? 'Saving...' : 'Save'}
|
|
25
|
+
* </button>
|
|
26
|
+
* )}
|
|
27
|
+
* </form.Subscribe>
|
|
28
|
+
* )
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
8
32
|
type AnyPreactFormApi = AnyFormApi & PreactTanStackFormComponents<any, any, any>;
|
|
9
33
|
//#endregion
|
|
10
34
|
export { AnyPreactFormApi, PreactFormApi, type PreactFormComponentMap };
|
|
@@ -4,6 +4,35 @@ import { AppFormOptions } from "../AppForm/appFormOptions.public.js";
|
|
|
4
4
|
import { AnyFormOptions, FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
5
5
|
//#region src/PreactForm/formType.public.d.ts
|
|
6
6
|
type PreactFormTypeErrorTypes<TFormValidators extends FormValidators<any>, TSubmitReturn> = unknown extends TSubmitReturn ? any : ToFormErrorTypes<TFormValidators, TSubmitReturn>;
|
|
7
|
+
/**
|
|
8
|
+
* Derives the Preact form API type represented by a reusable options object.
|
|
9
|
+
*
|
|
10
|
+
* Use it to type props for components that belong to one known form shape. It
|
|
11
|
+
* preserves the inferred form data and any components registered through
|
|
12
|
+
* `appFormOptions`. Options such as `onSubmit` can be defined either in the
|
|
13
|
+
* shared options or when the form is created in the component.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const profileOptions = formOptions({
|
|
18
|
+
* defaultValues: { name: '' },
|
|
19
|
+
* })
|
|
20
|
+
*
|
|
21
|
+
* type ProfileForm = PreactFormType<typeof profileOptions>
|
|
22
|
+
*
|
|
23
|
+
* function NameField(props: {
|
|
24
|
+
* form: ProfileForm
|
|
25
|
+
* }) {
|
|
26
|
+
* return (
|
|
27
|
+
* <props.form.Field name="name">
|
|
28
|
+
* {(field) => <input value={field.value} />}
|
|
29
|
+
* </props.form.Field>
|
|
30
|
+
* )
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @typeParam TOptions - The reusable form or app-form options from which the API derives its form data, error, and registered-component types.
|
|
35
|
+
*/
|
|
7
36
|
type PreactFormType<TOptions extends AnyFormOptions | AppFormOptions<any, any, any, AnyPreactFormComponentMap>> = TOptions extends AppFormOptions<infer TFormData, infer TFormValidators, infer TSubmitReturn, infer TComponents> ? PreactFormApi<TFormData, PreactFormTypeErrorTypes<TFormValidators, TSubmitReturn>, TComponents> : TOptions extends FormOptions<infer TFormData, infer TFormValidators, infer TSubmitReturn> ? PreactFormApi<TFormData, PreactFormTypeErrorTypes<TFormValidators, TSubmitReturn>, DefaultPreactFormComponentMap> : never;
|
|
8
37
|
//#endregion
|
|
9
38
|
export { PreactFormType };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/preact-form",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.1",
|
|
4
4
|
"description": "Powerful, type-safe forms for Preact.",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,12 +31,11 @@
|
|
|
31
31
|
"node": ">=18"
|
|
32
32
|
},
|
|
33
33
|
"files": [
|
|
34
|
-
"dist"
|
|
35
|
-
"src"
|
|
34
|
+
"dist"
|
|
36
35
|
],
|
|
37
36
|
"dependencies": {
|
|
38
37
|
"@tanstack/preact-store": "^0.13.2",
|
|
39
|
-
"@tanstack/form-core": "2.0.0-alpha.
|
|
38
|
+
"@tanstack/form-core": "2.0.0-alpha.1"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
41
|
"@eslint-react/eslint-plugin": "^5.18.3",
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import Preact from 'preact/compat'
|
|
2
|
-
|
|
3
|
-
import { InternalFormGroupApi } from '@tanstack/form-core/internals'
|
|
4
|
-
import { attachPreactFormComponents } from '../PreactForm/Components.lib'
|
|
5
|
-
import { useField } from '../PreactForm/useField.lib'
|
|
6
|
-
import { useValueFieldSubscription } from '../PreactForm/fieldSubscriptions.lib'
|
|
7
|
-
import { Subscribe } from '../Subscribe.public'
|
|
8
|
-
import { FieldContext, FormContext } from './contexts.lib'
|
|
9
|
-
import type { AnyInternalFormApi } from '@tanstack/form-core/internals'
|
|
10
|
-
import type { FunctionComponent } from 'preact/compat'
|
|
11
|
-
import type {
|
|
12
|
-
AppFormComponent,
|
|
13
|
-
PreactAppFormApi,
|
|
14
|
-
} from './PreactAppFormApi.public'
|
|
15
|
-
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
16
|
-
import type { CrossVersionPreactNode } from '../preactTypes.public'
|
|
17
|
-
import type {
|
|
18
|
-
PreactFormFieldProps,
|
|
19
|
-
PreactFormGroupProps,
|
|
20
|
-
} from '../PreactForm/Components.public'
|
|
21
|
-
import type { InternalPreactFormApi } from '../PreactForm/PreactFormApi.lib'
|
|
22
|
-
|
|
23
|
-
type AnyPreactAppFormApi = PreactAppFormApi<any, any, AnyPreactFormComponentMap>
|
|
24
|
-
|
|
25
|
-
export function attachPreactAppFormComponents(
|
|
26
|
-
form: AnyInternalFormApi,
|
|
27
|
-
formComponents: Record<string, FunctionComponent<any>>,
|
|
28
|
-
fieldComponents: Record<string, FunctionComponent<any>>,
|
|
29
|
-
): AnyPreactAppFormApi {
|
|
30
|
-
const resultForm = attachPreactFormComponents(
|
|
31
|
-
form,
|
|
32
|
-
fieldComponents,
|
|
33
|
-
) as never as AnyPreactAppFormApi
|
|
34
|
-
resultForm.AppForm = createAppForm(form)
|
|
35
|
-
resultForm.Field = createFieldWithContext(
|
|
36
|
-
form,
|
|
37
|
-
fieldComponents,
|
|
38
|
-
) as AnyPreactAppFormApi['Field']
|
|
39
|
-
resultForm.FormGroup = createFormGroupWithContext(
|
|
40
|
-
resultForm as any,
|
|
41
|
-
) as AnyPreactAppFormApi['FormGroup']
|
|
42
|
-
|
|
43
|
-
return Object.assign(resultForm, formComponents)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function createAppForm(form: AnyInternalFormApi): AppFormComponent {
|
|
47
|
-
const AppForm: FunctionComponent<{
|
|
48
|
-
children: Exclude<CrossVersionPreactNode, Promise<any>>
|
|
49
|
-
}> = function AppFormComponent(props) {
|
|
50
|
-
// eslint-disable-next-line @eslint-react/no-context-provider
|
|
51
|
-
return <FormContext.Provider value={form as never} {...props} />
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
AppForm.displayName = 'TanStackForm.AppForm'
|
|
55
|
-
|
|
56
|
-
return AppForm
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
type AnyFieldComponent = FunctionComponent<
|
|
60
|
-
PreactFormFieldProps<any, any, any, any, never, any, any, any>
|
|
61
|
-
>
|
|
62
|
-
|
|
63
|
-
function createFieldWithContext(
|
|
64
|
-
form: AnyInternalFormApi,
|
|
65
|
-
fieldComponents: Record<string, FunctionComponent<any>>,
|
|
66
|
-
) {
|
|
67
|
-
const TanStackFormField: AnyFieldComponent = (props) => {
|
|
68
|
-
const fieldApi = useField({ ...props, form }, fieldComponents)
|
|
69
|
-
const field = useValueFieldSubscription(fieldApi)
|
|
70
|
-
|
|
71
|
-
return (
|
|
72
|
-
// eslint-disable-next-line @eslint-react/no-context-provider
|
|
73
|
-
<FieldContext.Provider value={field}>
|
|
74
|
-
{props.children(field) as never}
|
|
75
|
-
</FieldContext.Provider>
|
|
76
|
-
)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
TanStackFormField.displayName = 'TanStackForm.Field'
|
|
80
|
-
|
|
81
|
-
return TanStackFormField
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
type AnyFormGroupComponent = FunctionComponent<
|
|
85
|
-
PreactFormGroupProps<any, any, any, any, any, any>
|
|
86
|
-
>
|
|
87
|
-
|
|
88
|
-
function createFormGroupWithContext(
|
|
89
|
-
form: InternalPreactFormApi,
|
|
90
|
-
): AnyFormGroupComponent {
|
|
91
|
-
const TanStackFormGroup: AnyFormGroupComponent = (props) => {
|
|
92
|
-
const groupRef =
|
|
93
|
-
Preact.useRef<InternalFormGroupApi<any, any, any, any, any>>(null)
|
|
94
|
-
|
|
95
|
-
if (!groupRef.current) {
|
|
96
|
-
groupRef.current = attachAppFormGroupComponents(
|
|
97
|
-
new InternalFormGroupApi({ ...props, form } as never),
|
|
98
|
-
form,
|
|
99
|
-
)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
Preact.useEffect(() => groupRef.current?.update({ ...props, form }))
|
|
103
|
-
|
|
104
|
-
Preact.useEffect(() => {
|
|
105
|
-
const group = groupRef.current!
|
|
106
|
-
group.mount()
|
|
107
|
-
return () => group._cleanup()
|
|
108
|
-
}, [])
|
|
109
|
-
|
|
110
|
-
return props.children(groupRef.current as never)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
TanStackFormGroup.displayName = 'TanStackForm.FormGroup'
|
|
114
|
-
|
|
115
|
-
return TanStackFormGroup
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function attachAppFormGroupComponents(
|
|
119
|
-
group: InternalFormGroupApi<any, any, any, any, any>,
|
|
120
|
-
form: InternalPreactFormApi,
|
|
121
|
-
) {
|
|
122
|
-
type GroupWithComponents = InternalFormGroupApi<any, any, any, any, any> & {
|
|
123
|
-
Field: FunctionComponent<any>
|
|
124
|
-
ArrayField: FunctionComponent<any>
|
|
125
|
-
Subscribe: FunctionComponent<any>
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const resultGroup: GroupWithComponents = group as never
|
|
129
|
-
|
|
130
|
-
resultGroup.Field = function Field(props) {
|
|
131
|
-
return (
|
|
132
|
-
<form.Field
|
|
133
|
-
{...(group._getFormFieldOptions(props, (base, overrides) => ({
|
|
134
|
-
...base,
|
|
135
|
-
...overrides,
|
|
136
|
-
})) as any)}
|
|
137
|
-
/>
|
|
138
|
-
)
|
|
139
|
-
}
|
|
140
|
-
resultGroup.Field.displayName = 'TanStackForm.FormGroup.Field'
|
|
141
|
-
|
|
142
|
-
resultGroup.ArrayField = function ArrayField(props) {
|
|
143
|
-
return (
|
|
144
|
-
<form.ArrayField
|
|
145
|
-
{...(group._getFormFieldOptions(props, (base, overrides) => ({
|
|
146
|
-
...base,
|
|
147
|
-
...overrides,
|
|
148
|
-
})) as any)}
|
|
149
|
-
/>
|
|
150
|
-
)
|
|
151
|
-
}
|
|
152
|
-
resultGroup.ArrayField.displayName = 'TanStackForm.FormGroup.ArrayField'
|
|
153
|
-
|
|
154
|
-
resultGroup.Subscribe = (props) => {
|
|
155
|
-
return <Subscribe source={group.atom} {...props} />
|
|
156
|
-
}
|
|
157
|
-
resultGroup.Subscribe.displayName = 'TanStackForm.FormGroup.Subscribe'
|
|
158
|
-
|
|
159
|
-
return resultGroup
|
|
160
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { FormErrorTypes } from '@tanstack/form-core'
|
|
2
|
-
import type { PreactFormApi } from '../PreactForm/formApiTypes.public'
|
|
3
|
-
import type { FunctionComponent } from 'preact/compat'
|
|
4
|
-
import type { CrossVersionPreactNode } from '../preactTypes.public'
|
|
5
|
-
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
6
|
-
|
|
7
|
-
export type AppFormComponent = FunctionComponent<{
|
|
8
|
-
children: Exclude<CrossVersionPreactNode, Promise<any>>
|
|
9
|
-
}>
|
|
10
|
-
|
|
11
|
-
export type PreactAppFormApi<
|
|
12
|
-
TFormData,
|
|
13
|
-
TFormErrorTypes extends FormErrorTypes,
|
|
14
|
-
TComponents extends AnyPreactFormComponentMap,
|
|
15
|
-
> = PreactFormApi<TFormData, TFormErrorTypes, TComponents> & {
|
|
16
|
-
AppForm: AppFormComponent
|
|
17
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
FormOptions,
|
|
3
|
-
FormValidatorData,
|
|
4
|
-
FormValidators,
|
|
5
|
-
InferUnion,
|
|
6
|
-
NullableSchemaData,
|
|
7
|
-
} from '@tanstack/form-core'
|
|
8
|
-
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
9
|
-
|
|
10
|
-
declare const componentsSymbol: unique symbol
|
|
11
|
-
|
|
12
|
-
export interface AppFormOptions<
|
|
13
|
-
in out TFormData,
|
|
14
|
-
in out TFormValidators extends FormValidators<TFormData>,
|
|
15
|
-
in out TSubmitReturn,
|
|
16
|
-
out TComponents extends AnyPreactFormComponentMap,
|
|
17
|
-
> extends FormOptions<TFormData, TFormValidators, TSubmitReturn> {
|
|
18
|
-
[componentsSymbol]: TComponents
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface AppFormOptionsApi<
|
|
22
|
-
in out TComponents extends AnyPreactFormComponentMap,
|
|
23
|
-
> {
|
|
24
|
-
<
|
|
25
|
-
TFormData,
|
|
26
|
-
const TFormValidators extends FormValidators<TFormData>,
|
|
27
|
-
TSubmitReturn,
|
|
28
|
-
>(
|
|
29
|
-
options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
|
|
30
|
-
): AppFormOptions<TFormData, TFormValidators, TSubmitReturn, TComponents>
|
|
31
|
-
|
|
32
|
-
strictSchema: <
|
|
33
|
-
const TFormValidators extends FormValidators<any>,
|
|
34
|
-
// Not quite sure why, but using FormValidatorData directly in the generic breaks things.
|
|
35
|
-
// Probably something recursive going on that resolves it to `never`?
|
|
36
|
-
TFormData extends FormValidatorData<TFormValidators>,
|
|
37
|
-
TSubmitReturn,
|
|
38
|
-
>(
|
|
39
|
-
options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
|
|
40
|
-
) => AppFormOptions<
|
|
41
|
-
FormValidatorData<TFormValidators>,
|
|
42
|
-
TFormValidators,
|
|
43
|
-
TSubmitReturn,
|
|
44
|
-
TComponents
|
|
45
|
-
>
|
|
46
|
-
|
|
47
|
-
looseSchema: <
|
|
48
|
-
const TFormValidators extends FormValidators<any>,
|
|
49
|
-
const TFormData extends NullableSchemaData<TFormValidators>,
|
|
50
|
-
TSubmitReturn,
|
|
51
|
-
>(
|
|
52
|
-
options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
|
|
53
|
-
) => AppFormOptions<
|
|
54
|
-
InferUnion<TFormData, FormValidatorData<TFormValidators>>,
|
|
55
|
-
TFormValidators,
|
|
56
|
-
TSubmitReturn,
|
|
57
|
-
TComponents
|
|
58
|
-
>
|
|
59
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { FunctionComponent } from 'preact/compat'
|
|
2
|
-
|
|
3
|
-
export interface PreactFormComponentMap<
|
|
4
|
-
in out TFormComponents extends Record<string, FunctionComponent<any>>,
|
|
5
|
-
in out TFieldComponents extends Record<string, FunctionComponent<any>>,
|
|
6
|
-
> {
|
|
7
|
-
formComponents: TFormComponents
|
|
8
|
-
fieldComponents: TFieldComponents
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type AnyPreactFormComponentMap = PreactFormComponentMap<
|
|
12
|
-
Record<string, FunctionComponent<any>>,
|
|
13
|
-
Record<string, FunctionComponent<any>>
|
|
14
|
-
>
|
|
15
|
-
|
|
16
|
-
export type DefaultPreactFormComponentMap = PreactFormComponentMap<
|
|
17
|
-
Record<never, never>,
|
|
18
|
-
Record<never, never>
|
|
19
|
-
>
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { createContext, useContext } from 'preact/compat'
|
|
2
|
-
import type { AnyInternalFieldApi } from '@tanstack/form-core/internals'
|
|
3
|
-
import type { InternalPreactFormApi } from '../PreactForm/PreactFormApi.lib'
|
|
4
|
-
|
|
5
|
-
export const FormContext = createContext<InternalPreactFormApi | null>(null)
|
|
6
|
-
export const FieldContext = createContext<AnyInternalFieldApi | null>(null)
|
|
7
|
-
|
|
8
|
-
export function useFieldContext(): AnyInternalFieldApi {
|
|
9
|
-
const field = useContext(FieldContext)
|
|
10
|
-
if (field === null) {
|
|
11
|
-
throw new Error(
|
|
12
|
-
'TanStack Form: Field components must be used within a `form.Field` component.',
|
|
13
|
-
)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return field
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function useFormContext(): InternalPreactFormApi {
|
|
20
|
-
const form = useContext(FormContext)
|
|
21
|
-
if (form === null) {
|
|
22
|
-
throw new Error(
|
|
23
|
-
'TanStack Form: Form components must be used within a `form.AppForm` component.',
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return form
|
|
28
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { useInternalForm } from '../PreactForm/PreactFormApi.lib'
|
|
2
|
-
import { defineFieldGroup } from '../FieldGroup/withFields.public'
|
|
3
|
-
import { createAppFormInitializer } from './initializeAppForm.lib'
|
|
4
|
-
import { useFormContext } from './contexts.lib'
|
|
5
|
-
import type { AppFormOptionsApi } from './appFormOptions.public'
|
|
6
|
-
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
7
|
-
import type {
|
|
8
|
-
AppFormHookResult,
|
|
9
|
-
UseAppFormHook,
|
|
10
|
-
} from './createFormHookTypes.public'
|
|
11
|
-
import type { FormOptions } from '@tanstack/form-core'
|
|
12
|
-
|
|
13
|
-
const appFormOptions = ((opts) => {
|
|
14
|
-
return opts
|
|
15
|
-
}) as AppFormOptionsApi<any>
|
|
16
|
-
|
|
17
|
-
appFormOptions.strictSchema = (opts) => opts as never
|
|
18
|
-
appFormOptions.looseSchema = (opts) => opts as never
|
|
19
|
-
|
|
20
|
-
export function createFormHook<
|
|
21
|
-
const TComponents extends AnyPreactFormComponentMap,
|
|
22
|
-
>(createOptions: TComponents): AppFormHookResult<TComponents> {
|
|
23
|
-
const initializeAppForm = createAppFormInitializer(createOptions)
|
|
24
|
-
|
|
25
|
-
function useExtendedForm(hookOptions: FormOptions<any, any, any>) {
|
|
26
|
-
const form = useInternalForm(hookOptions, initializeAppForm)
|
|
27
|
-
return form
|
|
28
|
-
}
|
|
29
|
-
const useAppForm = useExtendedForm as never as UseAppFormHook<TComponents>
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
useFormContext: useFormContext as never,
|
|
33
|
-
appFormOptions,
|
|
34
|
-
defineAppFieldGroup: defineFieldGroup,
|
|
35
|
-
useAppForm,
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { AppFormOptionsApi } from './appFormOptions.public'
|
|
2
|
-
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
3
|
-
import type { PreactAppFormApi } from './PreactAppFormApi.public'
|
|
4
|
-
import type { DefineFieldGroupFn } from '../FieldGroup/withFields.public'
|
|
5
|
-
import type {
|
|
6
|
-
FormOptions,
|
|
7
|
-
FormValidators,
|
|
8
|
-
ToFormErrorTypes,
|
|
9
|
-
} from '@tanstack/form-core'
|
|
10
|
-
|
|
11
|
-
export type UseAppFormHook<
|
|
12
|
-
in out TComponents extends AnyPreactFormComponentMap,
|
|
13
|
-
> = <
|
|
14
|
-
TFormData,
|
|
15
|
-
TFormValidators extends FormValidators<TFormData>,
|
|
16
|
-
TSubmitReturn,
|
|
17
|
-
>(
|
|
18
|
-
options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
|
|
19
|
-
) => PreactAppFormApi<
|
|
20
|
-
TFormData,
|
|
21
|
-
ToFormErrorTypes<TFormValidators, TSubmitReturn>,
|
|
22
|
-
TComponents
|
|
23
|
-
>
|
|
24
|
-
|
|
25
|
-
export interface AppFormHookResult<
|
|
26
|
-
in out TComponents extends AnyPreactFormComponentMap,
|
|
27
|
-
> {
|
|
28
|
-
appFormOptions: AppFormOptionsApi<TComponents>
|
|
29
|
-
defineAppFieldGroup: DefineFieldGroupFn<TComponents['fieldComponents']>
|
|
30
|
-
useAppForm: UseAppFormHook<TComponents>
|
|
31
|
-
useFormContext: () => PreactAppFormApi<any, any, TComponents>
|
|
32
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { useFieldContext } from './contexts.lib'
|
|
2
|
-
import type { FunctionComponent } from 'preact/compat'
|
|
3
|
-
import type { CrossVersionPreactNode } from '../preactTypes.public'
|
|
4
|
-
|
|
5
|
-
type AnyFieldComponent = (props: any) => CrossVersionPreactNode
|
|
6
|
-
|
|
7
|
-
export function wrapField(
|
|
8
|
-
Component: AnyFieldComponent,
|
|
9
|
-
fieldPropKey: string,
|
|
10
|
-
): AnyFieldComponent {
|
|
11
|
-
const Wrapper: FunctionComponent<any> = function TanStackFormFieldWrapper(
|
|
12
|
-
props,
|
|
13
|
-
) {
|
|
14
|
-
const field = useFieldContext()
|
|
15
|
-
|
|
16
|
-
const newProps = { ...props, [fieldPropKey]: field }
|
|
17
|
-
|
|
18
|
-
return <Component {...newProps} />
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
Wrapper.displayName = 'TanStackForm.FieldComponent'
|
|
22
|
-
|
|
23
|
-
return Wrapper as never
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function brandComponentFactory(): (
|
|
27
|
-
component: AnyFieldComponent,
|
|
28
|
-
) => AnyFieldComponent {
|
|
29
|
-
return (component) => component
|
|
30
|
-
}
|