@tanstack/preact-form 1.30.4 → 2.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AppForm/Components.lib.js +91 -0
- package/dist/AppForm/PreactAppFormApi.public.d.ts +14 -0
- package/dist/AppForm/appFormOptions.public.d.ts +14 -0
- package/dist/AppForm/componentMap.public.d.ts +10 -0
- package/dist/AppForm/contexts.lib.js +18 -0
- package/dist/AppForm/createFormHook.public.d.ts +6 -0
- package/dist/AppForm/createFormHook.public.js +26 -0
- package/dist/AppForm/createFormHookTypes.public.d.ts +15 -0
- package/dist/AppForm/fieldComponentHelpers.lib.js +22 -0
- package/dist/AppForm/getFormHookHelpers.public.d.ts +34 -0
- package/dist/AppForm/getFormHookHelpers.public.js +18 -0
- package/dist/AppForm/initializeAppForm.lib.js +13 -0
- package/dist/FieldGroup/FieldGroupApi.public.d.ts +24 -0
- package/dist/FieldGroup/withFields.lib.js +62 -0
- package/dist/FieldGroup/withFields.public.d.ts +55 -0
- package/dist/FieldGroup/withFields.public.js +8 -0
- package/dist/PreactForm/Components.lib.js +99 -0
- package/dist/PreactForm/Components.public.d.ts +66 -0
- package/dist/PreactForm/PreactFormApi.lib.js +28 -0
- package/dist/PreactForm/fieldSubscriptions.lib.js +29 -0
- package/dist/PreactForm/formApiTypes.public.d.ts +10 -0
- package/dist/PreactForm/formType.public.d.ts +9 -0
- package/dist/PreactForm/useField.lib.js +31 -0
- package/dist/PreactForm/useForm.public.d.ts +8 -0
- package/dist/PreactForm/useForm.public.js +10 -0
- package/dist/Subscribe.public.d.ts +29 -0
- package/dist/Subscribe.public.js +16 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +11 -0
- package/dist/preactTypes.public.d.ts +8 -0
- package/dist/types.public.d.ts +2 -0
- package/package.json +17 -18
- package/src/AppForm/Components.lib.tsx +160 -0
- package/src/AppForm/PreactAppFormApi.public.tsx +17 -0
- package/src/AppForm/appFormOptions.public.ts +59 -0
- package/src/AppForm/componentMap.public.ts +19 -0
- package/src/AppForm/contexts.lib.ts +28 -0
- package/src/AppForm/createFormHook.public.ts +37 -0
- package/src/AppForm/createFormHookTypes.public.ts +32 -0
- package/src/AppForm/fieldComponentHelpers.lib.tsx +30 -0
- package/src/AppForm/getFormHookHelpers.public.ts +114 -0
- package/src/AppForm/initializeAppForm.lib.ts +22 -0
- package/src/FieldGroup/FieldGroupApi.public.ts +91 -0
- package/src/FieldGroup/withFields.lib.ts +109 -0
- package/src/FieldGroup/withFields.public.ts +203 -0
- package/src/PreactForm/Components.lib.tsx +166 -0
- package/src/PreactForm/Components.public.ts +383 -0
- package/src/PreactForm/PreactFormApi.lib.tsx +48 -0
- package/src/PreactForm/fieldSubscriptions.lib.ts +32 -0
- package/src/PreactForm/formApiTypes.public.ts +29 -0
- package/src/PreactForm/formType.public.ts +52 -0
- package/src/PreactForm/useField.lib.ts +52 -0
- package/src/PreactForm/useForm.public.ts +34 -0
- package/src/Subscribe.public.tsx +56 -0
- package/src/index.ts +16 -9
- package/src/preactTypes.public.ts +6 -0
- package/src/types.public.ts +2 -0
- package/dist/cjs/createFormHook.cjs +0 -101
- package/dist/cjs/createFormHook.cjs.map +0 -1
- package/dist/cjs/createFormHook.d.cts +0 -80
- package/dist/cjs/index.cjs +0 -34
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -9
- package/dist/cjs/types.d.cts +0 -17
- package/dist/cjs/useField.cjs +0 -108
- package/dist/cjs/useField.cjs.map +0 -1
- package/dist/cjs/useField.d.cts +0 -52
- package/dist/cjs/useFieldGroup.cjs +0 -48
- package/dist/cjs/useFieldGroup.cjs.map +0 -1
- package/dist/cjs/useFieldGroup.d.cts +0 -30
- package/dist/cjs/useForm.cjs +0 -78
- package/dist/cjs/useForm.cjs.map +0 -1
- package/dist/cjs/useForm.d.cts +0 -31
- package/dist/cjs/useFormGroup.cjs +0 -163
- package/dist/cjs/useFormGroup.cjs.map +0 -1
- package/dist/cjs/useFormGroup.d.cts +0 -13
- package/dist/cjs/useFormId.cjs +0 -6
- package/dist/cjs/useFormId.cjs.map +0 -1
- package/dist/cjs/useFormId.d.cts +0 -2
- package/dist/cjs/useIsomorphicLayoutEffect.cjs +0 -6
- package/dist/cjs/useIsomorphicLayoutEffect.cjs.map +0 -1
- package/dist/cjs/useIsomorphicLayoutEffect.d.cts +0 -2
- package/dist/esm/createFormHook.d.ts +0 -80
- package/dist/esm/createFormHook.js +0 -101
- package/dist/esm/createFormHook.js.map +0 -1
- package/dist/esm/index.d.ts +0 -9
- package/dist/esm/index.js +0 -22
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/types.d.ts +0 -17
- package/dist/esm/useField.d.ts +0 -52
- package/dist/esm/useField.js +0 -108
- package/dist/esm/useField.js.map +0 -1
- package/dist/esm/useFieldGroup.d.ts +0 -30
- package/dist/esm/useFieldGroup.js +0 -48
- package/dist/esm/useFieldGroup.js.map +0 -1
- package/dist/esm/useForm.d.ts +0 -31
- package/dist/esm/useForm.js +0 -78
- package/dist/esm/useForm.js.map +0 -1
- package/dist/esm/useFormGroup.d.ts +0 -13
- package/dist/esm/useFormGroup.js +0 -163
- package/dist/esm/useFormGroup.js.map +0 -1
- package/dist/esm/useFormId.d.ts +0 -2
- package/dist/esm/useFormId.js +0 -6
- package/dist/esm/useFormId.js.map +0 -1
- package/dist/esm/useIsomorphicLayoutEffect.d.ts +0 -2
- package/dist/esm/useIsomorphicLayoutEffect.js +0 -6
- package/dist/esm/useIsomorphicLayoutEffect.js.map +0 -1
- package/src/createFormHook.tsx +0 -603
- package/src/types.ts +0 -143
- package/src/useField.tsx +0 -730
- package/src/useFieldGroup.tsx +0 -259
- package/src/useForm.tsx +0 -300
- package/src/useFormGroup.tsx +0 -741
- package/src/useFormId.ts +0 -3
- package/src/useIsomorphicLayoutEffect.ts +0 -4
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Subscribe } from "../Subscribe.public.js";
|
|
2
|
+
import { useValueFieldSubscription } from "../PreactForm/fieldSubscriptions.lib.js";
|
|
3
|
+
import { useField } from "../PreactForm/useField.lib.js";
|
|
4
|
+
import { attachPreactFormComponents } from "../PreactForm/Components.lib.js";
|
|
5
|
+
import { FieldContext, FormContext } from "./contexts.lib.js";
|
|
6
|
+
import { InternalFormGroupApi } from "@tanstack/form-core/internals";
|
|
7
|
+
import Preact from "preact/compat";
|
|
8
|
+
import { jsx } from "preact/jsx-runtime";
|
|
9
|
+
|
|
10
|
+
//#region src/AppForm/Components.lib.tsx
|
|
11
|
+
function attachPreactAppFormComponents(form, formComponents, fieldComponents) {
|
|
12
|
+
const resultForm = attachPreactFormComponents(form, fieldComponents);
|
|
13
|
+
resultForm.AppForm = createAppForm(form);
|
|
14
|
+
resultForm.Field = createFieldWithContext(form, fieldComponents);
|
|
15
|
+
resultForm.FormGroup = createFormGroupWithContext(resultForm);
|
|
16
|
+
return Object.assign(resultForm, formComponents);
|
|
17
|
+
}
|
|
18
|
+
function createAppForm(form) {
|
|
19
|
+
const AppForm = function AppFormComponent(props) {
|
|
20
|
+
return /* @__PURE__ */ jsx(FormContext.Provider, {
|
|
21
|
+
value: form,
|
|
22
|
+
...props
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
AppForm.displayName = "TanStackForm.AppForm";
|
|
26
|
+
return AppForm;
|
|
27
|
+
}
|
|
28
|
+
function createFieldWithContext(form, fieldComponents) {
|
|
29
|
+
const TanStackFormField = (props) => {
|
|
30
|
+
const fieldApi = useField({
|
|
31
|
+
...props,
|
|
32
|
+
form
|
|
33
|
+
}, fieldComponents);
|
|
34
|
+
const field = useValueFieldSubscription(fieldApi);
|
|
35
|
+
return /* @__PURE__ */ jsx(FieldContext.Provider, {
|
|
36
|
+
value: field,
|
|
37
|
+
children: props.children(field)
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
TanStackFormField.displayName = "TanStackForm.Field";
|
|
41
|
+
return TanStackFormField;
|
|
42
|
+
}
|
|
43
|
+
function createFormGroupWithContext(form) {
|
|
44
|
+
const TanStackFormGroup = (props) => {
|
|
45
|
+
const groupRef = Preact.useRef(null);
|
|
46
|
+
if (!groupRef.current) groupRef.current = attachAppFormGroupComponents(new InternalFormGroupApi({
|
|
47
|
+
...props,
|
|
48
|
+
form
|
|
49
|
+
}), form);
|
|
50
|
+
Preact.useEffect(() => groupRef.current?.update({
|
|
51
|
+
...props,
|
|
52
|
+
form
|
|
53
|
+
}));
|
|
54
|
+
Preact.useEffect(() => {
|
|
55
|
+
const group = groupRef.current;
|
|
56
|
+
group.mount();
|
|
57
|
+
return () => group._cleanup();
|
|
58
|
+
}, []);
|
|
59
|
+
return props.children(groupRef.current);
|
|
60
|
+
};
|
|
61
|
+
TanStackFormGroup.displayName = "TanStackForm.FormGroup";
|
|
62
|
+
return TanStackFormGroup;
|
|
63
|
+
}
|
|
64
|
+
function attachAppFormGroupComponents(group, form) {
|
|
65
|
+
const resultGroup = group;
|
|
66
|
+
resultGroup.Field = function Field(props) {
|
|
67
|
+
return /* @__PURE__ */ jsx(form.Field, { ...group._getFormFieldOptions(props, (base, overrides) => ({
|
|
68
|
+
...base,
|
|
69
|
+
...overrides
|
|
70
|
+
})) });
|
|
71
|
+
};
|
|
72
|
+
resultGroup.Field.displayName = "TanStackForm.FormGroup.Field";
|
|
73
|
+
resultGroup.ArrayField = function ArrayField(props) {
|
|
74
|
+
return /* @__PURE__ */ jsx(form.ArrayField, { ...group._getFormFieldOptions(props, (base, overrides) => ({
|
|
75
|
+
...base,
|
|
76
|
+
...overrides
|
|
77
|
+
})) });
|
|
78
|
+
};
|
|
79
|
+
resultGroup.ArrayField.displayName = "TanStackForm.FormGroup.ArrayField";
|
|
80
|
+
resultGroup.Subscribe = (props) => {
|
|
81
|
+
return /* @__PURE__ */ jsx(Subscribe, {
|
|
82
|
+
source: group.atom,
|
|
83
|
+
...props
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
resultGroup.Subscribe.displayName = "TanStackForm.FormGroup.Subscribe";
|
|
87
|
+
return resultGroup;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
export { attachPreactAppFormComponents };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
|
+
import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
3
|
+
import { PreactFormApi } from "../PreactForm/formApiTypes.public.js";
|
|
4
|
+
import { FormErrorTypes } from "@tanstack/form-core";
|
|
5
|
+
import { FunctionComponent } from "preact/compat";
|
|
6
|
+
//#region src/AppForm/PreactAppFormApi.public.d.ts
|
|
7
|
+
type AppFormComponent = FunctionComponent<{
|
|
8
|
+
children: Exclude<CrossVersionPreactNode, Promise<any>>;
|
|
9
|
+
}>;
|
|
10
|
+
type PreactAppFormApi<TFormData, TFormErrorTypes extends FormErrorTypes, TComponents extends AnyPreactFormComponentMap> = PreactFormApi<TFormData, TFormErrorTypes, TComponents> & {
|
|
11
|
+
AppForm: AppFormComponent;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { AppFormComponent, PreactAppFormApi };
|
|
@@ -0,0 +1,14 @@
|
|
|
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 };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FunctionComponent } from "preact/compat";
|
|
2
|
+
//#region src/AppForm/componentMap.public.d.ts
|
|
3
|
+
interface PreactFormComponentMap<in out TFormComponents extends Record<string, FunctionComponent<any>>, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
4
|
+
formComponents: TFormComponents;
|
|
5
|
+
fieldComponents: TFieldComponents;
|
|
6
|
+
}
|
|
7
|
+
type AnyPreactFormComponentMap = PreactFormComponentMap<Record<string, FunctionComponent<any>>, Record<string, FunctionComponent<any>>>;
|
|
8
|
+
type DefaultPreactFormComponentMap = PreactFormComponentMap<Record<never, never>, Record<never, never>>;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { AnyPreactFormComponentMap, DefaultPreactFormComponentMap, PreactFormComponentMap };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createContext, useContext } from "preact/compat";
|
|
2
|
+
|
|
3
|
+
//#region src/AppForm/contexts.lib.ts
|
|
4
|
+
const FormContext = createContext(null);
|
|
5
|
+
const FieldContext = createContext(null);
|
|
6
|
+
function useFieldContext() {
|
|
7
|
+
const field = useContext(FieldContext);
|
|
8
|
+
if (field === null) throw new Error("TanStack Form: Field components must be used within a `form.Field` component.");
|
|
9
|
+
return field;
|
|
10
|
+
}
|
|
11
|
+
function useFormContext() {
|
|
12
|
+
const form = useContext(FormContext);
|
|
13
|
+
if (form === null) throw new Error("TanStack Form: Form components must be used within a `form.AppForm` component.");
|
|
14
|
+
return form;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { FieldContext, FormContext, useFieldContext, useFormContext };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
|
+
import { AppFormHookResult } from "./createFormHookTypes.public.js";
|
|
3
|
+
//#region src/AppForm/createFormHook.public.d.ts
|
|
4
|
+
declare function createFormHook<const TComponents extends AnyPreactFormComponentMap>(createOptions: TComponents): AppFormHookResult<TComponents>;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { createFormHook };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useInternalForm } from "../PreactForm/PreactFormApi.lib.js";
|
|
2
|
+
import { defineFieldGroup } from "../FieldGroup/withFields.public.js";
|
|
3
|
+
import { useFormContext } from "./contexts.lib.js";
|
|
4
|
+
import { createAppFormInitializer } from "./initializeAppForm.lib.js";
|
|
5
|
+
|
|
6
|
+
//#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
|
+
function createFormHook(createOptions) {
|
|
13
|
+
const initializeAppForm = createAppFormInitializer(createOptions);
|
|
14
|
+
function useExtendedForm(hookOptions) {
|
|
15
|
+
return useInternalForm(hookOptions, initializeAppForm);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
useFormContext,
|
|
19
|
+
appFormOptions,
|
|
20
|
+
defineAppFieldGroup: defineFieldGroup,
|
|
21
|
+
useAppForm: useExtendedForm
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { createFormHook };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
|
+
import { AppFormOptionsApi } from "./appFormOptions.public.js";
|
|
3
|
+
import { DefineFieldGroupFn } from "../FieldGroup/withFields.public.js";
|
|
4
|
+
import { PreactAppFormApi } from "./PreactAppFormApi.public.js";
|
|
5
|
+
import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
6
|
+
//#region src/AppForm/createFormHookTypes.public.d.ts
|
|
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
|
+
interface AppFormHookResult<in out TComponents extends AnyPreactFormComponentMap> {
|
|
9
|
+
appFormOptions: AppFormOptionsApi<TComponents>;
|
|
10
|
+
defineAppFieldGroup: DefineFieldGroupFn<TComponents['fieldComponents']>;
|
|
11
|
+
useAppForm: UseAppFormHook<TComponents>;
|
|
12
|
+
useFormContext: () => PreactAppFormApi<any, any, TComponents>;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { AppFormHookResult, UseAppFormHook };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useFieldContext } from "./contexts.lib.js";
|
|
2
|
+
import { jsx } from "preact/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/AppForm/fieldComponentHelpers.lib.tsx
|
|
5
|
+
function wrapField(Component, fieldPropKey) {
|
|
6
|
+
const Wrapper = function TanStackFormFieldWrapper(props) {
|
|
7
|
+
const field = useFieldContext();
|
|
8
|
+
const newProps = {
|
|
9
|
+
...props,
|
|
10
|
+
[fieldPropKey]: field
|
|
11
|
+
};
|
|
12
|
+
return /* @__PURE__ */ jsx(Component, { ...newProps });
|
|
13
|
+
};
|
|
14
|
+
Wrapper.displayName = "TanStackForm.FieldComponent";
|
|
15
|
+
return Wrapper;
|
|
16
|
+
}
|
|
17
|
+
function brandComponentFactory() {
|
|
18
|
+
return (component) => component;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { brandComponentFactory, wrapField };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
2
|
+
import { FieldWithValue } from "@tanstack/form-core";
|
|
3
|
+
//#region src/AppForm/getFormHookHelpers.public.d.ts
|
|
4
|
+
type ExactFieldBrand<out TValue> = {
|
|
5
|
+
readonly __tanstackFieldExactType: TValue;
|
|
6
|
+
};
|
|
7
|
+
type AcceptsFieldBrand<out TAcceptedValue> = {
|
|
8
|
+
readonly __tanstackFieldAcceptsType: TAcceptedValue;
|
|
9
|
+
};
|
|
10
|
+
type AnyFieldComponent = (props: any) => CrossVersionPreactNode;
|
|
11
|
+
type PropsOf<TComponent> = TComponent extends ((props: infer TProps) => CrossVersionPreactNode) ? TProps : never;
|
|
12
|
+
type FieldValuePropKeys<TProps> = { [K in keyof TProps]-?: TProps[K] extends FieldWithValue<any> ? K : never; }[keyof TProps];
|
|
13
|
+
type FieldValueFromProp<TProp> = TProp extends FieldWithValue<infer TValue> ? TValue : never;
|
|
14
|
+
type FieldValueFromComponentProp<TProps, TFieldPropKey extends keyof TProps> = FieldValueFromProp<TProps[TFieldPropKey]>;
|
|
15
|
+
type PublicPropsReplacingField<TProps, TFieldPropKey extends keyof TProps> = Omit<TProps, TFieldPropKey>;
|
|
16
|
+
type StrictInjectedFieldComponent<TComponent extends AnyFieldComponent, TProps = PropsOf<TComponent>, TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>> = ((props: PublicPropsReplacingField<TProps, TFieldPropKey>) => CrossVersionPreactNode) & ExactFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>;
|
|
17
|
+
type LooseInjectedFieldComponent<TComponent extends AnyFieldComponent, TProps = PropsOf<TComponent>, TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>> = ((props: PublicPropsReplacingField<TProps, TFieldPropKey>) => CrossVersionPreactNode) & AcceptsFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>;
|
|
18
|
+
type StrictBrandedFieldComponent<TComponent extends AnyFieldComponent, TValue> = TComponent & ExactFieldBrand<TValue>;
|
|
19
|
+
type LooseBrandedFieldComponent<TComponent extends AnyFieldComponent, TAcceptedValue> = TComponent & AcceptsFieldBrand<TAcceptedValue>;
|
|
20
|
+
interface FieldComponentHelper {
|
|
21
|
+
strict: <TComponent extends AnyFieldComponent, TProps = PropsOf<TComponent>, TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>>(Component: TComponent, fieldPropKey: TFieldPropKey) => StrictInjectedFieldComponent<TComponent, TProps, TFieldPropKey>;
|
|
22
|
+
loose: <TComponent extends AnyFieldComponent, TProps = PropsOf<TComponent>, TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>>(Component: TComponent, fieldPropKey: TFieldPropKey) => LooseInjectedFieldComponent<TComponent, TProps, TFieldPropKey>;
|
|
23
|
+
}
|
|
24
|
+
interface FieldBrandHelper {
|
|
25
|
+
strict: <TValue>() => <TComponent extends AnyFieldComponent>(Component: TComponent) => StrictBrandedFieldComponent<TComponent, TValue>;
|
|
26
|
+
loose: <TValue>() => <TComponent extends AnyFieldComponent>(Component: TComponent) => LooseBrandedFieldComponent<TComponent, TValue>;
|
|
27
|
+
}
|
|
28
|
+
interface FormHookHelpers {
|
|
29
|
+
fieldBrand: FieldBrandHelper;
|
|
30
|
+
fieldComponent: FieldComponentHelper;
|
|
31
|
+
}
|
|
32
|
+
declare function getFormHookHelpers(): FormHookHelpers;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { FormHookHelpers, getFormHookHelpers };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { brandComponentFactory, wrapField } from "./fieldComponentHelpers.lib.js";
|
|
2
|
+
|
|
3
|
+
//#region src/AppForm/getFormHookHelpers.public.ts
|
|
4
|
+
function getFormHookHelpers() {
|
|
5
|
+
return {
|
|
6
|
+
fieldComponent: {
|
|
7
|
+
loose: wrapField,
|
|
8
|
+
strict: wrapField
|
|
9
|
+
},
|
|
10
|
+
fieldBrand: {
|
|
11
|
+
loose: brandComponentFactory,
|
|
12
|
+
strict: brandComponentFactory
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { getFormHookHelpers };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { attachPreactAppFormComponents } from "./Components.lib.js";
|
|
2
|
+
import { InternalFormApi } from "@tanstack/form-core/internals";
|
|
3
|
+
|
|
4
|
+
//#region src/AppForm/initializeAppForm.lib.ts
|
|
5
|
+
function createAppFormInitializer(createOptions) {
|
|
6
|
+
return (options) => {
|
|
7
|
+
const form = new InternalFormApi(options);
|
|
8
|
+
return attachPreactAppFormComponents(form, createOptions.formComponents, createOptions.fieldComponents);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { createAppFormInitializer };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
2
|
+
import { PreactFormFieldProps, PreactFormSubscribeProps } from "../PreactForm/Components.public.js";
|
|
3
|
+
import { DeepKeys, DeepKeysWhereValueIncludes, DeepValue, FieldValidators, FormApiArrayMethods, FormApiFieldMethods, FormErrorTypes, FormState, ValidationIssue } from "@tanstack/form-core";
|
|
4
|
+
import { ReadonlyAtom } from "@tanstack/preact-store";
|
|
5
|
+
import { FunctionComponent } from "preact/compat";
|
|
6
|
+
//#region src/FieldGroup/FieldGroupApi.public.d.ts
|
|
7
|
+
interface FieldGroupFieldComponent<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
8
|
+
<const TFieldName extends DeepKeys<TFieldData>>(props: PreactFormFieldProps<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>, FieldValidators<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>>, ValidationIssue, unknown, FormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
9
|
+
}
|
|
10
|
+
interface FieldGroupArrayFieldComponent<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
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
|
+
}
|
|
13
|
+
type FieldGroupSubscribeProps<TSelected> = PreactFormSubscribeProps<unknown, FormErrorTypes, TSelected>;
|
|
14
|
+
type FieldGroupSubscribeComponent = <TSelected>(props: FieldGroupSubscribeProps<TSelected>) => CrossVersionPreactNode;
|
|
15
|
+
interface FieldGroupApi<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>> extends FormApiFieldMethods<TFieldData>, FormApiArrayMethods<TFieldData> {
|
|
16
|
+
atom: ReadonlyAtom<TFieldData>;
|
|
17
|
+
Field: FieldGroupFieldComponent<TFieldData, TFieldComponents>;
|
|
18
|
+
ArrayField: FieldGroupArrayFieldComponent<TFieldData, TFieldComponents>;
|
|
19
|
+
Subscribe: FieldGroupSubscribeComponent;
|
|
20
|
+
}
|
|
21
|
+
type AnyFieldGroupApi = FieldGroupApi<any, Record<string, FunctionComponent<any>>>;
|
|
22
|
+
type FieldGroupFormState = FormState<unknown, FormErrorTypes>;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { AnyFieldGroupApi, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupFieldComponent, FieldGroupFormState, FieldGroupSubscribeComponent, FieldGroupSubscribeProps };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { InternalFieldGroupApi, defineFieldGroupFieldsRuntime, fieldGroupHelperRuntime } from "@tanstack/form-core/internals";
|
|
2
|
+
import Preact from "preact/compat";
|
|
3
|
+
|
|
4
|
+
//#region src/FieldGroup/withFields.lib.ts
|
|
5
|
+
function attachPreactFieldGroupComponents(group, form) {
|
|
6
|
+
const Field = (props) => {
|
|
7
|
+
const FormField = form.Field;
|
|
8
|
+
return Preact.createElement(FormField, group._getFormFieldOptions(props, (base, overrides) => ({
|
|
9
|
+
...base,
|
|
10
|
+
...overrides
|
|
11
|
+
})));
|
|
12
|
+
};
|
|
13
|
+
Field.displayName = "TanStackForm.FieldGroup.Field";
|
|
14
|
+
const ArrayField = (props) => {
|
|
15
|
+
const FormArrayField = form.ArrayField;
|
|
16
|
+
return Preact.createElement(FormArrayField, group._getFormFieldOptions(props, (base, overrides) => ({
|
|
17
|
+
...base,
|
|
18
|
+
...overrides
|
|
19
|
+
})));
|
|
20
|
+
};
|
|
21
|
+
ArrayField.displayName = "TanStackForm.FieldGroup.ArrayField";
|
|
22
|
+
const Subscribe = (props) => {
|
|
23
|
+
const FormSubscribe = form.Subscribe;
|
|
24
|
+
return Preact.createElement(FormSubscribe, props);
|
|
25
|
+
};
|
|
26
|
+
Subscribe.displayName = "TanStackForm.FieldGroup.Subscribe";
|
|
27
|
+
return Object.assign(group, {
|
|
28
|
+
Field,
|
|
29
|
+
ArrayField,
|
|
30
|
+
Subscribe
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function defineFieldGroupRuntime(defineFieldGroupFn) {
|
|
34
|
+
const fields = defineFieldGroupFieldsRuntime(defineFieldGroupFn(fieldGroupHelperRuntime));
|
|
35
|
+
return {
|
|
36
|
+
fields,
|
|
37
|
+
bindComponent: (Component, fieldsPropName) => withFieldsRuntime(fields, Component, fieldsPropName)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function withFieldsRuntime(fields, Component, fieldsPropName) {
|
|
41
|
+
const fieldNames = Object.keys(fields);
|
|
42
|
+
const FieldGroupComponent = (props) => {
|
|
43
|
+
const { form, ...restProps } = props;
|
|
44
|
+
const bindingsRef = Preact.useRef(props[fieldsPropName]);
|
|
45
|
+
if (!form) throw new Error("TanStack Form: Field groups must receive a `form` prop.");
|
|
46
|
+
bindingsRef.current = props[fieldsPropName];
|
|
47
|
+
const fieldGroupApi = Preact.useMemo(() => attachPreactFieldGroupComponents(new InternalFieldGroupApi({
|
|
48
|
+
form,
|
|
49
|
+
fieldNames,
|
|
50
|
+
getBindings: () => bindingsRef.current
|
|
51
|
+
}), form), [form]);
|
|
52
|
+
return Component({
|
|
53
|
+
...restProps,
|
|
54
|
+
[fieldsPropName]: fieldGroupApi
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
FieldGroupComponent.displayName = "TanStackForm.FieldGroup";
|
|
58
|
+
return FieldGroupComponent;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { defineFieldGroupRuntime };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
2
|
+
import { PreactTanStackFormComponents } from "../PreactForm/Components.public.js";
|
|
3
|
+
import { FieldGroupApi } from "./FieldGroupApi.public.js";
|
|
4
|
+
import { DeepKeys, DeepKeysWhereValueIncludes, DeepValue, FormApi } from "@tanstack/form-core";
|
|
5
|
+
import { FunctionComponent } from "preact/compat";
|
|
6
|
+
//#region src/FieldGroup/withFields.public.d.ts
|
|
7
|
+
declare const fieldGroupFieldSlotValueSymbol: unique symbol;
|
|
8
|
+
declare const fieldGroupFieldsSymbol: unique symbol;
|
|
9
|
+
type FieldGroupFieldSlotMode = 'strict' | 'loose';
|
|
10
|
+
interface FieldGroupFieldSlot<out TValue, out TMode extends FieldGroupFieldSlotMode = FieldGroupFieldSlotMode> {
|
|
11
|
+
readonly mode: TMode;
|
|
12
|
+
readonly [fieldGroupFieldSlotValueSymbol]: TValue;
|
|
13
|
+
}
|
|
14
|
+
type AnyFieldGroupFieldSlot = FieldGroupFieldSlot<any>;
|
|
15
|
+
type StrictFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<TValue, 'strict'>;
|
|
16
|
+
type LooseFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<TValue, 'loose'>;
|
|
17
|
+
type FieldGroupFieldSlotValue<TSlot> = TSlot extends FieldGroupFieldSlot<infer TValue> ? TValue : never;
|
|
18
|
+
type FieldGroupFieldSlotModeOf<TSlot> = TSlot extends FieldGroupFieldSlot<any, infer TMode> ? TMode : never;
|
|
19
|
+
type IsSame<TTypeA, TTypeB> = [TTypeA] extends [TTypeB] ? [TTypeB] extends [TTypeA] ? true : false : false;
|
|
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>];
|
|
22
|
+
type FieldGroupFields = Record<string, AnyFieldGroupFieldSlot>;
|
|
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; };
|
|
25
|
+
type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
|
|
26
|
+
readonly [fieldGroupFieldsSymbol]: infer TFields;
|
|
27
|
+
} ? TFields : never;
|
|
28
|
+
type PreactFieldGroup<TFields extends FieldGroupFields, TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>> = FieldGroupApi<FieldGroupFieldData<TFields>, TFieldComponents> & {
|
|
29
|
+
readonly [fieldGroupFieldsSymbol]: TFields;
|
|
30
|
+
};
|
|
31
|
+
type FieldGroupFieldComponentsOf<TFieldGroup> = TFieldGroup extends PreactFieldGroup<any, infer TFieldComponents> ? TFieldComponents : never;
|
|
32
|
+
type FieldGroupForm<TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>, TFormData = any> = FormApi<TFormData, any> & PreactTanStackFormComponents<TFormData, any, TFieldComponents>;
|
|
33
|
+
type FieldGroupFieldBindingForSlot<TFormData, TSlot extends AnyFieldGroupFieldSlot> = TSlot extends FieldGroupFieldSlot<infer TValue, infer TMode> ? TMode extends 'strict' ? FieldGroupFieldNameForSlot<TFormData, TSlot> : DeepKeysWhereValueIncludes<TFormData, TValue> : never;
|
|
34
|
+
type FieldGroupFieldBindings<TFields extends FieldGroupFields, TFormData = any> = { [TFieldName in keyof TFields]: FieldGroupFieldBindingForSlot<TFormData, TFields[TFieldName]>; };
|
|
35
|
+
type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> = FieldGroupFieldsOf<TFieldGroup> extends FieldGroupFields ? FieldGroupFieldBindings<FieldGroupFieldsOf<TFieldGroup>, TFormData> : never;
|
|
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'> & {
|
|
38
|
+
form: FieldGroupForm<FieldGroupFieldComponentsOf<TFieldGroup>, TFormData>;
|
|
39
|
+
} & { [TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<TFieldGroup, TFormData>; }) => CrossVersionPreactNode;
|
|
40
|
+
interface FieldGroupHelper {
|
|
41
|
+
strict: <TValue>() => StrictFieldGroupFieldSlot<TValue>;
|
|
42
|
+
loose: <TValue>() => LooseFieldGroupFieldSlot<TValue>;
|
|
43
|
+
}
|
|
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>>;
|
|
49
|
+
}
|
|
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>>;
|
|
54
|
+
//#endregion
|
|
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 };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Subscribe } from "../Subscribe.public.js";
|
|
2
|
+
import { useArrayFieldSubscription, useValueFieldSubscription } from "./fieldSubscriptions.lib.js";
|
|
3
|
+
import { useField } from "./useField.lib.js";
|
|
4
|
+
import { InternalFormGroupApi } from "@tanstack/form-core/internals";
|
|
5
|
+
import Preact, { useEffect } from "preact/compat";
|
|
6
|
+
import { jsx } from "preact/jsx-runtime";
|
|
7
|
+
|
|
8
|
+
//#region src/PreactForm/Components.lib.tsx
|
|
9
|
+
function attachPreactFormComponents(form, fieldComponents) {
|
|
10
|
+
const resultForm = form;
|
|
11
|
+
resultForm.Field = createFieldComponent(form, fieldComponents);
|
|
12
|
+
resultForm.ArrayField = createArrayFieldComponent(form, fieldComponents);
|
|
13
|
+
resultForm.Subscribe = createSubscribeComponent(form);
|
|
14
|
+
resultForm.FormGroup = createFormGroupComponent(resultForm);
|
|
15
|
+
return resultForm;
|
|
16
|
+
}
|
|
17
|
+
function createFieldComponent(form, fieldComponents) {
|
|
18
|
+
const TanStackFormField = (props) => {
|
|
19
|
+
const fieldApi = useField({
|
|
20
|
+
...props,
|
|
21
|
+
form
|
|
22
|
+
}, fieldComponents);
|
|
23
|
+
const field = useValueFieldSubscription(fieldApi);
|
|
24
|
+
return props.children(field);
|
|
25
|
+
};
|
|
26
|
+
TanStackFormField.displayName = "TanStackForm.Field";
|
|
27
|
+
return TanStackFormField;
|
|
28
|
+
}
|
|
29
|
+
function createArrayFieldComponent(form, fieldComponents) {
|
|
30
|
+
const TanStackFormArrayField = (props) => {
|
|
31
|
+
const fieldApi = useField({
|
|
32
|
+
...props,
|
|
33
|
+
form
|
|
34
|
+
}, fieldComponents);
|
|
35
|
+
const field = useArrayFieldSubscription(fieldApi);
|
|
36
|
+
return props.children(field);
|
|
37
|
+
};
|
|
38
|
+
TanStackFormArrayField.displayName = "TanStackForm.ArrayField";
|
|
39
|
+
return TanStackFormArrayField;
|
|
40
|
+
}
|
|
41
|
+
function createSubscribeComponent(form) {
|
|
42
|
+
const TanStackFormSubscribe = (props) => {
|
|
43
|
+
return /* @__PURE__ */ jsx(Subscribe, {
|
|
44
|
+
source: form.atom,
|
|
45
|
+
...props
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
TanStackFormSubscribe.displayName = "TanStackForm.Subscribe";
|
|
49
|
+
return TanStackFormSubscribe;
|
|
50
|
+
}
|
|
51
|
+
function createFormGroupComponent(form) {
|
|
52
|
+
const TanStackFormGroup = (props) => {
|
|
53
|
+
const groupRef = Preact.useRef(null);
|
|
54
|
+
if (!groupRef.current) groupRef.current = attachPreactFormGroupComponents(new InternalFormGroupApi({
|
|
55
|
+
...props,
|
|
56
|
+
form
|
|
57
|
+
}), form);
|
|
58
|
+
useEffect(() => groupRef.current?.update({
|
|
59
|
+
...props,
|
|
60
|
+
form
|
|
61
|
+
}));
|
|
62
|
+
Preact.useEffect(() => {
|
|
63
|
+
const group = groupRef.current;
|
|
64
|
+
group.mount();
|
|
65
|
+
return () => group._cleanup();
|
|
66
|
+
}, []);
|
|
67
|
+
return props.children(groupRef.current);
|
|
68
|
+
};
|
|
69
|
+
TanStackFormGroup.displayName = "TanStackForm.FormGroup";
|
|
70
|
+
return TanStackFormGroup;
|
|
71
|
+
}
|
|
72
|
+
function attachPreactFormGroupComponents(group, form) {
|
|
73
|
+
const resultGroup = group;
|
|
74
|
+
resultGroup.Field = function Field(props) {
|
|
75
|
+
return /* @__PURE__ */ jsx(form.Field, { ...group._getFormFieldOptions(props, (base, overrides) => ({
|
|
76
|
+
...base,
|
|
77
|
+
...overrides
|
|
78
|
+
})) });
|
|
79
|
+
};
|
|
80
|
+
resultGroup.Field.displayName = "TanStackForm.FormGroup.Field";
|
|
81
|
+
resultGroup.ArrayField = function ArrayField(props) {
|
|
82
|
+
return /* @__PURE__ */ jsx(form.ArrayField, { ...group._getFormFieldOptions(props, (base, overrides) => ({
|
|
83
|
+
...base,
|
|
84
|
+
...overrides
|
|
85
|
+
})) });
|
|
86
|
+
};
|
|
87
|
+
resultGroup.ArrayField.displayName = "TanStackForm.FormGroup.ArrayField";
|
|
88
|
+
resultGroup.Subscribe = (props) => {
|
|
89
|
+
return /* @__PURE__ */ jsx(Subscribe, {
|
|
90
|
+
source: group.atom,
|
|
91
|
+
...props
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
resultGroup.Subscribe.displayName = "TanStackForm.FormGroup.Subscribe";
|
|
95
|
+
return resultGroup;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
export { attachPreactFormComponents };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
2
|
+
import { DeepKeys, DeepKeysWhereValueIncludes, DeepValue, FieldApi, FieldApiOptions, FieldValidators, FormErrorTypes, FormGroupApi, FormGroupOptions, FormGroupState, FormGroupValidators, FormState, ToFieldError, ToFormGroupErrorTypes } from "@tanstack/form-core";
|
|
3
|
+
import { FunctionComponent, MemoExoticComponent } from "preact/compat";
|
|
4
|
+
//#region src/PreactForm/Components.public.d.ts
|
|
5
|
+
type ExactFieldBrand<out TValue> = {
|
|
6
|
+
readonly __tanstackFieldExactType: TValue;
|
|
7
|
+
};
|
|
8
|
+
type AcceptsFieldBrand<out TAcceptedValue> = {
|
|
9
|
+
readonly __tanstackFieldAcceptsType: TAcceptedValue;
|
|
10
|
+
};
|
|
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;
|
|
12
|
+
type UnwrapComponent<TComponent> = TComponent extends MemoExoticComponent<infer TInner> ? UnwrapComponent<TInner> : TComponent;
|
|
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]; };
|
|
15
|
+
type FieldComponentsMatchingType<TFieldComponents extends Record<string, FunctionComponent<any>>, TTargetValue> = unknown extends TTargetValue ? TFieldComponents : string extends keyof TFieldComponents ? TFieldComponents : FilteredFieldComponents<TFieldComponents, TTargetValue>;
|
|
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>;
|
|
17
|
+
/**
|
|
18
|
+
* Subscribe to `form.atom` (full form state). The selector receives the full
|
|
19
|
+
* {@link FormState}.
|
|
20
|
+
*/
|
|
21
|
+
interface PreactSubscribeProps<in out TSourceData, in out TSelected> {
|
|
22
|
+
selector: (state: TSourceData) => TSelected;
|
|
23
|
+
when?: (selected: NoInfer<TSelected>) => boolean;
|
|
24
|
+
children: ((state: NoInfer<TSelected>) => CrossVersionPreactNode) | CrossVersionPreactNode;
|
|
25
|
+
}
|
|
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;
|
|
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
|
+
children: (fieldApi: PreactFieldApi<TFieldName, TFieldValue, ToFieldError<NoInfer<TFieldValidators>, TGroupFieldError, TFormErrorTypes>, TFormData, TFormErrorTypes, TFieldComponents>) => CrossVersionPreactNode;
|
|
30
|
+
}
|
|
31
|
+
type PreactFormFieldComponent<in out TFormData, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> = {
|
|
32
|
+
<TFieldName extends DeepKeys<TFormData>, const TFieldValidators extends FieldValidators<TFormData, TFieldName, DeepValue<TFormData, TFieldName>>>(props: PreactFormFieldProps<TFormData, TFieldName, DeepValue<TFormData, TFieldName>, TFieldValidators, never, TFormData, TFormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
33
|
+
};
|
|
34
|
+
type PreactFormArrayFieldComponent<in out TFormData, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> = {
|
|
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
|
+
};
|
|
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;
|
|
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
|
+
<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
|
+
}
|
|
42
|
+
type PreactFormGroupArrayFieldComponent<in out TFormData, in out TGroupValue, in out TGroupErrorTypes extends FormErrorTypes, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> = {
|
|
43
|
+
<TFieldName extends DeepKeysWhereValueIncludes<TGroupValue, Array<any>>, const TFieldValidators extends FieldValidators<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>>>(props: PreactFormFieldProps<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>, TFieldValidators, TGroupErrorTypes['fieldError'], TFormData, TFormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
44
|
+
};
|
|
45
|
+
interface PreactFormGroupApi<in out TFormData, in out TGroupName, in out TGroupValue, in out TGroupErrorTypes extends FormErrorTypes, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> extends FormGroupApi<TFormData, TGroupName, TGroupValue, TGroupErrorTypes, TFormErrorTypes> {
|
|
46
|
+
Field: PreactFormGroupFieldComponent<TFormData, TGroupValue, TGroupErrorTypes, TFormErrorTypes, TFieldComponents>;
|
|
47
|
+
ArrayField: PreactFormGroupArrayFieldComponent<TFormData, TGroupValue, TGroupErrorTypes, TFormErrorTypes, TFieldComponents>;
|
|
48
|
+
Subscribe: PreactFormGroupSubscribeComponent<TGroupValue, TGroupErrorTypes>;
|
|
49
|
+
}
|
|
50
|
+
interface PreactFormGroupProps<in out TFormData, in out TGroupName, in out TGroupValue, in out TGroupValidators extends FormGroupValidators<TGroupValue>, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> extends Omit<FormGroupOptions<TFormData, TGroupName, TGroupValue, TGroupValidators, TFormErrorTypes>, 'form'> {
|
|
51
|
+
children: (groupApi: PreactFormGroupApi<TFormData, TGroupName, TGroupValue, ToFormGroupErrorTypes<NoInfer<TGroupValidators>>, TFormErrorTypes, TFieldComponents>) => CrossVersionPreactNode;
|
|
52
|
+
}
|
|
53
|
+
interface PreactFormGroupComponent<in out TFormData, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
54
|
+
<TGroupName extends DeepKeys<TFormData>, TGroupValue extends DeepValue<TFormData, TGroupName>, const TGroupValidators extends FormGroupValidators<TGroupValue>>(props: PreactFormGroupProps<TFormData, TGroupName, TGroupValue, TGroupValidators, TFormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
55
|
+
}
|
|
56
|
+
interface PreactTanStackFormComponents<in out TFormData, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
57
|
+
/**
|
|
58
|
+
* TODO docs
|
|
59
|
+
*/
|
|
60
|
+
Field: PreactFormFieldComponent<TFormData, TFormErrorTypes, TFieldComponents>;
|
|
61
|
+
ArrayField: PreactFormArrayFieldComponent<TFormData, TFormErrorTypes, TFieldComponents>;
|
|
62
|
+
Subscribe: PreactFormSubscribeComponent<TFormData, TFormErrorTypes>;
|
|
63
|
+
FormGroup: PreactFormGroupComponent<TFormData, TFormErrorTypes, TFieldComponents>;
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
export { PreactFieldApi, PreactFormArrayFieldComponent, PreactFormFieldComponent, PreactFormFieldProps, PreactFormGroupApi, PreactFormGroupArrayFieldComponent, PreactFormGroupComponent, PreactFormGroupFieldComponent, PreactFormGroupProps, PreactFormGroupSubscribeComponent, PreactFormGroupSubscribeProps, PreactFormSubscribeComponent, PreactFormSubscribeProps, PreactTanStackFormComponents };
|