Open History to see the last commit
1import { createAtom, shallow } from '@tanstack/store'
2import { transformFieldOptionsFieldNames } from '../FieldApi/FieldApi.lib'
3import { getBy } from '../utils.lib'
4import type { AnyFieldApiOptions } from '../FieldApi/FieldApi.lib'
5import type { AnyInternalFormApi } from '../FormApi/FormApi.lib'
6import type { InternalFieldUpdateOptions } from '../types.lib'
7import type { ReadonlyAtom } from '@tanstack/store'
8β
9export type InternalFieldGroupBindings = Record<string, string>
10β
11export interface InternalFieldGroupOptions {
12 form: AnyInternalFormApi
13 fieldNames: ReadonlyArray<string>
14 getBindings: () => InternalFieldGroupBindings | undefined
15}
16β
17interface ResolvedNameCacheEntry {
18 binding: string
19 name: string
20}
21β
22function getRootSegmentEnd(name: string): number {
23 const dotIndex = name.indexOf('.')
24 const arrayIndex = name.indexOf('[')
25β
26 if (dotIndex === -1) return arrayIndex === -1 ? name.length : arrayIndex
27 if (arrayIndex === -1) return dotIndex
28 return Math.min(dotIndex, arrayIndex)
29}
30β
31export class InternalFieldGroupApi {
32 readonly atom: ReadonlyAtom<Record<string, unknown>>
33 readonly form: AnyInternalFormApi
34 readonly fieldNames: ReadonlyArray<string>
35 private readonly getBindings: () => InternalFieldGroupBindings | undefined
36 private readonly resolvedNameCache = new Map<string, ResolvedNameCacheEntry>()
37β
38 constructor(options: InternalFieldGroupOptions) {
39 this.form = options.form
40 this.fieldNames = options.fieldNames
41 this.getBindings = options.getBindings
42 this.atom = createAtom(
43 () => {
44 const nextValues: Record<string, unknown> = {}
45 const formValues = this.form._atoms.values.get()
46β
47 for (const fieldName of this.fieldNames) {
48 nextValues[fieldName] = getBy(
49 formValues,
50 this._resolveFieldName(fieldName),
51 )
52 }
53β
54 return nextValues
55 },
56 { compare: shallow },
57 )
58 }
59β
60 _resolveFieldName(fieldName: string): string {
61 const bindings = this.getBindings()
62 if (bindings === undefined) return fieldName
63β
64 const rootEnd = getRootSegmentEnd(fieldName)
65 const rootName = fieldName.slice(0, rootEnd)
66 const binding = bindings[rootName]
67β
68 if (binding === undefined) {
69 throw new Error(
70 `TanStack Form: Missing field group binding for "${rootName}".`,
71 )
72 }
73β
74 const cached = this.resolvedNameCache.get(fieldName)
75 if (cached?.binding === binding) return cached.name
76β
77 const resolvedName = `${binding}${fieldName.slice(rootEnd)}`
78 this.resolvedNameCache.set(fieldName, { binding, name: resolvedName })
79 return resolvedName
80 }
81β
82 _getFormFieldOptions<TOptions extends AnyFieldApiOptions>(
83 options: TOptions,
84 mergeOptions: (props: TOptions, overrides: Partial<TOptions>) => TOptions,
85 ): AnyFieldApiOptions {
86 return transformFieldOptionsFieldNames(
87 options,
88 (fieldName) => this._resolveFieldName(fieldName),
89 mergeOptions,
90 )
91 }
92β
93 getFieldValue = (fieldName: string) =>
94 this.form.getFieldValue(this._resolveFieldName(fieldName))
95β
96 setFieldValue = (
97 fieldName: string,
98 value: unknown,
99 options?: InternalFieldUpdateOptions,
100 ) =>
101 this.form.setFieldValue(this._resolveFieldName(fieldName), value, options)
102β
103 resetField = (fieldName: string) =>
104 this.form.resetField(this._resolveFieldName(fieldName))
105β
106 swapFieldValues = (
107 fieldName: string,
108 indexA: number,
109 indexB: number,
110 options?: InternalFieldUpdateOptions,
111 ) =>
112 this.form.swapFieldValues(
113 this._resolveFieldName(fieldName),
114 indexA,
115 indexB,
116 options,
117 )
118β
119 moveFieldValue = (
120 fieldName: string,
121 fromIndex: number,
122 toIndex: number,
123 options?: InternalFieldUpdateOptions,
124 ) =>
125 this.form.moveFieldValue(
126 this._resolveFieldName(fieldName),
127 fromIndex,
128 toIndex,
129 options,
130 )
131β
132 pushFieldValue = (
133 fieldName: string,
134 value: unknown,
135 options?: InternalFieldUpdateOptions,
136 ) =>
137 this.form.pushFieldValue(this._resolveFieldName(fieldName), value, options)
138β
139 insertFieldValue = (
140 fieldName: string,
141 index: number,
142 value: unknown,
143 options?: InternalFieldUpdateOptions,
144 ) =>
145 this.form.insertFieldValue(
146 this._resolveFieldName(fieldName),
147 index,
148 value,
149 options,
150 )
151β
152 clearFieldValues = (
153 fieldName: string,
154 options?: InternalFieldUpdateOptions,
155 ) => this.form.clearFieldValues(this._resolveFieldName(fieldName), options)
156β
157 replaceFieldValue = (
158 fieldName: string,
159 index: number,
160 value: unknown,
161 options?: InternalFieldUpdateOptions,
162 ) =>
163 this.form.replaceFieldValue(
164 this._resolveFieldName(fieldName),
165 index,
166 value,
167 options,
168 )
169β
170 removeFieldValue = (
171 fieldName: string,
172 index: number,
173 options?: InternalFieldUpdateOptions,
174 ) =>
175 this.form.removeFieldValue(
176 this._resolveFieldName(fieldName),
177 index,
178 options,
179 )
180β
181 filterFieldValues = (
182 fieldName: string,
183 predicate: (...args: Array<any>) => boolean,
184 options?: InternalFieldUpdateOptions & { thisArg?: any },
185 ) =>
186 this.form.filterFieldValues(
187 this._resolveFieldName(fieldName),
188 predicate,
189 options,
190 )
191}
192β
193export const fieldGroupHelperRuntime = {
194 strict: () => null,
195 loose: () => null,
196}
197β
198export function defineFieldGroupFieldsRuntime<TFields>(
199 fields: TFields,
200): TFields {
201 return fields
202}