Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

Field.svelte

155 lines3.9 KB

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 {
84 ...current,
85 name: current.name,
86 } as never,
87 'field',
88 )
89 })
90​
91 $effect.pre(() => {
92 fieldApi._update(options() as InternalFieldOptions, 'field')
93 })
94​
95 $effect(() => fieldApi._register())
96​
97 const selected = useDynamicFieldSelector(
98 () => fieldApi,
99 (array
100 ? (field) => ({
101 length: field.value.length,
102 version: (field.meta as InternalBaseFieldMeta)._arrayVersion,
103 })
104 : (field) => ({ value: field.value, meta: field.meta })) as (
105 field: AnyInternalFieldApi,
106 ) => any,
107 )
108​
109 return new Proxy({} as AnyInternalFieldApi, {
110 get(target, property) {
111 if (Reflect.has(target, property)) return Reflect.get(target, property)
112 const field = fieldApi
113 const state = selected.current
114 if (property === 'value') {
115 return array ? field.value : (state as any).value
116 }
117 if (property === 'meta') {
118 return array ? field.meta : (state as any).meta
119 }
120 if (property === 'errors') {
121 return array ? field.errors : (state as any).meta.errors
122 }
123 const value = Reflect.get(field, property, field)
124 return typeof value === 'function' ? value.bind(field) : value
125 },
126 }) as never
127 }
128</script>
129​
130<script lang="ts">
131 import { setContext, untrack, type Snippet } from 'svelte'
132 import { fieldContextKey } from './context-keys.js'
133​
134 interface Props extends InternalFieldOptions {
135 array?: boolean
136 children: Snippet<[any]>
137 fieldComponents?: Record<string, any>
138 }
139​
140 let {
141 children,
142 array = false,
143 fieldComponents = {},
144 ...fieldOptions
145 }: Props = $props()
146​
147 const fieldApi = createField(
148 () => fieldOptions,
149 untrack(() => array),
150 )
151 Object.assign(fieldApi, untrack(() => fieldComponents))
152 setContext(fieldContextKey, fieldApi)
153</script>
154​
155{@render children(fieldApi)}