@tanstack/preact-form 2.0.0-alpha.1 → 2.0.0-alpha.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.
@@ -1,5 +1,5 @@
1
1
  import { Subscribe } from "../Subscribe.public.js";
2
- import { useValueFieldSubscription } from "../PreactForm/fieldSubscriptions.lib.js";
2
+ import { useArrayFieldSubscription, useValueFieldSubscription } from "../PreactForm/fieldSubscriptions.lib.js";
3
3
  import { useField } from "../PreactForm/useField.lib.js";
4
4
  import { attachPreactFormComponents } from "../PreactForm/Components.lib.js";
5
5
  import { FieldContext, FormContext } from "./contexts.lib.js";
@@ -12,6 +12,9 @@ function attachPreactAppFormComponents(form, formComponents, fieldComponents) {
12
12
  const resultForm = attachPreactFormComponents(form, fieldComponents);
13
13
  resultForm.AppForm = createAppForm(form);
14
14
  resultForm.Field = createFieldWithContext(form, fieldComponents);
15
+ const arrayField = createFieldWithContext(form, fieldComponents, useArrayFieldSubscription);
16
+ arrayField.displayName = "TanStackForm.ArrayField";
17
+ resultForm.ArrayField = arrayField;
15
18
  resultForm.FormGroup = createFormGroupWithContext(resultForm);
16
19
  return Object.assign(resultForm, formComponents);
17
20
  }
@@ -25,13 +28,12 @@ function createAppForm(form) {
25
28
  AppForm.displayName = "TanStackForm.AppForm";
26
29
  return AppForm;
27
30
  }
28
- function createFieldWithContext(form, fieldComponents) {
31
+ function createFieldWithContext(form, fieldComponents, useFieldSubscription = useValueFieldSubscription) {
29
32
  const TanStackFormField = (props) => {
30
- const fieldApi = useField({
33
+ const field = useFieldSubscription(useField({
31
34
  ...props,
32
35
  form
33
- }, fieldComponents);
34
- const field = useValueFieldSubscription(fieldApi);
36
+ }, fieldComponents));
35
37
  return /* @__PURE__ */ jsx(FieldContext.Provider, {
36
38
  value: field,
37
39
  children: props.children(field)
@@ -1,6 +1,9 @@
1
- import { AnyPreactFormComponentMap } from "./componentMap.public.js";
2
- import { AppFormHookResult } from "./createFormHookTypes.public.js";
1
+ import { AppFormHookResult, CreateFormHookOptions } from "./createFormHookTypes.public.js";
2
+ import { FunctionComponent } from "preact/compat";
3
3
  //#region src/AppForm/createFormHook.public.d.ts
4
- declare function createFormHook<const TComponents extends AnyPreactFormComponentMap>(createOptions: TComponents): AppFormHookResult<TComponents>;
4
+ declare function createFormHook<const TFormComponents extends Record<string, FunctionComponent<any>>, const TFieldComponents extends Record<string, FunctionComponent<any>>>(createOptions: CreateFormHookOptions<TFormComponents, TFieldComponents>): AppFormHookResult<{
5
+ formComponents: TFormComponents;
6
+ fieldComponents: TFieldComponents;
7
+ }>;
5
8
  //#endregion
6
9
  export { createFormHook };
@@ -2,13 +2,9 @@ import { useInternalForm } from "../PreactForm/PreactFormApi.lib.js";
2
2
  import { defineFieldGroup } from "../FieldGroup/withFields.public.js";
3
3
  import { useFormContext } from "./contexts.lib.js";
4
4
  import { createAppFormInitializer } from "./initializeAppForm.lib.js";
5
+ import { formOptions } from "@tanstack/form-core";
5
6
 
6
7
  //#region src/AppForm/createFormHook.public.ts
7
- const appFormOptions = ((opts) => {
8
- return opts;
9
- });
10
- appFormOptions.strictSchema = (opts) => opts;
11
- appFormOptions.looseSchema = (opts) => opts;
12
8
  function createFormHook(createOptions) {
13
9
  const initializeAppForm = createAppFormInitializer(createOptions);
14
10
  function useExtendedForm(hookOptions) {
@@ -16,7 +12,7 @@ function createFormHook(createOptions) {
16
12
  }
17
13
  return {
18
14
  useFormContext,
19
- appFormOptions,
15
+ appFormOptions: formOptions,
20
16
  defineAppFieldGroup: defineFieldGroup,
21
17
  useAppForm: useExtendedForm
22
18
  };
@@ -1,10 +1,86 @@
1
- import { AnyPreactFormComponentMap } from "./componentMap.public.js";
2
- import { AppFormOptionsApi } from "./appFormOptions.public.js";
1
+ import { AnyPreactFormComponentMap, PreactFormComponentMap } from "./componentMap.public.js";
3
2
  import { DefineFieldGroupFn } from "../FieldGroup/withFields.public.js";
4
3
  import { PreactAppFormApi } from "./PreactAppFormApi.public.js";
5
- import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
4
+ import { DefaultFieldOptions, DefaultFormGroupOptions, DefaultFormOptions, FormOptions, FormOptionsApi, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
5
+ import { FunctionComponent } from "preact/compat";
6
6
  //#region src/AppForm/createFormHookTypes.public.d.ts
7
- type UseAppFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => PreactAppFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
7
+ /**
8
+ * Configures the components and reusable defaults returned by
9
+ * `createFormHook`.
10
+ *
11
+ * Form core resolves each default object before the corresponding usage-site
12
+ * options. Non-listener properties passed at the usage site take precedence,
13
+ * including when explicitly set to `undefined`. Listener arrays follow the
14
+ * configured `listenersMerge` strategy.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * const { useAppForm } = createFormHook({
19
+ * formComponents: {},
20
+ * fieldComponents: {
21
+ * TextField,
22
+ * },
23
+ * defaultFormOptions: {
24
+ * errorVisibility: ({ fieldState }) => fieldState.meta.isBlurred,
25
+ * },
26
+ * defaultFieldOptions: {
27
+ * errorBoundary: true,
28
+ * },
29
+ * })
30
+ * ```
31
+ *
32
+ * @typeParam TFormComponents - Library-managed. Do not specify explicitly.
33
+ * @typeParam TFieldComponents - Library-managed. Do not specify explicitly.
34
+ */
35
+ interface CreateFormHookOptions<in out TFormComponents extends Record<string, FunctionComponent<any>>, in out TFieldComponents extends Record<string, FunctionComponent<any>>> extends PreactFormComponentMap<TFormComponents, TFieldComponents> {
36
+ /**
37
+ * Defaults for every form created by `useAppForm`.
38
+ *
39
+ * Non-listener options passed to `useAppForm` override these defaults,
40
+ * including when explicitly set to `undefined`. Listener arrays follow
41
+ * `listenersMerge`.
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * defaultFormOptions: {
46
+ * errorVisibility: ({ state }) => state.submissionAttempts > 0,
47
+ * },
48
+ * ```
49
+ */
50
+ defaultFormOptions?: DefaultFormOptions;
51
+ /**
52
+ * Defaults for every field and array-field component owned by the form.
53
+ *
54
+ * Non-listener options passed to the component override these defaults,
55
+ * including when explicitly set to `undefined`. Listener arrays follow
56
+ * `listenersMerge`. This includes `group.Field` and `group.ArrayField`.
57
+ *
58
+ * @example
59
+ * ```tsx
60
+ * defaultFieldOptions: {
61
+ * errorVisibility: ({ fieldState }) => fieldState.meta.isBlurred,
62
+ * },
63
+ * ```
64
+ */
65
+ defaultFieldOptions?: DefaultFieldOptions;
66
+ /**
67
+ * Defaults for every `form.FormGroup` component.
68
+ *
69
+ * Options passed to the component override these defaults, including when
70
+ * an option is explicitly `undefined`.
71
+ *
72
+ * @example
73
+ * ```tsx
74
+ * defaultFormGroupOptions: {
75
+ * onSubmitInvalid: ({ groupApi }) => {
76
+ * console.error('Invalid group', groupApi.name)
77
+ * },
78
+ * },
79
+ * ```
80
+ */
81
+ defaultFormGroupOptions?: DefaultFormGroupOptions;
82
+ }
83
+ type UseAppFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn, unknown>) => PreactAppFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
8
84
  /**
9
85
  * App Form hooks and helpers bound to the components registered with
10
86
  * `createFormHook`.
@@ -36,7 +112,7 @@ interface AppFormHookResult<in out TComponents extends AnyPreactFormComponentMap
36
112
  * }
37
113
  * ```
38
114
  */
39
- appFormOptions: AppFormOptionsApi<TComponents>;
115
+ appFormOptions: FormOptionsApi<TComponents>;
40
116
  /**
41
117
  * Defines a reusable field group whose fields expose the field components
42
118
  * registered with `createFormHook`.
@@ -120,4 +196,4 @@ interface AppFormHookResult<in out TComponents extends AnyPreactFormComponentMap
120
196
  useFormContext: () => PreactAppFormApi<any, any, TComponents>;
121
197
  }
122
198
  //#endregion
123
- export { AppFormHookResult, UseAppFormHook };
199
+ export { AppFormHookResult, CreateFormHookOptions, UseAppFormHook };
@@ -3,8 +3,13 @@ import { InternalFormApi } from "@tanstack/form-core/internals";
3
3
 
4
4
  //#region src/AppForm/initializeAppForm.lib.ts
5
5
  function createAppFormInitializer(createOptions) {
6
+ const defaultOptions = createOptions.defaultFormOptions || createOptions.defaultFieldOptions || createOptions.defaultFormGroupOptions ? {
7
+ form: createOptions.defaultFormOptions,
8
+ field: createOptions.defaultFieldOptions,
9
+ formGroup: createOptions.defaultFormGroupOptions
10
+ } : void 0;
6
11
  return (options) => {
7
- const form = new InternalFormApi(options);
12
+ const form = new InternalFormApi(options, defaultOptions);
8
13
  return attachPreactAppFormComponents(form, createOptions.formComponents, createOptions.fieldComponents);
9
14
  };
10
15
  }
@@ -11,7 +11,7 @@ interface FieldGroupArrayFieldComponent<in out TFieldData, in out TFieldComponen
11
11
  <const TFieldName extends DeepKeysWhereValueIncludes<TFieldData, Array<any>>>(props: PreactFormFieldProps<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>, FieldValidators<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>>, ValidationIssue, unknown, FormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
12
12
  }
13
13
  type FieldGroupSubscribeProps<TSelected> = PreactFormSubscribeProps<unknown, FormErrorTypes, TSelected>;
14
- type FieldGroupSubscribeComponent = <TSelected>(props: FieldGroupSubscribeProps<TSelected>) => CrossVersionPreactNode;
14
+ type FieldGroupSubscribeComponent = <const TSelected>(props: FieldGroupSubscribeProps<TSelected>) => CrossVersionPreactNode;
15
15
  interface FieldGroupApi<in out TFieldData, in out TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>> extends FormApiFieldMethods<TFieldData>, FormApiArrayMethods<TFieldData> {
16
16
  atom: ReadonlyAtom<TFieldData>;
17
17
  Field: FieldGroupFieldComponent<TFieldData, TFieldComponents>;
@@ -1,46 +1,22 @@
1
1
  import { CrossVersionPreactNode } from "../preactTypes.public.js";
2
2
  import { PreactTanStackFormComponents } from "../PreactForm/Components.public.js";
3
3
  import { FieldGroupApi } from "./FieldGroupApi.public.js";
4
- import { DeepKeys, DeepKeysWhereValueIncludes, DeepValue, FormApi } from "@tanstack/form-core";
4
+ import { FieldGroupFieldBindings, FieldGroupFieldBindingsProps, FieldGroupFieldData, FieldGroupFields, FieldGroupFieldsPropName, FieldGroupHelper, FormApi } from "@tanstack/form-core";
5
5
  import { FunctionComponent } from "preact/compat";
6
6
  //#region src/FieldGroup/withFields.public.d.ts
7
- declare const fieldGroupFieldSlotValueSymbol: unique symbol;
8
7
  declare const fieldGroupFieldsSymbol: unique symbol;
9
- type FieldGroupFieldSlotMode = 'strict' | 'loose';
10
- interface FieldGroupFieldSlot<out TValue, out TMode extends FieldGroupFieldSlotMode = FieldGroupFieldSlotMode> {
11
- readonly mode: TMode;
12
- readonly [fieldGroupFieldSlotValueSymbol]: TValue;
13
- }
14
- type AnyFieldGroupFieldSlot = FieldGroupFieldSlot<any>;
15
- type StrictFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<TValue, 'strict'>;
16
- type LooseFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<TValue, 'loose'>;
17
- type FieldGroupFieldSlotValue<TSlot> = TSlot extends FieldGroupFieldSlot<infer TValue> ? TValue : never;
18
- type FieldGroupFieldSlotModeOf<TSlot> = TSlot extends FieldGroupFieldSlot<any, infer TMode> ? TMode : never;
19
- type IsSame<TTypeA, TTypeB> = [TTypeA] extends [TTypeB] ? [TTypeB] extends [TTypeA] ? true : false : false;
20
- type FieldGroupFieldSlotAllows<TSlot, TValue> = TSlot extends FieldGroupFieldSlot<infer TAcceptedValue, infer TMode> ? TMode extends 'strict' ? IsSame<TValue, TAcceptedValue> : [TValue] extends [TAcceptedValue] ? true : false : false;
21
- type FieldGroupFieldNameForSlot<TFieldData, TSlot extends AnyFieldGroupFieldSlot> = { [TFieldName in DeepKeys<TFieldData>]: FieldGroupFieldSlotAllows<TSlot, DeepValue<TFieldData, TFieldName>> extends true ? TFieldName : never; }[DeepKeys<TFieldData>];
22
- type FieldGroupFields = Record<string, AnyFieldGroupFieldSlot>;
23
- type FieldGroupFieldNames<TFieldData, TFields extends FieldGroupFields> = { [TFieldName in keyof TFields]: FieldGroupFieldNameForSlot<TFieldData, TFields[TFieldName]>; };
24
- type FieldGroupFieldData<TFields extends FieldGroupFields> = { [TFieldName in keyof TFields]: TFields[TFieldName] extends FieldGroupFieldSlot<infer TValue, any> ? TValue : never; };
25
8
  type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
26
- readonly [fieldGroupFieldsSymbol]: infer TFields;
9
+ readonly [fieldGroupFieldsSymbol]: infer TFields extends FieldGroupFields;
27
10
  } ? TFields : never;
28
11
  type PreactFieldGroup<TFields extends FieldGroupFields, TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>> = FieldGroupApi<FieldGroupFieldData<TFields>, TFieldComponents> & {
29
12
  readonly [fieldGroupFieldsSymbol]: TFields;
30
13
  };
31
14
  type FieldGroupFieldComponentsOf<TFieldGroup> = TFieldGroup extends PreactFieldGroup<any, infer TFieldComponents> ? TFieldComponents : never;
32
15
  type FieldGroupForm<TFieldComponents extends Record<string, FunctionComponent<any>> = Record<never, never>, TFormData = any> = FormApi<TFormData, any> & PreactTanStackFormComponents<TFormData, any, TFieldComponents>;
33
- type FieldGroupFieldBindingForSlot<TFormData, TSlot extends AnyFieldGroupFieldSlot> = TSlot extends FieldGroupFieldSlot<infer TValue, infer TMode> ? TMode extends 'strict' ? FieldGroupFieldNameForSlot<TFormData, TSlot> : DeepKeysWhereValueIncludes<TFormData, TValue> : never;
34
- type FieldGroupFieldBindings<TFields extends FieldGroupFields, TFormData = any> = { [TFieldName in keyof TFields]: FieldGroupFieldBindingForSlot<TFormData, TFields[TFieldName]>; };
35
16
  type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> = FieldGroupFieldsOf<TFieldGroup> extends FieldGroupFields ? FieldGroupFieldBindings<FieldGroupFieldsOf<TFieldGroup>, TFormData> : never;
36
- type FieldGroupFieldsPropName<TProps, TFieldGroup extends PreactFieldGroup<any, any>> = { [TPropName in keyof TProps]-?: IsSame<TProps[TPropName], TFieldGroup> extends true ? TPropName : never; }[keyof TProps];
37
17
  type FieldGroupWithFieldsFn<TFieldGroup extends PreactFieldGroup<any, any>> = <TProps extends object, TFieldsPropName extends FieldGroupFieldsPropName<TProps, TFieldGroup>>(Component: (props: TProps) => CrossVersionPreactNode, fieldsPropName: TFieldsPropName) => <TFormData>(props: Omit<TProps, TFieldsPropName | 'form'> & {
38
18
  form: FieldGroupForm<FieldGroupFieldComponentsOf<TFieldGroup>, TFormData>;
39
- } & { [TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<TFieldGroup, TFormData>; }) => CrossVersionPreactNode;
40
- interface FieldGroupHelper {
41
- strict: <TValue>() => StrictFieldGroupFieldSlot<TValue>;
42
- loose: <TValue>() => LooseFieldGroupFieldSlot<TValue>;
43
- }
19
+ } & FieldGroupFieldBindingsProps<FieldGroupFieldsOf<TFieldGroup>, TFormData, TFieldsPropName>) => CrossVersionPreactNode;
44
20
  interface FieldGroupDefinition<TFields extends FieldGroupFields, TFieldComponents extends Record<string, FunctionComponent<any>>> {
45
21
  /** The virtual field-group API injected into the bound component. */
46
22
  fields: PreactFieldGroup<TFields, TFieldComponents>;
@@ -52,4 +28,4 @@ type DefineFieldGroupFn<TFieldComponents extends Record<string, FunctionComponen
52
28
  /** Defines a reusable group of virtual fields. */
53
29
  declare const defineFieldGroup: DefineFieldGroupFn<Record<never, never>>;
54
30
  //#endregion
55
- export { AnyFieldGroupFieldSlot, DefineFieldGroupFn, FieldGroupDefinition, FieldGroupFieldBindingForSlot, FieldGroupFieldBindings, FieldGroupFieldBindingsOf, FieldGroupFieldComponentsOf, FieldGroupFieldData, FieldGroupFieldNameForSlot, FieldGroupFieldNames, FieldGroupFieldSlot, FieldGroupFieldSlotAllows, FieldGroupFieldSlotMode, FieldGroupFieldSlotModeOf, FieldGroupFieldSlotValue, FieldGroupFields, FieldGroupFieldsOf, FieldGroupFieldsPropName, FieldGroupForm, FieldGroupHelper, FieldGroupWithFieldsFn, LooseFieldGroupFieldSlot, PreactFieldGroup, StrictFieldGroupFieldSlot, defineFieldGroup };
31
+ export { DefineFieldGroupFn, FieldGroupDefinition, FieldGroupFieldBindingsOf, FieldGroupFieldComponentsOf, FieldGroupFieldsOf, FieldGroupForm, FieldGroupWithFieldsFn, PreactFieldGroup, defineFieldGroup };
@@ -24,7 +24,7 @@ interface PreactSubscribeProps<in out TSourceData, in out TSelected> {
24
24
  children: ((state: NoInfer<TSelected>) => CrossVersionPreactNode) | CrossVersionPreactNode;
25
25
  }
26
26
  type PreactFormSubscribeProps<TFormData, TFormErrorTypes extends FormErrorTypes, TSelected> = PreactSubscribeProps<FormState<TFormData, TFormErrorTypes>, TSelected>;
27
- type PreactFormSubscribeComponent<in out TFormData, in out TFormErrorTypes extends FormErrorTypes> = <TSelected>(props: PreactFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>) => CrossVersionPreactNode;
27
+ type PreactFormSubscribeComponent<in out TFormData, in out TFormErrorTypes extends FormErrorTypes> = <const TSelected>(props: PreactFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>) => CrossVersionPreactNode;
28
28
  interface PreactFormFieldProps<in out TFieldData, in out TFieldName, in out TFieldValue, in out TFieldValidators extends FieldValidators<TFieldData, TFieldName, TFieldValue>, in out TGroupFieldError, in out TFormData, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> extends FieldApiOptions<TFieldData, TFieldName, TFieldValue, TFieldValidators, TGroupFieldError, TFormData, TFormErrorTypes> {
29
29
  children: (fieldApi: PreactFieldApi<TFieldName, TFieldValue, ToFieldError<NoInfer<TFieldValidators>, TGroupFieldError, TFormErrorTypes>, TFormData, TFormErrorTypes, TFieldComponents>) => CrossVersionPreactNode;
30
30
  }
@@ -35,7 +35,7 @@ type PreactFormArrayFieldComponent<in out TFormData, in out TFormErrorTypes exte
35
35
  <TFieldName extends DeepKeysWhereValueIncludes<TFormData, Array<any>>, const TFieldValidators extends FieldValidators<TFormData, TFieldName, DeepValue<TFormData, TFieldName>>>(props: PreactFormFieldProps<TFormData, TFieldName, DeepValue<TFormData, TFieldName>, TFieldValidators, never, TFormData, TFormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
36
36
  };
37
37
  type PreactFormGroupSubscribeProps<TGroupValue, TGroupErrorTypes extends FormErrorTypes, TSelected> = PreactSubscribeProps<FormGroupState<TGroupValue, TGroupErrorTypes>, TSelected>;
38
- type PreactFormGroupSubscribeComponent<in out TGroupValue, in out TGroupErrorTypes extends FormErrorTypes> = <TSelected>(props: PreactFormGroupSubscribeProps<TGroupValue, TGroupErrorTypes, TSelected>) => CrossVersionPreactNode;
38
+ type PreactFormGroupSubscribeComponent<in out TGroupValue, in out TGroupErrorTypes extends FormErrorTypes> = <const TSelected>(props: PreactFormGroupSubscribeProps<TGroupValue, TGroupErrorTypes, TSelected>) => CrossVersionPreactNode;
39
39
  interface PreactFormGroupFieldComponent<in out TFormData, in out TGroupValue, in out TGroupErrorTypes extends FormErrorTypes, in out TFormErrorTypes extends FormErrorTypes, in out TFieldComponents extends Record<string, FunctionComponent<any>>> {
40
40
  <TFieldName extends DeepKeys<TGroupValue>, const TFieldValidators extends FieldValidators<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>>>(props: PreactFormFieldProps<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>, TFieldValidators, TGroupErrorTypes['fieldError'], TFormData, TFormErrorTypes, TFieldComponents>): CrossVersionPreactNode;
41
41
  }
@@ -1,6 +1,5 @@
1
1
  import { AnyPreactFormComponentMap, DefaultPreactFormComponentMap } from "../AppForm/componentMap.public.js";
2
2
  import { PreactFormApi } from "./formApiTypes.public.js";
3
- import { AppFormOptions } from "../AppForm/appFormOptions.public.js";
4
3
  import { AnyFormOptions, FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
5
4
  //#region src/PreactForm/formType.public.d.ts
6
5
  type PreactFormTypeErrorTypes<TFormValidators extends FormValidators<any>, TSubmitReturn> = unknown extends TSubmitReturn ? any : ToFormErrorTypes<TFormValidators, TSubmitReturn>;
@@ -33,6 +32,6 @@ type PreactFormTypeErrorTypes<TFormValidators extends FormValidators<any>, TSubm
33
32
  *
34
33
  * @typeParam TOptions - The reusable form or app-form options from which the API derives its form data, error, and registered-component types.
35
34
  */
36
- type PreactFormType<TOptions extends AnyFormOptions | AppFormOptions<any, any, any, AnyPreactFormComponentMap>> = TOptions extends AppFormOptions<infer TFormData, infer TFormValidators, infer TSubmitReturn, infer TComponents> ? PreactFormApi<TFormData, PreactFormTypeErrorTypes<TFormValidators, TSubmitReturn>, TComponents> : TOptions extends FormOptions<infer TFormData, infer TFormValidators, infer TSubmitReturn> ? PreactFormApi<TFormData, PreactFormTypeErrorTypes<TFormValidators, TSubmitReturn>, DefaultPreactFormComponentMap> : never;
35
+ type PreactFormType<TOptions extends AnyFormOptions> = TOptions extends FormOptions<infer TFormData, infer TFormValidators, infer TSubmitReturn, infer TComponents> ? PreactFormApi<TFormData, PreactFormTypeErrorTypes<TFormValidators, TSubmitReturn>, TComponents extends AnyPreactFormComponentMap ? TComponents : DefaultPreactFormComponentMap> : never;
37
36
  //#endregion
38
37
  export { PreactFormType };
@@ -10,7 +10,7 @@ function useField(options, fieldComponents) {
10
10
  const field = options.form._getOrCreateFieldApi({
11
11
  ...optionsRef.current,
12
12
  name: options.name
13
- });
13
+ }, "field");
14
14
  if (fieldComponents === null) return field;
15
15
  Object.assign(field, fieldComponents);
16
16
  return field;
@@ -20,7 +20,7 @@ function useField(options, fieldComponents) {
20
20
  resetVersion,
21
21
  fieldComponents
22
22
  ]);
23
- useEffect(() => fieldApi._update(options));
23
+ useEffect(() => fieldApi._update(options, "field"));
24
24
  useEffect(() => {
25
25
  return fieldApi._register();
26
26
  }, [fieldApi]);
@@ -2,7 +2,39 @@ import { AnyPreactFormComponentMap, DefaultPreactFormComponentMap } from "../App
2
2
  import { PreactFormApi } from "./formApiTypes.public.js";
3
3
  import { FormOptions, FormValidators, ToFormErrorTypes } from "@tanstack/form-core";
4
4
  //#region src/PreactForm/useForm.public.d.ts
5
- type UseFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => PreactFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
5
+ type UseFormHook<in out TComponents extends AnyPreactFormComponentMap> = <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn, unknown>) => PreactFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
6
+ /**
7
+ * Creates a Preact form whose instance and lifecycle are owned by the current
8
+ * component.
9
+ *
10
+ * `defaultValues` establish the initial state and inferred form value type.
11
+ * Later renders apply changed options to the same form instance, and unmounting
12
+ * the component cleans it up.
13
+ *
14
+ * Call this hook at the top level of a Preact component or custom hook.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * function ProfileForm() {
19
+ * const form = useForm({
20
+ * defaultValues: { name: '' },
21
+ * onSubmit: ({ value }) => saveProfile(value),
22
+ * })
23
+ *
24
+ * return (
25
+ * <form
26
+ * onSubmit={(event) => {
27
+ * event.preventDefault()
28
+ * void form.handleSubmit()
29
+ * }}
30
+ * />
31
+ * )
32
+ * }
33
+ * ```
34
+ *
35
+ * @returns The Preact form API with typed field, subscription, and form-group
36
+ * components attached.
37
+ */
6
38
  declare const useForm: UseFormHook<DefaultPreactFormComponentMap>;
7
39
  //#endregion
8
40
  export { UseFormHook, useForm };
@@ -4,6 +4,38 @@ import { initializeForm, useInternalForm } from "./PreactFormApi.lib.js";
4
4
  function useFormHook(options) {
5
5
  return useInternalForm(options, initializeForm);
6
6
  }
7
+ /**
8
+ * Creates a Preact form whose instance and lifecycle are owned by the current
9
+ * component.
10
+ *
11
+ * `defaultValues` establish the initial state and inferred form value type.
12
+ * Later renders apply changed options to the same form instance, and unmounting
13
+ * the component cleans it up.
14
+ *
15
+ * Call this hook at the top level of a Preact component or custom hook.
16
+ *
17
+ * @example
18
+ * ```tsx
19
+ * function ProfileForm() {
20
+ * const form = useForm({
21
+ * defaultValues: { name: '' },
22
+ * onSubmit: ({ value }) => saveProfile(value),
23
+ * })
24
+ *
25
+ * return (
26
+ * <form
27
+ * onSubmit={(event) => {
28
+ * event.preventDefault()
29
+ * void form.handleSubmit()
30
+ * }}
31
+ * />
32
+ * )
33
+ * }
34
+ * ```
35
+ *
36
+ * @returns The Preact form API with typed field, subscription, and form-group
37
+ * components attached.
38
+ */
7
39
  const useForm = useFormHook;
8
40
 
9
41
  //#endregion
package/dist/index.d.ts CHANGED
@@ -3,16 +3,15 @@ import { CrossVersionPreactNode } from "./preactTypes.public.js";
3
3
  import { PreactFieldApi, PreactFormArrayFieldComponent, PreactFormFieldComponent, PreactFormFieldProps, PreactFormGroupApi, PreactFormGroupArrayFieldComponent, PreactFormGroupComponent, PreactFormGroupFieldComponent, PreactFormGroupProps, PreactFormGroupSubscribeComponent, PreactFormGroupSubscribeProps, PreactFormSubscribeComponent, PreactFormSubscribeProps, PreactTanStackFormComponents } from "./PreactForm/Components.public.js";
4
4
  import { AnyPreactFormApi, PreactFormApi } from "./PreactForm/formApiTypes.public.js";
5
5
  import { UseFormHook, useForm } from "./PreactForm/useForm.public.js";
6
- import { AppFormOptions, AppFormOptionsApi } from "./AppForm/appFormOptions.public.js";
7
6
  import { PreactFormType } from "./PreactForm/formType.public.js";
8
7
  import { AnyFieldGroupApi, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupFieldComponent, FieldGroupFormState, FieldGroupSubscribeComponent, FieldGroupSubscribeProps } from "./FieldGroup/FieldGroupApi.public.js";
9
- 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";
8
+ import { DefineFieldGroupFn, FieldGroupDefinition, FieldGroupFieldBindingsOf, FieldGroupFieldComponentsOf, FieldGroupFieldsOf, FieldGroupForm, FieldGroupWithFieldsFn, PreactFieldGroup, defineFieldGroup } from "./FieldGroup/withFields.public.js";
10
9
  import "./types.public.js";
11
10
  import { Subscribe, SubscribeProps, SubscribeSource } from "./Subscribe.public.js";
12
11
  import { AppFormComponent, PreactAppFormApi } from "./AppForm/PreactAppFormApi.public.js";
13
- import { AppFormHookResult, UseAppFormHook } from "./AppForm/createFormHookTypes.public.js";
12
+ import { AppFormHookResult, CreateFormHookOptions, UseAppFormHook } from "./AppForm/createFormHookTypes.public.js";
14
13
  import { createFormHook } from "./AppForm/createFormHook.public.js";
15
14
  import { FormHookHelpers, getFormHookHelpers } from "./AppForm/getFormHookHelpers.public.js";
16
15
  export * from "@tanstack/form-core";
17
16
  export * from "@tanstack/preact-store";
18
- export { AnyFieldGroupApi, AnyFieldGroupFieldSlot, AnyPreactFormApi, AnyPreactFormComponentMap, AppFormComponent, AppFormHookResult, 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 };
17
+ export { AnyFieldGroupApi, AnyPreactFormApi, AnyPreactFormComponentMap, AppFormComponent, AppFormHookResult, CreateFormHookOptions, CrossVersionPreactNode, DefaultPreactFormComponentMap, DefineFieldGroupFn, FieldGroupApi, FieldGroupArrayFieldComponent, FieldGroupDefinition, FieldGroupFieldBindingsOf, FieldGroupFieldComponent, FieldGroupFieldComponentsOf, FieldGroupFieldsOf, FieldGroupForm, FieldGroupFormState, FieldGroupSubscribeComponent, FieldGroupSubscribeProps, FieldGroupWithFieldsFn, FormHookHelpers, PreactAppFormApi, PreactFieldApi, PreactFieldGroup, PreactFormApi, PreactFormArrayFieldComponent, PreactFormComponentMap, PreactFormFieldComponent, PreactFormFieldProps, PreactFormGroupApi, PreactFormGroupArrayFieldComponent, PreactFormGroupComponent, PreactFormGroupFieldComponent, PreactFormGroupProps, PreactFormGroupSubscribeComponent, PreactFormGroupSubscribeProps, PreactFormSubscribeComponent, PreactFormSubscribeProps, PreactFormType, PreactTanStackFormComponents, Subscribe, SubscribeProps, SubscribeSource, UseAppFormHook, UseFormHook, createFormHook, defineFieldGroup, getFormHookHelpers, useForm };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-form",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.3",
4
4
  "description": "Powerful, type-safe forms for Preact.",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -34,14 +34,14 @@
34
34
  "dist"
35
35
  ],
36
36
  "dependencies": {
37
- "@tanstack/preact-store": "^0.13.2",
38
- "@tanstack/form-core": "2.0.0-alpha.1"
37
+ "@tanstack/form-core": "2.0.0-alpha.3",
38
+ "@tanstack/preact-store": "^0.13.2"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@eslint-react/eslint-plugin": "^5.18.3",
42
42
  "@preact/preset-vite": "^2.10.2",
43
- "@testing-library/preact": "^3.2.4",
44
43
  "preact": "^10.26.4",
44
+ "vitest-browser-preact": "^1.1.0",
45
45
  "zod": "^4.4.3"
46
46
  },
47
47
  "peerDependencies": {
@@ -1,14 +0,0 @@
1
- import { AnyPreactFormComponentMap } from "./componentMap.public.js";
2
- import { FormOptions, FormValidatorData, FormValidators, InferUnion, NullableSchemaData } from "@tanstack/form-core";
3
- //#region src/AppForm/appFormOptions.public.d.ts
4
- declare const componentsSymbol: unique symbol;
5
- interface AppFormOptions<in out TFormData, in out TFormValidators extends FormValidators<TFormData>, in out TSubmitReturn, out TComponents extends AnyPreactFormComponentMap> extends FormOptions<TFormData, TFormValidators, TSubmitReturn> {
6
- [componentsSymbol]: TComponents;
7
- }
8
- interface AppFormOptionsApi<in out TComponents extends AnyPreactFormComponentMap> {
9
- <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>): AppFormOptions<TFormData, TFormValidators, TSubmitReturn, TComponents>;
10
- strictSchema: <const TFormValidators extends FormValidators<any>, TFormData extends FormValidatorData<TFormValidators>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => AppFormOptions<FormValidatorData<TFormValidators>, TFormValidators, TSubmitReturn, TComponents>;
11
- looseSchema: <const TFormValidators extends FormValidators<any>, const TFormData extends NullableSchemaData<TFormValidators>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => AppFormOptions<InferUnion<TFormData, FormValidatorData<TFormValidators>>, TFormValidators, TSubmitReturn, TComponents>;
12
- }
13
- //#endregion
14
- export { AppFormOptions, AppFormOptionsApi };