@tanstack/svelte-form 1.26.0 → 1.27.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.
- package/dist/AppField.svelte +19 -0
- package/dist/AppField.svelte.d.ts +10 -0
- package/dist/AppForm.svelte +16 -0
- package/dist/AppForm.svelte.d.ts +8 -0
- package/dist/InnerAppField.svelte +17 -0
- package/dist/InnerAppField.svelte.d.ts +9 -0
- package/dist/context-keys.d.ts +2 -0
- package/dist/context-keys.js +2 -0
- package/dist/createForm.svelte.d.ts +4 -0
- package/dist/createFormCreator.svelte.d.ts +25 -0
- package/dist/createFormCreator.svelte.js +47 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +3 -3
- package/package.json +2 -2
- package/src/AppField.svelte +19 -0
- package/src/AppForm.svelte +16 -0
- package/src/InnerAppField.svelte +17 -0
- package/src/context-keys.ts +2 -0
- package/src/createForm.svelte.ts +47 -2
- package/src/createFormCreator.svelte.ts +273 -0
- package/src/index.ts +5 -0
- package/src/types.ts +8 -3
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<!-- Take "form" as an prop, pass it to context, and render children -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import type { Snippet } from 'svelte'
|
|
4
|
+
import InnerAppField from './InnerAppField.svelte'
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
form: any
|
|
8
|
+
fieldComponents: any
|
|
9
|
+
children: Snippet
|
|
10
|
+
fieldProps: any
|
|
11
|
+
}
|
|
12
|
+
const { children, form, fieldComponents, fieldProps }: Props = $props()
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<form.Field {...fieldProps}>
|
|
16
|
+
{#snippet children(field: any)}
|
|
17
|
+
<InnerAppField field={field} children={children} fieldComponents={fieldComponents}/>
|
|
18
|
+
{/snippet}
|
|
19
|
+
</form.Field>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
interface Props {
|
|
3
|
+
form: any;
|
|
4
|
+
fieldComponents: any;
|
|
5
|
+
children: Snippet;
|
|
6
|
+
fieldProps: any;
|
|
7
|
+
}
|
|
8
|
+
declare const AppField: import("svelte").Component<Props, {}, "">;
|
|
9
|
+
type AppField = ReturnType<typeof AppField>;
|
|
10
|
+
export default AppField;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!-- Take "form" as an prop, pass it to context, and render children -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import { setContext, type Snippet } from 'svelte'
|
|
4
|
+
import { formContextKey } from './context-keys.js'
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
form: any
|
|
8
|
+
children: Snippet
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const { children, form }: Props = $props()
|
|
12
|
+
|
|
13
|
+
setContext(formContextKey, form)
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
{@render children?.()}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!-- Take "form" as an prop, pass it to context, and render children -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import { setContext, type Snippet } from 'svelte'
|
|
4
|
+
import { fieldContextKey } from './context-keys.js'
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
field: any
|
|
8
|
+
fieldComponents: any
|
|
9
|
+
children: Snippet<[any]>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const { children, field, fieldComponents }: Props = $props()
|
|
13
|
+
|
|
14
|
+
setContext(fieldContextKey, field)
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
{@render children?.(Object.assign(field, fieldComponents))}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Snippet } from 'svelte';
|
|
2
|
+
interface Props {
|
|
3
|
+
field: any;
|
|
4
|
+
fieldComponents: any;
|
|
5
|
+
children: Snippet<[any]>;
|
|
6
|
+
}
|
|
7
|
+
declare const InnerAppField: import("svelte").Component<Props, {}, "">;
|
|
8
|
+
type InnerAppField = ReturnType<typeof InnerAppField>;
|
|
9
|
+
export default InnerAppField;
|
|
@@ -16,4 +16,8 @@ export interface SvelteFormApi<TParentData, TFormOnMount extends undefined | For
|
|
|
16
16
|
children: Snippet<[NoInfer<TSelected>]>;
|
|
17
17
|
}>) => SvelteComponent) & WithoutFunction<Component>;
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* An extended version of the `FormApi` class that includes Svelte-specific functionalities from `SvelteFormApi`
|
|
21
|
+
*/
|
|
22
|
+
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>;
|
|
19
23
|
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>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { FieldApi, FormAsyncValidateOrFn, FormOptions, FormValidateOrFn } from '@tanstack/form-core';
|
|
2
|
+
import type { FieldComponent } from './types.js';
|
|
3
|
+
import type { SvelteFormExtendedApi } from './createForm.svelte';
|
|
4
|
+
import type { Component, Snippet } from 'svelte';
|
|
5
|
+
export declare function createFormCreatorContexts(): {
|
|
6
|
+
useFieldContext: <TData>() => FieldApi<any, string, TData, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
|
7
|
+
useFormContext: () => SvelteFormExtendedApi<Record<string, never>, any, any, any, any, any, any, any, any, any, any, any>;
|
|
8
|
+
};
|
|
9
|
+
interface CreateFormRuneProps<TFieldComponents extends Record<string, Component<any, any>>, TFormComponents extends Record<string, Component<any, any>>> {
|
|
10
|
+
fieldComponents: TFieldComponents;
|
|
11
|
+
formComponents: TFormComponents;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @private
|
|
15
|
+
*/
|
|
16
|
+
type AppFieldExtendedSvelteFormApi<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, TFieldComponents extends Record<string, Component<any, any>>, TFormComponents extends Record<string, Component<any, any>>> = SvelteFormExtendedApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta> & NoInfer<TFormComponents> & {
|
|
17
|
+
AppField: FieldComponent<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, NoInfer<TFieldComponents>>;
|
|
18
|
+
AppForm: Component<{
|
|
19
|
+
children: Snippet;
|
|
20
|
+
}>;
|
|
21
|
+
};
|
|
22
|
+
export declare function createFormCreator<const TComponents extends Record<string, Component<any, any>>, const TFormComponents extends Record<string, Component<any, any>>>({ fieldComponents, formComponents, }: CreateFormRuneProps<TComponents, TFormComponents>): {
|
|
23
|
+
createAppForm: <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>(props: () => FormOptions<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>) => AppFieldExtendedSvelteFormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, TComponents, TFormComponents>;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { getContext } from 'svelte';
|
|
2
|
+
import { createForm } from './createForm.svelte';
|
|
3
|
+
import AppFormSvelte from './AppForm.svelte';
|
|
4
|
+
import AppFieldSvelte from './AppField.svelte';
|
|
5
|
+
import { fieldContextKey, formContextKey } from './context-keys.js';
|
|
6
|
+
export function createFormCreatorContexts() {
|
|
7
|
+
function useFieldContext() {
|
|
8
|
+
const field = getContext(fieldContextKey);
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
10
|
+
if (!field) {
|
|
11
|
+
throw new Error('`fieldContext` only works when within a `fieldComponent` passed to `createFormCreator`');
|
|
12
|
+
}
|
|
13
|
+
return field;
|
|
14
|
+
}
|
|
15
|
+
function useFormContext() {
|
|
16
|
+
const form = getContext(formContextKey);
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
18
|
+
if (!form) {
|
|
19
|
+
throw new Error('`formContext` only works when within a `formComponent` passed to `createFormCreator`');
|
|
20
|
+
}
|
|
21
|
+
return form;
|
|
22
|
+
}
|
|
23
|
+
return { useFieldContext, useFormContext };
|
|
24
|
+
}
|
|
25
|
+
export function createFormCreator({ fieldComponents, formComponents, }) {
|
|
26
|
+
function createAppForm(props) {
|
|
27
|
+
const form = createForm(props);
|
|
28
|
+
const AppForm = ((internal, props) => {
|
|
29
|
+
return AppFormSvelte(internal, { ...props, form });
|
|
30
|
+
});
|
|
31
|
+
const AppField = ((internal, { children, ...fieldProps }) => AppFieldSvelte(internal, {
|
|
32
|
+
fieldProps,
|
|
33
|
+
form,
|
|
34
|
+
fieldComponents,
|
|
35
|
+
children,
|
|
36
|
+
}));
|
|
37
|
+
const extendedForm = Object.assign(form, {
|
|
38
|
+
AppField,
|
|
39
|
+
AppForm,
|
|
40
|
+
...formComponents,
|
|
41
|
+
});
|
|
42
|
+
return extendedForm;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
createAppForm,
|
|
46
|
+
};
|
|
47
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { useStore } from '@tanstack/svelte-store';
|
|
|
3
3
|
export { createForm, type SvelteFormApi } from './createForm.svelte.js';
|
|
4
4
|
export { default as Field, createField } from './Field.svelte';
|
|
5
5
|
export type { CreateField, FieldComponent } from './types.js';
|
|
6
|
+
export { createFormCreator, createFormCreatorContexts, } from './createFormCreator.svelte.js';
|
package/dist/index.js
CHANGED
|
@@ -2,3 +2,4 @@ export * from '@tanstack/form-core';
|
|
|
2
2
|
export { useStore } from '@tanstack/svelte-store';
|
|
3
3
|
export { createForm } from './createForm.svelte.js';
|
|
4
4
|
export { default as Field, createField } from './Field.svelte';
|
|
5
|
+
export { createFormCreator, createFormCreatorContexts, } from './createFormCreator.svelte.js';
|
package/dist/types.d.ts
CHANGED
|
@@ -9,10 +9,10 @@ export type WithoutFunction<T> = {
|
|
|
9
9
|
export interface SvelteFieldApi<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>, TParentSubmitMeta> {
|
|
10
10
|
Field: FieldComponent<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta>;
|
|
11
11
|
}
|
|
12
|
-
export type FieldComponent<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>, TParentSubmitMeta> = (<TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>>(internal: any, { children, ...fieldOptions }: Omit<FieldComponentProps<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta>, 'form'>) => {}) & (new <TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>>(opts: ComponentConstructorOptions<Omit<FieldComponentProps<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta>, 'form'>>) => SvelteComponent) & WithoutFunction<Component>;
|
|
13
|
-
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, 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>, TParentSubmitMeta> = {
|
|
12
|
+
export type FieldComponent<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>, TParentSubmitMeta, ExtendedApi = {}> = (<TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>>(internal: any, { children, ...fieldOptions }: Omit<FieldComponentProps<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta, ExtendedApi>, 'form'>) => {}) & (new <TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>>(opts: ComponentConstructorOptions<Omit<FieldComponentProps<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta, ExtendedApi>, 'form'>>) => SvelteComponent) & WithoutFunction<Component>;
|
|
13
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, 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>, TParentSubmitMeta, ExtendedApi = {}> = {
|
|
14
14
|
children: Snippet<[
|
|
15
|
-
FieldApi<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta>
|
|
15
|
+
FieldApi<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta> & ExtendedApi
|
|
16
16
|
]>;
|
|
17
17
|
} & Omit<CreateFieldOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta>, 'form'>;
|
|
18
18
|
export type CreateField<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>, TParentSubmitMeta> = <TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TSubmitMeta>(opts: () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-form",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.27.1",
|
|
4
4
|
"description": "Powerful, type-safe forms for Svelte.",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@tanstack/svelte-store": "^0.7.7",
|
|
35
|
-
"@tanstack/form-core": "1.
|
|
35
|
+
"@tanstack/form-core": "1.27.1"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@sveltejs/package": "^2.5.3",
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<!-- Take "form" as an prop, pass it to context, and render children -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import type { Snippet } from 'svelte'
|
|
4
|
+
import InnerAppField from './InnerAppField.svelte'
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
form: any
|
|
8
|
+
fieldComponents: any
|
|
9
|
+
children: Snippet
|
|
10
|
+
fieldProps: any
|
|
11
|
+
}
|
|
12
|
+
const { children, form, fieldComponents, fieldProps }: Props = $props()
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<form.Field {...fieldProps}>
|
|
16
|
+
{#snippet children(field: any)}
|
|
17
|
+
<InnerAppField field={field} children={children} fieldComponents={fieldComponents}/>
|
|
18
|
+
{/snippet}
|
|
19
|
+
</form.Field>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!-- Take "form" as an prop, pass it to context, and render children -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import { setContext, type Snippet } from 'svelte'
|
|
4
|
+
import { formContextKey } from './context-keys.js'
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
form: any
|
|
8
|
+
children: Snippet
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const { children, form }: Props = $props()
|
|
12
|
+
|
|
13
|
+
setContext(formContextKey, form)
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
{@render children?.()}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!-- Take "form" as an prop, pass it to context, and render children -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import { setContext, type Snippet } from 'svelte'
|
|
4
|
+
import { fieldContextKey } from './context-keys.js'
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
field: any
|
|
8
|
+
fieldComponents: any
|
|
9
|
+
children: Snippet<[any]>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const { children, field, fieldComponents }: Props = $props()
|
|
13
|
+
|
|
14
|
+
setContext(fieldContextKey, field)
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
{@render children?.(Object.assign(field, fieldComponents))}
|
package/src/createForm.svelte.ts
CHANGED
|
@@ -179,6 +179,51 @@ export interface SvelteFormApi<
|
|
|
179
179
|
WithoutFunction<Component>
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
+
/**
|
|
183
|
+
* An extended version of the `FormApi` class that includes Svelte-specific functionalities from `SvelteFormApi`
|
|
184
|
+
*/
|
|
185
|
+
export type SvelteFormExtendedApi<
|
|
186
|
+
TFormData,
|
|
187
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
188
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
189
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
190
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
191
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
192
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
193
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
194
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
195
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
196
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
197
|
+
TSubmitMeta,
|
|
198
|
+
> = FormApi<
|
|
199
|
+
TFormData,
|
|
200
|
+
TOnMount,
|
|
201
|
+
TOnChange,
|
|
202
|
+
TOnChangeAsync,
|
|
203
|
+
TOnBlur,
|
|
204
|
+
TOnBlurAsync,
|
|
205
|
+
TOnSubmit,
|
|
206
|
+
TOnSubmitAsync,
|
|
207
|
+
TOnDynamic,
|
|
208
|
+
TOnDynamicAsync,
|
|
209
|
+
TOnServer,
|
|
210
|
+
TSubmitMeta
|
|
211
|
+
> &
|
|
212
|
+
SvelteFormApi<
|
|
213
|
+
TFormData,
|
|
214
|
+
TOnMount,
|
|
215
|
+
TOnChange,
|
|
216
|
+
TOnChangeAsync,
|
|
217
|
+
TOnBlur,
|
|
218
|
+
TOnBlurAsync,
|
|
219
|
+
TOnSubmit,
|
|
220
|
+
TOnSubmitAsync,
|
|
221
|
+
TOnDynamic,
|
|
222
|
+
TOnDynamicAsync,
|
|
223
|
+
TOnServer,
|
|
224
|
+
TSubmitMeta
|
|
225
|
+
>
|
|
226
|
+
|
|
182
227
|
export function createForm<
|
|
183
228
|
TParentData,
|
|
184
229
|
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
|
|
@@ -241,10 +286,10 @@ export function createForm<
|
|
|
241
286
|
|
|
242
287
|
// @ts-expect-error constructor definition exists only on a type level
|
|
243
288
|
extendedApi.Field = (internal, props) =>
|
|
244
|
-
Field(internal, { ...props, form: api })
|
|
289
|
+
Field(internal, { ...props, form: api as never } as never)
|
|
245
290
|
extendedApi.createField = (props) =>
|
|
246
291
|
createField(() => {
|
|
247
|
-
return { ...props(), form: api }
|
|
292
|
+
return { ...props(), form: api } as never
|
|
248
293
|
}) as never // Type cast because else "Error: Type instantiation is excessively deep and possibly infinite."
|
|
249
294
|
extendedApi.useStore = (selector) => useStore(api.store, selector)
|
|
250
295
|
// @ts-expect-error constructor definition exists only on a type level
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { getContext } from 'svelte'
|
|
2
|
+
import { createForm } from './createForm.svelte'
|
|
3
|
+
import AppFormSvelte from './AppForm.svelte'
|
|
4
|
+
import AppFieldSvelte from './AppField.svelte'
|
|
5
|
+
import { fieldContextKey, formContextKey } from './context-keys.js'
|
|
6
|
+
import type {
|
|
7
|
+
AnyFieldApi,
|
|
8
|
+
AnyFormApi,
|
|
9
|
+
FieldApi,
|
|
10
|
+
FormAsyncValidateOrFn,
|
|
11
|
+
FormOptions,
|
|
12
|
+
FormValidateOrFn,
|
|
13
|
+
} from '@tanstack/form-core'
|
|
14
|
+
import type { FieldComponent } from './types.js'
|
|
15
|
+
import type { SvelteFormExtendedApi } from './createForm.svelte'
|
|
16
|
+
import type { Component, Snippet, SvelteComponent } from 'svelte'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* TypeScript inferencing is weird.
|
|
20
|
+
*
|
|
21
|
+
* If you have:
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
*
|
|
25
|
+
* interface Args<T> {
|
|
26
|
+
* arg?: T
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* function test<T>(arg?: Partial<Args<T>>): T {
|
|
30
|
+
* return 0 as any;
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* const a = test({});
|
|
34
|
+
*
|
|
35
|
+
* Then `T` will default to `unknown`.
|
|
36
|
+
*
|
|
37
|
+
* However, if we change `test` to be:
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
*
|
|
41
|
+
* function test<T extends undefined>(arg?: Partial<Args<T>>): T;
|
|
42
|
+
*
|
|
43
|
+
* Then `T` becomes `undefined`.
|
|
44
|
+
*
|
|
45
|
+
* Here, we are checking if the passed type `T` extends `DefaultT` and **only**
|
|
46
|
+
* `DefaultT`, as if that's the case we assume that inferencing has not occured.
|
|
47
|
+
*/
|
|
48
|
+
type UnwrapOrAny<T> = [unknown] extends [T] ? any : T
|
|
49
|
+
type UnwrapDefaultOrAny<DefaultT, T> = [DefaultT] extends [T]
|
|
50
|
+
? [T] extends [DefaultT]
|
|
51
|
+
? any
|
|
52
|
+
: T
|
|
53
|
+
: T
|
|
54
|
+
|
|
55
|
+
export function createFormCreatorContexts() {
|
|
56
|
+
function useFieldContext<TData>() {
|
|
57
|
+
const field = getContext(fieldContextKey) as AnyFieldApi
|
|
58
|
+
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
60
|
+
if (!field) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
'`fieldContext` only works when within a `fieldComponent` passed to `createFormCreator`',
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return field as FieldApi<
|
|
67
|
+
any,
|
|
68
|
+
string,
|
|
69
|
+
TData,
|
|
70
|
+
any,
|
|
71
|
+
any,
|
|
72
|
+
any,
|
|
73
|
+
any,
|
|
74
|
+
any,
|
|
75
|
+
any,
|
|
76
|
+
any,
|
|
77
|
+
any,
|
|
78
|
+
any,
|
|
79
|
+
any,
|
|
80
|
+
any,
|
|
81
|
+
any,
|
|
82
|
+
any,
|
|
83
|
+
any,
|
|
84
|
+
any,
|
|
85
|
+
any,
|
|
86
|
+
any,
|
|
87
|
+
any,
|
|
88
|
+
any,
|
|
89
|
+
any
|
|
90
|
+
>
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function useFormContext() {
|
|
94
|
+
const form = getContext(formContextKey) as AnyFormApi
|
|
95
|
+
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
97
|
+
if (!form) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
'`formContext` only works when within a `formComponent` passed to `createFormCreator`',
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return form as SvelteFormExtendedApi<
|
|
104
|
+
// If you need access to the form data, you need to use `withForm` instead
|
|
105
|
+
Record<string, never>,
|
|
106
|
+
any,
|
|
107
|
+
any,
|
|
108
|
+
any,
|
|
109
|
+
any,
|
|
110
|
+
any,
|
|
111
|
+
any,
|
|
112
|
+
any,
|
|
113
|
+
any,
|
|
114
|
+
any,
|
|
115
|
+
any,
|
|
116
|
+
any
|
|
117
|
+
>
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return { useFieldContext, useFormContext }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
interface CreateFormRuneProps<
|
|
124
|
+
TFieldComponents extends Record<string, Component<any, any>>,
|
|
125
|
+
TFormComponents extends Record<string, Component<any, any>>,
|
|
126
|
+
> {
|
|
127
|
+
fieldComponents: TFieldComponents
|
|
128
|
+
formComponents: TFormComponents
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @private
|
|
133
|
+
*/
|
|
134
|
+
type AppFieldExtendedSvelteFormApi<
|
|
135
|
+
TFormData,
|
|
136
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
137
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
138
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
139
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
140
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
141
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
142
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
143
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
144
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
145
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
146
|
+
TSubmitMeta,
|
|
147
|
+
TFieldComponents extends Record<string, Component<any, any>>,
|
|
148
|
+
TFormComponents extends Record<string, Component<any, any>>,
|
|
149
|
+
> = SvelteFormExtendedApi<
|
|
150
|
+
TFormData,
|
|
151
|
+
TOnMount,
|
|
152
|
+
TOnChange,
|
|
153
|
+
TOnChangeAsync,
|
|
154
|
+
TOnBlur,
|
|
155
|
+
TOnBlurAsync,
|
|
156
|
+
TOnSubmit,
|
|
157
|
+
TOnSubmitAsync,
|
|
158
|
+
TOnDynamic,
|
|
159
|
+
TOnDynamicAsync,
|
|
160
|
+
TOnServer,
|
|
161
|
+
TSubmitMeta
|
|
162
|
+
> &
|
|
163
|
+
NoInfer<TFormComponents> & {
|
|
164
|
+
AppField: FieldComponent<
|
|
165
|
+
TFormData,
|
|
166
|
+
TOnMount,
|
|
167
|
+
TOnChange,
|
|
168
|
+
TOnChangeAsync,
|
|
169
|
+
TOnBlur,
|
|
170
|
+
TOnBlurAsync,
|
|
171
|
+
TOnSubmit,
|
|
172
|
+
TOnSubmitAsync,
|
|
173
|
+
TOnDynamic,
|
|
174
|
+
TOnDynamicAsync,
|
|
175
|
+
TOnServer,
|
|
176
|
+
TSubmitMeta,
|
|
177
|
+
NoInfer<TFieldComponents>
|
|
178
|
+
>
|
|
179
|
+
AppForm: Component<{ children: Snippet }>
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function createFormCreator<
|
|
183
|
+
const TComponents extends Record<string, Component<any, any>>,
|
|
184
|
+
const TFormComponents extends Record<string, Component<any, any>>,
|
|
185
|
+
>({
|
|
186
|
+
fieldComponents,
|
|
187
|
+
formComponents,
|
|
188
|
+
}: CreateFormRuneProps<TComponents, TFormComponents>) {
|
|
189
|
+
function createAppForm<
|
|
190
|
+
TFormData,
|
|
191
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
192
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
193
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
194
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
195
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
196
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
197
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
198
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
199
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
200
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
201
|
+
TSubmitMeta,
|
|
202
|
+
>(
|
|
203
|
+
props: () => FormOptions<
|
|
204
|
+
TFormData,
|
|
205
|
+
TOnMount,
|
|
206
|
+
TOnChange,
|
|
207
|
+
TOnChangeAsync,
|
|
208
|
+
TOnBlur,
|
|
209
|
+
TOnBlurAsync,
|
|
210
|
+
TOnSubmit,
|
|
211
|
+
TOnSubmitAsync,
|
|
212
|
+
TOnDynamic,
|
|
213
|
+
TOnDynamicAsync,
|
|
214
|
+
TOnServer,
|
|
215
|
+
TSubmitMeta
|
|
216
|
+
>,
|
|
217
|
+
): AppFieldExtendedSvelteFormApi<
|
|
218
|
+
TFormData,
|
|
219
|
+
TOnMount,
|
|
220
|
+
TOnChange,
|
|
221
|
+
TOnChangeAsync,
|
|
222
|
+
TOnBlur,
|
|
223
|
+
TOnBlurAsync,
|
|
224
|
+
TOnSubmit,
|
|
225
|
+
TOnSubmitAsync,
|
|
226
|
+
TOnDynamic,
|
|
227
|
+
TOnDynamicAsync,
|
|
228
|
+
TOnServer,
|
|
229
|
+
TSubmitMeta,
|
|
230
|
+
TComponents,
|
|
231
|
+
TFormComponents
|
|
232
|
+
> {
|
|
233
|
+
const form = createForm(props)
|
|
234
|
+
|
|
235
|
+
const AppForm = ((internal, props) => {
|
|
236
|
+
return AppFormSvelte(internal, { ...props, form })
|
|
237
|
+
}) as Component<{ children: Snippet }>
|
|
238
|
+
|
|
239
|
+
const AppField = ((internal, { children, ...fieldProps }) =>
|
|
240
|
+
AppFieldSvelte(internal, {
|
|
241
|
+
fieldProps,
|
|
242
|
+
form,
|
|
243
|
+
fieldComponents,
|
|
244
|
+
children,
|
|
245
|
+
} as never)) as FieldComponent<
|
|
246
|
+
TFormData,
|
|
247
|
+
TOnMount,
|
|
248
|
+
TOnChange,
|
|
249
|
+
TOnChangeAsync,
|
|
250
|
+
TOnBlur,
|
|
251
|
+
TOnBlurAsync,
|
|
252
|
+
TOnSubmit,
|
|
253
|
+
TOnSubmitAsync,
|
|
254
|
+
TOnDynamic,
|
|
255
|
+
TOnDynamicAsync,
|
|
256
|
+
TOnServer,
|
|
257
|
+
TSubmitMeta,
|
|
258
|
+
TComponents
|
|
259
|
+
>
|
|
260
|
+
|
|
261
|
+
const extendedForm = Object.assign(form, {
|
|
262
|
+
AppField,
|
|
263
|
+
AppForm,
|
|
264
|
+
...formComponents,
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
return extendedForm
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return {
|
|
271
|
+
createAppForm,
|
|
272
|
+
}
|
|
273
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -7,3 +7,8 @@ export { createForm, type SvelteFormApi } from './createForm.svelte.js'
|
|
|
7
7
|
export { default as Field, createField } from './Field.svelte'
|
|
8
8
|
|
|
9
9
|
export type { CreateField, FieldComponent } from './types.js'
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
createFormCreator,
|
|
13
|
+
createFormCreatorContexts,
|
|
14
|
+
} from './createFormCreator.svelte.js'
|
package/src/types.ts
CHANGED
|
@@ -123,6 +123,7 @@ export type FieldComponent<
|
|
|
123
123
|
TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
124
124
|
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
125
125
|
TParentSubmitMeta,
|
|
126
|
+
ExtendedApi = {},
|
|
126
127
|
> =
|
|
127
128
|
// This giant type allows the type
|
|
128
129
|
// - to be used as a function (which they are now in Svelte 5)
|
|
@@ -178,7 +179,8 @@ export type FieldComponent<
|
|
|
178
179
|
TFormOnDynamic,
|
|
179
180
|
TFormOnDynamicAsync,
|
|
180
181
|
TFormOnServer,
|
|
181
|
-
TParentSubmitMeta
|
|
182
|
+
TParentSubmitMeta,
|
|
183
|
+
ExtendedApi
|
|
182
184
|
>,
|
|
183
185
|
'form'
|
|
184
186
|
>,
|
|
@@ -235,7 +237,8 @@ export type FieldComponent<
|
|
|
235
237
|
TFormOnDynamic,
|
|
236
238
|
TFormOnDynamicAsync,
|
|
237
239
|
TFormOnServer,
|
|
238
|
-
TParentSubmitMeta
|
|
240
|
+
TParentSubmitMeta,
|
|
241
|
+
ExtendedApi
|
|
239
242
|
>,
|
|
240
243
|
'form'
|
|
241
244
|
>
|
|
@@ -275,6 +278,7 @@ type FieldComponentProps<
|
|
|
275
278
|
TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
276
279
|
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
277
280
|
TParentSubmitMeta,
|
|
281
|
+
ExtendedApi = {},
|
|
278
282
|
> = {
|
|
279
283
|
children: Snippet<
|
|
280
284
|
[
|
|
@@ -302,7 +306,8 @@ type FieldComponentProps<
|
|
|
302
306
|
TFormOnDynamicAsync,
|
|
303
307
|
TFormOnServer,
|
|
304
308
|
TParentSubmitMeta
|
|
305
|
-
|
|
309
|
+
> &
|
|
310
|
+
ExtendedApi,
|
|
306
311
|
]
|
|
307
312
|
>
|
|
308
313
|
} & Omit<
|