@tanstack/preact-form 1.33.3 → 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/Components.lib.js +3 -2
- package/dist/AppForm/PreactAppFormApi.public.d.ts +0 -1
- package/dist/AppForm/appFormOptions.public.d.ts +0 -1
- package/dist/AppForm/componentMap.public.d.ts +0 -1
- package/dist/AppForm/createFormHook.public.d.ts +0 -1
- package/dist/AppForm/createFormHook.public.js +2 -2
- package/dist/AppForm/createFormHookTypes.public.d.ts +110 -3
- package/dist/AppForm/fieldComponentHelpers.lib.js +3 -2
- package/dist/AppForm/getFormHookHelpers.public.d.ts +1 -2
- package/dist/AppForm/initializeAppForm.lib.js +2 -1
- package/dist/FieldGroup/FieldGroupApi.public.d.ts +0 -1
- package/dist/FieldGroup/withFields.lib.js +27 -63
- package/dist/FieldGroup/withFields.public.d.ts +26 -24
- package/dist/FieldGroup/withFields.public.js +4 -15
- package/dist/PreactForm/Components.lib.js +6 -4
- package/dist/PreactForm/Components.public.d.ts +2 -3
- package/dist/PreactForm/PreactFormApi.lib.js +2 -1
- package/dist/PreactForm/fieldSubscriptions.lib.js +4 -2
- package/dist/PreactForm/formApiTypes.public.d.ts +25 -2
- package/dist/PreactForm/formType.public.d.ts +30 -2
- package/dist/PreactForm/useForm.public.d.ts +0 -1
- package/dist/Subscribe.public.d.ts +0 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/preactTypes.public.d.ts +0 -2
- package/package.json +5 -6
- 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 -34
- 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 -191
- package/src/FieldGroup/withFields.public.ts +0 -221
- 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
|
@@ -27,10 +27,11 @@ function createAppForm(form) {
|
|
|
27
27
|
}
|
|
28
28
|
function createFieldWithContext(form, fieldComponents) {
|
|
29
29
|
const TanStackFormField = (props) => {
|
|
30
|
-
const
|
|
30
|
+
const fieldApi = useField({
|
|
31
31
|
...props,
|
|
32
32
|
form
|
|
33
|
-
}, fieldComponents)
|
|
33
|
+
}, fieldComponents);
|
|
34
|
+
const field = useValueFieldSubscription(fieldApi);
|
|
34
35
|
return /* @__PURE__ */ jsx(FieldContext.Provider, {
|
|
35
36
|
value: field,
|
|
36
37
|
children: props.children(field)
|
|
@@ -3,7 +3,6 @@ import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
|
3
3
|
import { PreactFormApi } from "../PreactForm/formApiTypes.public.js";
|
|
4
4
|
import { FormErrorTypes } from "@tanstack/form-core";
|
|
5
5
|
import { FunctionComponent } from "preact/compat";
|
|
6
|
-
|
|
7
6
|
//#region src/AppForm/PreactAppFormApi.public.d.ts
|
|
8
7
|
type AppFormComponent = FunctionComponent<{
|
|
9
8
|
children: Exclude<CrossVersionPreactNode, Promise<any>>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
2
|
import { FormOptions, FormValidatorData, FormValidators, InferUnion, NullableSchemaData } from "@tanstack/form-core";
|
|
3
|
-
|
|
4
3
|
//#region src/AppForm/appFormOptions.public.d.ts
|
|
5
4
|
declare const componentsSymbol: unique symbol;
|
|
6
5
|
interface AppFormOptions<in out TFormData, in out TFormValidators extends FormValidators<TFormData>, in out TSubmitReturn, out TComponents extends AnyPreactFormComponentMap> extends FormOptions<TFormData, TFormValidators, TSubmitReturn> {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { FunctionComponent } from "preact/compat";
|
|
2
|
-
|
|
3
2
|
//#region src/AppForm/componentMap.public.d.ts
|
|
4
3
|
interface PreactFormComponentMap<in out TFormComponents extends Record<string, FunctionComponent<any>>, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
5
4
|
formComponents: TFormComponents;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
2
|
import { AppFormHookResult } from "./createFormHookTypes.public.js";
|
|
3
|
-
|
|
4
3
|
//#region src/AppForm/createFormHook.public.d.ts
|
|
5
4
|
declare function createFormHook<const TComponents extends AnyPreactFormComponentMap>(createOptions: TComponents): AppFormHookResult<TComponents>;
|
|
6
5
|
//#endregion
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useInternalForm } from "../PreactForm/PreactFormApi.lib.js";
|
|
2
|
-
import {
|
|
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
5
|
|
|
@@ -17,7 +17,7 @@ function createFormHook(createOptions) {
|
|
|
17
17
|
return {
|
|
18
18
|
useFormContext,
|
|
19
19
|
appFormOptions,
|
|
20
|
-
|
|
20
|
+
defineAppFieldGroup: defineFieldGroup,
|
|
21
21
|
useAppForm: useExtendedForm
|
|
22
22
|
};
|
|
23
23
|
}
|
|
@@ -1,15 +1,122 @@
|
|
|
1
1
|
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
2
|
import { AppFormOptionsApi } from "./appFormOptions.public.js";
|
|
3
|
-
import {
|
|
3
|
+
import { DefineFieldGroupFn } from "../FieldGroup/withFields.public.js";
|
|
4
4
|
import { PreactAppFormApi } from "./PreactAppFormApi.public.js";
|
|
5
5
|
import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
6
|
-
|
|
7
6
|
//#region src/AppForm/createFormHookTypes.public.d.ts
|
|
8
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
|
+
*/
|
|
9
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
|
+
*/
|
|
10
39
|
appFormOptions: AppFormOptionsApi<TComponents>;
|
|
11
|
-
|
|
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
|
+
*/
|
|
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
|
+
*/
|
|
12
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
|
+
*/
|
|
13
120
|
useFormContext: () => PreactAppFormApi<any, any, TComponents>;
|
|
14
121
|
}
|
|
15
122
|
//#endregion
|
|
@@ -5,10 +5,11 @@ import { jsx } from "preact/jsx-runtime";
|
|
|
5
5
|
function wrapField(Component, fieldPropKey) {
|
|
6
6
|
const Wrapper = function TanStackFormFieldWrapper(props) {
|
|
7
7
|
const field = useFieldContext();
|
|
8
|
-
|
|
8
|
+
const newProps = {
|
|
9
9
|
...props,
|
|
10
10
|
[fieldPropKey]: field
|
|
11
|
-
}
|
|
11
|
+
};
|
|
12
|
+
return /* @__PURE__ */ jsx(Component, { ...newProps });
|
|
12
13
|
};
|
|
13
14
|
Wrapper.displayName = "TanStackForm.FieldComponent";
|
|
14
15
|
return Wrapper;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
2
2
|
import { FieldWithValue } from "@tanstack/form-core";
|
|
3
|
-
|
|
4
3
|
//#region src/AppForm/getFormHookHelpers.public.d.ts
|
|
5
4
|
type ExactFieldBrand<out TValue> = {
|
|
6
5
|
readonly __tanstackFieldExactType: TValue;
|
|
@@ -10,7 +9,7 @@ type AcceptsFieldBrand<out TAcceptedValue> = {
|
|
|
10
9
|
};
|
|
11
10
|
type AnyFieldComponent = (props: any) => CrossVersionPreactNode;
|
|
12
11
|
type PropsOf<TComponent> = TComponent extends ((props: infer TProps) => CrossVersionPreactNode) ? TProps : never;
|
|
13
|
-
type FieldValuePropKeys<TProps> = { [K in keyof TProps]-?: TProps[K] extends FieldWithValue<any> ? K : never }[keyof TProps];
|
|
12
|
+
type FieldValuePropKeys<TProps> = { [K in keyof TProps]-?: TProps[K] extends FieldWithValue<any> ? K : never; }[keyof TProps];
|
|
14
13
|
type FieldValueFromProp<TProp> = TProp extends FieldWithValue<infer TValue> ? TValue : never;
|
|
15
14
|
type FieldValueFromComponentProp<TProps, TFieldPropKey extends keyof TProps> = FieldValueFromProp<TProps[TFieldPropKey]>;
|
|
16
15
|
type PublicPropsReplacingField<TProps, TFieldPropKey extends keyof TProps> = Omit<TProps, TFieldPropKey>;
|
|
@@ -4,7 +4,8 @@ import { InternalFormApi } from "@tanstack/form-core/internals";
|
|
|
4
4
|
//#region src/AppForm/initializeAppForm.lib.ts
|
|
5
5
|
function createAppFormInitializer(createOptions) {
|
|
6
6
|
return (options) => {
|
|
7
|
-
|
|
7
|
+
const form = new InternalFormApi(options);
|
|
8
|
+
return attachPreactAppFormComponents(form, createOptions.formComponents, createOptions.fieldComponents);
|
|
8
9
|
};
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -3,7 +3,6 @@ import { PreactFormFieldProps, PreactFormSubscribeProps } from "../PreactForm/Co
|
|
|
3
3
|
import { DeepKeys, DeepKeysWhereValueIncludes, DeepValue, FieldValidators, FormApiArrayMethods, FormApiFieldMethods, FormErrorTypes, FormState, ValidationIssue } from "@tanstack/form-core";
|
|
4
4
|
import { ReadonlyAtom } from "@tanstack/preact-store";
|
|
5
5
|
import { FunctionComponent } from "preact/compat";
|
|
6
|
-
|
|
7
6
|
//#region src/FieldGroup/FieldGroupApi.public.d.ts
|
|
8
7
|
interface FieldGroupFieldComponent<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
9
8
|
<const TFieldName extends DeepKeys<TFieldData>>(props: PreactFormFieldProps<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>, FieldValidators<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>>, ValidationIssue, unknown, FormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
@@ -1,52 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getBy, transformFieldOptionsFieldNames } from "@tanstack/form-core/internals";
|
|
1
|
+
import { InternalFieldGroupApi, defineFieldGroupFieldsRuntime, fieldGroupHelperRuntime } from "@tanstack/form-core/internals";
|
|
3
2
|
import Preact from "preact/compat";
|
|
4
3
|
|
|
5
4
|
//#region src/FieldGroup/withFields.lib.ts
|
|
6
|
-
function
|
|
7
|
-
const dotIndex = name.indexOf(".");
|
|
8
|
-
const arrayIndex = name.indexOf("[");
|
|
9
|
-
if (dotIndex === -1) return arrayIndex === -1 ? name.length : arrayIndex;
|
|
10
|
-
if (arrayIndex === -1) return dotIndex;
|
|
11
|
-
return Math.min(dotIndex, arrayIndex);
|
|
12
|
-
}
|
|
13
|
-
function resolveFieldName(bindings, cache, fieldName) {
|
|
14
|
-
const rootEnd = getRootSegmentEnd(fieldName);
|
|
15
|
-
const rootName = fieldName.slice(0, rootEnd);
|
|
16
|
-
const binding = bindings[rootName];
|
|
17
|
-
if (binding === void 0) throw new Error(`TanStack Form: Missing field group binding for "${rootName}".`);
|
|
18
|
-
const cached = cache.get(fieldName);
|
|
19
|
-
if (cached?.binding === binding) return cached.name;
|
|
20
|
-
const resolvedName = `${binding}${fieldName.slice(rootEnd)}`;
|
|
21
|
-
cache.set(fieldName, {
|
|
22
|
-
binding,
|
|
23
|
-
name: resolvedName
|
|
24
|
-
});
|
|
25
|
-
return resolvedName;
|
|
26
|
-
}
|
|
27
|
-
function resolveFieldProps(props, resolveName) {
|
|
28
|
-
return transformFieldOptionsFieldNames(props, resolveName, (base, overrides) => ({
|
|
29
|
-
...base,
|
|
30
|
-
...overrides
|
|
31
|
-
}));
|
|
32
|
-
}
|
|
33
|
-
function createFieldGroupApi(form, bindingsRef, fieldNames) {
|
|
34
|
-
const cache = /* @__PURE__ */ new Map();
|
|
35
|
-
const resolveName = (fieldName) => resolveFieldName(bindingsRef.current, cache, fieldName);
|
|
36
|
-
const atom = createAtom(() => {
|
|
37
|
-
const nextValues = {};
|
|
38
|
-
const formValues = form._atoms.values.get();
|
|
39
|
-
for (const fieldName of fieldNames) nextValues[fieldName] = getBy(formValues, resolveName(fieldName));
|
|
40
|
-
return nextValues;
|
|
41
|
-
}, { compare: shallow });
|
|
5
|
+
function attachPreactFieldGroupComponents(group, form) {
|
|
42
6
|
const Field = (props) => {
|
|
43
7
|
const FormField = form.Field;
|
|
44
|
-
return Preact.createElement(FormField,
|
|
8
|
+
return Preact.createElement(FormField, group._getFormFieldOptions(props, (base, overrides) => ({
|
|
9
|
+
...base,
|
|
10
|
+
...overrides
|
|
11
|
+
})));
|
|
45
12
|
};
|
|
46
13
|
Field.displayName = "TanStackForm.FieldGroup.Field";
|
|
47
14
|
const ArrayField = (props) => {
|
|
48
15
|
const FormArrayField = form.ArrayField;
|
|
49
|
-
return Preact.createElement(FormArrayField,
|
|
16
|
+
return Preact.createElement(FormArrayField, group._getFormFieldOptions(props, (base, overrides) => ({
|
|
17
|
+
...base,
|
|
18
|
+
...overrides
|
|
19
|
+
})));
|
|
50
20
|
};
|
|
51
21
|
ArrayField.displayName = "TanStackForm.FieldGroup.ArrayField";
|
|
52
22
|
const Subscribe = (props) => {
|
|
@@ -54,37 +24,31 @@ function createFieldGroupApi(form, bindingsRef, fieldNames) {
|
|
|
54
24
|
return Preact.createElement(FormSubscribe, props);
|
|
55
25
|
};
|
|
56
26
|
Subscribe.displayName = "TanStackForm.FieldGroup.Subscribe";
|
|
57
|
-
return {
|
|
58
|
-
atom,
|
|
27
|
+
return Object.assign(group, {
|
|
59
28
|
Field,
|
|
60
29
|
ArrayField,
|
|
61
|
-
Subscribe
|
|
62
|
-
|
|
63
|
-
setFieldValue: (fieldName, value, options) => form.setFieldValue(resolveName(fieldName), value, options),
|
|
64
|
-
resetField: (fieldName) => form.resetField(resolveName(fieldName)),
|
|
65
|
-
swapFieldValues: (fieldName, indexA, indexB, options) => form.swapFieldValues(resolveName(fieldName), indexA, indexB, options),
|
|
66
|
-
pushFieldValue: (fieldName, value, options) => form.pushFieldValue(resolveName(fieldName), value, options),
|
|
67
|
-
insertFieldValue: (fieldName, index, value, options) => form.insertFieldValue(resolveName(fieldName), index, value, options),
|
|
68
|
-
clearFieldValues: (fieldName, options) => form.clearFieldValues(resolveName(fieldName), options),
|
|
69
|
-
removeFieldValue: (fieldName, index, options) => form.removeFieldValue(resolveName(fieldName), index, options),
|
|
70
|
-
filterFieldValues: (fieldName, predicate, options) => form.filterFieldValues(resolveName(fieldName), predicate, options)
|
|
71
|
-
};
|
|
30
|
+
Subscribe
|
|
31
|
+
});
|
|
72
32
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
33
|
+
function defineFieldGroupRuntime(defineFieldGroupFn) {
|
|
34
|
+
const fields = defineFieldGroupFieldsRuntime(defineFieldGroupFn(fieldGroupHelperRuntime));
|
|
35
|
+
return {
|
|
36
|
+
fields,
|
|
37
|
+
bindComponent: (Component, fieldsPropName) => withFieldsRuntime(fields, Component, fieldsPropName)
|
|
38
|
+
};
|
|
79
39
|
}
|
|
80
|
-
function withFieldsRuntime(
|
|
81
|
-
const fieldNames = Object.keys(
|
|
40
|
+
function withFieldsRuntime(fields, Component, fieldsPropName) {
|
|
41
|
+
const fieldNames = Object.keys(fields);
|
|
82
42
|
const FieldGroupComponent = (props) => {
|
|
83
43
|
const { form, ...restProps } = props;
|
|
84
44
|
const bindingsRef = Preact.useRef(props[fieldsPropName]);
|
|
85
45
|
if (!form) throw new Error("TanStack Form: Field groups must receive a `form` prop.");
|
|
86
46
|
bindingsRef.current = props[fieldsPropName];
|
|
87
|
-
const fieldGroupApi = Preact.useMemo(() =>
|
|
47
|
+
const fieldGroupApi = Preact.useMemo(() => attachPreactFieldGroupComponents(new InternalFieldGroupApi({
|
|
48
|
+
form,
|
|
49
|
+
fieldNames,
|
|
50
|
+
getBindings: () => bindingsRef.current
|
|
51
|
+
}), form), [form]);
|
|
88
52
|
return Component({
|
|
89
53
|
...restProps,
|
|
90
54
|
[fieldsPropName]: fieldGroupApi
|
|
@@ -95,4 +59,4 @@ function withFieldsRuntime(_fields, Component, fieldsPropName) {
|
|
|
95
59
|
}
|
|
96
60
|
|
|
97
61
|
//#endregion
|
|
98
|
-
export {
|
|
62
|
+
export { defineFieldGroupRuntime };
|
|
@@ -3,51 +3,53 @@ import { PreactTanStackFormComponents } from "../PreactForm/Components.public.js
|
|
|
3
3
|
import { FieldGroupApi } from "./FieldGroupApi.public.js";
|
|
4
4
|
import { DeepKeys, DeepKeysWhereValueIncludes, DeepValue, FormApi } from "@tanstack/form-core";
|
|
5
5
|
import { FunctionComponent } from "preact/compat";
|
|
6
|
-
|
|
7
6
|
//#region src/FieldGroup/withFields.public.d.ts
|
|
8
7
|
declare const fieldGroupFieldSlotValueSymbol: unique symbol;
|
|
9
8
|
declare const fieldGroupFieldsSymbol: unique symbol;
|
|
10
9
|
type FieldGroupFieldSlotMode = 'strict' | 'loose';
|
|
11
|
-
interface FieldGroupFieldSlot<out TValue
|
|
10
|
+
interface FieldGroupFieldSlot<out TValue, out TMode extends FieldGroupFieldSlotMode = FieldGroupFieldSlotMode> {
|
|
12
11
|
readonly mode: TMode;
|
|
13
|
-
readonly [fieldGroupFieldSlotValueSymbol]: TValue
|
|
12
|
+
readonly [fieldGroupFieldSlotValueSymbol]: TValue;
|
|
14
13
|
}
|
|
15
14
|
type AnyFieldGroupFieldSlot = FieldGroupFieldSlot<any>;
|
|
16
|
-
type StrictFieldGroupFieldSlot<TValue
|
|
17
|
-
type LooseFieldGroupFieldSlot<TValue
|
|
15
|
+
type StrictFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<TValue, 'strict'>;
|
|
16
|
+
type LooseFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<TValue, 'loose'>;
|
|
18
17
|
type FieldGroupFieldSlotValue<TSlot> = TSlot extends FieldGroupFieldSlot<infer TValue> ? TValue : never;
|
|
19
18
|
type FieldGroupFieldSlotModeOf<TSlot> = TSlot extends FieldGroupFieldSlot<any, infer TMode> ? TMode : never;
|
|
20
19
|
type IsSame<TTypeA, TTypeB> = [TTypeA] extends [TTypeB] ? [TTypeB] extends [TTypeA] ? true : false : false;
|
|
21
|
-
type FieldGroupFieldSlotAllows<TSlot, TValue
|
|
22
|
-
type FieldGroupFieldNameForSlot<TFieldData, TSlot extends AnyFieldGroupFieldSlot> = { [TFieldName in DeepKeys<TFieldData>]: FieldGroupFieldSlotAllows<TSlot, DeepValue<TFieldData, TFieldName>> extends true ? TFieldName : never }[DeepKeys<TFieldData>];
|
|
20
|
+
type FieldGroupFieldSlotAllows<TSlot, TValue> = TSlot extends FieldGroupFieldSlot<infer TAcceptedValue, infer TMode> ? TMode extends 'strict' ? IsSame<TValue, TAcceptedValue> : [TValue] extends [TAcceptedValue] ? true : false : false;
|
|
21
|
+
type FieldGroupFieldNameForSlot<TFieldData, TSlot extends AnyFieldGroupFieldSlot> = { [TFieldName in DeepKeys<TFieldData>]: FieldGroupFieldSlotAllows<TSlot, DeepValue<TFieldData, TFieldName>> extends true ? TFieldName : never; }[DeepKeys<TFieldData>];
|
|
23
22
|
type FieldGroupFields = Record<string, AnyFieldGroupFieldSlot>;
|
|
24
|
-
type FieldGroupFieldNames<TFieldData, TFields extends FieldGroupFields> = { [TFieldName in keyof TFields]: FieldGroupFieldNameForSlot<TFieldData, TFields[TFieldName]
|
|
25
|
-
type FieldGroupFieldData<TFields extends FieldGroupFields> = { [TFieldName in keyof TFields]: TFields[TFieldName] extends FieldGroupFieldSlot<infer TValue, any> ? TValue : never };
|
|
23
|
+
type FieldGroupFieldNames<TFieldData, TFields extends FieldGroupFields> = { [TFieldName in keyof TFields]: FieldGroupFieldNameForSlot<TFieldData, TFields[TFieldName]>; };
|
|
24
|
+
type FieldGroupFieldData<TFields extends FieldGroupFields> = { [TFieldName in keyof TFields]: TFields[TFieldName] extends FieldGroupFieldSlot<infer TValue, any> ? TValue : never; };
|
|
26
25
|
type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
|
|
27
26
|
readonly [fieldGroupFieldsSymbol]: infer TFields;
|
|
28
27
|
} ? TFields : never;
|
|
29
|
-
type
|
|
28
|
+
type PreactFieldGroup<TFields extends FieldGroupFields, TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>> = FieldGroupApi<FieldGroupFieldData<TFields>, TFieldComponents> & {
|
|
30
29
|
readonly [fieldGroupFieldsSymbol]: TFields;
|
|
31
30
|
};
|
|
32
|
-
type FieldGroupFieldComponentsOf<TFieldGroup> = TFieldGroup extends
|
|
31
|
+
type FieldGroupFieldComponentsOf<TFieldGroup> = TFieldGroup extends PreactFieldGroup<any, infer TFieldComponents> ? TFieldComponents : never;
|
|
33
32
|
type FieldGroupForm<TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>, TFormData = any> = FormApi<TFormData, any> & PreactTanStackFormComponents<TFormData, any, TFieldComponents>;
|
|
34
33
|
type FieldGroupFieldBindingForSlot<TFormData, TSlot extends AnyFieldGroupFieldSlot> = TSlot extends FieldGroupFieldSlot<infer TValue, infer TMode> ? TMode extends 'strict' ? FieldGroupFieldNameForSlot<TFormData, TSlot> : DeepKeysWhereValueIncludes<TFormData, TValue> : never;
|
|
35
|
-
type FieldGroupFieldBindings<TFields extends FieldGroupFields, TFormData = any> = { [TFieldName in keyof TFields]: FieldGroupFieldBindingForSlot<TFormData, TFields[TFieldName]
|
|
34
|
+
type FieldGroupFieldBindings<TFields extends FieldGroupFields, TFormData = any> = { [TFieldName in keyof TFields]: FieldGroupFieldBindingForSlot<TFormData, TFields[TFieldName]>; };
|
|
36
35
|
type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> = FieldGroupFieldsOf<TFieldGroup> extends FieldGroupFields ? FieldGroupFieldBindings<FieldGroupFieldsOf<TFieldGroup>, TFormData> : never;
|
|
37
|
-
type FieldGroupFieldsPropName<TProps, TFieldGroup extends
|
|
38
|
-
type FieldGroupWithFieldsFn
|
|
36
|
+
type FieldGroupFieldsPropName<TProps, TFieldGroup extends PreactFieldGroup<any, any>> = { [TPropName in keyof TProps]-?: IsSame<TProps[TPropName], TFieldGroup> extends true ? TPropName : never; }[keyof TProps];
|
|
37
|
+
type FieldGroupWithFieldsFn<TFieldGroup extends PreactFieldGroup<any, any>> = <TProps extends object, TFieldsPropName extends FieldGroupFieldsPropName<TProps, TFieldGroup>>(Component: (props: TProps) => CrossVersionPreactNode, fieldsPropName: TFieldsPropName) => <TFormData>(props: Omit<TProps, TFieldsPropName | 'form'> & {
|
|
39
38
|
form: FieldGroupForm<FieldGroupFieldComponentsOf<TFieldGroup>, TFormData>;
|
|
40
|
-
} & { [TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<TFieldGroup, TFormData
|
|
39
|
+
} & { [TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<TFieldGroup, TFormData>; }) => CrossVersionPreactNode;
|
|
41
40
|
interface FieldGroupHelper {
|
|
42
|
-
strict: <TValue
|
|
43
|
-
loose: <TValue
|
|
41
|
+
strict: <TValue>() => StrictFieldGroupFieldSlot<TValue>;
|
|
42
|
+
loose: <TValue>() => LooseFieldGroupFieldSlot<TValue>;
|
|
44
43
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
interface FieldGroupDefinition<TFields extends FieldGroupFields, TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
45
|
+
/** The virtual field-group API injected into the bound component. */
|
|
46
|
+
fields: PreactFieldGroup<TFields, TFieldComponents>;
|
|
47
|
+
/** Binds a component's virtual field API to concrete paths in a form. */
|
|
48
|
+
bindComponent: FieldGroupWithFieldsFn<PreactFieldGroup<TFields, TFieldComponents>>;
|
|
50
49
|
}
|
|
51
|
-
|
|
50
|
+
/** Signature shared by `defineFieldGroup` and app-form field-group definers. */
|
|
51
|
+
type DefineFieldGroupFn<TFieldComponents extends Record<string, FunctionComponent<any>>> = <const TFields extends FieldGroupFields>(defineFn: (helper: FieldGroupHelper) => TFields) => FieldGroupDefinition<TFields, TFieldComponents>;
|
|
52
|
+
/** Defines a reusable group of virtual fields. */
|
|
53
|
+
declare const defineFieldGroup: DefineFieldGroupFn<Record<never, never>>;
|
|
52
54
|
//#endregion
|
|
53
|
-
export { AnyFieldGroupFieldSlot,
|
|
55
|
+
export { 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 };
|
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineFieldGroupRuntime } from "./withFields.lib.js";
|
|
2
2
|
|
|
3
3
|
//#region src/FieldGroup/withFields.public.ts
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const withFields = withFieldsRuntime;
|
|
7
|
-
function createFieldGroupHelpers() {
|
|
8
|
-
return {
|
|
9
|
-
helper,
|
|
10
|
-
defineFields,
|
|
11
|
-
withFields
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
function getFieldGroupHelpers() {
|
|
15
|
-
return createFieldGroupHelpers();
|
|
16
|
-
}
|
|
4
|
+
/** Defines a reusable group of virtual fields. */
|
|
5
|
+
const defineFieldGroup = defineFieldGroupRuntime;
|
|
17
6
|
|
|
18
7
|
//#endregion
|
|
19
|
-
export {
|
|
8
|
+
export { defineFieldGroup };
|
|
@@ -16,10 +16,11 @@ function attachPreactFormComponents(form, fieldComponents) {
|
|
|
16
16
|
}
|
|
17
17
|
function createFieldComponent(form, fieldComponents) {
|
|
18
18
|
const TanStackFormField = (props) => {
|
|
19
|
-
const
|
|
19
|
+
const fieldApi = useField({
|
|
20
20
|
...props,
|
|
21
21
|
form
|
|
22
|
-
}, fieldComponents)
|
|
22
|
+
}, fieldComponents);
|
|
23
|
+
const field = useValueFieldSubscription(fieldApi);
|
|
23
24
|
return props.children(field);
|
|
24
25
|
};
|
|
25
26
|
TanStackFormField.displayName = "TanStackForm.Field";
|
|
@@ -27,10 +28,11 @@ function createFieldComponent(form, fieldComponents) {
|
|
|
27
28
|
}
|
|
28
29
|
function createArrayFieldComponent(form, fieldComponents) {
|
|
29
30
|
const TanStackFormArrayField = (props) => {
|
|
30
|
-
const
|
|
31
|
+
const fieldApi = useField({
|
|
31
32
|
...props,
|
|
32
33
|
form
|
|
33
|
-
}, fieldComponents)
|
|
34
|
+
}, fieldComponents);
|
|
35
|
+
const field = useArrayFieldSubscription(fieldApi);
|
|
34
36
|
return props.children(field);
|
|
35
37
|
};
|
|
36
38
|
TanStackFormArrayField.displayName = "TanStackForm.ArrayField";
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
2
2
|
import { DeepKeys, DeepKeysWhereValueIncludes, DeepValue, FieldApi, FieldApiOptions, FieldValidators, FormErrorTypes, FormGroupApi, FormGroupOptions, FormGroupState, FormGroupValidators, FormState, ToFieldError, ToFormGroupErrorTypes } from "@tanstack/form-core";
|
|
3
3
|
import { FunctionComponent, MemoExoticComponent } from "preact/compat";
|
|
4
|
-
|
|
5
4
|
//#region src/PreactForm/Components.public.d.ts
|
|
6
5
|
type ExactFieldBrand<out TValue> = {
|
|
7
6
|
readonly __tanstackFieldExactType: TValue;
|
|
@@ -11,8 +10,8 @@ type AcceptsFieldBrand<out TAcceptedValue> = {
|
|
|
11
10
|
};
|
|
12
11
|
type CompatibleFieldKey<TKey, TComponent, TTargetValue> = [TComponent] extends [ExactFieldBrand<infer TExact>] ? [TExact] extends [TTargetValue] ? [TTargetValue] extends [TExact] ? TKey : never : never : [TComponent] extends [AcceptsFieldBrand<infer TLoose>] ? [TTargetValue] extends [TLoose] ? TKey : never : TKey;
|
|
13
12
|
type UnwrapComponent<TComponent> = TComponent extends MemoExoticComponent<infer TInner> ? UnwrapComponent<TInner> : TComponent;
|
|
14
|
-
type UnwrappedFieldComponents<in out TFieldComponents extends Record<string, FunctionComponent<any>>> = { [K in keyof TFieldComponents]: UnwrapComponent<TFieldComponents[K]
|
|
15
|
-
type FilteredFieldComponents<in out TFieldComponents extends Record<string, FunctionComponent<any>>, in out TTargetValue, in out TUnwrappedFieldComponents extends { [K in keyof TFieldComponents]: any } = UnwrappedFieldComponents<TFieldComponents>> = { [K in keyof TFieldComponents as CompatibleFieldKey<K, TUnwrappedFieldComponents[K], TTargetValue>]: TFieldComponents[K] };
|
|
13
|
+
type UnwrappedFieldComponents<in out TFieldComponents extends Record<string, FunctionComponent<any>>> = { [K in keyof TFieldComponents]: UnwrapComponent<TFieldComponents[K]>; };
|
|
14
|
+
type FilteredFieldComponents<in out TFieldComponents extends Record<string, FunctionComponent<any>>, in out TTargetValue, in out TUnwrappedFieldComponents extends { [K in keyof TFieldComponents]: any; } = UnwrappedFieldComponents<TFieldComponents>> = { [K in keyof TFieldComponents as CompatibleFieldKey<K, TUnwrappedFieldComponents[K], TTargetValue>]: TFieldComponents[K]; };
|
|
16
15
|
type FieldComponentsMatchingType<TFieldComponents extends Record<string, FunctionComponent<any>>, TTargetValue> = unknown extends TTargetValue ? TFieldComponents : string extends keyof TFieldComponents ? TFieldComponents : FilteredFieldComponents<TFieldComponents, TTargetValue>;
|
|
17
16
|
type PreactFieldApi<TFieldName, TFieldValue, TFieldError, TFormData, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, FunctionComponent<any>>> = FieldApi<TFieldName, TFieldValue, TFieldError, TFormData, TFormErrorTypes> & FieldComponentsMatchingType<TFieldComponents, TFieldValue>;
|
|
18
17
|
/**
|
|
@@ -6,7 +6,8 @@ import { useEffect, useRef } from "preact/compat";
|
|
|
6
6
|
//#region src/PreactForm/PreactFormApi.lib.tsx
|
|
7
7
|
const usePreactId = Preact$1.useId ?? (() => void 0);
|
|
8
8
|
function initializeForm(options) {
|
|
9
|
-
|
|
9
|
+
const form = new InternalFormApi(options);
|
|
10
|
+
return attachPreactFormComponents(form, null);
|
|
10
11
|
}
|
|
11
12
|
function useInternalForm(options, initializeFn) {
|
|
12
13
|
const preactFormId = usePreactId();
|
|
@@ -4,21 +4,23 @@ import { useMemo } from "preact/compat";
|
|
|
4
4
|
//#region src/PreactForm/fieldSubscriptions.lib.ts
|
|
5
5
|
function useValueFieldSubscription(fieldApi) {
|
|
6
6
|
const value = useSelector(fieldApi.atom, (state) => state.value);
|
|
7
|
+
const meta = useSelector(fieldApi.atom, (state) => state.meta);
|
|
7
8
|
return useMemo(() => {
|
|
8
9
|
return Object.create(fieldApi);
|
|
9
10
|
}, [
|
|
10
11
|
fieldApi,
|
|
11
|
-
|
|
12
|
+
meta,
|
|
12
13
|
value
|
|
13
14
|
]);
|
|
14
15
|
}
|
|
15
16
|
function useArrayFieldSubscription(fieldApi) {
|
|
16
17
|
const length = useSelector(fieldApi.atom, (state) => state.value.length);
|
|
18
|
+
const arrayVersion = useSelector(fieldApi.atom, (state) => state.meta._arrayVersion);
|
|
17
19
|
return useMemo(() => {
|
|
18
20
|
return Object.create(fieldApi);
|
|
19
21
|
}, [
|
|
20
22
|
fieldApi,
|
|
21
|
-
|
|
23
|
+
arrayVersion,
|
|
22
24
|
length
|
|
23
25
|
]);
|
|
24
26
|
}
|
|
@@ -2,10 +2,33 @@ import { AnyPreactFormComponentMap, PreactFormComponentMap } from "../AppForm/co
|
|
|
2
2
|
import { PreactTanStackFormComponents } from "./Components.public.js";
|
|
3
3
|
import { AnyFormApi, FormApi, FormErrorTypes } from "@tanstack/form-core";
|
|
4
4
|
import { FunctionComponent } from "preact/compat";
|
|
5
|
-
|
|
6
5
|
//#region src/PreactForm/formApiTypes.public.d.ts
|
|
7
6
|
type ExtendedFormApi<TFormData, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, FunctionComponent<any>>> = FormApi<TFormData, TFormErrorTypes> & PreactTanStackFormComponents<TFormData, TFormErrorTypes, TFieldComponents>;
|
|
8
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
|
+
*/
|
|
9
32
|
type AnyPreactFormApi = AnyFormApi & PreactTanStackFormComponents<any, any, any>;
|
|
10
33
|
//#endregion
|
|
11
|
-
export { AnyPreactFormApi, PreactFormApi };
|
|
34
|
+
export { AnyPreactFormApi, PreactFormApi, type PreactFormComponentMap };
|
|
@@ -2,9 +2,37 @@ import { AnyPreactFormComponentMap, DefaultPreactFormComponentMap } from "../App
|
|
|
2
2
|
import { PreactFormApi } from "./formApiTypes.public.js";
|
|
3
3
|
import { AppFormOptions } from "../AppForm/appFormOptions.public.js";
|
|
4
4
|
import { AnyFormOptions, FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
5
|
-
|
|
6
5
|
//#region src/PreactForm/formType.public.d.ts
|
|
7
|
-
type PreactFormTypeErrorTypes<TFormValidators
|
|
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
|
+
*/
|
|
8
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;
|
|
9
37
|
//#endregion
|
|
10
38
|
export { PreactFormType };
|