@tanstack/svelte-form 2.0.0-alpha.0 → 2.0.0-alpha.2

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.
Files changed (43) hide show
  1. package/dist/AppForm/createFormHook.public.d.ts +6 -3
  2. package/dist/AppForm/createFormHook.public.js +2 -4
  3. package/dist/AppForm/createFormHookTypes.public.d.ts +81 -5
  4. package/dist/AppForm/initializeAppForm.lib.d.ts +11 -3
  5. package/dist/AppForm/initializeAppForm.lib.js +11 -1
  6. package/dist/Components.public.d.ts +2 -2
  7. package/dist/Field.svelte +8 -5
  8. package/dist/FieldGroup/FieldGroupApi.public.d.ts +1 -1
  9. package/dist/createForm.public.d.ts +23 -1
  10. package/dist/createForm.public.js +22 -0
  11. package/dist/createForm.svelte.d.ts +2 -2
  12. package/dist/formApiTypes.public.d.ts +24 -0
  13. package/dist/formType.public.d.ts +1 -2
  14. package/dist/index.d.ts +4 -3
  15. package/dist/index.js +4 -3
  16. package/package.json +3 -4
  17. package/dist/AppForm/appFormOptions.public.d.ts +0 -12
  18. package/dist/AppForm/appFormOptions.public.js +0 -1
  19. package/src/AppForm/AppForm.svelte +0 -14
  20. package/src/AppForm/Components.lib.ts +0 -28
  21. package/src/AppForm/SvelteAppFormApi.public.ts +0 -14
  22. package/src/AppForm/appFormOptions.public.ts +0 -57
  23. package/src/AppForm/componentMap.public.ts +0 -19
  24. package/src/AppForm/contexts.lib.ts +0 -24
  25. package/src/AppForm/createFormHook.public.ts +0 -33
  26. package/src/AppForm/createFormHookTypes.public.ts +0 -30
  27. package/src/AppForm/fieldComponentHelpers.lib.ts +0 -24
  28. package/src/AppForm/getFormHookHelpers.public.ts +0 -82
  29. package/src/AppForm/initializeAppForm.lib.ts +0 -18
  30. package/src/Components.public.ts +0 -371
  31. package/src/Field.svelte +0 -152
  32. package/src/FieldGroup/FieldGroupApi.public.ts +0 -100
  33. package/src/FieldGroup/withFields.lib.ts +0 -82
  34. package/src/FieldGroup/withFields.public.ts +0 -179
  35. package/src/FormGroup.svelte +0 -101
  36. package/src/Subscribe.svelte +0 -26
  37. package/src/context-keys.ts +0 -2
  38. package/src/createForm.public.ts +0 -26
  39. package/src/createForm.svelte.ts +0 -76
  40. package/src/formApiTypes.public.ts +0 -48
  41. package/src/formType.public.ts +0 -46
  42. package/src/index.ts +0 -17
  43. package/src/utils.lib.ts +0 -30
@@ -1,3 +1,6 @@
1
- import type { AnySvelteFormComponentMap } from './componentMap.public';
2
- import type { AppFormHookResult } from './createFormHookTypes.public';
3
- export declare function createFormHook<const TComponents extends AnySvelteFormComponentMap>(createOptions: TComponents): AppFormHookResult<TComponents>;
1
+ import type { AppFormHookResult, CreateFormHookOptions } from './createFormHookTypes.public';
2
+ import type { Component } from 'svelte';
3
+ export declare function createFormHook<const TFormComponents extends Record<string, Component<any>>, const TFieldComponents extends Record<string, Component<any>>>(createOptions: CreateFormHookOptions<TFormComponents, TFieldComponents>): AppFormHookResult<{
4
+ formComponents: TFormComponents;
5
+ fieldComponents: TFieldComponents;
6
+ }>;
@@ -1,10 +1,8 @@
1
+ import { formOptions } from '@tanstack/form-core';
1
2
  import { createInternalForm } from '../createForm.svelte';
2
3
  import { defineFieldGroup } from '../FieldGroup/withFields.public';
3
4
  import { createAppFormInitializer } from './initializeAppForm.lib';
4
5
  import { useFormContext } from './contexts.lib';
5
- const appFormOptions = ((opts) => opts);
6
- appFormOptions.strictSchema = (opts) => opts;
7
- appFormOptions.looseSchema = (opts) => opts;
8
6
  export function createFormHook(createOptions) {
9
7
  const initializeAppForm = createAppFormInitializer(createOptions);
10
8
  function useExtendedForm(options) {
@@ -13,7 +11,7 @@ export function createFormHook(createOptions) {
13
11
  const useAppForm = useExtendedForm;
14
12
  return {
15
13
  useFormContext: useFormContext,
16
- appFormOptions,
14
+ appFormOptions: formOptions,
17
15
  defineAppFieldGroup: defineFieldGroup,
18
16
  useAppForm,
19
17
  };
@@ -1,11 +1,87 @@
1
- import type { AppFormOptionsApi } from './appFormOptions.public';
2
- import type { AnySvelteFormComponentMap } from './componentMap.public';
1
+ import type { AnySvelteFormComponentMap, SvelteFormComponentMap } from './componentMap.public';
3
2
  import type { SvelteAppFormApi } from './SvelteAppFormApi.public';
4
3
  import type { DefineFieldGroupFn } from '../FieldGroup/withFields.public';
5
- import type { FormOptions, FormValidators, ToFormErrorTypes } from '@tanstack/form-core';
6
- export type UseAppFormHook<TComponents extends AnySvelteFormComponentMap> = <TFormData, TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: () => FormOptions<TFormData, TFormValidators, TSubmitReturn>) => SvelteAppFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
4
+ import type { Component } from 'svelte';
5
+ import type { DefaultFieldOptions, DefaultFormGroupOptions, DefaultFormOptions, FormOptions, FormOptionsApi, FormValidators, ToFormErrorTypes } from '@tanstack/form-core';
6
+ /**
7
+ * Configures the components and reusable defaults returned by
8
+ * `createFormHook`.
9
+ *
10
+ * Form core resolves each default object before the corresponding usage-site
11
+ * options. Non-listener properties passed at the usage site take precedence,
12
+ * including when explicitly set to `undefined`. Listener arrays follow the
13
+ * configured `listenersMerge` strategy.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const { useAppForm } = createFormHook({
18
+ * formComponents: {},
19
+ * fieldComponents: {
20
+ * TextField,
21
+ * },
22
+ * defaultFormOptions: {
23
+ * errorVisibility: ({ fieldState }) => fieldState.meta.isBlurred,
24
+ * },
25
+ * defaultFieldOptions: {
26
+ * errorBoundary: true,
27
+ * },
28
+ * })
29
+ * ```
30
+ *
31
+ * @typeParam TFormComponents - Library-managed. Do not specify explicitly.
32
+ * @typeParam TFieldComponents - Library-managed. Do not specify explicitly.
33
+ */
34
+ export interface CreateFormHookOptions<TFormComponents extends Record<string, Component<any>>, TFieldComponents extends Record<string, Component<any>>> extends SvelteFormComponentMap<TFormComponents, TFieldComponents> {
35
+ /**
36
+ * Defaults for every form created by `useAppForm`.
37
+ *
38
+ * Non-listener options returned by the `useAppForm` options accessor
39
+ * override these defaults, including when explicitly set to `undefined`.
40
+ * Listener arrays follow `listenersMerge`.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * defaultFormOptions: {
45
+ * errorVisibility: ({ state }) => state.submissionAttempts > 0,
46
+ * },
47
+ * ```
48
+ */
49
+ defaultFormOptions?: DefaultFormOptions;
50
+ /**
51
+ * Defaults for every field and array-field component owned by the form.
52
+ *
53
+ * Non-listener options passed to the component override these defaults,
54
+ * including when explicitly set to `undefined`. Listener arrays follow
55
+ * `listenersMerge`. This includes `group.Field` and `group.ArrayField`.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * defaultFieldOptions: {
60
+ * errorVisibility: ({ fieldState }) => fieldState.meta.isBlurred,
61
+ * },
62
+ * ```
63
+ */
64
+ defaultFieldOptions?: DefaultFieldOptions;
65
+ /**
66
+ * Defaults for every `form.FormGroup` component.
67
+ *
68
+ * Options passed to the component override these defaults, including when
69
+ * an option is explicitly `undefined`.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * defaultFormGroupOptions: {
74
+ * onSubmitInvalid: ({ groupApi }) => {
75
+ * console.error('Invalid group', groupApi.name)
76
+ * },
77
+ * },
78
+ * ```
79
+ */
80
+ defaultFormGroupOptions?: DefaultFormGroupOptions;
81
+ }
82
+ export type UseAppFormHook<TComponents extends AnySvelteFormComponentMap> = <TFormData, TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: () => FormOptions<TFormData, TFormValidators, TSubmitReturn, unknown>) => SvelteAppFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, TComponents>;
7
83
  export interface AppFormHookResult<TComponents extends AnySvelteFormComponentMap> {
8
- appFormOptions: AppFormOptionsApi<TComponents>;
84
+ appFormOptions: FormOptionsApi<TComponents>;
9
85
  defineAppFieldGroup: DefineFieldGroupFn<TComponents['fieldComponents']>;
10
86
  useAppForm: UseAppFormHook<TComponents>;
11
87
  useFormContext: () => SvelteAppFormApi<any, any, TComponents>;
@@ -1,4 +1,12 @@
1
- import type { FormOptions } from '@tanstack/form-core';
1
+ import type { DefaultFieldOptions, DefaultFormGroupOptions, DefaultFormOptions, FormOptions } from '@tanstack/form-core';
2
2
  import type { InternalSvelteFormApi } from '../createForm.svelte.js';
3
- import type { AnySvelteFormComponentMap } from './componentMap.public';
4
- export declare function createAppFormInitializer<TComponents extends AnySvelteFormComponentMap>(createOptions: TComponents): (options: FormOptions<any, any, any>) => InternalSvelteFormApi;
3
+ import type { Component } from 'svelte';
4
+ interface AnyCreateFormHookOptions {
5
+ formComponents: Record<string, Component<any>>;
6
+ fieldComponents: Record<string, Component<any>>;
7
+ defaultFormOptions?: DefaultFormOptions;
8
+ defaultFieldOptions?: DefaultFieldOptions;
9
+ defaultFormGroupOptions?: DefaultFormGroupOptions;
10
+ }
11
+ export declare function createAppFormInitializer(createOptions: AnyCreateFormHookOptions): (options: FormOptions<any, any, any, unknown>) => InternalSvelteFormApi;
12
+ export {};
@@ -1,5 +1,15 @@
1
1
  import { InternalFormApi } from '@tanstack/form-core/internals';
2
2
  import { attachSvelteAppFormComponents } from './Components.lib';
3
3
  export function createAppFormInitializer(createOptions) {
4
- return (options) => attachSvelteAppFormComponents(new InternalFormApi(options), createOptions.formComponents, createOptions.fieldComponents);
4
+ const hasDefaultOptions = createOptions.defaultFormOptions ||
5
+ createOptions.defaultFieldOptions ||
6
+ createOptions.defaultFormGroupOptions;
7
+ const defaultOptions = hasDefaultOptions
8
+ ? {
9
+ form: createOptions.defaultFormOptions,
10
+ field: createOptions.defaultFieldOptions,
11
+ formGroup: createOptions.defaultFormGroupOptions,
12
+ }
13
+ : undefined;
14
+ return (options) => attachSvelteAppFormComponents(new InternalFormApi(options, defaultOptions), createOptions.formComponents, createOptions.fieldComponents);
5
15
  }
@@ -21,7 +21,7 @@ interface SvelteSubscribeProps<TSourceData, TSelected> {
21
21
  children: Snippet<[NoInfer<TSelected>]>;
22
22
  }
23
23
  export type SvelteFormSubscribeProps<TFormData, TFormErrorTypes extends FormErrorTypes, TSelected> = SvelteSubscribeProps<FormState<TFormData, TFormErrorTypes>, TSelected>;
24
- export type SvelteFormSubscribeComponent<TFormData, TFormErrorTypes extends FormErrorTypes> = (new <TSelected>(options: ComponentConstructorOptions<SvelteFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
24
+ export type SvelteFormSubscribeComponent<TFormData, TFormErrorTypes extends FormErrorTypes> = (new <const TSelected>(options: ComponentConstructorOptions<SvelteFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
25
25
  export interface SvelteFormFieldProps<TFieldData, TFieldName, TFieldValue, TFieldValidators extends FieldValidators<TFieldData, TFieldName, TFieldValue>, TGroupFieldError, TFormData, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, Component<any>>> extends FieldApiOptions<TFieldData, TFieldName, TFieldValue, TFieldValidators, TGroupFieldError, TFormData, TFormErrorTypes> {
26
26
  children: Snippet<[
27
27
  SvelteFieldApi<TFieldName, TFieldValue, ToFieldError<NoInfer<TFieldValidators>, TGroupFieldError, TFormErrorTypes>, TFormData, TFormErrorTypes, TFieldComponents>
@@ -29,7 +29,7 @@ export interface SvelteFormFieldProps<TFieldData, TFieldName, TFieldValue, TFiel
29
29
  }
30
30
  export type SvelteFormFieldComponent<TFormData, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, Component<any>>> = (new <TFieldName extends DeepKeys<TFormData>, const TFieldValidators extends FieldValidators<TFormData, TFieldName, DeepValue<TFormData, TFieldName>>>(options: ComponentConstructorOptions<SvelteFormFieldProps<TFormData, TFieldName, DeepValue<TFormData, TFieldName>, TFieldValidators, never, TFormData, TFormErrorTypes, TFieldComponents>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
31
31
  export type SvelteFormArrayFieldComponent<TFormData, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, Component<any>>> = (new <TFieldName extends DeepKeysWhereValueIncludes<TFormData, Array<any>>, const TFieldValidators extends FieldValidators<TFormData, TFieldName, DeepValue<TFormData, TFieldName>>>(options: ComponentConstructorOptions<SvelteFormFieldProps<TFormData, TFieldName, DeepValue<TFormData, TFieldName>, TFieldValidators, never, TFormData, TFormErrorTypes, TFieldComponents>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
32
- export type SvelteFormGroupSubscribeComponent<TGroupValue, TGroupErrorTypes extends FormErrorTypes> = (new <TSelected>(options: ComponentConstructorOptions<SvelteSubscribeProps<FormGroupState<TGroupValue, TGroupErrorTypes>, TSelected>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
32
+ export type SvelteFormGroupSubscribeComponent<TGroupValue, TGroupErrorTypes extends FormErrorTypes> = (new <const TSelected>(options: ComponentConstructorOptions<SvelteSubscribeProps<FormGroupState<TGroupValue, TGroupErrorTypes>, TSelected>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
33
33
  export type SvelteFormGroupFieldComponent<TFormData, TGroupValue, TGroupErrorTypes extends FormErrorTypes, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, Component<any>>> = (new <TFieldName extends DeepKeys<TGroupValue>, const TFieldValidators extends FieldValidators<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>>>(options: ComponentConstructorOptions<SvelteFormFieldProps<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>, TFieldValidators, TGroupErrorTypes['fieldError'], TFormData, TFormErrorTypes, TFieldComponents>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
34
34
  export type SvelteFormGroupArrayFieldComponent<TFormData, TGroupValue, TGroupErrorTypes extends FormErrorTypes, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, Component<any>>> = (new <TFieldName extends DeepKeysWhereValueIncludes<TGroupValue, Array<any>>, const TFieldValidators extends FieldValidators<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>>>(options: ComponentConstructorOptions<SvelteFormFieldProps<TGroupValue, TFieldName, DeepValue<TGroupValue, TFieldName>, TFieldValidators, TGroupErrorTypes['fieldError'], TFormData, TFormErrorTypes, TFieldComponents>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
35
35
  export interface SvelteFormGroupApi<TFormData, TGroupName, TGroupValue, TGroupErrorTypes extends FormErrorTypes, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, Component<any>>> extends FormGroupApi<TFormData, TGroupName, TGroupValue, TGroupErrorTypes, TFormErrorTypes> {
package/dist/Field.svelte CHANGED
@@ -79,14 +79,17 @@
79
79
  const fieldApi = $derived.by(() => {
80
80
  resetVersion.current
81
81
  const current = options()
82
- return current.form._getOrCreateFieldApi({
83
- ...current,
84
- name: current.name,
85
- } as never)
82
+ return current.form._getOrCreateFieldApi(
83
+ {
84
+ ...current,
85
+ name: current.name,
86
+ } as never,
87
+ 'field',
88
+ )
86
89
  })
87
90
 
88
91
  $effect.pre(() => {
89
- fieldApi._update(options() as InternalFieldOptions)
92
+ fieldApi._update(options() as InternalFieldOptions, 'field')
90
93
  })
91
94
 
92
95
  $effect(() => fieldApi._register())
@@ -4,7 +4,7 @@ import type { SvelteFormFieldProps, SvelteFormSubscribeProps, WithoutFunction }
4
4
  import type { ReadonlyAtom } from '@tanstack/svelte-store';
5
5
  export type FieldGroupFieldComponent<TFieldData, TFieldComponents extends Record<string, Component<any>>> = (new <const TFieldName extends DeepKeys<TFieldData>>(options: ComponentConstructorOptions<SvelteFormFieldProps<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>, FieldValidators<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>>, ValidationIssue, unknown, FormErrorTypes, TFieldComponents>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
6
6
  export type FieldGroupArrayFieldComponent<TFieldData, TFieldComponents extends Record<string, Component<any>>> = (new <const TFieldName extends DeepKeysWhereValueIncludes<TFieldData, Array<any>>>(options: ComponentConstructorOptions<SvelteFormFieldProps<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>, FieldValidators<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>>, ValidationIssue, unknown, FormErrorTypes, TFieldComponents>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
7
- export type FieldGroupSubscribeComponent = (new <TSelected>(options: ComponentConstructorOptions<SvelteFormSubscribeProps<unknown, FormErrorTypes, TSelected>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
7
+ export type FieldGroupSubscribeComponent = (new <const TSelected>(options: ComponentConstructorOptions<SvelteFormSubscribeProps<unknown, FormErrorTypes, TSelected>>) => SvelteComponent) & Component<any> & WithoutFunction<Component>;
8
8
  export interface FieldGroupApi<TFieldData, TFieldComponents extends Record<string, Component<any>> = Record<never, never>> extends FormApiFieldMethods<TFieldData>, FormApiArrayMethods<TFieldData> {
9
9
  atom: ReadonlyAtom<TFieldData>;
10
10
  Field: FieldGroupFieldComponent<TFieldData, TFieldComponents>;
@@ -1,5 +1,27 @@
1
1
  import type { FormOptions, FormValidators, ToFormErrorTypes } from '@tanstack/form-core';
2
2
  import type { SvelteFormApi } from './formApiTypes.public';
3
3
  import type { DefaultSvelteFormComponentMap } from './AppForm/componentMap.public';
4
- export type CreateForm = <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: () => FormOptions<TFormData, TFormValidators, TSubmitReturn>) => SvelteFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, DefaultSvelteFormComponentMap>;
4
+ export type CreateForm = <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: () => FormOptions<TFormData, TFormValidators, TSubmitReturn, unknown>) => SvelteFormApi<TFormData, ToFormErrorTypes<TFormValidators, TSubmitReturn>, DefaultSvelteFormComponentMap>;
5
+ /**
6
+ * Creates a Svelte form whose instance and lifecycle are owned by the current
7
+ * component.
8
+ *
9
+ * Pass an options function. `defaultValues` establish the initial state and
10
+ * inferred form value type, while reactive values read by the function update
11
+ * the same form instance when they change.
12
+ *
13
+ * Call this during component initialization. The form mounts with the
14
+ * component and is cleaned up when the component unmounts.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const form = createForm(() => ({
19
+ * defaultValues: { name: '' },
20
+ * onSubmit: ({ value }) => saveProfile(value),
21
+ * }))
22
+ * ```
23
+ *
24
+ * @returns The Svelte form API with typed field, subscription, and form-group
25
+ * components attached.
26
+ */
5
27
  export declare const createForm: CreateForm;
@@ -2,4 +2,26 @@ import { createInternalForm, initializeForm } from './createForm.svelte';
2
2
  function createFormImpl(options) {
3
3
  return createInternalForm(options, initializeForm);
4
4
  }
5
+ /**
6
+ * Creates a Svelte form whose instance and lifecycle are owned by the current
7
+ * component.
8
+ *
9
+ * Pass an options function. `defaultValues` establish the initial state and
10
+ * inferred form value type, while reactive values read by the function update
11
+ * the same form instance when they change.
12
+ *
13
+ * Call this during component initialization. The form mounts with the
14
+ * component and is cleaned up when the component unmounts.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const form = createForm(() => ({
19
+ * defaultValues: { name: '' },
20
+ * onSubmit: ({ value }) => saveProfile(value),
21
+ * }))
22
+ * ```
23
+ *
24
+ * @returns The Svelte form API with typed field, subscription, and form-group
25
+ * components attached.
26
+ */
5
27
  export const createForm = createFormImpl;
@@ -9,5 +9,5 @@ export interface InternalSvelteFormApi extends AnyInternalFormApi, SvelteTanStac
9
9
  };
10
10
  }
11
11
  export declare function attachSvelteFormComponents(form: AnyInternalFormApi, fieldComponents: Record<string, Component<any>> | null): InternalSvelteFormApi;
12
- export declare function initializeForm(options: FormOptions<any, any, any>): InternalSvelteFormApi;
13
- export declare function createInternalForm(options: () => FormOptions<any, any, any>, initialize: (options: FormOptions<any, any, any>) => InternalSvelteFormApi): InternalSvelteFormApi;
12
+ export declare function initializeForm(options: FormOptions<any, any, any, unknown>): InternalSvelteFormApi;
13
+ export declare function createInternalForm(options: () => FormOptions<any, any, any, unknown>, initialize: (options: FormOptions<any, any, any, unknown>) => InternalSvelteFormApi): InternalSvelteFormApi;
@@ -9,5 +9,29 @@ export interface SvelteFormSelectors<TFormData, TFormErrorTypes extends FormErro
9
9
  }
10
10
  type ExtendedFormApi<TFormData, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, Component<any>>> = FormApi<TFormData, TFormErrorTypes> & SvelteTanStackFormComponents<TFormData, TFormErrorTypes, TFieldComponents> & SvelteFormSelectors<TFormData, TFormErrorTypes>;
11
11
  export type SvelteFormApi<TFormData, TFormErrorTypes extends FormErrorTypes, TComponents extends AnySvelteFormComponentMap = DefaultSvelteFormComponentMap> = unknown extends TComponents['formComponents'] ? ExtendedFormApi<TFormData, TFormErrorTypes, TComponents['fieldComponents']> : ExtendedFormApi<TFormData, TFormErrorTypes, TComponents['fieldComponents']> & TComponents['formComponents'];
12
+ /**
13
+ * A Svelte form API whose form data and error types are erased.
14
+ *
15
+ * Use it for reusable Svelte components that only need core form operations,
16
+ * selectors, and the `Field`, `ArrayField`, `Subscribe`, or `FormGroup`
17
+ * components common to every Svelte form. Field paths and values are not
18
+ * checked against a particular form shape; use `SvelteFormType` when a
19
+ * component depends on one known form.
20
+ *
21
+ * @example
22
+ * ```svelte
23
+ * <script lang="ts">
24
+ * const { form }: { form: AnySvelteFormApi } = $props()
25
+ * </script>
26
+ *
27
+ * <form.Subscribe selector={(state) => state.isSubmitting}>
28
+ * {#snippet children(isSubmitting)}
29
+ * <button type="submit" disabled={isSubmitting}>
30
+ * {isSubmitting ? 'Saving...' : 'Save'}
31
+ * </button>
32
+ * {/snippet}
33
+ * </form.Subscribe>
34
+ * ```
35
+ */
12
36
  export type AnySvelteFormApi = AnyFormApi & SvelteTanStackFormComponents<any, any, any> & SvelteFormSelectors<any, any>;
13
37
  export type { SvelteFormComponentMap } from './AppForm/componentMap.public';
@@ -1,7 +1,6 @@
1
1
  import type { AnyFormOptions, FormOptions, FormValidators, ToFormErrorTypes } from '@tanstack/form-core';
2
- import type { AppFormOptions } from './AppForm/appFormOptions.public';
3
2
  import type { AnySvelteFormComponentMap, DefaultSvelteFormComponentMap } from './AppForm/componentMap.public';
4
3
  import type { SvelteFormApi } from './formApiTypes.public';
5
4
  type SvelteFormTypeErrorTypes<TValidators extends FormValidators<any>, TSubmitReturn> = unknown extends TSubmitReturn ? any : ToFormErrorTypes<TValidators, TSubmitReturn>;
6
- export type SvelteFormType<TOptions extends AnyFormOptions | AppFormOptions<any, any, any, AnySvelteFormComponentMap>> = TOptions extends AppFormOptions<infer TData, infer TValidators, infer TSubmitReturn, infer TComponents> ? SvelteFormApi<TData, SvelteFormTypeErrorTypes<TValidators, TSubmitReturn>, TComponents> : TOptions extends FormOptions<infer TData, infer TValidators, infer TSubmitReturn> ? SvelteFormApi<TData, SvelteFormTypeErrorTypes<TValidators, TSubmitReturn>, DefaultSvelteFormComponentMap> : never;
5
+ export type SvelteFormType<TOptions extends AnyFormOptions> = TOptions extends FormOptions<infer TData, infer TValidators, infer TSubmitReturn, infer TComponents> ? SvelteFormApi<TData, SvelteFormTypeErrorTypes<TValidators, TSubmitReturn>, TComponents extends AnySvelteFormComponentMap ? TComponents : DefaultSvelteFormComponentMap> : never;
7
6
  export {};
package/dist/index.d.ts CHANGED
@@ -4,12 +4,13 @@ export * from './createForm.public';
4
4
  export * from './formApiTypes.public';
5
5
  export * from './formType.public';
6
6
  export * from './Components.public';
7
- export { default as Field, createField } from './Field.svelte';
8
- export { default as FormGroup, createFormGroup } from './FormGroup.svelte';
7
+ export * from './Field.svelte';
8
+ export { default as Field } from './Field.svelte';
9
+ export * from './FormGroup.svelte';
10
+ export { default as FormGroup } from './FormGroup.svelte';
9
11
  export * from './FieldGroup/FieldGroupApi.public';
10
12
  export * from './FieldGroup/withFields.public';
11
13
  export * from './AppForm/SvelteAppFormApi.public';
12
- export * from './AppForm/appFormOptions.public';
13
14
  export * from './AppForm/componentMap.public';
14
15
  export * from './AppForm/createFormHook.public';
15
16
  export * from './AppForm/createFormHookTypes.public';
package/dist/index.js CHANGED
@@ -4,12 +4,13 @@ export * from './createForm.public';
4
4
  export * from './formApiTypes.public';
5
5
  export * from './formType.public';
6
6
  export * from './Components.public';
7
- export { default as Field, createField } from './Field.svelte';
8
- export { default as FormGroup, createFormGroup } from './FormGroup.svelte';
7
+ export * from './Field.svelte';
8
+ export { default as Field } from './Field.svelte';
9
+ export * from './FormGroup.svelte';
10
+ export { default as FormGroup } from './FormGroup.svelte';
9
11
  export * from './FieldGroup/FieldGroupApi.public';
10
12
  export * from './FieldGroup/withFields.public';
11
13
  export * from './AppForm/SvelteAppFormApi.public';
12
- export * from './AppForm/appFormOptions.public';
13
14
  export * from './AppForm/componentMap.public';
14
15
  export * from './AppForm/createFormHook.public';
15
16
  export * from './AppForm/createFormHookTypes.public';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-form",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.2",
4
4
  "description": "Powerful, type-safe forms for Svelte.",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -27,12 +27,11 @@
27
27
  },
28
28
  "sideEffects": false,
29
29
  "files": [
30
- "dist",
31
- "src"
30
+ "dist"
32
31
  ],
33
32
  "dependencies": {
34
33
  "@tanstack/svelte-store": "^0.12.1",
35
- "@tanstack/form-core": "2.0.0-alpha.0"
34
+ "@tanstack/form-core": "2.0.0-alpha.2"
36
35
  },
37
36
  "devDependencies": {
38
37
  "@sveltejs/package": "^2.5.3",
@@ -1,12 +0,0 @@
1
- import type { FormOptions, FormValidatorData, FormValidators, InferUnion, NullableSchemaData } from '@tanstack/form-core';
2
- import type { AnySvelteFormComponentMap } from './componentMap.public';
3
- declare const componentsSymbol: unique symbol;
4
- export interface AppFormOptions<TFormData, TFormValidators extends FormValidators<TFormData>, TSubmitReturn, TComponents extends AnySvelteFormComponentMap> extends FormOptions<TFormData, TFormValidators, TSubmitReturn> {
5
- [componentsSymbol]: TComponents;
6
- }
7
- export interface AppFormOptionsApi<TComponents extends AnySvelteFormComponentMap> {
8
- <TFormData, const TFormValidators extends FormValidators<TFormData>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>): AppFormOptions<TFormData, TFormValidators, TSubmitReturn, TComponents>;
9
- strictSchema: <const TFormValidators extends FormValidators<any>, TFormData extends FormValidatorData<TFormValidators>, TSubmitReturn>(options: FormOptions<TFormData, TFormValidators, TSubmitReturn>) => AppFormOptions<FormValidatorData<TFormValidators>, TFormValidators, TSubmitReturn, TComponents>;
10
- 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>;
11
- }
12
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,14 +0,0 @@
1
- <script lang="ts">
2
- import { setContext, untrack, type Snippet } from 'svelte'
3
- import { formContextKey } from '../context-keys.js'
4
-
5
- interface Props {
6
- form: any
7
- children: Snippet
8
- }
9
-
10
- const { children, form }: Props = $props()
11
- setContext(formContextKey, untrack(() => form))
12
- </script>
13
-
14
- {@render children()}
@@ -1,28 +0,0 @@
1
- import { attachSvelteFormComponents } from '../createForm.svelte'
2
- import { withComponentProps } from '../utils.lib.js'
3
- import AppForm from './AppForm.svelte'
4
- import type { AnyInternalFormApi } from '@tanstack/form-core/internals'
5
- import type { Component } from 'svelte'
6
- import type { AnySvelteFormComponentMap } from './componentMap.public'
7
- import type { SvelteAppFormApi } from './SvelteAppFormApi.public'
8
-
9
- type AnySvelteAppFormApi = SvelteAppFormApi<any, any, AnySvelteFormComponentMap>
10
-
11
- export function attachSvelteAppFormComponents(
12
- form: AnyInternalFormApi,
13
- formComponents: Record<string, Component<any>>,
14
- fieldComponents: Record<string, Component<any>>,
15
- ): AnySvelteAppFormApi {
16
- const result = attachSvelteFormComponents(
17
- form,
18
- fieldComponents,
19
- ) as never as AnySvelteAppFormApi
20
-
21
- result.AppForm = ((internals: any, props: any) =>
22
- AppForm(
23
- internals,
24
- withComponentProps(props, { form: result }),
25
- )) as Component<any>
26
-
27
- return Object.assign(result, formComponents)
28
- }
@@ -1,14 +0,0 @@
1
- import type { FormErrorTypes } from '@tanstack/form-core'
2
- import type { Component, Snippet } from 'svelte'
3
- import type { SvelteFormApi } from '../formApiTypes.public'
4
- import type { AnySvelteFormComponentMap } from './componentMap.public'
5
-
6
- export type AppFormComponent = Component<{ children: Snippet }>
7
-
8
- export type SvelteAppFormApi<
9
- TFormData,
10
- TFormErrorTypes extends FormErrorTypes,
11
- TComponents extends AnySvelteFormComponentMap,
12
- > = SvelteFormApi<TFormData, TFormErrorTypes, TComponents> & {
13
- AppForm: AppFormComponent
14
- }
@@ -1,57 +0,0 @@
1
- import type {
2
- FormOptions,
3
- FormValidatorData,
4
- FormValidators,
5
- InferUnion,
6
- NullableSchemaData,
7
- } from '@tanstack/form-core'
8
- import type { AnySvelteFormComponentMap } from './componentMap.public'
9
-
10
- declare const componentsSymbol: unique symbol
11
-
12
- export interface AppFormOptions<
13
- TFormData,
14
- TFormValidators extends FormValidators<TFormData>,
15
- TSubmitReturn,
16
- TComponents extends AnySvelteFormComponentMap,
17
- > extends FormOptions<TFormData, TFormValidators, TSubmitReturn> {
18
- [componentsSymbol]: TComponents
19
- }
20
-
21
- export interface AppFormOptionsApi<
22
- TComponents extends AnySvelteFormComponentMap,
23
- > {
24
- <
25
- TFormData,
26
- const TFormValidators extends FormValidators<TFormData>,
27
- TSubmitReturn,
28
- >(
29
- options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
30
- ): AppFormOptions<TFormData, TFormValidators, TSubmitReturn, TComponents>
31
-
32
- strictSchema: <
33
- const TFormValidators extends FormValidators<any>,
34
- TFormData extends FormValidatorData<TFormValidators>,
35
- TSubmitReturn,
36
- >(
37
- options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
38
- ) => AppFormOptions<
39
- FormValidatorData<TFormValidators>,
40
- TFormValidators,
41
- TSubmitReturn,
42
- TComponents
43
- >
44
-
45
- looseSchema: <
46
- const TFormValidators extends FormValidators<any>,
47
- const TFormData extends NullableSchemaData<TFormValidators>,
48
- TSubmitReturn,
49
- >(
50
- options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
51
- ) => AppFormOptions<
52
- InferUnion<TFormData, FormValidatorData<TFormValidators>>,
53
- TFormValidators,
54
- TSubmitReturn,
55
- TComponents
56
- >
57
- }
@@ -1,19 +0,0 @@
1
- import type { Component } from 'svelte'
2
-
3
- export interface SvelteFormComponentMap<
4
- TFormComponents extends Record<string, Component<any>>,
5
- TFieldComponents extends Record<string, Component<any>>,
6
- > {
7
- formComponents: TFormComponents
8
- fieldComponents: TFieldComponents
9
- }
10
-
11
- export type AnySvelteFormComponentMap = SvelteFormComponentMap<
12
- Record<string, Component<any>>,
13
- Record<string, Component<any>>
14
- >
15
-
16
- export type DefaultSvelteFormComponentMap = SvelteFormComponentMap<
17
- Record<never, never>,
18
- Record<never, never>
19
- >
@@ -1,24 +0,0 @@
1
- import { getContext } from 'svelte'
2
- import { fieldContextKey, formContextKey } from '../context-keys.js'
3
- import type { AnyInternalFieldApi } from '@tanstack/form-core/internals'
4
- import type { InternalSvelteFormApi } from '../createForm.svelte.js'
5
-
6
- export function useFieldContext(): AnyInternalFieldApi {
7
- const field = getContext<AnyInternalFieldApi | undefined>(fieldContextKey)
8
- if (field === undefined) {
9
- throw new Error(
10
- 'TanStack Form: Field components must be used within a `form.Field` component.',
11
- )
12
- }
13
- return field
14
- }
15
-
16
- export function useFormContext(): InternalSvelteFormApi {
17
- const form = getContext<InternalSvelteFormApi | undefined>(formContextKey)
18
- if (form === undefined) {
19
- throw new Error(
20
- 'TanStack Form: Form components must be used within a `form.AppForm` component.',
21
- )
22
- }
23
- return form
24
- }
@@ -1,33 +0,0 @@
1
- import { createInternalForm } from '../createForm.svelte'
2
- import { defineFieldGroup } from '../FieldGroup/withFields.public'
3
- import { createAppFormInitializer } from './initializeAppForm.lib'
4
- import { useFormContext } from './contexts.lib'
5
- import type { AppFormOptionsApi } from './appFormOptions.public'
6
- import type { AnySvelteFormComponentMap } from './componentMap.public'
7
- import type {
8
- AppFormHookResult,
9
- UseAppFormHook,
10
- } from './createFormHookTypes.public'
11
- import type { FormOptions } from '@tanstack/form-core'
12
-
13
- const appFormOptions = ((opts: unknown) => opts) as AppFormOptionsApi<any>
14
- appFormOptions.strictSchema = (opts) => opts as never
15
- appFormOptions.looseSchema = (opts) => opts as never
16
-
17
- export function createFormHook<
18
- const TComponents extends AnySvelteFormComponentMap,
19
- >(createOptions: TComponents): AppFormHookResult<TComponents> {
20
- const initializeAppForm = createAppFormInitializer(createOptions)
21
-
22
- function useExtendedForm(options: () => FormOptions<any, any, any>) {
23
- return createInternalForm(options, initializeAppForm)
24
- }
25
- const useAppForm = useExtendedForm as never as UseAppFormHook<TComponents>
26
-
27
- return {
28
- useFormContext: useFormContext as never,
29
- appFormOptions,
30
- defineAppFieldGroup: defineFieldGroup,
31
- useAppForm,
32
- }
33
- }
@@ -1,30 +0,0 @@
1
- import type { AppFormOptionsApi } from './appFormOptions.public'
2
- import type { AnySvelteFormComponentMap } from './componentMap.public'
3
- import type { SvelteAppFormApi } from './SvelteAppFormApi.public'
4
- import type { DefineFieldGroupFn } from '../FieldGroup/withFields.public'
5
- import type {
6
- FormOptions,
7
- FormValidators,
8
- ToFormErrorTypes,
9
- } from '@tanstack/form-core'
10
-
11
- export type UseAppFormHook<TComponents extends AnySvelteFormComponentMap> = <
12
- TFormData,
13
- TFormValidators extends FormValidators<TFormData>,
14
- TSubmitReturn,
15
- >(
16
- options: () => FormOptions<TFormData, TFormValidators, TSubmitReturn>,
17
- ) => SvelteAppFormApi<
18
- TFormData,
19
- ToFormErrorTypes<TFormValidators, TSubmitReturn>,
20
- TComponents
21
- >
22
-
23
- export interface AppFormHookResult<
24
- TComponents extends AnySvelteFormComponentMap,
25
- > {
26
- appFormOptions: AppFormOptionsApi<TComponents>
27
- defineAppFieldGroup: DefineFieldGroupFn<TComponents['fieldComponents']>
28
- useAppForm: UseAppFormHook<TComponents>
29
- useFormContext: () => SvelteAppFormApi<any, any, TComponents>
30
- }