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

FieldGroupApi.lib.ts

202 lines5.3 KB

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
16​
17interface ResolvedNameCacheEntry {
18 binding: string
19 name: string
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)
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 )
192​
193export const fieldGroupHelperRuntime = {
194 strict: () => null,
195 loose: () => null,
197​
198export function defineFieldGroupFieldsRuntime<TFields>(
199 fields: TFields,
200): TFields {
201 return fields