@tanstack/svelte-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.
Files changed (86) hide show
  1. package/dist/AppForm/AppForm.svelte +14 -0
  2. package/dist/AppForm/Components.lib.d.ts +7 -0
  3. package/dist/AppForm/Components.lib.js +8 -0
  4. package/dist/AppForm/SvelteAppFormApi.public.d.ts +10 -0
  5. package/dist/AppForm/appFormOptions.public.d.ts +12 -0
  6. package/dist/AppForm/appFormOptions.public.js +1 -0
  7. package/dist/AppForm/componentMap.public.d.ts +7 -0
  8. package/dist/AppForm/componentMap.public.js +1 -0
  9. package/dist/AppForm/contexts.lib.d.ts +4 -0
  10. package/dist/AppForm/contexts.lib.js +16 -0
  11. package/dist/AppForm/createFormHook.public.d.ts +3 -0
  12. package/dist/AppForm/createFormHook.public.js +20 -0
  13. package/dist/AppForm/createFormHookTypes.public.d.ts +12 -0
  14. package/dist/AppForm/createFormHookTypes.public.js +1 -0
  15. package/dist/AppForm/fieldComponentHelpers.lib.d.ts +5 -0
  16. package/dist/AppForm/fieldComponentHelpers.lib.js +10 -0
  17. package/dist/AppForm/getFormHookHelpers.public.d.ts +31 -0
  18. package/dist/AppForm/getFormHookHelpers.public.js +13 -0
  19. package/dist/AppForm/initializeAppForm.lib.d.ts +4 -0
  20. package/dist/AppForm/initializeAppForm.lib.js +5 -0
  21. package/dist/Components.public.d.ts +52 -0
  22. package/dist/Components.public.js +1 -0
  23. package/dist/Field.svelte +125 -314
  24. package/dist/Field.svelte.d.ts +14 -30
  25. package/dist/FieldGroup/FieldGroupApi.public.d.ts +15 -0
  26. package/dist/FieldGroup/FieldGroupApi.public.js +1 -0
  27. package/dist/FieldGroup/withFields.lib.d.ts +5 -0
  28. package/dist/FieldGroup/withFields.lib.js +44 -0
  29. package/dist/FieldGroup/withFields.public.d.ts +70 -0
  30. package/dist/FieldGroup/withFields.public.js +3 -0
  31. package/dist/FormGroup.svelte +81 -302
  32. package/dist/FormGroup.svelte.d.ts +13 -30
  33. package/dist/Subscribe.svelte +18 -8
  34. package/dist/Subscribe.svelte.d.ts +5 -3
  35. package/dist/createForm.public.d.ts +5 -0
  36. package/dist/createForm.public.js +5 -0
  37. package/dist/createForm.svelte.d.ts +11 -27
  38. package/dist/createForm.svelte.js +29 -19
  39. package/dist/formApiTypes.public.d.ts +13 -0
  40. package/dist/formApiTypes.public.js +1 -0
  41. package/dist/formType.public.d.ts +7 -0
  42. package/dist/formType.public.js +1 -0
  43. package/dist/index.d.ts +13 -4
  44. package/dist/index.js +13 -3
  45. package/dist/utils.lib.d.ts +1 -0
  46. package/dist/utils.lib.js +28 -0
  47. package/package.json +5 -5
  48. package/src/AppForm/AppForm.svelte +14 -0
  49. package/src/AppForm/Components.lib.ts +28 -0
  50. package/src/AppForm/SvelteAppFormApi.public.ts +14 -0
  51. package/src/AppForm/appFormOptions.public.ts +57 -0
  52. package/src/AppForm/componentMap.public.ts +19 -0
  53. package/src/AppForm/contexts.lib.ts +24 -0
  54. package/src/AppForm/createFormHook.public.ts +33 -0
  55. package/src/AppForm/createFormHookTypes.public.ts +30 -0
  56. package/src/AppForm/fieldComponentHelpers.lib.ts +24 -0
  57. package/src/AppForm/getFormHookHelpers.public.ts +82 -0
  58. package/src/AppForm/initializeAppForm.lib.ts +18 -0
  59. package/src/Components.public.ts +371 -0
  60. package/src/Field.svelte +125 -314
  61. package/src/FieldGroup/FieldGroupApi.public.ts +100 -0
  62. package/src/FieldGroup/withFields.lib.ts +82 -0
  63. package/src/FieldGroup/withFields.public.ts +179 -0
  64. package/src/FormGroup.svelte +81 -302
  65. package/src/Subscribe.svelte +18 -8
  66. package/src/createForm.public.ts +26 -0
  67. package/src/createForm.svelte.ts +62 -336
  68. package/src/formApiTypes.public.ts +48 -0
  69. package/src/formType.public.ts +46 -0
  70. package/src/index.ts +13 -12
  71. package/src/utils.lib.ts +30 -0
  72. package/dist/AppField.svelte +0 -19
  73. package/dist/AppField.svelte.d.ts +0 -10
  74. package/dist/AppForm.svelte +0 -16
  75. package/dist/InnerAppField.svelte +0 -17
  76. package/dist/InnerAppField.svelte.d.ts +0 -9
  77. package/dist/createFormCreator.svelte.d.ts +0 -26
  78. package/dist/createFormCreator.svelte.js +0 -51
  79. package/dist/types.d.ts +0 -21
  80. package/src/AppField.svelte +0 -19
  81. package/src/AppForm.svelte +0 -16
  82. package/src/InnerAppField.svelte +0 -17
  83. package/src/createFormCreator.svelte.ts +0 -321
  84. package/src/types.ts +0 -567
  85. /package/dist/{AppForm.svelte.d.ts → AppForm/AppForm.svelte.d.ts} +0 -0
  86. /package/dist/{types.js → AppForm/SvelteAppFormApi.public.js} +0 -0
@@ -1,29 +1,13 @@
1
- import { FormApi } from '@tanstack/form-core';
2
- import type { Component, ComponentConstructorOptions, Snippet, SvelteComponent } from 'svelte';
3
- import type { FormAsyncValidateOrFn, FormOptions, FormState, FormValidateOrFn } from '@tanstack/form-core';
4
- import type { FieldComponent, FormGroupComponent, WithoutFunction } from './types.js';
5
- export interface SvelteFormApi<TParentData, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>, TSubmitMeta> {
6
- Field: FieldComponent<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TSubmitMeta>;
7
- FormGroup: FormGroupComponent<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TSubmitMeta>;
8
- useSelector: <TSelected = NoInfer<FormState<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer>>>(selector?: (state: NoInfer<FormState<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer>>) => TSelected) => {
9
- current: TSelected;
1
+ import type { FormOptions } from '@tanstack/form-core';
2
+ import type { Component } from 'svelte';
3
+ import type { AnyInternalFormApi } from '@tanstack/form-core/internals';
4
+ import type { SvelteTanStackFormComponents } from './Components.public';
5
+ export interface InternalSvelteFormApi extends AnyInternalFormApi, SvelteTanStackFormComponents<any, any, any> {
6
+ _Subscribe: Component<any>;
7
+ useSelector: <TSelected = any>(selector?: (state: any) => TSelected) => {
8
+ readonly current: TSelected;
10
9
  };
11
- /**
12
- * @deprecated Use `form.useSelector` instead.
13
- */
14
- useStore: <TSelected = NoInfer<FormState<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer>>>(selector?: (state: NoInfer<FormState<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer>>) => TSelected) => {
15
- current: TSelected;
16
- };
17
- Subscribe: (<TSelected = NoInfer<FormState<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer>>>(internal: any, props: {
18
- selector?: (state: NoInfer<FormState<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer>>) => TSelected;
19
- children: Snippet<[NoInfer<TSelected>]>;
20
- }) => {}) & (new <TSelected = NoInfer<FormState<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer>>>(opts: ComponentConstructorOptions<{
21
- selector?: (state: NoInfer<FormState<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer>>) => TSelected;
22
- children: Snippet<[NoInfer<TSelected>]>;
23
- }>) => SvelteComponent) & WithoutFunction<Component>;
24
10
  }
25
- /**
26
- * An extended version of the `FormApi` class that includes Svelte-specific functionalities from `SvelteFormApi`
27
- */
28
- export type SvelteFormExtendedApi<TFormData, TOnMount extends undefined | FormValidateOrFn<TFormData>, TOnChange extends undefined | FormValidateOrFn<TFormData>, TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnBlur extends undefined | FormValidateOrFn<TFormData>, TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnSubmit extends undefined | FormValidateOrFn<TFormData>, TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnDynamic extends undefined | FormValidateOrFn<TFormData>, TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>, TSubmitMeta> = FormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta> & SvelteFormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>;
29
- export declare function createForm<TParentData, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>, TSubmitMeta>(opts?: () => FormOptions<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TSubmitMeta>): FormApi<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TSubmitMeta> & SvelteFormApi<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TSubmitMeta>;
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;
@@ -1,25 +1,35 @@
1
- import { FormApi } from '@tanstack/form-core';
1
+ import { InternalFormApi } from '@tanstack/form-core/internals';
2
2
  import { useSelector } from '@tanstack/svelte-store';
3
3
  import { onMount } from 'svelte';
4
4
  import Field from './Field.svelte';
5
5
  import FormGroup from './FormGroup.svelte';
6
6
  import Subscribe from './Subscribe.svelte';
7
- export function createForm(opts) {
8
- const options = opts?.();
9
- const api = new FormApi(options);
10
- const extendedApi = api;
11
- // @ts-expect-error constructor definition exists only on a type level
12
- extendedApi.Field = (internal, props) => Field(internal, { ...props, form: api });
13
- // @ts-expect-error constructor definition exists only on a type level
14
- extendedApi.FormGroup = (internal, props) => FormGroup(internal, { ...props, form: api });
15
- extendedApi.useSelector = (selector) => useSelector(api.store, selector);
16
- /** @deprecated Use `form.useSelector` instead. */
17
- extendedApi.useStore = extendedApi.useSelector;
18
- // @ts-expect-error constructor definition exists only on a type level
19
- extendedApi.Subscribe = (internal, props) => Subscribe(internal, { ...props, store: api.store });
20
- onMount(api.mount);
21
- // formApi.update should not have any side effects. Think of it like a `useRef`
22
- // that we need to keep updated every render with the most up-to-date information.
23
- $effect.pre(() => api.update(opts?.()));
24
- return extendedApi;
7
+ import { withComponentProps } from './utils.lib.js';
8
+ export function attachSvelteFormComponents(form, fieldComponents) {
9
+ const result = form;
10
+ result.Field = ((internals, props) => Field(internals, withComponentProps(props, {
11
+ form,
12
+ fieldComponents: fieldComponents ?? {},
13
+ })));
14
+ result.ArrayField = ((internals, props) => Field(internals, withComponentProps(props, {
15
+ form,
16
+ array: true,
17
+ fieldComponents: fieldComponents ?? {},
18
+ })));
19
+ result._Subscribe = (internals, props) => Subscribe(internals, props);
20
+ result.Subscribe = ((internals, props) => Subscribe(internals, withComponentProps(props, { source: form.atom })));
21
+ result.FormGroup = ((internals, props) => FormGroup(internals, withComponentProps(props, { form })));
22
+ result.useSelector = ((selector) => useSelector(form.atom, selector));
23
+ return result;
24
+ }
25
+ export function initializeForm(options) {
26
+ return attachSvelteFormComponents(new InternalFormApi(options), null);
27
+ }
28
+ export function createInternalForm(options, initialize) {
29
+ const form = initialize(options());
30
+ $effect.pre(() => {
31
+ form._update(options());
32
+ });
33
+ onMount(() => form.mount());
34
+ return form;
25
35
  }
@@ -0,0 +1,13 @@
1
+ import type { AnyFormApi, FormApi, FormErrorTypes, FormState } from '@tanstack/form-core';
2
+ import type { Component } from 'svelte';
3
+ import type { SvelteTanStackFormComponents } from './Components.public';
4
+ import type { AnySvelteFormComponentMap, DefaultSvelteFormComponentMap } from './AppForm/componentMap.public';
5
+ export interface SvelteFormSelectors<TFormData, TFormErrorTypes extends FormErrorTypes> {
6
+ useSelector: <TSelected = FormState<TFormData, TFormErrorTypes>>(selector?: (state: FormState<TFormData, TFormErrorTypes>) => TSelected) => {
7
+ readonly current: TSelected;
8
+ };
9
+ }
10
+ type ExtendedFormApi<TFormData, TFormErrorTypes extends FormErrorTypes, TFieldComponents extends Record<string, Component<any>>> = FormApi<TFormData, TFormErrorTypes> & SvelteTanStackFormComponents<TFormData, TFormErrorTypes, TFieldComponents> & SvelteFormSelectors<TFormData, TFormErrorTypes>;
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
+ export type AnySvelteFormApi = AnyFormApi & SvelteTanStackFormComponents<any, any, any> & SvelteFormSelectors<any, any>;
13
+ export type { SvelteFormComponentMap } from './AppForm/componentMap.public';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { AnyFormOptions, FormOptions, FormValidators, ToFormErrorTypes } from '@tanstack/form-core';
2
+ import type { AppFormOptions } from './AppForm/appFormOptions.public';
3
+ import type { AnySvelteFormComponentMap, DefaultSvelteFormComponentMap } from './AppForm/componentMap.public';
4
+ import type { SvelteFormApi } from './formApiTypes.public';
5
+ 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;
7
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,7 +1,16 @@
1
1
  export * from '@tanstack/form-core';
2
- export { useSelector, useStore } from '@tanstack/svelte-store';
3
- export { createForm, type SvelteFormApi } from './createForm.svelte.js';
2
+ export * from '@tanstack/svelte-store';
3
+ export * from './createForm.public';
4
+ export * from './formApiTypes.public';
5
+ export * from './formType.public';
6
+ export * from './Components.public';
4
7
  export { default as Field, createField } from './Field.svelte';
5
8
  export { default as FormGroup, createFormGroup } from './FormGroup.svelte';
6
- export type { FieldComponent, FormGroupComponent } from './types.js';
7
- export { createFormCreator, createFormCreatorContexts, } from './createFormCreator.svelte.js';
9
+ export * from './FieldGroup/FieldGroupApi.public';
10
+ export * from './FieldGroup/withFields.public';
11
+ export * from './AppForm/SvelteAppFormApi.public';
12
+ export * from './AppForm/appFormOptions.public';
13
+ export * from './AppForm/componentMap.public';
14
+ export * from './AppForm/createFormHook.public';
15
+ export * from './AppForm/createFormHookTypes.public';
16
+ export * from './AppForm/getFormHookHelpers.public';
package/dist/index.js CHANGED
@@ -1,6 +1,16 @@
1
1
  export * from '@tanstack/form-core';
2
- export { useSelector, useStore } from '@tanstack/svelte-store';
3
- export { createForm } from './createForm.svelte.js';
2
+ export * from '@tanstack/svelte-store';
3
+ export * from './createForm.public';
4
+ export * from './formApiTypes.public';
5
+ export * from './formType.public';
6
+ export * from './Components.public';
4
7
  export { default as Field, createField } from './Field.svelte';
5
8
  export { default as FormGroup, createFormGroup } from './FormGroup.svelte';
6
- export { createFormCreator, createFormCreatorContexts, } from './createFormCreator.svelte.js';
9
+ export * from './FieldGroup/FieldGroupApi.public';
10
+ export * from './FieldGroup/withFields.public';
11
+ export * from './AppForm/SvelteAppFormApi.public';
12
+ export * from './AppForm/appFormOptions.public';
13
+ export * from './AppForm/componentMap.public';
14
+ export * from './AppForm/createFormHook.public';
15
+ export * from './AppForm/createFormHookTypes.public';
16
+ export * from './AppForm/getFormHookHelpers.public';
@@ -0,0 +1 @@
1
+ export declare function withComponentProps<TProps extends object, TExtra extends object>(props: TProps, extra: TExtra): TProps & TExtra;
@@ -0,0 +1,28 @@
1
+ export function withComponentProps(props, extra) {
2
+ return new Proxy(props, {
3
+ get(target, property, receiver) {
4
+ if (Reflect.has(extra, property))
5
+ return Reflect.get(extra, property);
6
+ return Reflect.get(target, property, receiver);
7
+ },
8
+ has(target, property) {
9
+ return Reflect.has(extra, property) || Reflect.has(target, property);
10
+ },
11
+ ownKeys(target) {
12
+ return [
13
+ ...new Set([...Reflect.ownKeys(target), ...Reflect.ownKeys(extra)]),
14
+ ];
15
+ },
16
+ getOwnPropertyDescriptor(target, property) {
17
+ if (Reflect.has(extra, property)) {
18
+ return {
19
+ configurable: true,
20
+ enumerable: true,
21
+ value: Reflect.get(extra, property),
22
+ writable: false,
23
+ };
24
+ }
25
+ return Reflect.getOwnPropertyDescriptor(target, property);
26
+ },
27
+ });
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-form",
3
- "version": "1.33.3",
3
+ "version": "2.0.0-alpha.0",
4
4
  "description": "Powerful, type-safe forms for Svelte.",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -31,15 +31,15 @@
31
31
  "src"
32
32
  ],
33
33
  "dependencies": {
34
- "@tanstack/svelte-store": "^0.12.0",
35
- "@tanstack/form-core": "1.33.3"
34
+ "@tanstack/svelte-store": "^0.12.1",
35
+ "@tanstack/form-core": "2.0.0-alpha.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@sveltejs/package": "^2.5.3",
39
- "@sveltejs/vite-plugin-svelte": "^5.1.1",
39
+ "@sveltejs/vite-plugin-svelte": "^7.2.0",
40
40
  "@testing-library/svelte": "^5.2.8",
41
41
  "svelte": "^5.39.4",
42
- "svelte-check": "^4.3.1"
42
+ "svelte-check": "^4.7.5"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "svelte": "^5.0.0"
@@ -0,0 +1,14 @@
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()}
@@ -0,0 +1,28 @@
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
+ }
@@ -0,0 +1,14 @@
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
+ }
@@ -0,0 +1,57 @@
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
+ }
@@ -0,0 +1,19 @@
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
+ >
@@ -0,0 +1,24 @@
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
+ }
@@ -0,0 +1,33 @@
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
+ }
@@ -0,0 +1,30 @@
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
+ }
@@ -0,0 +1,24 @@
1
+ import { withComponentProps } from '../utils.lib.js'
2
+ import { useFieldContext } from './contexts.lib'
3
+ import type { Component } from 'svelte'
4
+
5
+ type AnyFieldComponent = Component<any>
6
+
7
+ export function wrapField(
8
+ Component: AnyFieldComponent,
9
+ fieldPropKey: string,
10
+ ): AnyFieldComponent {
11
+ return ((internals: any, props: any) =>
12
+ Component(
13
+ internals,
14
+ withComponentProps(props, {
15
+ [fieldPropKey]: useFieldContext(),
16
+ }),
17
+ )) as AnyFieldComponent
18
+ }
19
+
20
+ export function brandComponentFactory(): (
21
+ component: AnyFieldComponent,
22
+ ) => AnyFieldComponent {
23
+ return (component) => component
24
+ }
@@ -0,0 +1,82 @@
1
+ import { brandComponentFactory, wrapField } from './fieldComponentHelpers.lib'
2
+ import type { Component, ComponentProps } from 'svelte'
3
+ import type { FieldWithValue } from '@tanstack/form-core'
4
+
5
+ type ExactFieldBrand<out TValue> = {
6
+ readonly __tanstackFieldExactType: TValue
7
+ }
8
+ type AcceptsFieldBrand<out TAcceptedValue> = {
9
+ readonly __tanstackFieldAcceptsType: TAcceptedValue
10
+ }
11
+ type AnyFieldComponent = Component<any>
12
+ type PropsOf<TComponent extends AnyFieldComponent> = ComponentProps<TComponent>
13
+ type FieldValuePropKeys<TProps> = {
14
+ [K in keyof TProps]-?: TProps[K] extends FieldWithValue<any> ? K : never
15
+ }[keyof TProps]
16
+ type FieldValueFromProp<TProp> =
17
+ TProp extends FieldWithValue<infer TValue> ? TValue : never
18
+ type PublicPropsReplacingField<
19
+ TProps,
20
+ TFieldPropKey extends keyof TProps,
21
+ > = Omit<TProps, TFieldPropKey>
22
+
23
+ type StrictInjectedFieldComponent<
24
+ TComponent extends AnyFieldComponent,
25
+ TProps = PropsOf<TComponent>,
26
+ TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>,
27
+ > = Component<PublicPropsReplacingField<TProps, TFieldPropKey>> &
28
+ ExactFieldBrand<FieldValueFromProp<TProps[TFieldPropKey]>>
29
+ type LooseInjectedFieldComponent<
30
+ TComponent extends AnyFieldComponent,
31
+ TProps = PropsOf<TComponent>,
32
+ TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>,
33
+ > = Component<PublicPropsReplacingField<TProps, TFieldPropKey>> &
34
+ AcceptsFieldBrand<FieldValueFromProp<TProps[TFieldPropKey]>>
35
+
36
+ interface FieldComponentHelper {
37
+ strict: <
38
+ TComponent extends AnyFieldComponent,
39
+ TProps = PropsOf<TComponent>,
40
+ TFieldPropKey extends FieldValuePropKeys<TProps> =
41
+ FieldValuePropKeys<TProps>,
42
+ >(
43
+ Component: TComponent,
44
+ fieldPropKey: TFieldPropKey,
45
+ ) => StrictInjectedFieldComponent<TComponent, TProps, TFieldPropKey>
46
+ loose: <
47
+ TComponent extends AnyFieldComponent,
48
+ TProps = PropsOf<TComponent>,
49
+ TFieldPropKey extends FieldValuePropKeys<TProps> =
50
+ FieldValuePropKeys<TProps>,
51
+ >(
52
+ Component: TComponent,
53
+ fieldPropKey: TFieldPropKey,
54
+ ) => LooseInjectedFieldComponent<TComponent, TProps, TFieldPropKey>
55
+ }
56
+
57
+ interface FieldBrandHelper {
58
+ strict: <TValue>() => <TComponent extends AnyFieldComponent>(
59
+ component: TComponent,
60
+ ) => TComponent & ExactFieldBrand<TValue>
61
+ loose: <TValue>() => <TComponent extends AnyFieldComponent>(
62
+ component: TComponent,
63
+ ) => TComponent & AcceptsFieldBrand<TValue>
64
+ }
65
+
66
+ export interface FormHookHelpers {
67
+ fieldBrand: FieldBrandHelper
68
+ fieldComponent: FieldComponentHelper
69
+ }
70
+
71
+ export function getFormHookHelpers(): FormHookHelpers {
72
+ return {
73
+ fieldComponent: {
74
+ strict: wrapField as FieldComponentHelper['strict'],
75
+ loose: wrapField as FieldComponentHelper['loose'],
76
+ },
77
+ fieldBrand: {
78
+ strict: brandComponentFactory as FieldBrandHelper['strict'],
79
+ loose: brandComponentFactory as FieldBrandHelper['loose'],
80
+ },
81
+ }
82
+ }
@@ -0,0 +1,18 @@
1
+ import { InternalFormApi } from '@tanstack/form-core/internals'
2
+ import { attachSvelteAppFormComponents } from './Components.lib'
3
+ import type { FormOptions } from '@tanstack/form-core'
4
+ import type { InternalSvelteFormApi } from '../createForm.svelte.js'
5
+ import type { AnySvelteFormComponentMap } from './componentMap.public'
6
+
7
+ export function createAppFormInitializer<
8
+ TComponents extends AnySvelteFormComponentMap,
9
+ >(
10
+ createOptions: TComponents,
11
+ ): (options: FormOptions<any, any, any>) => InternalSvelteFormApi {
12
+ return (options) =>
13
+ attachSvelteAppFormComponents(
14
+ new InternalFormApi(options),
15
+ createOptions.formComponents,
16
+ createOptions.fieldComponents,
17
+ ) as never
18
+ }