@tanstack/preact-form 1.30.3 → 1.33.3
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/README.md +15 -1
- package/dist/AppForm/Components.lib.js +90 -0
- package/dist/AppForm/PreactAppFormApi.public.d.ts +15 -0
- package/dist/AppForm/appFormOptions.public.d.ts +15 -0
- package/dist/AppForm/componentMap.public.d.ts +11 -0
- package/dist/AppForm/contexts.lib.js +18 -0
- package/dist/AppForm/createFormHook.public.d.ts +7 -0
- package/dist/AppForm/createFormHook.public.js +26 -0
- package/dist/AppForm/createFormHookTypes.public.d.ts +16 -0
- package/dist/AppForm/fieldComponentHelpers.lib.js +21 -0
- package/dist/AppForm/getFormHookHelpers.public.d.ts +35 -0
- package/dist/AppForm/getFormHookHelpers.public.js +18 -0
- package/dist/AppForm/initializeAppForm.lib.js +12 -0
- package/dist/FieldGroup/FieldGroupApi.public.d.ts +25 -0
- package/dist/FieldGroup/withFields.lib.js +98 -0
- package/dist/FieldGroup/withFields.public.d.ts +53 -0
- package/dist/FieldGroup/withFields.public.js +19 -0
- package/dist/PreactForm/Components.lib.js +97 -0
- package/dist/PreactForm/Components.public.d.ts +67 -0
- package/dist/PreactForm/PreactFormApi.lib.js +27 -0
- package/dist/PreactForm/fieldSubscriptions.lib.js +27 -0
- package/dist/PreactForm/formApiTypes.public.d.ts +11 -0
- package/dist/PreactForm/formType.public.d.ts +10 -0
- package/dist/PreactForm/useField.lib.js +31 -0
- package/dist/PreactForm/useForm.public.d.ts +9 -0
- package/dist/PreactForm/useForm.public.js +10 -0
- package/dist/Subscribe.public.d.ts +30 -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 +10 -0
- package/dist/types.public.d.ts +2 -0
- package/package.json +15 -16
- 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 +34 -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 +191 -0
- package/src/FieldGroup/withFields.public.ts +221 -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
package/README.md
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
<img src="https://static.scarf.sh/a.png?x-pxid=be2d8a11-9712-4c1d-9963-580b2d4fb133" />
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<picture>
|
|
4
|
+
<source
|
|
5
|
+
media="(prefers-color-scheme: dark)"
|
|
6
|
+
srcset="https://tanstack.com/api/readme/form.png?framework=preact&theme=dark"
|
|
7
|
+
/>
|
|
8
|
+
<source
|
|
9
|
+
media="(prefers-color-scheme: light)"
|
|
10
|
+
srcset="https://tanstack.com/api/readme/form.png?framework=preact"
|
|
11
|
+
/>
|
|
12
|
+
<img
|
|
13
|
+
src="https://tanstack.com/api/readme/form.png?framework=preact"
|
|
14
|
+
alt="TanStack Preact Form"
|
|
15
|
+
width="900"
|
|
16
|
+
/>
|
|
17
|
+
</picture>
|
|
4
18
|
|
|
5
19
|
Hooks for managing form state in Preact
|
|
6
20
|
|
|
@@ -0,0 +1,90 @@
|
|
|
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 field = useValueFieldSubscription(useField({
|
|
31
|
+
...props,
|
|
32
|
+
form
|
|
33
|
+
}, fieldComponents));
|
|
34
|
+
return /* @__PURE__ */ jsx(FieldContext.Provider, {
|
|
35
|
+
value: field,
|
|
36
|
+
children: props.children(field)
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
TanStackFormField.displayName = "TanStackForm.Field";
|
|
40
|
+
return TanStackFormField;
|
|
41
|
+
}
|
|
42
|
+
function createFormGroupWithContext(form) {
|
|
43
|
+
const TanStackFormGroup = (props) => {
|
|
44
|
+
const groupRef = Preact.useRef(null);
|
|
45
|
+
if (!groupRef.current) groupRef.current = attachAppFormGroupComponents(new InternalFormGroupApi({
|
|
46
|
+
...props,
|
|
47
|
+
form
|
|
48
|
+
}), form);
|
|
49
|
+
Preact.useEffect(() => groupRef.current?.update({
|
|
50
|
+
...props,
|
|
51
|
+
form
|
|
52
|
+
}));
|
|
53
|
+
Preact.useEffect(() => {
|
|
54
|
+
const group = groupRef.current;
|
|
55
|
+
group.mount();
|
|
56
|
+
return () => group._cleanup();
|
|
57
|
+
}, []);
|
|
58
|
+
return props.children(groupRef.current);
|
|
59
|
+
};
|
|
60
|
+
TanStackFormGroup.displayName = "TanStackForm.FormGroup";
|
|
61
|
+
return TanStackFormGroup;
|
|
62
|
+
}
|
|
63
|
+
function attachAppFormGroupComponents(group, form) {
|
|
64
|
+
const resultGroup = group;
|
|
65
|
+
resultGroup.Field = function Field(props) {
|
|
66
|
+
return /* @__PURE__ */ jsx(form.Field, { ...group._getFormFieldOptions(props, (base, overrides) => ({
|
|
67
|
+
...base,
|
|
68
|
+
...overrides
|
|
69
|
+
})) });
|
|
70
|
+
};
|
|
71
|
+
resultGroup.Field.displayName = "TanStackForm.FormGroup.Field";
|
|
72
|
+
resultGroup.ArrayField = function ArrayField(props) {
|
|
73
|
+
return /* @__PURE__ */ jsx(form.ArrayField, { ...group._getFormFieldOptions(props, (base, overrides) => ({
|
|
74
|
+
...base,
|
|
75
|
+
...overrides
|
|
76
|
+
})) });
|
|
77
|
+
};
|
|
78
|
+
resultGroup.ArrayField.displayName = "TanStackForm.FormGroup.ArrayField";
|
|
79
|
+
resultGroup.Subscribe = (props) => {
|
|
80
|
+
return /* @__PURE__ */ jsx(Subscribe, {
|
|
81
|
+
source: group.atom,
|
|
82
|
+
...props
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
resultGroup.Subscribe.displayName = "TanStackForm.FormGroup.Subscribe";
|
|
86
|
+
return resultGroup;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
//#endregion
|
|
90
|
+
export { attachPreactAppFormComponents };
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
|
|
7
|
+
//#region src/AppForm/PreactAppFormApi.public.d.ts
|
|
8
|
+
type AppFormComponent = FunctionComponent<{
|
|
9
|
+
children: Exclude<CrossVersionPreactNode, Promise<any>>;
|
|
10
|
+
}>;
|
|
11
|
+
type PreactAppFormApi<TFormData, TFormErrorTypes extends FormErrorTypes, TComponents extends AnyPreactFormComponentMap> = PreactFormApi<TFormData, TFormErrorTypes, TComponents> & {
|
|
12
|
+
AppForm: AppFormComponent;
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { AppFormComponent, PreactAppFormApi };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
|
+
import { FormOptions, FormValidatorData, FormValidators, InferUnion, NullableSchemaData } from "@tanstack/form-core";
|
|
3
|
+
|
|
4
|
+
//#region src/AppForm/appFormOptions.public.d.ts
|
|
5
|
+
declare const componentsSymbol: unique symbol;
|
|
6
|
+
interface AppFormOptions<in out TFormData, in out TFormValidators extends FormValidators<TFormData>, in out TSubmitReturn, out TComponents extends AnyPreactFormComponentMap> extends FormOptions<TFormData, TFormValidators, TSubmitReturn> {
|
|
7
|
+
[componentsSymbol]: TComponents;
|
|
8
|
+
}
|
|
9
|
+
interface AppFormOptionsApi<in out TComponents extends AnyPreactFormComponentMap> {
|
|
10
|
+
<TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>): AppFormOptions<TFormData, TFormValidators, TSubmitReturn, TComponents>;
|
|
11
|
+
strictSchema: <const TFormValidators extends FormValidators<any>, TFormData extends FormValidatorData<TFormValidators>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => AppFormOptions<FormValidatorData<TFormValidators>, TFormValidators, TSubmitReturn, TComponents>;
|
|
12
|
+
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>;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { AppFormOptions, AppFormOptionsApi };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FunctionComponent } from "preact/compat";
|
|
2
|
+
|
|
3
|
+
//#region src/AppForm/componentMap.public.d.ts
|
|
4
|
+
interface PreactFormComponentMap<in out TFormComponents extends Record<string, FunctionComponent<any>>, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
5
|
+
formComponents: TFormComponents;
|
|
6
|
+
fieldComponents: TFieldComponents;
|
|
7
|
+
}
|
|
8
|
+
type AnyPreactFormComponentMap = PreactFormComponentMap<Record<string, FunctionComponent<any>>, Record<string, FunctionComponent<any>>>;
|
|
9
|
+
type DefaultPreactFormComponentMap = PreactFormComponentMap<Record<never, never>, Record<never, never>>;
|
|
10
|
+
//#endregion
|
|
11
|
+
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,7 @@
|
|
|
1
|
+
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
|
+
import { AppFormHookResult } from "./createFormHookTypes.public.js";
|
|
3
|
+
|
|
4
|
+
//#region src/AppForm/createFormHook.public.d.ts
|
|
5
|
+
declare function createFormHook<const TComponents extends AnyPreactFormComponentMap>(createOptions: TComponents): AppFormHookResult<TComponents>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { createFormHook };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useInternalForm } from "../PreactForm/PreactFormApi.lib.js";
|
|
2
|
+
import { getFieldGroupHelpers } 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
|
+
getAppFieldGroupHelpers: getFieldGroupHelpers,
|
|
21
|
+
useAppForm: useExtendedForm
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { createFormHook };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AnyPreactFormComponentMap } from "./componentMap.public.js";
|
|
2
|
+
import { AppFormOptionsApi } from "./appFormOptions.public.js";
|
|
3
|
+
import { FieldGroupHelpers } from "../FieldGroup/withFields.public.js";
|
|
4
|
+
import { PreactAppFormApi } from "./PreactAppFormApi.public.js";
|
|
5
|
+
import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
6
|
+
|
|
7
|
+
//#region src/AppForm/createFormHookTypes.public.d.ts
|
|
8
|
+
type UseAppFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => PreactAppFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
|
|
9
|
+
interface AppFormHookResult<in out TComponents extends AnyPreactFormComponentMap> {
|
|
10
|
+
appFormOptions: AppFormOptionsApi<TComponents>;
|
|
11
|
+
getAppFieldGroupHelpers: () => FieldGroupHelpers<TComponents['fieldComponents']>;
|
|
12
|
+
useAppForm: UseAppFormHook<TComponents>;
|
|
13
|
+
useFormContext: () => PreactAppFormApi<any, any, TComponents>;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { AppFormHookResult, UseAppFormHook };
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
return /* @__PURE__ */ jsx(Component, {
|
|
9
|
+
...props,
|
|
10
|
+
[fieldPropKey]: field
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
Wrapper.displayName = "TanStackForm.FieldComponent";
|
|
14
|
+
return Wrapper;
|
|
15
|
+
}
|
|
16
|
+
function brandComponentFactory() {
|
|
17
|
+
return (component) => component;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { brandComponentFactory, wrapField };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { CrossVersionPreactNode } from "../preactTypes.public.js";
|
|
2
|
+
import { FieldWithValue } from "@tanstack/form-core";
|
|
3
|
+
|
|
4
|
+
//#region src/AppForm/getFormHookHelpers.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 AnyFieldComponent = (props: any) => CrossVersionPreactNode;
|
|
12
|
+
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];
|
|
14
|
+
type FieldValueFromProp<TProp> = TProp extends FieldWithValue<infer TValue> ? TValue : never;
|
|
15
|
+
type FieldValueFromComponentProp<TProps, TFieldPropKey extends keyof TProps> = FieldValueFromProp<TProps[TFieldPropKey]>;
|
|
16
|
+
type PublicPropsReplacingField<TProps, TFieldPropKey extends keyof TProps> = Omit<TProps, TFieldPropKey>;
|
|
17
|
+
type StrictInjectedFieldComponent<TComponent extends AnyFieldComponent, TProps = PropsOf<TComponent>, TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>> = ((props: PublicPropsReplacingField<TProps, TFieldPropKey>) => CrossVersionPreactNode) & ExactFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>;
|
|
18
|
+
type LooseInjectedFieldComponent<TComponent extends AnyFieldComponent, TProps = PropsOf<TComponent>, TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>> = ((props: PublicPropsReplacingField<TProps, TFieldPropKey>) => CrossVersionPreactNode) & AcceptsFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>;
|
|
19
|
+
type StrictBrandedFieldComponent<TComponent extends AnyFieldComponent, TValue> = TComponent & ExactFieldBrand<TValue>;
|
|
20
|
+
type LooseBrandedFieldComponent<TComponent extends AnyFieldComponent, TAcceptedValue> = TComponent & AcceptsFieldBrand<TAcceptedValue>;
|
|
21
|
+
interface FieldComponentHelper {
|
|
22
|
+
strict: <TComponent extends AnyFieldComponent, TProps = PropsOf<TComponent>, TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>>(Component: TComponent, fieldPropKey: TFieldPropKey) => StrictInjectedFieldComponent<TComponent, TProps, TFieldPropKey>;
|
|
23
|
+
loose: <TComponent extends AnyFieldComponent, TProps = PropsOf<TComponent>, TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>>(Component: TComponent, fieldPropKey: TFieldPropKey) => LooseInjectedFieldComponent<TComponent, TProps, TFieldPropKey>;
|
|
24
|
+
}
|
|
25
|
+
interface FieldBrandHelper {
|
|
26
|
+
strict: <TValue>() => <TComponent extends AnyFieldComponent>(Component: TComponent) => StrictBrandedFieldComponent<TComponent, TValue>;
|
|
27
|
+
loose: <TValue>() => <TComponent extends AnyFieldComponent>(Component: TComponent) => LooseBrandedFieldComponent<TComponent, TValue>;
|
|
28
|
+
}
|
|
29
|
+
interface FormHookHelpers {
|
|
30
|
+
fieldBrand: FieldBrandHelper;
|
|
31
|
+
fieldComponent: FieldComponentHelper;
|
|
32
|
+
}
|
|
33
|
+
declare function getFormHookHelpers(): FormHookHelpers;
|
|
34
|
+
//#endregion
|
|
35
|
+
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,12 @@
|
|
|
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
|
+
return attachPreactAppFormComponents(new InternalFormApi(options), createOptions.formComponents, createOptions.fieldComponents);
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { createAppFormInitializer };
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
|
|
7
|
+
//#region src/FieldGroup/FieldGroupApi.public.d.ts
|
|
8
|
+
interface FieldGroupFieldComponent<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
9
|
+
<const TFieldName extends DeepKeys<TFieldData>>(props: PreactFormFieldProps<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>, FieldValidators<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>>, ValidationIssue, unknown, FormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
|
|
10
|
+
}
|
|
11
|
+
interface FieldGroupArrayFieldComponent<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
12
|
+
<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;
|
|
13
|
+
}
|
|
14
|
+
type FieldGroupSubscribeProps<TSelected> = PreactFormSubscribeProps<unknown, FormErrorTypes, TSelected>;
|
|
15
|
+
type FieldGroupSubscribeComponent = <TSelected>(props: FieldGroupSubscribeProps<TSelected>) => CrossVersionPreactNode;
|
|
16
|
+
interface FieldGroupApi<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>> extends FormApiFieldMethods<TFieldData>, FormApiArrayMethods<TFieldData> {
|
|
17
|
+
atom: ReadonlyAtom<TFieldData>;
|
|
18
|
+
Field: FieldGroupFieldComponent<TFieldData, TFieldComponents>;
|
|
19
|
+
ArrayField: FieldGroupArrayFieldComponent<TFieldData, TFieldComponents>;
|
|
20
|
+
Subscribe: FieldGroupSubscribeComponent;
|
|
21
|
+
}
|
|
22
|
+
type AnyFieldGroupApi = FieldGroupApi<any, Record<string, FunctionComponent<any>>>;
|
|
23
|
+
type FieldGroupFormState = FormState<unknown, FormErrorTypes>;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { AnyFieldGroupApi, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupFieldComponent, FieldGroupFormState, FieldGroupSubscribeComponent, FieldGroupSubscribeProps };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { createAtom, shallow } from "@tanstack/preact-store";
|
|
2
|
+
import { getBy, transformFieldOptionsFieldNames } from "@tanstack/form-core/internals";
|
|
3
|
+
import Preact from "preact/compat";
|
|
4
|
+
|
|
5
|
+
//#region src/FieldGroup/withFields.lib.ts
|
|
6
|
+
function getRootSegmentEnd(name) {
|
|
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 });
|
|
42
|
+
const Field = (props) => {
|
|
43
|
+
const FormField = form.Field;
|
|
44
|
+
return Preact.createElement(FormField, resolveFieldProps(props, resolveName));
|
|
45
|
+
};
|
|
46
|
+
Field.displayName = "TanStackForm.FieldGroup.Field";
|
|
47
|
+
const ArrayField = (props) => {
|
|
48
|
+
const FormArrayField = form.ArrayField;
|
|
49
|
+
return Preact.createElement(FormArrayField, resolveFieldProps(props, resolveName));
|
|
50
|
+
};
|
|
51
|
+
ArrayField.displayName = "TanStackForm.FieldGroup.ArrayField";
|
|
52
|
+
const Subscribe = (props) => {
|
|
53
|
+
const FormSubscribe = form.Subscribe;
|
|
54
|
+
return Preact.createElement(FormSubscribe, props);
|
|
55
|
+
};
|
|
56
|
+
Subscribe.displayName = "TanStackForm.FieldGroup.Subscribe";
|
|
57
|
+
return {
|
|
58
|
+
atom,
|
|
59
|
+
Field,
|
|
60
|
+
ArrayField,
|
|
61
|
+
Subscribe,
|
|
62
|
+
getFieldValue: (fieldName) => form.getFieldValue(resolveName(fieldName)),
|
|
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
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const helperRuntime = {
|
|
74
|
+
strict: () => null,
|
|
75
|
+
loose: () => null
|
|
76
|
+
};
|
|
77
|
+
function defineFieldsRuntime(fields) {
|
|
78
|
+
return fields;
|
|
79
|
+
}
|
|
80
|
+
function withFieldsRuntime(_fields, Component, fieldsPropName) {
|
|
81
|
+
const fieldNames = Object.keys(_fields);
|
|
82
|
+
const FieldGroupComponent = (props) => {
|
|
83
|
+
const { form, ...restProps } = props;
|
|
84
|
+
const bindingsRef = Preact.useRef(props[fieldsPropName]);
|
|
85
|
+
if (!form) throw new Error("TanStack Form: Field groups must receive a `form` prop.");
|
|
86
|
+
bindingsRef.current = props[fieldsPropName];
|
|
87
|
+
const fieldGroupApi = Preact.useMemo(() => createFieldGroupApi(form, bindingsRef, fieldNames), [form]);
|
|
88
|
+
return Component({
|
|
89
|
+
...restProps,
|
|
90
|
+
[fieldsPropName]: fieldGroupApi
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
FieldGroupComponent.displayName = "TanStackForm.FieldGroup";
|
|
94
|
+
return FieldGroupComponent;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
export { defineFieldsRuntime, helperRuntime, withFieldsRuntime };
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
|
|
7
|
+
//#region src/FieldGroup/withFields.public.d.ts
|
|
8
|
+
declare const fieldGroupFieldSlotValueSymbol: unique symbol;
|
|
9
|
+
declare const fieldGroupFieldsSymbol: unique symbol;
|
|
10
|
+
type FieldGroupFieldSlotMode = 'strict' | 'loose';
|
|
11
|
+
interface FieldGroupFieldSlot<out TValue$1, out TMode extends FieldGroupFieldSlotMode = FieldGroupFieldSlotMode> {
|
|
12
|
+
readonly mode: TMode;
|
|
13
|
+
readonly [fieldGroupFieldSlotValueSymbol]: TValue$1;
|
|
14
|
+
}
|
|
15
|
+
type AnyFieldGroupFieldSlot = FieldGroupFieldSlot<any>;
|
|
16
|
+
type StrictFieldGroupFieldSlot<TValue$1> = FieldGroupFieldSlot<TValue$1, 'strict'>;
|
|
17
|
+
type LooseFieldGroupFieldSlot<TValue$1> = FieldGroupFieldSlot<TValue$1, 'loose'>;
|
|
18
|
+
type FieldGroupFieldSlotValue<TSlot> = TSlot extends FieldGroupFieldSlot<infer TValue> ? TValue : never;
|
|
19
|
+
type FieldGroupFieldSlotModeOf<TSlot> = TSlot extends FieldGroupFieldSlot<any, infer TMode> ? TMode : never;
|
|
20
|
+
type IsSame<TTypeA, TTypeB> = [TTypeA] extends [TTypeB] ? [TTypeB] extends [TTypeA] ? true : false : false;
|
|
21
|
+
type FieldGroupFieldSlotAllows<TSlot, TValue$1> = TSlot extends FieldGroupFieldSlot<infer TAcceptedValue, infer TMode> ? TMode extends 'strict' ? IsSame<TValue$1, TAcceptedValue> : [TValue$1] extends [TAcceptedValue] ? true : false : false;
|
|
22
|
+
type FieldGroupFieldNameForSlot<TFieldData, TSlot extends AnyFieldGroupFieldSlot> = { [TFieldName in DeepKeys<TFieldData>]: FieldGroupFieldSlotAllows<TSlot, DeepValue<TFieldData, TFieldName>> extends true ? TFieldName : never }[DeepKeys<TFieldData>];
|
|
23
|
+
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 };
|
|
26
|
+
type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
|
|
27
|
+
readonly [fieldGroupFieldsSymbol]: infer TFields;
|
|
28
|
+
} ? TFields : never;
|
|
29
|
+
type FieldGroupDefinition<TFields extends FieldGroupFields, TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>> = FieldGroupApi<FieldGroupFieldData<TFields>, TFieldComponents> & {
|
|
30
|
+
readonly [fieldGroupFieldsSymbol]: TFields;
|
|
31
|
+
};
|
|
32
|
+
type FieldGroupFieldComponentsOf<TFieldGroup> = TFieldGroup extends FieldGroupDefinition<any, infer TFieldComponents> ? TFieldComponents : never;
|
|
33
|
+
type FieldGroupForm<TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>, TFormData = any> = FormApi<TFormData, any> & PreactTanStackFormComponents<TFormData, any, TFieldComponents>;
|
|
34
|
+
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]> };
|
|
36
|
+
type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> = FieldGroupFieldsOf<TFieldGroup> extends FieldGroupFields ? FieldGroupFieldBindings<FieldGroupFieldsOf<TFieldGroup>, TFormData> : never;
|
|
37
|
+
type FieldGroupFieldsPropName<TProps, TFieldGroup extends FieldGroupDefinition<any, any>> = { [TPropName in keyof TProps]-?: IsSame<TProps[TPropName], TFieldGroup> extends true ? TPropName : never }[keyof TProps];
|
|
38
|
+
type FieldGroupWithFieldsFn = <TFieldGroup extends FieldGroupDefinition<any, any>, TProps extends object, TFieldsPropName extends FieldGroupFieldsPropName<TProps, TFieldGroup>>(fields: TFieldGroup, Component: (props: TProps) => CrossVersionPreactNode, fieldsPropName: TFieldsPropName) => <TFormData>(props: Omit<TProps, TFieldsPropName | 'form'> & {
|
|
39
|
+
form: FieldGroupForm<FieldGroupFieldComponentsOf<TFieldGroup>, TFormData>;
|
|
40
|
+
} & { [TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<TFieldGroup, TFormData> }) => CrossVersionPreactNode;
|
|
41
|
+
interface FieldGroupHelper {
|
|
42
|
+
strict: <TValue$1>() => StrictFieldGroupFieldSlot<TValue$1>;
|
|
43
|
+
loose: <TValue$1>() => LooseFieldGroupFieldSlot<TValue$1>;
|
|
44
|
+
}
|
|
45
|
+
type DefineFieldsFn<TFieldComponents extends Record<string, FunctionComponent<any>>> = <const TFields extends FieldGroupFields>(fields: TFields) => FieldGroupDefinition<TFields, TFieldComponents>;
|
|
46
|
+
interface FieldGroupHelpers<TFieldComponents extends Record<string, FunctionComponent<any>>> {
|
|
47
|
+
helper: FieldGroupHelper;
|
|
48
|
+
defineFields: DefineFieldsFn<TFieldComponents>;
|
|
49
|
+
withFields: FieldGroupWithFieldsFn;
|
|
50
|
+
}
|
|
51
|
+
declare function getFieldGroupHelpers(): FieldGroupHelpers<Record<never, never>>;
|
|
52
|
+
//#endregion
|
|
53
|
+
export { AnyFieldGroupFieldSlot, DefineFieldsFn, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupHelper, FieldGroupHelpers, FieldGroupWithFieldsFn, LooseFieldGroupFieldSlot, StrictFieldGroupFieldSlot, getFieldGroupHelpers };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineFieldsRuntime, helperRuntime, withFieldsRuntime } from "./withFields.lib.js";
|
|
2
|
+
|
|
3
|
+
//#region src/FieldGroup/withFields.public.ts
|
|
4
|
+
const helper = helperRuntime;
|
|
5
|
+
const defineFields = defineFieldsRuntime;
|
|
6
|
+
const withFields = withFieldsRuntime;
|
|
7
|
+
function createFieldGroupHelpers() {
|
|
8
|
+
return {
|
|
9
|
+
helper,
|
|
10
|
+
defineFields,
|
|
11
|
+
withFields
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function getFieldGroupHelpers() {
|
|
15
|
+
return createFieldGroupHelpers();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { getFieldGroupHelpers };
|
|
@@ -0,0 +1,97 @@
|
|
|
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 field = useValueFieldSubscription(useField({
|
|
20
|
+
...props,
|
|
21
|
+
form
|
|
22
|
+
}, fieldComponents));
|
|
23
|
+
return props.children(field);
|
|
24
|
+
};
|
|
25
|
+
TanStackFormField.displayName = "TanStackForm.Field";
|
|
26
|
+
return TanStackFormField;
|
|
27
|
+
}
|
|
28
|
+
function createArrayFieldComponent(form, fieldComponents) {
|
|
29
|
+
const TanStackFormArrayField = (props) => {
|
|
30
|
+
const field = useArrayFieldSubscription(useField({
|
|
31
|
+
...props,
|
|
32
|
+
form
|
|
33
|
+
}, fieldComponents));
|
|
34
|
+
return props.children(field);
|
|
35
|
+
};
|
|
36
|
+
TanStackFormArrayField.displayName = "TanStackForm.ArrayField";
|
|
37
|
+
return TanStackFormArrayField;
|
|
38
|
+
}
|
|
39
|
+
function createSubscribeComponent(form) {
|
|
40
|
+
const TanStackFormSubscribe = (props) => {
|
|
41
|
+
return /* @__PURE__ */ jsx(Subscribe, {
|
|
42
|
+
source: form.atom,
|
|
43
|
+
...props
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
TanStackFormSubscribe.displayName = "TanStackForm.Subscribe";
|
|
47
|
+
return TanStackFormSubscribe;
|
|
48
|
+
}
|
|
49
|
+
function createFormGroupComponent(form) {
|
|
50
|
+
const TanStackFormGroup = (props) => {
|
|
51
|
+
const groupRef = Preact.useRef(null);
|
|
52
|
+
if (!groupRef.current) groupRef.current = attachPreactFormGroupComponents(new InternalFormGroupApi({
|
|
53
|
+
...props,
|
|
54
|
+
form
|
|
55
|
+
}), form);
|
|
56
|
+
useEffect(() => groupRef.current?.update({
|
|
57
|
+
...props,
|
|
58
|
+
form
|
|
59
|
+
}));
|
|
60
|
+
Preact.useEffect(() => {
|
|
61
|
+
const group = groupRef.current;
|
|
62
|
+
group.mount();
|
|
63
|
+
return () => group._cleanup();
|
|
64
|
+
}, []);
|
|
65
|
+
return props.children(groupRef.current);
|
|
66
|
+
};
|
|
67
|
+
TanStackFormGroup.displayName = "TanStackForm.FormGroup";
|
|
68
|
+
return TanStackFormGroup;
|
|
69
|
+
}
|
|
70
|
+
function attachPreactFormGroupComponents(group, form) {
|
|
71
|
+
const resultGroup = group;
|
|
72
|
+
resultGroup.Field = function Field(props) {
|
|
73
|
+
return /* @__PURE__ */ jsx(form.Field, { ...group._getFormFieldOptions(props, (base, overrides) => ({
|
|
74
|
+
...base,
|
|
75
|
+
...overrides
|
|
76
|
+
})) });
|
|
77
|
+
};
|
|
78
|
+
resultGroup.Field.displayName = "TanStackForm.FormGroup.Field";
|
|
79
|
+
resultGroup.ArrayField = function ArrayField(props) {
|
|
80
|
+
return /* @__PURE__ */ jsx(form.ArrayField, { ...group._getFormFieldOptions(props, (base, overrides) => ({
|
|
81
|
+
...base,
|
|
82
|
+
...overrides
|
|
83
|
+
})) });
|
|
84
|
+
};
|
|
85
|
+
resultGroup.ArrayField.displayName = "TanStackForm.FormGroup.ArrayField";
|
|
86
|
+
resultGroup.Subscribe = (props) => {
|
|
87
|
+
return /* @__PURE__ */ jsx(Subscribe, {
|
|
88
|
+
source: group.atom,
|
|
89
|
+
...props
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
resultGroup.Subscribe.displayName = "TanStackForm.FormGroup.Subscribe";
|
|
93
|
+
return resultGroup;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
//#endregion
|
|
97
|
+
export { attachPreactFormComponents };
|