@tanstack/svelte-form 1.33.4 → 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.
- package/dist/AppForm/AppForm.svelte +14 -0
- package/dist/AppForm/Components.lib.d.ts +7 -0
- package/dist/AppForm/Components.lib.js +8 -0
- package/dist/AppForm/SvelteAppFormApi.public.d.ts +10 -0
- package/dist/AppForm/appFormOptions.public.d.ts +12 -0
- package/dist/AppForm/appFormOptions.public.js +1 -0
- package/dist/AppForm/componentMap.public.d.ts +7 -0
- package/dist/AppForm/componentMap.public.js +1 -0
- package/dist/AppForm/contexts.lib.d.ts +4 -0
- package/dist/AppForm/contexts.lib.js +16 -0
- package/dist/AppForm/createFormHook.public.d.ts +3 -0
- package/dist/AppForm/createFormHook.public.js +20 -0
- package/dist/AppForm/createFormHookTypes.public.d.ts +12 -0
- package/dist/AppForm/createFormHookTypes.public.js +1 -0
- package/dist/AppForm/fieldComponentHelpers.lib.d.ts +5 -0
- package/dist/AppForm/fieldComponentHelpers.lib.js +10 -0
- package/dist/AppForm/getFormHookHelpers.public.d.ts +31 -0
- package/dist/AppForm/getFormHookHelpers.public.js +13 -0
- package/dist/AppForm/initializeAppForm.lib.d.ts +4 -0
- package/dist/AppForm/initializeAppForm.lib.js +5 -0
- package/dist/Components.public.d.ts +52 -0
- package/dist/Components.public.js +1 -0
- package/dist/Field.svelte +125 -314
- package/dist/Field.svelte.d.ts +14 -30
- package/dist/FieldGroup/FieldGroupApi.public.d.ts +15 -0
- package/dist/FieldGroup/FieldGroupApi.public.js +1 -0
- package/dist/FieldGroup/withFields.lib.d.ts +5 -0
- package/dist/FieldGroup/withFields.lib.js +44 -0
- package/dist/FieldGroup/withFields.public.d.ts +70 -0
- package/dist/FieldGroup/withFields.public.js +3 -0
- package/dist/FormGroup.svelte +81 -302
- package/dist/FormGroup.svelte.d.ts +13 -30
- package/dist/Subscribe.svelte +18 -8
- package/dist/Subscribe.svelte.d.ts +5 -3
- package/dist/createForm.public.d.ts +5 -0
- package/dist/createForm.public.js +5 -0
- package/dist/createForm.svelte.d.ts +11 -27
- package/dist/createForm.svelte.js +29 -19
- package/dist/formApiTypes.public.d.ts +13 -0
- package/dist/formApiTypes.public.js +1 -0
- package/dist/formType.public.d.ts +7 -0
- package/dist/formType.public.js +1 -0
- package/dist/index.d.ts +13 -4
- package/dist/index.js +13 -3
- package/dist/utils.lib.d.ts +1 -0
- package/dist/utils.lib.js +28 -0
- package/package.json +5 -5
- package/src/AppForm/AppForm.svelte +14 -0
- package/src/AppForm/Components.lib.ts +28 -0
- package/src/AppForm/SvelteAppFormApi.public.ts +14 -0
- package/src/AppForm/appFormOptions.public.ts +57 -0
- package/src/AppForm/componentMap.public.ts +19 -0
- package/src/AppForm/contexts.lib.ts +24 -0
- package/src/AppForm/createFormHook.public.ts +33 -0
- package/src/AppForm/createFormHookTypes.public.ts +30 -0
- package/src/AppForm/fieldComponentHelpers.lib.ts +24 -0
- package/src/AppForm/getFormHookHelpers.public.ts +82 -0
- package/src/AppForm/initializeAppForm.lib.ts +18 -0
- package/src/Components.public.ts +371 -0
- package/src/Field.svelte +125 -314
- package/src/FieldGroup/FieldGroupApi.public.ts +100 -0
- package/src/FieldGroup/withFields.lib.ts +82 -0
- package/src/FieldGroup/withFields.public.ts +179 -0
- package/src/FormGroup.svelte +81 -302
- package/src/Subscribe.svelte +18 -8
- package/src/createForm.public.ts +26 -0
- package/src/createForm.svelte.ts +62 -336
- package/src/formApiTypes.public.ts +48 -0
- package/src/formType.public.ts +46 -0
- package/src/index.ts +13 -12
- package/src/utils.lib.ts +30 -0
- package/dist/AppField.svelte +0 -19
- package/dist/AppField.svelte.d.ts +0 -10
- package/dist/AppForm.svelte +0 -16
- package/dist/InnerAppField.svelte +0 -17
- package/dist/InnerAppField.svelte.d.ts +0 -9
- package/dist/createFormCreator.svelte.d.ts +0 -26
- package/dist/createFormCreator.svelte.js +0 -51
- package/dist/types.d.ts +0 -21
- package/src/AppField.svelte +0 -19
- package/src/AppForm.svelte +0 -16
- package/src/InnerAppField.svelte +0 -17
- package/src/createFormCreator.svelte.ts +0 -321
- package/src/types.ts +0 -567
- /package/dist/{AppForm.svelte.d.ts → AppForm/AppForm.svelte.d.ts} +0 -0
- /package/dist/{types.js → AppForm/SvelteAppFormApi.public.js} +0 -0
package/src/Field.svelte
CHANGED
|
@@ -1,341 +1,152 @@
|
|
|
1
1
|
<script module lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
type DeepValue,
|
|
2
|
+
import { shallow, useSelector } from '@tanstack/svelte-store'
|
|
3
|
+
import type {
|
|
5
4
|
FieldApi,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
FieldApiOptions,
|
|
6
|
+
FieldValidators,
|
|
7
|
+
FormErrorTypes,
|
|
8
|
+
ToFieldError,
|
|
10
9
|
} from '@tanstack/form-core'
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
import type {
|
|
11
|
+
AnyInternalFieldApi,
|
|
12
|
+
AnyInternalFormApi,
|
|
13
|
+
InternalBaseFieldMeta,
|
|
14
|
+
} from '@tanstack/form-core/internals'
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
24
|
-
TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
25
|
-
TOnBlurAsync extends
|
|
26
|
-
| undefined
|
|
27
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
28
|
-
TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
29
|
-
TOnSubmitAsync extends
|
|
30
|
-
| undefined
|
|
31
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
32
|
-
TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
33
|
-
TOnDynamicAsync extends
|
|
34
|
-
| undefined
|
|
35
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
36
|
-
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
|
|
37
|
-
TFormOnChange extends undefined | FormValidateOrFn<TParentData>,
|
|
38
|
-
TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
39
|
-
TFormOnBlur extends undefined | FormValidateOrFn<TParentData>,
|
|
40
|
-
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
41
|
-
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
|
|
42
|
-
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
43
|
-
TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>,
|
|
44
|
-
TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
45
|
-
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
46
|
-
TParentSubmitMeta,
|
|
47
|
-
>(
|
|
48
|
-
opts: () => CreateFieldOptions<
|
|
49
|
-
TParentData,
|
|
50
|
-
TName,
|
|
51
|
-
TData,
|
|
52
|
-
TOnMount,
|
|
53
|
-
TOnChange,
|
|
54
|
-
TOnChangeAsync,
|
|
55
|
-
TOnBlur,
|
|
56
|
-
TOnBlurAsync,
|
|
57
|
-
TOnSubmit,
|
|
58
|
-
TOnSubmitAsync,
|
|
59
|
-
TOnDynamic,
|
|
60
|
-
TOnDynamicAsync,
|
|
61
|
-
TFormOnMount,
|
|
62
|
-
TFormOnChange,
|
|
63
|
-
TFormOnChangeAsync,
|
|
64
|
-
TFormOnBlur,
|
|
65
|
-
TFormOnBlurAsync,
|
|
66
|
-
TFormOnSubmit,
|
|
67
|
-
TFormOnSubmitAsync,
|
|
68
|
-
TFormOnDynamic,
|
|
69
|
-
TFormOnDynamicAsync,
|
|
70
|
-
TFormOnServer,
|
|
71
|
-
TParentSubmitMeta
|
|
72
|
-
>,
|
|
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,
|
|
73
24
|
) {
|
|
74
|
-
|
|
25
|
+
let selected = $state.raw(selector(getField()))
|
|
75
26
|
|
|
76
|
-
|
|
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
|
+
})
|
|
77
37
|
|
|
78
|
-
|
|
38
|
+
return {
|
|
39
|
+
get current() {
|
|
40
|
+
return selected
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
}
|
|
79
44
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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)
|
|
89
86
|
})
|
|
90
87
|
|
|
91
88
|
$effect.pre(() => {
|
|
92
|
-
|
|
93
|
-
// Changes to options are seen by the effect because signals inside them are picked up.
|
|
94
|
-
const current = opts()
|
|
95
|
-
if (!mounted) return
|
|
96
|
-
api.update(current)
|
|
89
|
+
fieldApi._update(options() as InternalFieldOptions)
|
|
97
90
|
})
|
|
98
91
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
)
|
|
112
|
-
const metaIsDirtySub = useSelector(api.store, (state) => state.meta.isDirty)
|
|
113
|
-
const metaErrorMapSub = useSelector(api.store, (state) => state.meta.errorMap)
|
|
114
|
-
const metaErrorSourceMapSub = useSelector(
|
|
115
|
-
api.store,
|
|
116
|
-
(state) => state.meta.errorSourceMap,
|
|
117
|
-
)
|
|
118
|
-
const metaIsValidatingSub = useSelector(
|
|
119
|
-
api.store,
|
|
120
|
-
(state) => state.meta.isValidating,
|
|
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,
|
|
121
104
|
)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
// getter on the same object.
|
|
131
|
-
const trackedValue = storeSub.current
|
|
132
|
-
const isTouched = metaIsTouchedSub.current
|
|
133
|
-
const isBlurred = metaIsBlurredSub.current
|
|
134
|
-
const isDirty = metaIsDirtySub.current
|
|
135
|
-
const errorMap = metaErrorMapSub.current
|
|
136
|
-
const errorSourceMap = metaErrorSourceMapSub.current
|
|
137
|
-
const isValidating = metaIsValidatingSub.current
|
|
138
|
-
const baseState = api.store.state
|
|
139
|
-
return {
|
|
140
|
-
...baseState,
|
|
141
|
-
value: options.mode === 'array' ? baseState.value : trackedValue,
|
|
142
|
-
meta: {
|
|
143
|
-
...baseState.meta,
|
|
144
|
-
isTouched,
|
|
145
|
-
isBlurred,
|
|
146
|
-
isDirty,
|
|
147
|
-
errorMap,
|
|
148
|
-
errorSourceMap,
|
|
149
|
-
isValidating,
|
|
150
|
-
},
|
|
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
|
|
151
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
|
|
152
122
|
},
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
return extendedApi
|
|
123
|
+
}) as never
|
|
156
124
|
}
|
|
157
125
|
</script>
|
|
158
126
|
|
|
159
|
-
<script
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
| undefined
|
|
169
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
170
|
-
TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
171
|
-
TOnBlurAsync extends
|
|
172
|
-
| undefined
|
|
173
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
174
|
-
TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
175
|
-
TOnSubmitAsync extends
|
|
176
|
-
| undefined
|
|
177
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
178
|
-
TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
179
|
-
TOnDynamicAsync extends
|
|
180
|
-
| undefined
|
|
181
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
182
|
-
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
|
|
183
|
-
TFormOnChange extends undefined | FormValidateOrFn<TParentData>,
|
|
184
|
-
TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
185
|
-
TFormOnBlur extends undefined | FormValidateOrFn<TParentData>,
|
|
186
|
-
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
187
|
-
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
|
|
188
|
-
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
189
|
-
TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>,
|
|
190
|
-
TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
191
|
-
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
192
|
-
TParentSubmitMeta,
|
|
193
|
-
"
|
|
194
|
-
>
|
|
195
|
-
type Props<
|
|
196
|
-
TParentData,
|
|
197
|
-
TName extends DeepKeys<TParentData>,
|
|
198
|
-
TData extends DeepValue<TParentData, TName>,
|
|
199
|
-
TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
200
|
-
TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
201
|
-
TOnChangeAsync extends
|
|
202
|
-
| undefined
|
|
203
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
204
|
-
TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
205
|
-
TOnBlurAsync extends
|
|
206
|
-
| undefined
|
|
207
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
208
|
-
TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
209
|
-
TOnSubmitAsync extends
|
|
210
|
-
| undefined
|
|
211
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
212
|
-
TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
213
|
-
TOnDynamicAsync extends
|
|
214
|
-
| undefined
|
|
215
|
-
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
216
|
-
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
|
|
217
|
-
TFormOnChange extends undefined | FormValidateOrFn<TParentData>,
|
|
218
|
-
TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
219
|
-
TFormOnBlur extends undefined | FormValidateOrFn<TParentData>,
|
|
220
|
-
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
221
|
-
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
|
|
222
|
-
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
223
|
-
TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>,
|
|
224
|
-
TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
225
|
-
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
226
|
-
TParentSubmitMeta,
|
|
227
|
-
> = {
|
|
228
|
-
children: Snippet<
|
|
229
|
-
[
|
|
230
|
-
FieldApi<
|
|
231
|
-
TParentData,
|
|
232
|
-
TName,
|
|
233
|
-
TData,
|
|
234
|
-
TOnMount,
|
|
235
|
-
TOnChange,
|
|
236
|
-
TOnChangeAsync,
|
|
237
|
-
TOnBlur,
|
|
238
|
-
TOnBlurAsync,
|
|
239
|
-
TOnSubmit,
|
|
240
|
-
TOnSubmitAsync,
|
|
241
|
-
TOnDynamic,
|
|
242
|
-
TOnDynamicAsync,
|
|
243
|
-
TFormOnMount,
|
|
244
|
-
TFormOnChange,
|
|
245
|
-
TFormOnChangeAsync,
|
|
246
|
-
TFormOnBlur,
|
|
247
|
-
TFormOnBlurAsync,
|
|
248
|
-
TFormOnSubmit,
|
|
249
|
-
TFormOnSubmitAsync,
|
|
250
|
-
TFormOnDynamic,
|
|
251
|
-
TFormOnDynamicAsync,
|
|
252
|
-
TFormOnServer,
|
|
253
|
-
TParentSubmitMeta
|
|
254
|
-
>,
|
|
255
|
-
]
|
|
256
|
-
>
|
|
257
|
-
} & CreateFieldOptions<
|
|
258
|
-
TParentData,
|
|
259
|
-
TName,
|
|
260
|
-
TData,
|
|
261
|
-
TOnMount,
|
|
262
|
-
TOnChange,
|
|
263
|
-
TOnChangeAsync,
|
|
264
|
-
TOnBlur,
|
|
265
|
-
TOnBlurAsync,
|
|
266
|
-
TOnSubmit,
|
|
267
|
-
TOnSubmitAsync,
|
|
268
|
-
TOnDynamic,
|
|
269
|
-
TOnDynamicAsync,
|
|
270
|
-
TFormOnMount,
|
|
271
|
-
TFormOnChange,
|
|
272
|
-
TFormOnChangeAsync,
|
|
273
|
-
TFormOnBlur,
|
|
274
|
-
TFormOnBlurAsync,
|
|
275
|
-
TFormOnSubmit,
|
|
276
|
-
TFormOnSubmitAsync,
|
|
277
|
-
TFormOnDynamic,
|
|
278
|
-
TFormOnDynamicAsync,
|
|
279
|
-
TFormOnServer,
|
|
280
|
-
TParentSubmitMeta
|
|
281
|
-
>
|
|
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
|
+
}
|
|
282
136
|
|
|
283
137
|
let {
|
|
284
138
|
children,
|
|
139
|
+
array = false,
|
|
140
|
+
fieldComponents = {},
|
|
285
141
|
...fieldOptions
|
|
286
|
-
}: Props
|
|
287
|
-
TParentData,
|
|
288
|
-
TName,
|
|
289
|
-
TData,
|
|
290
|
-
TOnMount,
|
|
291
|
-
TOnChange,
|
|
292
|
-
TOnChangeAsync,
|
|
293
|
-
TOnBlur,
|
|
294
|
-
TOnBlurAsync,
|
|
295
|
-
TOnSubmit,
|
|
296
|
-
TOnSubmitAsync,
|
|
297
|
-
TOnDynamic,
|
|
298
|
-
TOnDynamicAsync,
|
|
299
|
-
TFormOnMount,
|
|
300
|
-
TFormOnChange,
|
|
301
|
-
TFormOnChangeAsync,
|
|
302
|
-
TFormOnBlur,
|
|
303
|
-
TFormOnBlurAsync,
|
|
304
|
-
TFormOnSubmit,
|
|
305
|
-
TFormOnSubmitAsync,
|
|
306
|
-
TFormOnDynamic,
|
|
307
|
-
TFormOnDynamicAsync,
|
|
308
|
-
TFormOnServer,
|
|
309
|
-
TParentSubmitMeta
|
|
310
|
-
> = $props()
|
|
142
|
+
}: Props = $props()
|
|
311
143
|
|
|
312
|
-
const fieldApi = createField
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
TOnChangeAsync,
|
|
319
|
-
TOnBlur,
|
|
320
|
-
TOnBlurAsync,
|
|
321
|
-
TOnSubmit,
|
|
322
|
-
TOnSubmitAsync,
|
|
323
|
-
TOnDynamic,
|
|
324
|
-
TOnDynamicAsync,
|
|
325
|
-
TFormOnMount,
|
|
326
|
-
TFormOnChange,
|
|
327
|
-
TFormOnChangeAsync,
|
|
328
|
-
TFormOnBlur,
|
|
329
|
-
TFormOnBlurAsync,
|
|
330
|
-
TFormOnSubmit,
|
|
331
|
-
TFormOnSubmitAsync,
|
|
332
|
-
TFormOnDynamic,
|
|
333
|
-
TFormOnDynamicAsync,
|
|
334
|
-
TFormOnServer,
|
|
335
|
-
TParentSubmitMeta
|
|
336
|
-
>(() => {
|
|
337
|
-
return fieldOptions
|
|
338
|
-
})
|
|
144
|
+
const fieldApi = createField(
|
|
145
|
+
() => fieldOptions,
|
|
146
|
+
untrack(() => array),
|
|
147
|
+
)
|
|
148
|
+
Object.assign(fieldApi, untrack(() => fieldComponents))
|
|
149
|
+
setContext(fieldContextKey, fieldApi)
|
|
339
150
|
</script>
|
|
340
151
|
|
|
341
152
|
{@render children(fieldApi)}
|
|
@@ -0,0 +1,100 @@
|
|
|
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>
|
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
}
|