@tanstack/preact-form 1.33.3 → 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 +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 +2 -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 +1 -2
- package/dist/PreactForm/formType.public.d.ts +1 -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 +4 -4
- package/src/AppForm/createFormHook.public.ts +2 -2
- package/src/AppForm/createFormHookTypes.public.ts +2 -4
- package/src/FieldGroup/withFields.lib.ts +44 -126
- package/src/FieldGroup/withFields.public.ts +23 -41
|
@@ -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,14 +1,13 @@
|
|
|
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>;
|
|
9
8
|
interface AppFormHookResult<in out TComponents extends AnyPreactFormComponentMap> {
|
|
10
9
|
appFormOptions: AppFormOptionsApi<TComponents>;
|
|
11
|
-
|
|
10
|
+
defineAppFieldGroup: DefineFieldGroupFn<TComponents['fieldComponents']>;
|
|
12
11
|
useAppForm: UseAppFormHook<TComponents>;
|
|
13
12
|
useFormContext: () => PreactAppFormApi<any, any, TComponents>;
|
|
14
13
|
}
|
|
@@ -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,9 @@ 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'];
|
|
9
8
|
type AnyPreactFormApi = AnyFormApi & PreactTanStackFormComponents<any, any, any>;
|
|
10
9
|
//#endregion
|
|
11
|
-
export { AnyPreactFormApi, PreactFormApi };
|
|
10
|
+
export { AnyPreactFormApi, PreactFormApi, type PreactFormComponentMap };
|
|
@@ -2,9 +2,8 @@ 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>;
|
|
8
7
|
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
8
|
//#endregion
|
|
10
9
|
export { PreactFormType };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { AnyPreactFormComponentMap, DefaultPreactFormComponentMap } from "../AppForm/componentMap.public.js";
|
|
2
2
|
import { PreactFormApi } from "./formApiTypes.public.js";
|
|
3
3
|
import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
|
|
4
|
-
|
|
5
4
|
//#region src/PreactForm/useForm.public.d.ts
|
|
6
5
|
type UseFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => PreactFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
|
|
7
6
|
declare const useForm: UseFormHook<DefaultPreactFormComponentMap>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CrossVersionPreactNode } from "./preactTypes.public.js";
|
|
2
2
|
import { Atom, ReadonlyAtom, ReadonlyStore, Store } from "@tanstack/preact-store";
|
|
3
|
-
|
|
4
3
|
//#region src/Subscribe.public.d.ts
|
|
5
4
|
type SubscribeSource<TValue> = Atom<TValue> | ReadonlyAtom<TValue> | Store<TValue> | ReadonlyStore<TValue>;
|
|
6
5
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { UseFormHook, useForm } from "./PreactForm/useForm.public.js";
|
|
|
6
6
|
import { AppFormOptions, AppFormOptionsApi } from "./AppForm/appFormOptions.public.js";
|
|
7
7
|
import { PreactFormType } from "./PreactForm/formType.public.js";
|
|
8
8
|
import { AnyFieldGroupApi, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupFieldComponent, FieldGroupFormState, FieldGroupSubscribeComponent, FieldGroupSubscribeProps } from "./FieldGroup/FieldGroupApi.public.js";
|
|
9
|
-
import { AnyFieldGroupFieldSlot,
|
|
9
|
+
import { AnyFieldGroupFieldSlot, DefineFieldGroupFn, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupHelper, FieldGroupWithFieldsFn, LooseFieldGroupFieldSlot, PreactFieldGroup, StrictFieldGroupFieldSlot, defineFieldGroup } from "./FieldGroup/withFields.public.js";
|
|
10
10
|
import "./types.public.js";
|
|
11
11
|
import { Subscribe, SubscribeProps, SubscribeSource } from "./Subscribe.public.js";
|
|
12
12
|
import { AppFormComponent, PreactAppFormApi } from "./AppForm/PreactAppFormApi.public.js";
|
|
@@ -15,4 +15,4 @@ import { createFormHook } from "./AppForm/createFormHook.public.js";
|
|
|
15
15
|
import { FormHookHelpers, getFormHookHelpers } from "./AppForm/getFormHookHelpers.public.js";
|
|
16
16
|
export * from "@tanstack/form-core";
|
|
17
17
|
export * from "@tanstack/preact-store";
|
|
18
|
-
export { AnyFieldGroupApi, AnyFieldGroupFieldSlot, AnyPreactFormApi, AnyPreactFormComponentMap, AppFormComponent, AppFormHookResult, AppFormOptions, AppFormOptionsApi, CrossVersionPreactNode, DefaultPreactFormComponentMap,
|
|
18
|
+
export { AnyFieldGroupApi, AnyFieldGroupFieldSlot, AnyPreactFormApi, AnyPreactFormComponentMap, AppFormComponent, AppFormHookResult, AppFormOptions, AppFormOptionsApi, CrossVersionPreactNode, DefaultPreactFormComponentMap, DefineFieldGroupFn, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponent, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupFormState, FieldGroupHelper, FieldGroupSubscribeComponent, FieldGroupSubscribeProps, FieldGroupWithFieldsFn, FormHookHelpers, LooseFieldGroupFieldSlot, PreactAppFormApi, PreactFieldApi, PreactFieldGroup, PreactFormApi, PreactFormArrayFieldComponent, PreactFormComponentMap, PreactFormFieldComponent, PreactFormFieldProps, PreactFormGroupApi, PreactFormGroupArrayFieldComponent, PreactFormGroupComponent, PreactFormGroupFieldComponent, PreactFormGroupProps, PreactFormGroupSubscribeComponent, PreactFormGroupSubscribeProps, PreactFormSubscribeComponent, PreactFormSubscribeProps, PreactFormType, PreactTanStackFormComponents, StrictFieldGroupFieldSlot, Subscribe, SubscribeProps, SubscribeSource, UseAppFormHook, UseFormHook, createFormHook, defineFieldGroup, getFormHookHelpers, useForm };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Subscribe } from "./Subscribe.public.js";
|
|
2
2
|
import { useForm } from "./PreactForm/useForm.public.js";
|
|
3
|
-
import {
|
|
3
|
+
import { defineFieldGroup } from "./FieldGroup/withFields.public.js";
|
|
4
4
|
import { createFormHook } from "./AppForm/createFormHook.public.js";
|
|
5
5
|
import { getFormHookHelpers } from "./AppForm/getFormHookHelpers.public.js";
|
|
6
6
|
|
|
@@ -8,4 +8,4 @@ export * from "@tanstack/form-core"
|
|
|
8
8
|
|
|
9
9
|
export * from "@tanstack/preact-store"
|
|
10
10
|
|
|
11
|
-
export { Subscribe, createFormHook,
|
|
11
|
+
export { Subscribe, createFormHook, defineFieldGroup, getFormHookHelpers, useForm };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/preact-form",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-alpha.0",
|
|
4
4
|
"description": "Powerful, type-safe forms for Preact.",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"src"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@tanstack/preact-store": "^0.13.
|
|
39
|
-
"@tanstack/form-core": "
|
|
38
|
+
"@tanstack/preact-store": "^0.13.2",
|
|
39
|
+
"@tanstack/form-core": "2.0.0-alpha.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@eslint-react/eslint-plugin": "^5.18.
|
|
42
|
+
"@eslint-react/eslint-plugin": "^5.18.3",
|
|
43
43
|
"@preact/preset-vite": "^2.10.2",
|
|
44
44
|
"@testing-library/preact": "^3.2.4",
|
|
45
45
|
"preact": "^10.26.4",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useInternalForm } from '../PreactForm/PreactFormApi.lib'
|
|
2
|
-
import {
|
|
2
|
+
import { defineFieldGroup } from '../FieldGroup/withFields.public'
|
|
3
3
|
import { createAppFormInitializer } from './initializeAppForm.lib'
|
|
4
4
|
import { useFormContext } from './contexts.lib'
|
|
5
5
|
import type { AppFormOptionsApi } from './appFormOptions.public'
|
|
@@ -31,7 +31,7 @@ export function createFormHook<
|
|
|
31
31
|
return {
|
|
32
32
|
useFormContext: useFormContext as never,
|
|
33
33
|
appFormOptions,
|
|
34
|
-
|
|
34
|
+
defineAppFieldGroup: defineFieldGroup,
|
|
35
35
|
useAppForm,
|
|
36
36
|
}
|
|
37
37
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AppFormOptionsApi } from './appFormOptions.public'
|
|
2
2
|
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
3
3
|
import type { PreactAppFormApi } from './PreactAppFormApi.public'
|
|
4
|
-
import type {
|
|
4
|
+
import type { DefineFieldGroupFn } from '../FieldGroup/withFields.public'
|
|
5
5
|
import type {
|
|
6
6
|
FormOptions,
|
|
7
7
|
FormValidators,
|
|
@@ -26,9 +26,7 @@ export interface AppFormHookResult<
|
|
|
26
26
|
in out TComponents extends AnyPreactFormComponentMap,
|
|
27
27
|
> {
|
|
28
28
|
appFormOptions: AppFormOptionsApi<TComponents>
|
|
29
|
-
|
|
30
|
-
TComponents['fieldComponents']
|
|
31
|
-
>
|
|
29
|
+
defineAppFieldGroup: DefineFieldGroupFn<TComponents['fieldComponents']>
|
|
32
30
|
useAppForm: UseAppFormHook<TComponents>
|
|
33
31
|
useFormContext: () => PreactAppFormApi<any, any, TComponents>
|
|
34
32
|
}
|
|
@@ -1,98 +1,25 @@
|
|
|
1
1
|
import Preact from 'preact/compat'
|
|
2
|
-
import { createAtom, shallow } from '@tanstack/preact-store'
|
|
3
2
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
InternalFieldGroupApi,
|
|
4
|
+
defineFieldGroupFieldsRuntime,
|
|
5
|
+
fieldGroupHelperRuntime,
|
|
6
6
|
} from '@tanstack/form-core/internals'
|
|
7
|
+
import type { InternalFieldGroupBindings } from '@tanstack/form-core/internals'
|
|
7
8
|
import type { FunctionComponent } from 'preact/compat'
|
|
8
9
|
import type { AnyFieldGroupApi } from './FieldGroupApi.public'
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
interface ResolvedNameCacheEntry {
|
|
13
|
-
binding: string
|
|
14
|
-
name: string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
type ResolvedNameCache = Map<string, ResolvedNameCacheEntry>
|
|
18
|
-
|
|
19
|
-
function getRootSegmentEnd(name: string): number {
|
|
20
|
-
const dotIndex = name.indexOf('.')
|
|
21
|
-
const arrayIndex = name.indexOf('[')
|
|
22
|
-
|
|
23
|
-
if (dotIndex === -1) return arrayIndex === -1 ? name.length : arrayIndex
|
|
24
|
-
if (arrayIndex === -1) return dotIndex
|
|
25
|
-
return Math.min(dotIndex, arrayIndex)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function resolveFieldName(
|
|
29
|
-
bindings: FieldBindings,
|
|
30
|
-
cache: ResolvedNameCache,
|
|
31
|
-
fieldName: string,
|
|
32
|
-
): string {
|
|
33
|
-
const rootEnd = getRootSegmentEnd(fieldName)
|
|
34
|
-
const rootName = fieldName.slice(0, rootEnd)
|
|
35
|
-
const binding = bindings[rootName]
|
|
36
|
-
|
|
37
|
-
if (binding === undefined) {
|
|
38
|
-
throw new Error(
|
|
39
|
-
`TanStack Form: Missing field group binding for "${rootName}".`,
|
|
40
|
-
)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const cached = cache.get(fieldName)
|
|
44
|
-
if (cached?.binding === binding) return cached.name
|
|
45
|
-
|
|
46
|
-
const resolvedName = `${binding}${fieldName.slice(rootEnd)}`
|
|
47
|
-
cache.set(fieldName, {
|
|
48
|
-
binding,
|
|
49
|
-
name: resolvedName,
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
return resolvedName
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function resolveFieldProps<TProps extends { name: string }>(
|
|
56
|
-
props: TProps,
|
|
57
|
-
resolveName: (name: string) => string,
|
|
58
|
-
): TProps {
|
|
59
|
-
return transformFieldOptionsFieldNames(
|
|
60
|
-
props,
|
|
61
|
-
resolveName,
|
|
62
|
-
(base, overrides) => ({
|
|
63
|
-
...base,
|
|
64
|
-
...overrides,
|
|
65
|
-
}),
|
|
66
|
-
)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function createFieldGroupApi(
|
|
11
|
+
function attachPreactFieldGroupComponents(
|
|
12
|
+
group: InternalFieldGroupApi,
|
|
70
13
|
form: any,
|
|
71
|
-
bindingsRef: Preact.RefObject<FieldBindings>,
|
|
72
|
-
fieldNames: Array<string>,
|
|
73
14
|
): AnyFieldGroupApi {
|
|
74
|
-
const cache: ResolvedNameCache = new Map()
|
|
75
|
-
const resolveName = (fieldName: string) =>
|
|
76
|
-
resolveFieldName(bindingsRef.current!, cache, fieldName)
|
|
77
|
-
const atom = createAtom(
|
|
78
|
-
() => {
|
|
79
|
-
const nextValues: Record<string, unknown> = {}
|
|
80
|
-
const formValues = form._atoms.values.get()
|
|
81
|
-
|
|
82
|
-
for (const fieldName of fieldNames) {
|
|
83
|
-
nextValues[fieldName] = getBy(formValues, resolveName(fieldName))
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return nextValues
|
|
87
|
-
},
|
|
88
|
-
{ compare: shallow },
|
|
89
|
-
)
|
|
90
|
-
|
|
91
15
|
const Field: FunctionComponent<any> = (props) => {
|
|
92
16
|
const FormField = form.Field
|
|
93
17
|
return Preact.createElement(
|
|
94
18
|
FormField as FunctionComponent<any>,
|
|
95
|
-
|
|
19
|
+
group._getFormFieldOptions(props, (base, overrides) => ({
|
|
20
|
+
...base,
|
|
21
|
+
...overrides,
|
|
22
|
+
})),
|
|
96
23
|
)
|
|
97
24
|
}
|
|
98
25
|
Field.displayName = 'TanStackForm.FieldGroup.Field'
|
|
@@ -101,7 +28,10 @@ function createFieldGroupApi(
|
|
|
101
28
|
const FormArrayField = form.ArrayField
|
|
102
29
|
return Preact.createElement(
|
|
103
30
|
FormArrayField as FunctionComponent<any>,
|
|
104
|
-
|
|
31
|
+
group._getFormFieldOptions(props, (base, overrides) => ({
|
|
32
|
+
...base,
|
|
33
|
+
...overrides,
|
|
34
|
+
})),
|
|
105
35
|
)
|
|
106
36
|
}
|
|
107
37
|
ArrayField.displayName = 'TanStackForm.FieldGroup.ArrayField'
|
|
@@ -112,61 +42,41 @@ function createFieldGroupApi(
|
|
|
112
42
|
}
|
|
113
43
|
Subscribe.displayName = 'TanStackForm.FieldGroup.Subscribe'
|
|
114
44
|
|
|
115
|
-
return {
|
|
116
|
-
atom,
|
|
45
|
+
return Object.assign(group, {
|
|
117
46
|
Field,
|
|
118
47
|
ArrayField,
|
|
119
48
|
Subscribe,
|
|
120
|
-
|
|
121
|
-
form.getFieldValue(resolveName(fieldName)),
|
|
122
|
-
setFieldValue: (fieldName: string, value: unknown, options?: unknown) =>
|
|
123
|
-
form.setFieldValue(resolveName(fieldName), value, options),
|
|
124
|
-
resetField: (fieldName: string) => form.resetField(resolveName(fieldName)),
|
|
125
|
-
swapFieldValues: (
|
|
126
|
-
fieldName: string,
|
|
127
|
-
indexA: number,
|
|
128
|
-
indexB: number,
|
|
129
|
-
options?: unknown,
|
|
130
|
-
) => form.swapFieldValues(resolveName(fieldName), indexA, indexB, options),
|
|
131
|
-
pushFieldValue: (fieldName: string, value: unknown, options?: unknown) =>
|
|
132
|
-
form.pushFieldValue(resolveName(fieldName), value, options),
|
|
133
|
-
insertFieldValue: (
|
|
134
|
-
fieldName: string,
|
|
135
|
-
index: number,
|
|
136
|
-
value: unknown,
|
|
137
|
-
options?: unknown,
|
|
138
|
-
) => form.insertFieldValue(resolveName(fieldName), index, value, options),
|
|
139
|
-
clearFieldValues: (fieldName: string, options?: unknown) =>
|
|
140
|
-
form.clearFieldValues(resolveName(fieldName), options),
|
|
141
|
-
removeFieldValue: (fieldName: string, index: number, options?: unknown) =>
|
|
142
|
-
form.removeFieldValue(resolveName(fieldName), index, options),
|
|
143
|
-
filterFieldValues: (
|
|
144
|
-
fieldName: string,
|
|
145
|
-
predicate: (...args: Array<any>) => boolean,
|
|
146
|
-
options?: unknown,
|
|
147
|
-
) => form.filterFieldValues(resolveName(fieldName), predicate, options),
|
|
148
|
-
} as never
|
|
49
|
+
}) as never
|
|
149
50
|
}
|
|
150
51
|
|
|
151
|
-
export
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
52
|
+
export function defineFieldGroupRuntime<
|
|
53
|
+
TFields extends Record<string, unknown>,
|
|
54
|
+
>(defineFieldGroupFn: (helperRuntime: any) => TFields) {
|
|
55
|
+
const fields = defineFieldGroupFieldsRuntime(
|
|
56
|
+
defineFieldGroupFn(fieldGroupHelperRuntime),
|
|
57
|
+
)
|
|
155
58
|
|
|
156
|
-
|
|
157
|
-
|
|
59
|
+
return {
|
|
60
|
+
fields,
|
|
61
|
+
bindComponent: (
|
|
62
|
+
Component: (props: any) => unknown,
|
|
63
|
+
fieldsPropName: string,
|
|
64
|
+
) => withFieldsRuntime(fields, Component, fieldsPropName),
|
|
65
|
+
}
|
|
158
66
|
}
|
|
159
67
|
|
|
160
|
-
|
|
161
|
-
|
|
68
|
+
function withFieldsRuntime(
|
|
69
|
+
fields: Record<string, unknown>,
|
|
162
70
|
Component: (props: any) => unknown,
|
|
163
71
|
fieldsPropName: string,
|
|
164
72
|
) {
|
|
165
|
-
const fieldNames = Object.keys(
|
|
73
|
+
const fieldNames = Object.keys(fields)
|
|
166
74
|
|
|
167
75
|
const FieldGroupComponent = (props: any) => {
|
|
168
76
|
const { form, ...restProps } = props
|
|
169
|
-
const bindingsRef = Preact.useRef<
|
|
77
|
+
const bindingsRef = Preact.useRef<InternalFieldGroupBindings>(
|
|
78
|
+
props[fieldsPropName],
|
|
79
|
+
)
|
|
170
80
|
|
|
171
81
|
if (!form) {
|
|
172
82
|
throw new Error('TanStack Form: Field groups must receive a `form` prop.')
|
|
@@ -175,7 +85,15 @@ export function withFieldsRuntime(
|
|
|
175
85
|
bindingsRef.current = props[fieldsPropName]
|
|
176
86
|
|
|
177
87
|
const fieldGroupApi = Preact.useMemo(
|
|
178
|
-
() =>
|
|
88
|
+
() =>
|
|
89
|
+
attachPreactFieldGroupComponents(
|
|
90
|
+
new InternalFieldGroupApi({
|
|
91
|
+
form,
|
|
92
|
+
fieldNames,
|
|
93
|
+
getBindings: () => bindingsRef.current!,
|
|
94
|
+
}),
|
|
95
|
+
form,
|
|
96
|
+
),
|
|
179
97
|
[form],
|
|
180
98
|
)
|
|
181
99
|
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
defineFieldsRuntime,
|
|
3
|
-
helperRuntime,
|
|
4
|
-
withFieldsRuntime,
|
|
5
|
-
} from './withFields.lib'
|
|
1
|
+
import { defineFieldGroupRuntime } from './withFields.lib'
|
|
6
2
|
import type {
|
|
7
3
|
DeepKeys,
|
|
8
4
|
DeepKeysWhereValueIncludes,
|
|
@@ -98,7 +94,7 @@ export type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
|
|
|
98
94
|
? TFields
|
|
99
95
|
: never
|
|
100
96
|
|
|
101
|
-
export type
|
|
97
|
+
export type PreactFieldGroup<
|
|
102
98
|
TFields extends FieldGroupFields,
|
|
103
99
|
TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
|
|
104
100
|
never,
|
|
@@ -109,7 +105,7 @@ export type FieldGroupDefinition<
|
|
|
109
105
|
}
|
|
110
106
|
|
|
111
107
|
export type FieldGroupFieldComponentsOf<TFieldGroup> =
|
|
112
|
-
TFieldGroup extends
|
|
108
|
+
TFieldGroup extends PreactFieldGroup<any, infer TFieldComponents>
|
|
113
109
|
? TFieldComponents
|
|
114
110
|
: never
|
|
115
111
|
|
|
@@ -149,7 +145,7 @@ export type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> =
|
|
|
149
145
|
|
|
150
146
|
export type FieldGroupFieldsPropName<
|
|
151
147
|
TProps,
|
|
152
|
-
TFieldGroup extends
|
|
148
|
+
TFieldGroup extends PreactFieldGroup<any, any>,
|
|
153
149
|
> = {
|
|
154
150
|
[TPropName in keyof TProps]-?: IsSame<
|
|
155
151
|
TProps[TPropName],
|
|
@@ -159,12 +155,12 @@ export type FieldGroupFieldsPropName<
|
|
|
159
155
|
: never
|
|
160
156
|
}[keyof TProps]
|
|
161
157
|
|
|
162
|
-
export type FieldGroupWithFieldsFn
|
|
163
|
-
TFieldGroup extends
|
|
158
|
+
export type FieldGroupWithFieldsFn<
|
|
159
|
+
TFieldGroup extends PreactFieldGroup<any, any>,
|
|
160
|
+
> = <
|
|
164
161
|
TProps extends object,
|
|
165
162
|
TFieldsPropName extends FieldGroupFieldsPropName<TProps, TFieldGroup>,
|
|
166
163
|
>(
|
|
167
|
-
fields: TFieldGroup,
|
|
168
164
|
Component: (props: TProps) => CrossVersionPreactNode,
|
|
169
165
|
fieldsPropName: TFieldsPropName,
|
|
170
166
|
) => <TFormData>(
|
|
@@ -183,39 +179,25 @@ export interface FieldGroupHelper {
|
|
|
183
179
|
loose: <TValue>() => LooseFieldGroupFieldSlot<TValue>
|
|
184
180
|
}
|
|
185
181
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
export type DefineFieldsFn<
|
|
189
|
-
TFieldComponents extends Record<string, FunctionComponent<any>>,
|
|
190
|
-
> = <const TFields extends FieldGroupFields>(
|
|
191
|
-
fields: TFields,
|
|
192
|
-
) => FieldGroupDefinition<TFields, TFieldComponents>
|
|
193
|
-
|
|
194
|
-
const defineFields: DefineFieldsFn<Record<never, never>> =
|
|
195
|
-
defineFieldsRuntime as never
|
|
196
|
-
|
|
197
|
-
const withFields: FieldGroupWithFieldsFn = withFieldsRuntime as never
|
|
198
|
-
|
|
199
|
-
export interface FieldGroupHelpers<
|
|
182
|
+
export interface FieldGroupDefinition<
|
|
183
|
+
TFields extends FieldGroupFields,
|
|
200
184
|
TFieldComponents extends Record<string, FunctionComponent<any>>,
|
|
201
185
|
> {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
186
|
+
/** The virtual field-group API injected into the bound component. */
|
|
187
|
+
fields: PreactFieldGroup<TFields, TFieldComponents>
|
|
188
|
+
/** Binds a component's virtual field API to concrete paths in a form. */
|
|
189
|
+
bindComponent: FieldGroupWithFieldsFn<
|
|
190
|
+
PreactFieldGroup<TFields, TFieldComponents>
|
|
191
|
+
>
|
|
205
192
|
}
|
|
206
193
|
|
|
207
|
-
|
|
194
|
+
/** Signature shared by `defineFieldGroup` and app-form field-group definers. */
|
|
195
|
+
export type DefineFieldGroupFn<
|
|
208
196
|
TFieldComponents extends Record<string, FunctionComponent<any>>,
|
|
209
|
-
>
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
defineFields: defineFields as never,
|
|
213
|
-
withFields,
|
|
214
|
-
}
|
|
215
|
-
}
|
|
197
|
+
> = <const TFields extends FieldGroupFields>(
|
|
198
|
+
defineFn: (helper: FieldGroupHelper) => TFields,
|
|
199
|
+
) => FieldGroupDefinition<TFields, TFieldComponents>
|
|
216
200
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
return createFieldGroupHelpers()
|
|
221
|
-
}
|
|
201
|
+
/** Defines a reusable group of virtual fields. */
|
|
202
|
+
export const defineFieldGroup: DefineFieldGroupFn<Record<never, never>> =
|
|
203
|
+
defineFieldGroupRuntime as never
|