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

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.
@@ -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';
package/dist/index.d.ts CHANGED
@@ -4,8 +4,10 @@ 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';
package/dist/index.js CHANGED
@@ -4,8 +4,10 @@ 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';
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.1",
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.1"
36
35
  },
37
36
  "devDependencies": {
38
37
  "@sveltejs/package": "^2.5.3",
@@ -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
- }
@@ -1,24 +0,0 @@
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
- }
@@ -1,82 +0,0 @@
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
- }
@@ -1,18 +0,0 @@
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
- }