@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.
@@ -1,371 +0,0 @@
1
- import type {
2
- DeepKeys,
3
- DeepKeysWhereValueIncludes,
4
- DeepValue,
5
- FieldApi,
6
- FieldApiOptions,
7
- FieldValidators,
8
- FormErrorTypes,
9
- FormGroupApi,
10
- FormGroupOptions,
11
- FormGroupState,
12
- FormGroupValidators,
13
- FormState,
14
- ToFieldError,
15
- ToFormGroupErrorTypes,
16
- } from '@tanstack/form-core'
17
- import type {
18
- Component,
19
- ComponentConstructorOptions,
20
- Snippet,
21
- SvelteComponent,
22
- } from 'svelte'
23
-
24
- export type WithoutFunction<T> = {
25
- [K in keyof T as T[K] extends Function ? never : K]: T[K]
26
- }
27
-
28
- type ExactFieldBrand<out TValue> = {
29
- readonly __tanstackFieldExactType: TValue
30
- }
31
- type AcceptsFieldBrand<out TAcceptedValue> = {
32
- readonly __tanstackFieldAcceptsType: TAcceptedValue
33
- }
34
- type CompatibleFieldKey<TKey, TComponent, TTargetValue> =
35
- TComponent extends ExactFieldBrand<infer TExact>
36
- ? [TExact] extends [TTargetValue]
37
- ? [TTargetValue] extends [TExact]
38
- ? TKey
39
- : never
40
- : never
41
- : TComponent extends AcceptsFieldBrand<infer TLoose>
42
- ? [TTargetValue] extends [TLoose]
43
- ? TKey
44
- : never
45
- : TKey
46
- type FilteredFieldComponents<
47
- TFieldComponents extends Record<string, Component<any>>,
48
- TTargetValue,
49
- > = {
50
- [
51
- K in keyof TFieldComponents as CompatibleFieldKey<
52
- K,
53
- TFieldComponents[K],
54
- TTargetValue
55
- >
56
- ]: TFieldComponents[K]
57
- }
58
- type FieldComponentsMatchingType<
59
- TFieldComponents extends Record<string, Component<any>>,
60
- TTargetValue,
61
- > = unknown extends TTargetValue
62
- ? TFieldComponents
63
- : string extends keyof TFieldComponents
64
- ? TFieldComponents
65
- : FilteredFieldComponents<TFieldComponents, TTargetValue>
66
-
67
- export type SvelteFieldApi<
68
- TFieldName,
69
- TFieldValue,
70
- TFieldError,
71
- TFormData,
72
- TFormErrorTypes extends FormErrorTypes,
73
- TFieldComponents extends Record<string, Component<any>>,
74
- > = FieldApi<TFieldName, TFieldValue, TFieldError, TFormData, TFormErrorTypes> &
75
- FieldComponentsMatchingType<TFieldComponents, TFieldValue>
76
-
77
- interface SvelteSubscribeProps<TSourceData, TSelected> {
78
- selector: (state: TSourceData) => TSelected
79
- when?: (selected: NoInfer<TSelected>) => boolean
80
- children: Snippet<[NoInfer<TSelected>]>
81
- }
82
-
83
- export type SvelteFormSubscribeProps<
84
- TFormData,
85
- TFormErrorTypes extends FormErrorTypes,
86
- TSelected,
87
- > = SvelteSubscribeProps<FormState<TFormData, TFormErrorTypes>, TSelected>
88
-
89
- export type SvelteFormSubscribeComponent<
90
- TFormData,
91
- TFormErrorTypes extends FormErrorTypes,
92
- > = (new <TSelected>(
93
- options: ComponentConstructorOptions<
94
- SvelteFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>
95
- >,
96
- ) => SvelteComponent) &
97
- Component<any> &
98
- WithoutFunction<Component>
99
-
100
- export interface SvelteFormFieldProps<
101
- TFieldData,
102
- TFieldName,
103
- TFieldValue,
104
- TFieldValidators extends FieldValidators<TFieldData, TFieldName, TFieldValue>,
105
- TGroupFieldError,
106
- TFormData,
107
- TFormErrorTypes extends FormErrorTypes,
108
- TFieldComponents extends Record<string, Component<any>>,
109
- > extends FieldApiOptions<
110
- TFieldData,
111
- TFieldName,
112
- TFieldValue,
113
- TFieldValidators,
114
- TGroupFieldError,
115
- TFormData,
116
- TFormErrorTypes
117
- > {
118
- children: Snippet<
119
- [
120
- SvelteFieldApi<
121
- TFieldName,
122
- TFieldValue,
123
- ToFieldError<
124
- NoInfer<TFieldValidators>,
125
- TGroupFieldError,
126
- TFormErrorTypes
127
- >,
128
- TFormData,
129
- TFormErrorTypes,
130
- TFieldComponents
131
- >,
132
- ]
133
- >
134
- }
135
-
136
- export type SvelteFormFieldComponent<
137
- TFormData,
138
- TFormErrorTypes extends FormErrorTypes,
139
- TFieldComponents extends Record<string, Component<any>>,
140
- > = (new <
141
- TFieldName extends DeepKeys<TFormData>,
142
- const TFieldValidators extends FieldValidators<
143
- TFormData,
144
- TFieldName,
145
- DeepValue<TFormData, TFieldName>
146
- >,
147
- >(
148
- options: ComponentConstructorOptions<
149
- SvelteFormFieldProps<
150
- TFormData,
151
- TFieldName,
152
- DeepValue<TFormData, TFieldName>,
153
- TFieldValidators,
154
- never,
155
- TFormData,
156
- TFormErrorTypes,
157
- TFieldComponents
158
- >
159
- >,
160
- ) => SvelteComponent) &
161
- Component<any> &
162
- WithoutFunction<Component>
163
-
164
- export type SvelteFormArrayFieldComponent<
165
- TFormData,
166
- TFormErrorTypes extends FormErrorTypes,
167
- TFieldComponents extends Record<string, Component<any>>,
168
- > = (new <
169
- TFieldName extends DeepKeysWhereValueIncludes<TFormData, Array<any>>,
170
- const TFieldValidators extends FieldValidators<
171
- TFormData,
172
- TFieldName,
173
- DeepValue<TFormData, TFieldName>
174
- >,
175
- >(
176
- options: ComponentConstructorOptions<
177
- SvelteFormFieldProps<
178
- TFormData,
179
- TFieldName,
180
- DeepValue<TFormData, TFieldName>,
181
- TFieldValidators,
182
- never,
183
- TFormData,
184
- TFormErrorTypes,
185
- TFieldComponents
186
- >
187
- >,
188
- ) => SvelteComponent) &
189
- Component<any> &
190
- WithoutFunction<Component>
191
-
192
- export type SvelteFormGroupSubscribeComponent<
193
- TGroupValue,
194
- TGroupErrorTypes extends FormErrorTypes,
195
- > = (new <TSelected>(
196
- options: ComponentConstructorOptions<
197
- SvelteSubscribeProps<
198
- FormGroupState<TGroupValue, TGroupErrorTypes>,
199
- TSelected
200
- >
201
- >,
202
- ) => SvelteComponent) &
203
- Component<any> &
204
- WithoutFunction<Component>
205
-
206
- export type SvelteFormGroupFieldComponent<
207
- TFormData,
208
- TGroupValue,
209
- TGroupErrorTypes extends FormErrorTypes,
210
- TFormErrorTypes extends FormErrorTypes,
211
- TFieldComponents extends Record<string, Component<any>>,
212
- > = (new <
213
- TFieldName extends DeepKeys<TGroupValue>,
214
- const TFieldValidators extends FieldValidators<
215
- TGroupValue,
216
- TFieldName,
217
- DeepValue<TGroupValue, TFieldName>
218
- >,
219
- >(
220
- options: ComponentConstructorOptions<
221
- SvelteFormFieldProps<
222
- TGroupValue,
223
- TFieldName,
224
- DeepValue<TGroupValue, TFieldName>,
225
- TFieldValidators,
226
- TGroupErrorTypes['fieldError'],
227
- TFormData,
228
- TFormErrorTypes,
229
- TFieldComponents
230
- >
231
- >,
232
- ) => SvelteComponent) &
233
- Component<any> &
234
- WithoutFunction<Component>
235
-
236
- export type SvelteFormGroupArrayFieldComponent<
237
- TFormData,
238
- TGroupValue,
239
- TGroupErrorTypes extends FormErrorTypes,
240
- TFormErrorTypes extends FormErrorTypes,
241
- TFieldComponents extends Record<string, Component<any>>,
242
- > = (new <
243
- TFieldName extends DeepKeysWhereValueIncludes<TGroupValue, Array<any>>,
244
- const TFieldValidators extends FieldValidators<
245
- TGroupValue,
246
- TFieldName,
247
- DeepValue<TGroupValue, TFieldName>
248
- >,
249
- >(
250
- options: ComponentConstructorOptions<
251
- SvelteFormFieldProps<
252
- TGroupValue,
253
- TFieldName,
254
- DeepValue<TGroupValue, TFieldName>,
255
- TFieldValidators,
256
- TGroupErrorTypes['fieldError'],
257
- TFormData,
258
- TFormErrorTypes,
259
- TFieldComponents
260
- >
261
- >,
262
- ) => SvelteComponent) &
263
- Component<any> &
264
- WithoutFunction<Component>
265
-
266
- export interface SvelteFormGroupApi<
267
- TFormData,
268
- TGroupName,
269
- TGroupValue,
270
- TGroupErrorTypes extends FormErrorTypes,
271
- TFormErrorTypes extends FormErrorTypes,
272
- TFieldComponents extends Record<string, Component<any>>,
273
- > extends FormGroupApi<
274
- TFormData,
275
- TGroupName,
276
- TGroupValue,
277
- TGroupErrorTypes,
278
- TFormErrorTypes
279
- > {
280
- Field: SvelteFormGroupFieldComponent<
281
- TFormData,
282
- TGroupValue,
283
- TGroupErrorTypes,
284
- TFormErrorTypes,
285
- TFieldComponents
286
- >
287
- ArrayField: SvelteFormGroupArrayFieldComponent<
288
- TFormData,
289
- TGroupValue,
290
- TGroupErrorTypes,
291
- TFormErrorTypes,
292
- TFieldComponents
293
- >
294
- Subscribe: SvelteFormGroupSubscribeComponent<TGroupValue, TGroupErrorTypes>
295
- }
296
-
297
- export interface SvelteFormGroupProps<
298
- TFormData,
299
- TGroupName,
300
- TGroupValue,
301
- TGroupValidators extends FormGroupValidators<TGroupValue>,
302
- TFormErrorTypes extends FormErrorTypes,
303
- TFieldComponents extends Record<string, Component<any>>,
304
- > extends Omit<
305
- FormGroupOptions<
306
- TFormData,
307
- TGroupName,
308
- TGroupValue,
309
- TGroupValidators,
310
- TFormErrorTypes
311
- >,
312
- 'form'
313
- > {
314
- children: Snippet<
315
- [
316
- SvelteFormGroupApi<
317
- TFormData,
318
- TGroupName,
319
- TGroupValue,
320
- ToFormGroupErrorTypes<NoInfer<TGroupValidators>>,
321
- TFormErrorTypes,
322
- TFieldComponents
323
- >,
324
- ]
325
- >
326
- }
327
-
328
- export type SvelteFormGroupComponent<
329
- TFormData,
330
- TFormErrorTypes extends FormErrorTypes,
331
- TFieldComponents extends Record<string, Component<any>>,
332
- > = (new <
333
- TGroupName extends DeepKeys<TFormData>,
334
- TGroupValue extends DeepValue<TFormData, TGroupName>,
335
- const TGroupValidators extends FormGroupValidators<TGroupValue>,
336
- >(
337
- options: ComponentConstructorOptions<
338
- SvelteFormGroupProps<
339
- TFormData,
340
- TGroupName,
341
- TGroupValue,
342
- TGroupValidators,
343
- TFormErrorTypes,
344
- TFieldComponents
345
- >
346
- >,
347
- ) => SvelteComponent) &
348
- Component<any> &
349
- WithoutFunction<Component>
350
-
351
- export interface SvelteTanStackFormComponents<
352
- TFormData,
353
- TFormErrorTypes extends FormErrorTypes,
354
- TFieldComponents extends Record<string, Component<any>> = Record<
355
- never,
356
- never
357
- >,
358
- > {
359
- Field: SvelteFormFieldComponent<TFormData, TFormErrorTypes, TFieldComponents>
360
- ArrayField: SvelteFormArrayFieldComponent<
361
- TFormData,
362
- TFormErrorTypes,
363
- TFieldComponents
364
- >
365
- Subscribe: SvelteFormSubscribeComponent<TFormData, TFormErrorTypes>
366
- FormGroup: SvelteFormGroupComponent<
367
- TFormData,
368
- TFormErrorTypes,
369
- TFieldComponents
370
- >
371
- }
package/src/Field.svelte DELETED
@@ -1,152 +0,0 @@
1
- <script module lang="ts">
2
- import { shallow, useSelector } from '@tanstack/svelte-store'
3
- import type {
4
- FieldApi,
5
- FieldApiOptions,
6
- FieldValidators,
7
- FormErrorTypes,
8
- ToFieldError,
9
- } from '@tanstack/form-core'
10
- import type {
11
- AnyInternalFieldApi,
12
- AnyInternalFormApi,
13
- InternalBaseFieldMeta,
14
- } from '@tanstack/form-core/internals'
15
-
16
- interface InternalFieldOptions
17
- extends FieldApiOptions<any, any, any, any, any, any, any> {
18
- form: AnyInternalFormApi
19
- }
20
-
21
- function useDynamicFieldSelector<TSelected>(
22
- getField: () => AnyInternalFieldApi,
23
- selector: (field: AnyInternalFieldApi) => TSelected,
24
- ) {
25
- let selected = $state.raw(selector(getField()))
26
-
27
- $effect(() => {
28
- const field = getField()
29
- const update = () => {
30
- const next = selector(field)
31
- if (!shallow(selected, next)) selected = next
32
- }
33
- update()
34
- const subscription = field.atom.subscribe(update)
35
- return () => subscription.unsubscribe()
36
- })
37
-
38
- return {
39
- get current() {
40
- return selected
41
- },
42
- }
43
- }
44
-
45
- export function createField<
46
- TFieldData,
47
- TFieldName,
48
- TFieldValue,
49
- const TFieldValidators extends FieldValidators<
50
- TFieldData,
51
- TFieldName,
52
- TFieldValue
53
- >,
54
- TGroupFieldError,
55
- TFormData,
56
- TFormErrorTypes extends FormErrorTypes,
57
- >(
58
- options: () =>
59
- & FieldApiOptions<
60
- TFieldData,
61
- TFieldName,
62
- TFieldValue,
63
- TFieldValidators,
64
- TGroupFieldError,
65
- TFormData,
66
- TFormErrorTypes
67
- >
68
- & { form: AnyInternalFormApi },
69
- array = false,
70
- ): FieldApi<
71
- TFieldName,
72
- TFieldValue,
73
- ToFieldError<TFieldValidators, TGroupFieldError, TFormErrorTypes>,
74
- TFormData,
75
- TFormErrorTypes
76
- > {
77
- const initialForm = options().form
78
- const resetVersion = useSelector(initialForm._atoms.resetVersion)
79
- const fieldApi = $derived.by(() => {
80
- resetVersion.current
81
- const current = options()
82
- return current.form._getOrCreateFieldApi({
83
- ...current,
84
- name: current.name,
85
- } as never)
86
- })
87
-
88
- $effect.pre(() => {
89
- fieldApi._update(options() as InternalFieldOptions)
90
- })
91
-
92
- $effect(() => fieldApi._register())
93
-
94
- const selected = useDynamicFieldSelector(
95
- () => fieldApi,
96
- (array
97
- ? (field) => ({
98
- length: field.value.length,
99
- version: (field.meta as InternalBaseFieldMeta)._arrayVersion,
100
- })
101
- : (field) => ({ value: field.value, meta: field.meta })) as (
102
- field: AnyInternalFieldApi,
103
- ) => any,
104
- )
105
-
106
- return new Proxy({} as AnyInternalFieldApi, {
107
- get(target, property) {
108
- if (Reflect.has(target, property)) return Reflect.get(target, property)
109
- const field = fieldApi
110
- const state = selected.current
111
- if (property === 'value') {
112
- return array ? field.value : (state as any).value
113
- }
114
- if (property === 'meta') {
115
- return array ? field.meta : (state as any).meta
116
- }
117
- if (property === 'errors') {
118
- return array ? field.errors : (state as any).meta.errors
119
- }
120
- const value = Reflect.get(field, property, field)
121
- return typeof value === 'function' ? value.bind(field) : value
122
- },
123
- }) as never
124
- }
125
- </script>
126
-
127
- <script lang="ts">
128
- import { setContext, untrack, type Snippet } from 'svelte'
129
- import { fieldContextKey } from './context-keys.js'
130
-
131
- interface Props extends InternalFieldOptions {
132
- array?: boolean
133
- children: Snippet<[any]>
134
- fieldComponents?: Record<string, any>
135
- }
136
-
137
- let {
138
- children,
139
- array = false,
140
- fieldComponents = {},
141
- ...fieldOptions
142
- }: Props = $props()
143
-
144
- const fieldApi = createField(
145
- () => fieldOptions,
146
- untrack(() => array),
147
- )
148
- Object.assign(fieldApi, untrack(() => fieldComponents))
149
- setContext(fieldContextKey, fieldApi)
150
- </script>
151
-
152
- {@render children(fieldApi)}
@@ -1,100 +0,0 @@
1
- import type {
2
- DeepKeys,
3
- DeepKeysWhereValueIncludes,
4
- DeepValue,
5
- FieldValidators,
6
- FormApiArrayMethods,
7
- FormApiFieldMethods,
8
- FormErrorTypes,
9
- FormState,
10
- ValidationIssue,
11
- } from '@tanstack/form-core'
12
- import type {
13
- Component,
14
- ComponentConstructorOptions,
15
- SvelteComponent,
16
- } from 'svelte'
17
- import type {
18
- SvelteFormFieldProps,
19
- SvelteFormSubscribeProps,
20
- WithoutFunction,
21
- } from '../Components.public'
22
- import type { ReadonlyAtom } from '@tanstack/svelte-store'
23
-
24
- export type FieldGroupFieldComponent<
25
- TFieldData,
26
- TFieldComponents extends Record<string, Component<any>>,
27
- > = (new <const TFieldName extends DeepKeys<TFieldData>>(
28
- options: ComponentConstructorOptions<
29
- SvelteFormFieldProps<
30
- TFieldData,
31
- TFieldName,
32
- DeepValue<TFieldData, TFieldName>,
33
- FieldValidators<
34
- TFieldData,
35
- TFieldName,
36
- DeepValue<TFieldData, TFieldName>
37
- >,
38
- ValidationIssue,
39
- unknown,
40
- FormErrorTypes,
41
- TFieldComponents
42
- >
43
- >,
44
- ) => SvelteComponent) &
45
- Component<any> &
46
- WithoutFunction<Component>
47
-
48
- export type FieldGroupArrayFieldComponent<
49
- TFieldData,
50
- TFieldComponents extends Record<string, Component<any>>,
51
- > = (new <
52
- const TFieldName extends DeepKeysWhereValueIncludes<TFieldData, Array<any>>,
53
- >(
54
- options: ComponentConstructorOptions<
55
- SvelteFormFieldProps<
56
- TFieldData,
57
- TFieldName,
58
- DeepValue<TFieldData, TFieldName>,
59
- FieldValidators<
60
- TFieldData,
61
- TFieldName,
62
- DeepValue<TFieldData, TFieldName>
63
- >,
64
- ValidationIssue,
65
- unknown,
66
- FormErrorTypes,
67
- TFieldComponents
68
- >
69
- >,
70
- ) => SvelteComponent) &
71
- Component<any> &
72
- WithoutFunction<Component>
73
-
74
- export type FieldGroupSubscribeComponent = (new <TSelected>(
75
- options: ComponentConstructorOptions<
76
- SvelteFormSubscribeProps<unknown, FormErrorTypes, TSelected>
77
- >,
78
- ) => SvelteComponent) &
79
- Component<any> &
80
- WithoutFunction<Component>
81
-
82
- export interface FieldGroupApi<
83
- TFieldData,
84
- TFieldComponents extends Record<string, Component<any>> = Record<
85
- never,
86
- never
87
- >,
88
- >
89
- extends FormApiFieldMethods<TFieldData>, FormApiArrayMethods<TFieldData> {
90
- atom: ReadonlyAtom<TFieldData>
91
- Field: FieldGroupFieldComponent<TFieldData, TFieldComponents>
92
- ArrayField: FieldGroupArrayFieldComponent<TFieldData, TFieldComponents>
93
- Subscribe: FieldGroupSubscribeComponent
94
- }
95
-
96
- export type AnyFieldGroupApi = FieldGroupApi<
97
- any,
98
- Record<string, Component<any>>
99
- >
100
- export type FieldGroupFormState = FormState<unknown, FormErrorTypes>
@@ -1,82 +0,0 @@
1
- import {
2
- InternalFieldGroupApi,
3
- defineFieldGroupFieldsRuntime,
4
- fieldGroupHelperRuntime,
5
- } from '@tanstack/form-core/internals'
6
- import { withComponentProps } from '../utils.lib.js'
7
- import type { Component } from 'svelte'
8
- import type { InternalFieldGroupBindings } from '@tanstack/form-core/internals'
9
- import type { AnyFieldGroupApi } from './FieldGroupApi.public'
10
-
11
- function attachSvelteFieldGroupComponents(
12
- group: InternalFieldGroupApi,
13
- form: any,
14
- ): AnyFieldGroupApi {
15
- const resolveProps = (props: any) =>
16
- new Proxy(props, {
17
- get(target, property, receiver) {
18
- if (
19
- property === 'name' ||
20
- property === 'validators' ||
21
- property === 'listeners'
22
- ) {
23
- return Reflect.get(
24
- group._getFormFieldOptions({ ...target }, withComponentProps),
25
- property,
26
- )
27
- }
28
- return Reflect.get(target, property, receiver)
29
- },
30
- })
31
-
32
- return Object.assign(group, {
33
- Field: (internals: any, props: any) =>
34
- form.Field(internals, resolveProps(props)),
35
- ArrayField: (internals: any, props: any) =>
36
- form.ArrayField(internals, resolveProps(props)),
37
- Subscribe: (internals: any, props: any) => form.Subscribe(internals, props),
38
- }) as never
39
- }
40
-
41
- export function defineFieldGroupRuntime<
42
- TFields extends Record<string, unknown>,
43
- >(defineFieldGroupFn: (helperRuntime: any) => TFields) {
44
- const fields = defineFieldGroupFieldsRuntime(
45
- defineFieldGroupFn(fieldGroupHelperRuntime),
46
- )
47
-
48
- return {
49
- fields,
50
- bindComponent: (Component: Component<any>, fieldsPropName: string) =>
51
- withFieldsRuntime(fields, Component, fieldsPropName),
52
- }
53
- }
54
-
55
- function withFieldsRuntime(
56
- fields: Record<string, unknown>,
57
- Component: Component<any>,
58
- fieldsPropName: string,
59
- ) {
60
- const fieldNames = Object.keys(fields)
61
- return ((internals: any, props: any) => {
62
- const form = props.form
63
- if (!form) {
64
- throw new Error('TanStack Form: Field groups must receive a `form` prop.')
65
- }
66
- const api = attachSvelteFieldGroupComponents(
67
- new InternalFieldGroupApi({
68
- form,
69
- fieldNames,
70
- getBindings: () => props[fieldsPropName] as InternalFieldGroupBindings,
71
- }),
72
- form,
73
- )
74
- return Component(
75
- internals,
76
- withComponentProps(props, {
77
- form: undefined,
78
- [fieldsPropName]: api,
79
- }),
80
- )
81
- }) as Component<any>
82
- }