@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.
- package/dist/AppForm/createFormHook.public.d.ts +6 -3
- package/dist/AppForm/createFormHook.public.js +2 -6
- package/dist/AppForm/createFormHookTypes.public.d.ts +190 -6
- package/dist/AppForm/initializeAppForm.lib.js +6 -1
- package/dist/FieldGroup/FieldGroupApi.public.d.ts +1 -1
- package/dist/PreactForm/Components.public.d.ts +2 -2
- package/dist/PreactForm/formApiTypes.public.d.ts +24 -0
- package/dist/PreactForm/formType.public.d.ts +30 -2
- package/dist/PreactForm/useField.lib.js +2 -2
- package/dist/PreactForm/useForm.public.d.ts +33 -1
- package/dist/PreactForm/useForm.public.js +32 -0
- package/dist/index.d.ts +2 -3
- package/package.json +3 -4
- package/dist/AppForm/appFormOptions.public.d.ts +0 -14
- 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
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { AppFormHookResult, CreateFormHookOptions } from "./createFormHookTypes.public.js";
|
|
2
|
+
import { FunctionComponent } from "preact/compat";
|
|
3
3
|
//#region src/AppForm/createFormHook.public.d.ts
|
|
4
|
-
declare function createFormHook<const
|
|
4
|
+
declare function createFormHook<const TFormComponents extends Record<string, FunctionComponent<any>>, const TFieldComponents extends Record<string, FunctionComponent<any>>>(createOptions: CreateFormHookOptions<TFormComponents, TFieldComponents>): AppFormHookResult<{
|
|
5
|
+
formComponents: TFormComponents;
|
|
6
|
+
fieldComponents: TFieldComponents;
|
|
7
|
+
}>;
|
|
5
8
|
//#endregion
|
|
6
9
|
export { createFormHook };
|
|
@@ -2,13 +2,9 @@ import { useInternalForm } from "../PreactForm/PreactFormApi.lib.js";
|
|
|
2
2
|
import { defineFieldGroup } from "../FieldGroup/withFields.public.js";
|
|
3
3
|
import { useFormContext } from "./contexts.lib.js";
|
|
4
4
|
import { createAppFormInitializer } from "./initializeAppForm.lib.js";
|
|
5
|
+
import { formOptions } from "@tanstack/form-core";
|
|
5
6
|
|
|
6
7
|
//#region src/AppForm/createFormHook.public.ts
|
|
7
|
-
const appFormOptions = ((opts) => {
|
|
8
|
-
return opts;
|
|
9
|
-
});
|
|
10
|
-
appFormOptions.strictSchema = (opts) => opts;
|
|
11
|
-
appFormOptions.looseSchema = (opts) => opts;
|
|
12
8
|
function createFormHook(createOptions) {
|
|
13
9
|
const initializeAppForm = createAppFormInitializer(createOptions);
|
|
14
10
|
function useExtendedForm(hookOptions) {
|
|
@@ -16,7 +12,7 @@ function createFormHook(createOptions) {
|
|
|
16
12
|
}
|
|
17
13
|
return {
|
|
18
14
|
useFormContext,
|
|
19
|
-
appFormOptions,
|
|
15
|
+
appFormOptions: formOptions,
|
|
20
16
|
defineAppFieldGroup: defineFieldGroup,
|
|
21
17
|
useAppForm: useExtendedForm
|
|
22
18
|
};
|
|
@@ -1,15 +1,199 @@
|
|
|
1
|
-
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
|
-
import { AppFormOptionsApi } from "./appFormOptions.public.js";
|
|
1
|
+
import { AnyPreactFormComponentMap, PreactFormComponentMap } from "./componentMap.public.js";
|
|
3
2
|
import { DefineFieldGroupFn } from "../FieldGroup/withFields.public.js";
|
|
4
3
|
import { PreactAppFormApi } from "./PreactAppFormApi.public.js";
|
|
5
|
-
import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
4
|
+
import { DefaultFieldOptions, DefaultFormGroupOptions, DefaultFormOptions, FormOptions, FormOptionsApi, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
5
|
+
import { FunctionComponent } from "preact/compat";
|
|
6
6
|
//#region src/AppForm/createFormHookTypes.public.d.ts
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Configures the components and reusable defaults returned by
|
|
9
|
+
* `createFormHook`.
|
|
10
|
+
*
|
|
11
|
+
* Form core resolves each default object before the corresponding usage-site
|
|
12
|
+
* options. Non-listener properties passed at the usage site take precedence,
|
|
13
|
+
* including when explicitly set to `undefined`. Listener arrays follow the
|
|
14
|
+
* configured `listenersMerge` strategy.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* const { useAppForm } = createFormHook({
|
|
19
|
+
* formComponents: {},
|
|
20
|
+
* fieldComponents: {
|
|
21
|
+
* TextField,
|
|
22
|
+
* },
|
|
23
|
+
* defaultFormOptions: {
|
|
24
|
+
* errorVisibility: ({ fieldState }) => fieldState.meta.isBlurred,
|
|
25
|
+
* },
|
|
26
|
+
* defaultFieldOptions: {
|
|
27
|
+
* errorBoundary: true,
|
|
28
|
+
* },
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @typeParam TFormComponents - Library-managed. Do not specify explicitly.
|
|
33
|
+
* @typeParam TFieldComponents - Library-managed. Do not specify explicitly.
|
|
34
|
+
*/
|
|
35
|
+
interface CreateFormHookOptions<in out TFormComponents extends Record<string, FunctionComponent<any>>, in out TFieldComponents extends Record<string, FunctionComponent<any>>> extends PreactFormComponentMap<TFormComponents, TFieldComponents> {
|
|
36
|
+
/**
|
|
37
|
+
* Defaults for every form created by `useAppForm`.
|
|
38
|
+
*
|
|
39
|
+
* Non-listener options passed to `useAppForm` override these defaults,
|
|
40
|
+
* including when explicitly set to `undefined`. Listener arrays follow
|
|
41
|
+
* `listenersMerge`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* defaultFormOptions: {
|
|
46
|
+
* errorVisibility: ({ state }) => state.submissionAttempts > 0,
|
|
47
|
+
* },
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
defaultFormOptions?: DefaultFormOptions;
|
|
51
|
+
/**
|
|
52
|
+
* Defaults for every field and array-field component owned by the form.
|
|
53
|
+
*
|
|
54
|
+
* Non-listener options passed to the component override these defaults,
|
|
55
|
+
* including when explicitly set to `undefined`. Listener arrays follow
|
|
56
|
+
* `listenersMerge`. This includes `group.Field` and `group.ArrayField`.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* defaultFieldOptions: {
|
|
61
|
+
* errorVisibility: ({ fieldState }) => fieldState.meta.isBlurred,
|
|
62
|
+
* },
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
defaultFieldOptions?: DefaultFieldOptions;
|
|
66
|
+
/**
|
|
67
|
+
* Defaults for every `form.FormGroup` component.
|
|
68
|
+
*
|
|
69
|
+
* Options passed to the component override these defaults, including when
|
|
70
|
+
* an option is explicitly `undefined`.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```tsx
|
|
74
|
+
* defaultFormGroupOptions: {
|
|
75
|
+
* onSubmitInvalid: ({ groupApi }) => {
|
|
76
|
+
* console.error('Invalid group', groupApi.name)
|
|
77
|
+
* },
|
|
78
|
+
* },
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
defaultFormGroupOptions?: DefaultFormGroupOptions;
|
|
82
|
+
}
|
|
83
|
+
type UseAppFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn, unknown>) => PreactAppFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
|
|
84
|
+
/**
|
|
85
|
+
* App Form hooks and helpers bound to the components registered with
|
|
86
|
+
* `createFormHook`.
|
|
87
|
+
*
|
|
88
|
+
* @typeParam TComponents - Library-managed. Do not specify explicitly.
|
|
89
|
+
*/
|
|
8
90
|
interface AppFormHookResult<in out TComponents extends AnyPreactFormComponentMap> {
|
|
9
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Defines reusable form options that retain the components registered with
|
|
93
|
+
* `createFormHook`.
|
|
94
|
+
*
|
|
95
|
+
* Use the result with `PreactFormType` when a child component needs the type
|
|
96
|
+
* of one known App Form, including its registered field and form components.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```tsx
|
|
100
|
+
* const profileOptions = appFormOptions({
|
|
101
|
+
* defaultValues: { name: '' },
|
|
102
|
+
* })
|
|
103
|
+
*
|
|
104
|
+
* type ProfileForm = PreactFormType<typeof profileOptions>
|
|
105
|
+
*
|
|
106
|
+
* function NameField({ form }: { form: ProfileForm }) {
|
|
107
|
+
* return (
|
|
108
|
+
* <form.Field name="name">
|
|
109
|
+
* {(field) => <field.TextField label="Name" />}
|
|
110
|
+
* </form.Field>
|
|
111
|
+
* )
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
appFormOptions: FormOptionsApi<TComponents>;
|
|
116
|
+
/**
|
|
117
|
+
* Defines a reusable field group whose fields expose the field components
|
|
118
|
+
* registered with `createFormHook`.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```tsx
|
|
122
|
+
* const { defineAppFieldGroup } = createFormHook({
|
|
123
|
+
* fieldComponents: {
|
|
124
|
+
* TextField,
|
|
125
|
+
* },
|
|
126
|
+
* formComponents: {},
|
|
127
|
+
* })
|
|
128
|
+
*
|
|
129
|
+
* const contactFields = defineAppFieldGroup(({ strict }) => ({
|
|
130
|
+
* name: strict<string>(),
|
|
131
|
+
* email: strict<string>(),
|
|
132
|
+
* }))
|
|
133
|
+
*
|
|
134
|
+
* function ContactFields({
|
|
135
|
+
* fields,
|
|
136
|
+
* }: {
|
|
137
|
+
* fields: typeof contactFields.fields
|
|
138
|
+
* }) {
|
|
139
|
+
* return (
|
|
140
|
+
* <fields.Field name="name">
|
|
141
|
+
* {(field) => <field.TextField label="Name" />}
|
|
142
|
+
* </fields.Field>
|
|
143
|
+
* )
|
|
144
|
+
* }
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
10
147
|
defineAppFieldGroup: DefineFieldGroupFn<TComponents['fieldComponents']>;
|
|
148
|
+
/**
|
|
149
|
+
* Creates a Preact form API extended with the field and form components
|
|
150
|
+
* registered with `createFormHook`.
|
|
151
|
+
*
|
|
152
|
+
* Form data, validators, and submission results are inferred from the passed
|
|
153
|
+
* options.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```tsx
|
|
157
|
+
* function ProfileForm() {
|
|
158
|
+
* const form = useAppForm({
|
|
159
|
+
* defaultValues: { name: '' },
|
|
160
|
+
* })
|
|
161
|
+
*
|
|
162
|
+
* return (
|
|
163
|
+
* <form.AppForm>
|
|
164
|
+
* <form.Field name="name">
|
|
165
|
+
* {(field) => <field.TextField label="Name" />}
|
|
166
|
+
* </form.Field>
|
|
167
|
+
* </form.AppForm>
|
|
168
|
+
* )
|
|
169
|
+
* }
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
11
172
|
useAppForm: UseAppFormHook<TComponents>;
|
|
173
|
+
/**
|
|
174
|
+
* Reads the current App Form API from the nearest `form.AppForm` provider.
|
|
175
|
+
*
|
|
176
|
+
* Use this hook inside form components registered with `createFormHook`. It
|
|
177
|
+
* throws when called outside a `form.AppForm` subtree.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```tsx
|
|
181
|
+
* function SubmitButton({ label }: { label: string }) {
|
|
182
|
+
* const form = useFormContext()
|
|
183
|
+
*
|
|
184
|
+
* return (
|
|
185
|
+
* <form.Subscribe selector={(state) => state.isSubmitting}>
|
|
186
|
+
* {(isSubmitting) => (
|
|
187
|
+
* <button type="submit" disabled={isSubmitting}>
|
|
188
|
+
* {label}
|
|
189
|
+
* </button>
|
|
190
|
+
* )}
|
|
191
|
+
* </form.Subscribe>
|
|
192
|
+
* )
|
|
193
|
+
* }
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
12
196
|
useFormContext: () => PreactAppFormApi<any, any, TComponents>;
|
|
13
197
|
}
|
|
14
198
|
//#endregion
|
|
15
|
-
export { AppFormHookResult, UseAppFormHook };
|
|
199
|
+
export { AppFormHookResult, CreateFormHookOptions, UseAppFormHook };
|
|
@@ -3,8 +3,13 @@ import { InternalFormApi } from "@tanstack/form-core/internals";
|
|
|
3
3
|
|
|
4
4
|
//#region src/AppForm/initializeAppForm.lib.ts
|
|
5
5
|
function createAppFormInitializer(createOptions) {
|
|
6
|
+
const defaultOptions = createOptions.defaultFormOptions || createOptions.defaultFieldOptions || createOptions.defaultFormGroupOptions ? {
|
|
7
|
+
form: createOptions.defaultFormOptions,
|
|
8
|
+
field: createOptions.defaultFieldOptions,
|
|
9
|
+
formGroup: createOptions.defaultFormGroupOptions
|
|
10
|
+
} : void 0;
|
|
6
11
|
return (options) => {
|
|
7
|
-
const form = new InternalFormApi(options);
|
|
12
|
+
const form = new InternalFormApi(options, defaultOptions);
|
|
8
13
|
return attachPreactAppFormComponents(form, createOptions.formComponents, createOptions.fieldComponents);
|
|
9
14
|
};
|
|
10
15
|
}
|
|
@@ -11,7 +11,7 @@ interface FieldGroupArrayFieldComponent<in out TFieldData, in out TFieldComponen
|
|
|
11
11
|
<const TFieldName extends DeepKeysWhereValueIncludes<TFieldData, Array<any>>>(props: PreactFormFieldProps<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>, FieldValidators<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>>, ValidationIssue, unknown, FormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
12
12
|
}
|
|
13
13
|
type FieldGroupSubscribeProps<TSelected> = PreactFormSubscribeProps<unknown, FormErrorTypes, TSelected>;
|
|
14
|
-
type FieldGroupSubscribeComponent = <TSelected>(props: FieldGroupSubscribeProps<TSelected>) => CrossVersionPreactNode;
|
|
14
|
+
type FieldGroupSubscribeComponent = <const TSelected>(props: FieldGroupSubscribeProps<TSelected>) => CrossVersionPreactNode;
|
|
15
15
|
interface FieldGroupApi<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>> extends FormApiFieldMethods<TFieldData>, FormApiArrayMethods<TFieldData> {
|
|
16
16
|
atom: ReadonlyAtom<TFieldData>;
|
|
17
17
|
Field: FieldGroupFieldComponent<TFieldData, TFieldComponents>;
|
|
@@ -24,7 +24,7 @@ interface PreactSubscribeProps<in out TSourceData, in out TSelected> {
|
|
|
24
24
|
children: ((state: NoInfer<TSelected>) => CrossVersionPreactNode) | CrossVersionPreactNode;
|
|
25
25
|
}
|
|
26
26
|
type PreactFormSubscribeProps<TFormData, TFormErrorTypes extends FormErrorTypes, TSelected> = PreactSubscribeProps<FormState<TFormData, TFormErrorTypes>, TSelected>;
|
|
27
|
-
type PreactFormSubscribeComponent<in out TFormData, in out TFormErrorTypes extends FormErrorTypes> = <TSelected>(props: PreactFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>) => CrossVersionPreactNode;
|
|
27
|
+
type PreactFormSubscribeComponent<in out TFormData, in out TFormErrorTypes extends FormErrorTypes> = <const TSelected>(props: PreactFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>) => CrossVersionPreactNode;
|
|
28
28
|
interface PreactFormFieldProps<in out TFieldData, in out TFieldName, in out TFieldValue, in out TFieldValidators extends FieldValidators<TFieldData, TFieldName, TFieldValue>, in out TGroupFieldError, in out TFormData, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> extends FieldApiOptions<TFieldData, TFieldName, TFieldValue, TFieldValidators, TGroupFieldError, TFormData, TFormErrorTypes> {
|
|
29
29
|
children: (fieldApi: PreactFieldApi<TFieldName, TFieldValue, ToFieldError<NoInfer<TFieldValidators>, TGroupFieldError, TFormErrorTypes>, TFormData, TFormErrorTypes, TFieldComponents>) => CrossVersionPreactNode;
|
|
30
30
|
}
|
|
@@ -35,7 +35,7 @@ type PreactFormArrayFieldComponent<in out TFormData, in out TFormErrorTypes exte
|
|
|
35
35
|
<TFieldName extends DeepKeysWhereValueIncludes<TFormData, Array<any>>, const TFieldValidators extends FieldValidators<TFormData, TFieldName, DeepValue<TFormData, TFieldName>>>(props: PreactFormFieldProps<TFormData, TFieldName, DeepValue<TFormData, TFieldName>, TFieldValidators, never, TFormData, TFormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
36
36
|
};
|
|
37
37
|
type PreactFormGroupSubscribeProps<TGroupValue, TGroupErrorTypes extends FormErrorTypes, TSelected> = PreactSubscribeProps<FormGroupState<TGroupValue, TGroupErrorTypes>, TSelected>;
|
|
38
|
-
type PreactFormGroupSubscribeComponent<in out TGroupValue, in out TGroupErrorTypes extends FormErrorTypes> = <TSelected>(props: PreactFormGroupSubscribeProps<TGroupValue, TGroupErrorTypes, TSelected>) => CrossVersionPreactNode;
|
|
38
|
+
type PreactFormGroupSubscribeComponent<in out TGroupValue, in out TGroupErrorTypes extends FormErrorTypes> = <const TSelected>(props: PreactFormGroupSubscribeProps<TGroupValue, TGroupErrorTypes, TSelected>) => CrossVersionPreactNode;
|
|
39
39
|
interface PreactFormGroupFieldComponent<in out TFormData, in out TGroupValue, in out TGroupErrorTypes extends FormErrorTypes, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
40
40
|
<TFieldName extends DeepKeys<TGroupValue>, const TFieldValidators extends FieldValidators<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>>>(props: PreactFormFieldProps<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>, TFieldValidators, TGroupErrorTypes['fieldError'], TFormData, TFormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
41
41
|
}
|
|
@@ -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 };
|
|
@@ -1,9 +1,37 @@
|
|
|
1
1
|
import { AnyPreactFormComponentMap, DefaultPreactFormComponentMap } from "../AppForm/componentMap.public.js";
|
|
2
2
|
import { PreactFormApi } from "./formApiTypes.public.js";
|
|
3
|
-
import { AppFormOptions } from "../AppForm/appFormOptions.public.js";
|
|
4
3
|
import { AnyFormOptions, FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
5
4
|
//#region src/PreactForm/formType.public.d.ts
|
|
6
5
|
type PreactFormTypeErrorTypes<TFormValidators extends FormValidators<any>, TSubmitReturn> = unknown extends TSubmitReturn ? any : ToFormErrorTypes<TFormValidators, TSubmitReturn>;
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Derives the Preact form API type represented by a reusable options object.
|
|
8
|
+
*
|
|
9
|
+
* Use it to type props for components that belong to one known form shape. It
|
|
10
|
+
* preserves the inferred form data and any components registered through
|
|
11
|
+
* `appFormOptions`. Options such as `onSubmit` can be defined either in the
|
|
12
|
+
* shared options or when the form is created in the component.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const profileOptions = formOptions({
|
|
17
|
+
* defaultValues: { name: '' },
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* type ProfileForm = PreactFormType<typeof profileOptions>
|
|
21
|
+
*
|
|
22
|
+
* function NameField(props: {
|
|
23
|
+
* form: ProfileForm
|
|
24
|
+
* }) {
|
|
25
|
+
* return (
|
|
26
|
+
* <props.form.Field name="name">
|
|
27
|
+
* {(field) => <input value={field.value} />}
|
|
28
|
+
* </props.form.Field>
|
|
29
|
+
* )
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @typeParam TOptions - The reusable form or app-form options from which the API derives its form data, error, and registered-component types.
|
|
34
|
+
*/
|
|
35
|
+
type PreactFormType<TOptions extends AnyFormOptions> = TOptions extends FormOptions<infer TFormData, infer TFormValidators, infer TSubmitReturn, infer TComponents> ? PreactFormApi<TFormData, PreactFormTypeErrorTypes<TFormValidators, TSubmitReturn>, TComponents extends AnyPreactFormComponentMap ? TComponents : DefaultPreactFormComponentMap> : never;
|
|
8
36
|
//#endregion
|
|
9
37
|
export { PreactFormType };
|
|
@@ -10,7 +10,7 @@ function useField(options, fieldComponents) {
|
|
|
10
10
|
const field = options.form._getOrCreateFieldApi({
|
|
11
11
|
...optionsRef.current,
|
|
12
12
|
name: options.name
|
|
13
|
-
});
|
|
13
|
+
}, "field");
|
|
14
14
|
if (fieldComponents === null) return field;
|
|
15
15
|
Object.assign(field, fieldComponents);
|
|
16
16
|
return field;
|
|
@@ -20,7 +20,7 @@ function useField(options, fieldComponents) {
|
|
|
20
20
|
resetVersion,
|
|
21
21
|
fieldComponents
|
|
22
22
|
]);
|
|
23
|
-
useEffect(() => fieldApi._update(options));
|
|
23
|
+
useEffect(() => fieldApi._update(options, "field"));
|
|
24
24
|
useEffect(() => {
|
|
25
25
|
return fieldApi._register();
|
|
26
26
|
}, [fieldApi]);
|
|
@@ -2,7 +2,39 @@ import { AnyPreactFormComponentMap, DefaultPreactFormComponentMap } from "../App
|
|
|
2
2
|
import { PreactFormApi } from "./formApiTypes.public.js";
|
|
3
3
|
import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
4
4
|
//#region src/PreactForm/useForm.public.d.ts
|
|
5
|
-
type UseFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => PreactFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
|
|
5
|
+
type UseFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn, unknown>) => PreactFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a Preact form whose instance and lifecycle are owned by the current
|
|
8
|
+
* component.
|
|
9
|
+
*
|
|
10
|
+
* `defaultValues` establish the initial state and inferred form value type.
|
|
11
|
+
* Later renders apply changed options to the same form instance, and unmounting
|
|
12
|
+
* the component cleans it up.
|
|
13
|
+
*
|
|
14
|
+
* Call this hook at the top level of a Preact component or custom hook.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* function ProfileForm() {
|
|
19
|
+
* const form = useForm({
|
|
20
|
+
* defaultValues: { name: '' },
|
|
21
|
+
* onSubmit: ({ value }) => saveProfile(value),
|
|
22
|
+
* })
|
|
23
|
+
*
|
|
24
|
+
* return (
|
|
25
|
+
* <form
|
|
26
|
+
* onSubmit={(event) => {
|
|
27
|
+
* event.preventDefault()
|
|
28
|
+
* void form.handleSubmit()
|
|
29
|
+
* }}
|
|
30
|
+
* />
|
|
31
|
+
* )
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @returns The Preact form API with typed field, subscription, and form-group
|
|
36
|
+
* components attached.
|
|
37
|
+
*/
|
|
6
38
|
declare const useForm: UseFormHook<DefaultPreactFormComponentMap>;
|
|
7
39
|
//#endregion
|
|
8
40
|
export { UseFormHook, useForm };
|
|
@@ -4,6 +4,38 @@ import { initializeForm, useInternalForm } from "./PreactFormApi.lib.js";
|
|
|
4
4
|
function useFormHook(options) {
|
|
5
5
|
return useInternalForm(options, initializeForm);
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Preact form whose instance and lifecycle are owned by the current
|
|
9
|
+
* component.
|
|
10
|
+
*
|
|
11
|
+
* `defaultValues` establish the initial state and inferred form value type.
|
|
12
|
+
* Later renders apply changed options to the same form instance, and unmounting
|
|
13
|
+
* the component cleans it up.
|
|
14
|
+
*
|
|
15
|
+
* Call this hook at the top level of a Preact component or custom hook.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* function ProfileForm() {
|
|
20
|
+
* const form = useForm({
|
|
21
|
+
* defaultValues: { name: '' },
|
|
22
|
+
* onSubmit: ({ value }) => saveProfile(value),
|
|
23
|
+
* })
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* <form
|
|
27
|
+
* onSubmit={(event) => {
|
|
28
|
+
* event.preventDefault()
|
|
29
|
+
* void form.handleSubmit()
|
|
30
|
+
* }}
|
|
31
|
+
* />
|
|
32
|
+
* )
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @returns The Preact form API with typed field, subscription, and form-group
|
|
37
|
+
* components attached.
|
|
38
|
+
*/
|
|
7
39
|
const useForm = useFormHook;
|
|
8
40
|
|
|
9
41
|
//#endregion
|
package/dist/index.d.ts
CHANGED
|
@@ -3,16 +3,15 @@ import { CrossVersionPreactNode } from "./preactTypes.public.js";
|
|
|
3
3
|
import { PreactFieldApi, PreactFormArrayFieldComponent, PreactFormFieldComponent, PreactFormFieldProps, PreactFormGroupApi, PreactFormGroupArrayFieldComponent, PreactFormGroupComponent, PreactFormGroupFieldComponent, PreactFormGroupProps, PreactFormGroupSubscribeComponent, PreactFormGroupSubscribeProps, PreactFormSubscribeComponent, PreactFormSubscribeProps, PreactTanStackFormComponents } from "./PreactForm/Components.public.js";
|
|
4
4
|
import { AnyPreactFormApi, PreactFormApi } from "./PreactForm/formApiTypes.public.js";
|
|
5
5
|
import { UseFormHook, useForm } from "./PreactForm/useForm.public.js";
|
|
6
|
-
import { AppFormOptions, AppFormOptionsApi } from "./AppForm/appFormOptions.public.js";
|
|
7
6
|
import { PreactFormType } from "./PreactForm/formType.public.js";
|
|
8
7
|
import { AnyFieldGroupApi, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupFieldComponent, FieldGroupFormState, FieldGroupSubscribeComponent, FieldGroupSubscribeProps } from "./FieldGroup/FieldGroupApi.public.js";
|
|
9
8
|
import { AnyFieldGroupFieldSlot, DefineFieldGroupFn, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupHelper, FieldGroupWithFieldsFn, LooseFieldGroupFieldSlot, PreactFieldGroup, StrictFieldGroupFieldSlot, defineFieldGroup } from "./FieldGroup/withFields.public.js";
|
|
10
9
|
import "./types.public.js";
|
|
11
10
|
import { Subscribe, SubscribeProps, SubscribeSource } from "./Subscribe.public.js";
|
|
12
11
|
import { AppFormComponent, PreactAppFormApi } from "./AppForm/PreactAppFormApi.public.js";
|
|
13
|
-
import { AppFormHookResult, UseAppFormHook } from "./AppForm/createFormHookTypes.public.js";
|
|
12
|
+
import { AppFormHookResult, CreateFormHookOptions, UseAppFormHook } from "./AppForm/createFormHookTypes.public.js";
|
|
14
13
|
import { createFormHook } from "./AppForm/createFormHook.public.js";
|
|
15
14
|
import { FormHookHelpers, getFormHookHelpers } from "./AppForm/getFormHookHelpers.public.js";
|
|
16
15
|
export * from "@tanstack/form-core";
|
|
17
16
|
export * from "@tanstack/preact-store";
|
|
18
|
-
export { AnyFieldGroupApi, AnyFieldGroupFieldSlot, AnyPreactFormApi, AnyPreactFormComponentMap, AppFormComponent, AppFormHookResult,
|
|
17
|
+
export { AnyFieldGroupApi, AnyFieldGroupFieldSlot, AnyPreactFormApi, AnyPreactFormComponentMap, AppFormComponent, AppFormHookResult, CreateFormHookOptions, CrossVersionPreactNode, DefaultPreactFormComponentMap, DefineFieldGroupFn, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponent, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupFormState, FieldGroupHelper, FieldGroupSubscribeComponent, FieldGroupSubscribeProps, FieldGroupWithFieldsFn, FormHookHelpers, LooseFieldGroupFieldSlot, PreactAppFormApi, PreactFieldApi, PreactFieldGroup, PreactFormApi, PreactFormArrayFieldComponent, PreactFormComponentMap, PreactFormFieldComponent, PreactFormFieldProps, PreactFormGroupApi, PreactFormGroupArrayFieldComponent, PreactFormGroupComponent, PreactFormGroupFieldComponent, PreactFormGroupProps, PreactFormGroupSubscribeComponent, PreactFormGroupSubscribeProps, PreactFormSubscribeComponent, PreactFormSubscribeProps, PreactFormType, PreactTanStackFormComponents, StrictFieldGroupFieldSlot, Subscribe, SubscribeProps, SubscribeSource, UseAppFormHook, UseFormHook, createFormHook, defineFieldGroup, getFormHookHelpers, useForm };
|
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.2",
|
|
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.2"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
41
|
"@eslint-react/eslint-plugin": "^5.18.3",
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
|
-
import { FormOptions, FormValidatorData, FormValidators, InferUnion, NullableSchemaData } from "@tanstack/form-core";
|
|
3
|
-
//#region src/AppForm/appFormOptions.public.d.ts
|
|
4
|
-
declare const componentsSymbol: unique symbol;
|
|
5
|
-
interface AppFormOptions<in out TFormData, in out TFormValidators extends FormValidators<TFormData>, in out TSubmitReturn, out TComponents extends AnyPreactFormComponentMap> extends FormOptions<TFormData, TFormValidators, TSubmitReturn> {
|
|
6
|
-
[componentsSymbol]: TComponents;
|
|
7
|
-
}
|
|
8
|
-
interface AppFormOptionsApi<in out TComponents extends AnyPreactFormComponentMap> {
|
|
9
|
-
<TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>): AppFormOptions<TFormData, TFormValidators, TSubmitReturn, TComponents>;
|
|
10
|
-
strictSchema: <const TFormValidators extends FormValidators<any>, TFormData extends FormValidatorData<TFormValidators>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => AppFormOptions<FormValidatorData<TFormValidators>, TFormValidators, TSubmitReturn, TComponents>;
|
|
11
|
-
looseSchema: <const TFormValidators extends FormValidators<any>, const TFormData extends NullableSchemaData<TFormValidators>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => AppFormOptions<InferUnion<TFormData, FormValidatorData<TFormValidators>>, TFormValidators, TSubmitReturn, TComponents>;
|
|
12
|
-
}
|
|
13
|
-
//#endregion
|
|
14
|
-
export { AppFormOptions, AppFormOptionsApi };
|