Open History to see the last commit
1import {
2 ChangeDetectorRef,
3 Directive,
4 effect,
5 inject,
6 input,
7 signal,
8} from '@angular/core'
9import { InternalFormGroupApi } from '@tanstack/form-core/internals'
10import type {
11 DeepKeys,
12 DeepValue,
13 ErrorVisibility,
14 FieldApi,
15 FieldListeners,
16 FieldValidators,
17 FormValidators,
18 ToFieldError,
19 ToFormErrorTypes,
20 ToFormGroupErrorTypes,
21} from '@tanstack/form-core'
22import type {
23 AnyFieldApiOptions,
24 AnyInternalFieldApi,
25 AnyInternalFormApi,
26 InternalBaseFieldMeta,
27 InternalFormApi,
28} from '@tanstack/form-core/internals'
29β
30export type AngularFieldSource =
31 AnyInternalFormApi | InternalFormGroupApi<any, any, any, any, any>
32β
33export type AngularFieldData<TSource extends AngularFieldSource> =
34 TSource extends InternalFormGroupApi<any, any, infer TGroupValue, any, any>
35 ? TGroupValue
36 : TSource extends InternalFormApi<infer TFormData, any, any>
37 ? TFormData
38 : never
39β
40type AngularParentFormData<TSource extends AngularFieldSource> =
41 TSource extends InternalFormGroupApi<infer TFormData, any, any, any, any>
42 ? TFormData
43 : TSource extends InternalFormApi<infer TFormData, any, any>
44 ? TFormData
45 : never
46β
47type AngularSourceFormErrorTypes<TSource extends AngularFieldSource> =
48 TSource extends InternalFormGroupApi<
49 any,
50 any,
51 any,
52 any,
53 infer TFormErrorTypes
54 >
55 ? TFormErrorTypes
56 : TSource extends InternalFormApi<
57 any,
58 infer TFormValidators,
59 infer TSubmitReturn
60 >
61 ? ToFormErrorTypes<TFormValidators, TSubmitReturn>
62 : never
63β
64type AngularSourceGroupFieldError<TSource extends AngularFieldSource> =
65 TSource extends InternalFormGroupApi<
66 any,
67 any,
68 any,
69 infer TGroupValidators,
70 any
71 >
72 ? ToFormGroupErrorTypes<TGroupValidators>['fieldError']
73 : never
74β
75type AngularSourceFieldApi<
76 TSource extends AngularFieldSource,
77 TFieldName extends DeepKeys<AngularFieldData<TSource>>,
78 TFieldValue extends DeepValue<AngularFieldData<TSource>, TFieldName>,
79 TFieldValidators extends FieldValidators<
80 AngularFieldData<TSource>,
81 TFieldName,
82 TFieldValue
83 >,
84> = FieldApi<
85 TFieldName,
86 TFieldValue,
87 ToFieldError<
88 TFieldValidators,
89 AngularSourceGroupFieldError<TSource>,
90 AngularSourceFormErrorTypes<TSource>
91 >,
92 AngularParentFormData<TSource>,
93 AngularSourceFormErrorTypes<TSource>
94>
95β
96export type AngularFieldApi<
97 TFormData,
98 TFieldName extends DeepKeys<TFormData>,
99 TFieldValue extends DeepValue<TFormData, TFieldName>,
100 TFieldValidators extends FieldValidators<TFormData, TFieldName, TFieldValue>,
101 TFormValidators extends FormValidators<TFormData>,
102 TSubmitReturn,
103> = FieldApi<
104 TFieldName,
105 TFieldValue,
106 ToFieldError<
107 TFieldValidators,
108 never,
109 ToFormErrorTypes<TFormValidators, TSubmitReturn>
110 >,
111 TFormData,
112 ToFormErrorTypes<TFormValidators, TSubmitReturn>
113>
114β
115@Directive()
116abstract class TanStackFieldBase<
117 TSource extends AngularFieldSource,
118 const TFieldName extends DeepKeys<AngularFieldData<TSource>>,
119 TFieldValue extends DeepValue<AngularFieldData<TSource>, TFieldName>,
120 const TFieldValidators extends FieldValidators<
121 AngularFieldData<TSource>,
122 TFieldName,
123 TFieldValue
124 >,
125> {
126 name = input.required<TFieldName>()
127 validators = input<NoInfer<TFieldValidators>>()
128 listeners =
129 input<
130 NoInfer<
131 FieldListeners<
132 AngularFieldData<TSource>,
133 TFieldName,
134 TFieldValue,
135 ToFieldError<
136 TFieldValidators,
137 AngularSourceGroupFieldError<TSource>,
138 AngularSourceFormErrorTypes<TSource>
139 >,
140 AngularParentFormData<TSource>,
141 AngularSourceFormErrorTypes<TSource>
142 >
143 >
144 >()
145 errorVisibility =
146 input<
147 ErrorVisibility<
148 AngularParentFormData<TSource>,
149 AngularSourceFormErrorTypes<TSource>
150 >
151 >()
152 errorBoundary = input<boolean>()
153β
154 protected abstract readonly isArrayField: boolean
155 protected abstract getSource(): TSource
156β
157 private readonly changeDetector = inject(ChangeDetectorRef)
158 private readonly resetVersion = signal(0)
159β
160 private getFieldOptions(): AnyFieldApiOptions {
161 const options = {
162 name: this.name(),
163 validators: this.validators(),
164 listeners: this.listeners(),
165 errorVisibility: this.errorVisibility(),
166 errorBoundary: this.errorBoundary(),
167 }
168β
169 const source = this.getSource()
170 if (!(source instanceof InternalFormGroupApi)) return options as never
171β
172 return source._getFormFieldOptions<AnyFieldApiOptions>(
173 options as AnyFieldApiOptions,
174 (base, overrides) => ({ ...base, ...overrides }),
175 )
176 }
177β
178 private getForm(): AnyInternalFormApi {
179 const source = this.getSource()
180 return source instanceof InternalFormGroupApi ? source.form : source
181 }
182β
183 private getFieldApi() {
184 void this.resetVersion()
185 return this.getForm()._getOrCreateFieldApi({
186 name: this.getFieldOptions().name,
187 })
188 }
189β
190 get api(): AngularSourceFieldApi<
191 TSource,
192 TFieldName,
193 TFieldValue,
194 TFieldValidators
195 > {
196 // Angular may evaluate an exported directive reference before constructor
197 // effects run, so resolve the form-owned API synchronously from the inputs.
198 return this.getFieldApi() as never
199 }
200β
201 constructor() {
202 effect((onCleanup) => {
203 const form = this.getForm()
204 this.resetVersion.set(form._atoms.resetVersion.get())
205 const subscription = form._atoms.resetVersion.subscribe((version) => {
206 this.resetVersion.set(version)
207 })
208 onCleanup(() => subscription.unsubscribe())
209 })
210β
211 effect(() => {
212 const api = this.getFieldApi()
213 api._update(this.getFieldOptions() as never)
214 })
215β
216 effect((onCleanup) => {
217 const api = this.getFieldApi()
218 const unregister = api._register()
219 onCleanup(unregister)
220 })
221β
222 effect((onCleanup) => {
223 const api = this.getFieldApi()
224 let previous = this.selectRenderState(api)
225 this.changeDetector.markForCheck()
226β
227 const subscription = api.atom.subscribe(() => {
228 const next = this.selectRenderState(api)
229 if (previous[0] === next[0] && previous[1] === next[1]) return
230 previous = next
231 this.changeDetector.markForCheck()
232 })
233β
234 onCleanup(() => subscription.unsubscribe())
235 })
236 }
237β
238 private selectRenderState(api: AnyInternalFieldApi): readonly [any, any] {
239 if (this.isArrayField) {
240 return [
241 api.value.length,
242 (api.meta as InternalBaseFieldMeta)._arrayVersion,
243 ]
244 }
245β
246 return [api.value, api.meta]
247 }
248}
249β
250@Directive({
251 selector: '[tanstackField]',
252 standalone: true,
253 exportAs: 'field',
254})
255export class TanStackField<
256 TSource extends AngularFieldSource,
257 const TFieldName extends DeepKeys<AngularFieldData<TSource>>,
258 TFieldValue extends DeepValue<AngularFieldData<TSource>, TFieldName>,
259 const TFieldValidators extends FieldValidators<
260 AngularFieldData<TSource>,
261 TFieldName,
262 TFieldValue
263 >,
264> extends TanStackFieldBase<
265 TSource,
266 TFieldName,
267 TFieldValue,
268 TFieldValidators
269> {
270 tanstackField = input.required<TSource>()
271 protected readonly isArrayField = false
272 protected getSource() {
273 return this.tanstackField()
274 }
275}
276β
277@Directive({
278 selector: '[tanstackArrayField]',
279 standalone: true,
280 exportAs: 'arrayField',
281})
282export class TanStackArrayField<
283 TSource extends AngularFieldSource,
284 const TFieldName extends DeepKeys<AngularFieldData<TSource>>,
285 TFieldValue extends DeepValue<AngularFieldData<TSource>, TFieldName>,
286 const TFieldValidators extends FieldValidators<
287 AngularFieldData<TSource>,
288 TFieldName,
289 TFieldValue
290 >,
291> extends TanStackFieldBase<
292 TSource,
293 TFieldName,
294 TFieldValue,
295 TFieldValidators
296> {
297 tanstackArrayField = input.required<TSource>()
298 protected readonly isArrayField = true
299 protected getSource() {
300 return this.tanstackArrayField()
301 }
302}