@tanstack/form-core 1.28.0 → 1.28.2

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.
@@ -0,0 +1,155 @@
1
+ import { batch } from '@tanstack/store'
2
+ import { deepCopy } from './utils'
3
+ import type {
4
+ AnyBaseFormState,
5
+ FormApi,
6
+ FormAsyncValidateOrFn,
7
+ FormValidateOrFn,
8
+ } from './FormApi'
9
+
10
+ /**
11
+ * @private
12
+ */
13
+ export type FormTransform<
14
+ TFormData,
15
+ TOnMount extends undefined | FormValidateOrFn<TFormData>,
16
+ TOnChange extends undefined | FormValidateOrFn<TFormData>,
17
+ TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
18
+ TOnBlur extends undefined | FormValidateOrFn<TFormData>,
19
+ TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
20
+ TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
21
+ TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
22
+ TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
23
+ TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
24
+ TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
25
+ TSubmitMeta = never,
26
+ > = (
27
+ formBase: FormApi<
28
+ TFormData,
29
+ TOnMount,
30
+ TOnChange,
31
+ TOnChangeAsync,
32
+ TOnBlur,
33
+ TOnBlurAsync,
34
+ TOnSubmit,
35
+ TOnSubmitAsync,
36
+ TOnDynamic,
37
+ TOnDynamicAsync,
38
+ TOnServer,
39
+ TSubmitMeta
40
+ >,
41
+ ) => FormApi<
42
+ TFormData,
43
+ TOnMount,
44
+ TOnChange,
45
+ TOnChangeAsync,
46
+ TOnBlur,
47
+ TOnBlurAsync,
48
+ TOnSubmit,
49
+ TOnSubmitAsync,
50
+ TOnDynamic,
51
+ TOnDynamicAsync,
52
+ TOnServer,
53
+ TSubmitMeta
54
+ >
55
+
56
+ /**
57
+ * @private
58
+ */
59
+ export function mergeAndUpdate<
60
+ TFormData,
61
+ TOnMount extends undefined | FormValidateOrFn<TFormData>,
62
+ TOnChange extends undefined | FormValidateOrFn<TFormData>,
63
+ TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
64
+ TOnBlur extends undefined | FormValidateOrFn<TFormData>,
65
+ TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
66
+ TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
67
+ TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
68
+ TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
69
+ TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
70
+ TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
71
+ TSubmitMeta = never,
72
+ >(
73
+ form: FormApi<
74
+ TFormData,
75
+ TOnMount,
76
+ TOnChange,
77
+ TOnChangeAsync,
78
+ TOnBlur,
79
+ TOnBlurAsync,
80
+ TOnSubmit,
81
+ TOnSubmitAsync,
82
+ TOnDynamic,
83
+ TOnDynamicAsync,
84
+ TOnServer,
85
+ TSubmitMeta
86
+ >,
87
+ fn?: FormTransform<
88
+ TFormData,
89
+ TOnMount,
90
+ TOnChange,
91
+ TOnChangeAsync,
92
+ TOnBlur,
93
+ TOnBlurAsync,
94
+ TOnSubmit,
95
+ TOnSubmitAsync,
96
+ TOnDynamic,
97
+ TOnDynamicAsync,
98
+ TOnServer,
99
+ TSubmitMeta
100
+ >,
101
+ ) {
102
+ // Run the `transform` function on `form.state`, diff it, and update the relevant parts with what needs updating
103
+ if (!fn) return
104
+
105
+ const newObj = Object.assign({}, form, {
106
+ state: deepCopy(form.state),
107
+ })
108
+
109
+ fn(newObj)
110
+
111
+ if (newObj.fieldInfo !== form.fieldInfo) {
112
+ form.fieldInfo = newObj.fieldInfo
113
+ }
114
+
115
+ if (newObj.options !== form.options) {
116
+ form.options = newObj.options
117
+ }
118
+
119
+ const baseFormKeys = Object.keys({
120
+ values: null,
121
+ validationMetaMap: null,
122
+ fieldMetaBase: null,
123
+ isSubmitting: null,
124
+ isSubmitted: null,
125
+ isValidating: null,
126
+ submissionAttempts: null,
127
+ isSubmitSuccessful: null,
128
+ _force_re_eval: null,
129
+ // Do not remove this, it ensures that we have all the keys in `BaseFormState`
130
+ } satisfies Record<
131
+ // Exclude errorMap since we need to handle that uniquely
132
+ Exclude<keyof AnyBaseFormState, 'errorMap'>,
133
+ null
134
+ >) as Array<keyof AnyBaseFormState>
135
+
136
+ const diffedObject = baseFormKeys.reduce((prev, key) => {
137
+ if (form.state[key] !== newObj.state[key]) {
138
+ prev[key] = newObj.state[key]
139
+ }
140
+ return prev
141
+ }, {} as Partial<AnyBaseFormState>)
142
+
143
+ batch(() => {
144
+ if (Object.keys(diffedObject).length) {
145
+ form.baseStore.setState((prev) => ({ ...prev, ...diffedObject }))
146
+ }
147
+
148
+ if (newObj.state.errorMap !== form.state.errorMap) {
149
+ // Check if we need to update `fieldMetaBase` with `errorMaps` set by
150
+ form.setErrorMap(newObj.state.errorMap)
151
+ }
152
+ })
153
+
154
+ return newObj
155
+ }
package/src/utils.ts CHANGED
@@ -615,3 +615,47 @@ export const throttleFormState = liteThrottle(
615
615
  wait: 300,
616
616
  },
617
617
  )
618
+
619
+ // Do not use a serialize and deserialize method like JSON.stringify/parse
620
+ // as that will drop functions, dates, undefined, Infinity, NaN, etc.
621
+ export function deepCopy<T>(obj: T): T {
622
+ if (obj === null || typeof obj !== 'object') {
623
+ return obj
624
+ }
625
+
626
+ if (obj instanceof Date) {
627
+ return new Date(obj.getTime()) as any
628
+ }
629
+
630
+ if (Array.isArray(obj)) {
631
+ const arrCopy = [] as any[]
632
+ for (let i = 0; i < obj.length; i++) {
633
+ arrCopy[i] = deepCopy(obj[i])
634
+ }
635
+ return arrCopy as any
636
+ }
637
+
638
+ if (obj instanceof Map) {
639
+ const mapCopy = new Map()
640
+ obj.forEach((value, key) => {
641
+ mapCopy.set(key, deepCopy(value))
642
+ })
643
+ return mapCopy as any
644
+ }
645
+
646
+ if (obj instanceof Set) {
647
+ const setCopy = new Set()
648
+ obj.forEach((value) => {
649
+ setCopy.add(deepCopy(value))
650
+ })
651
+ return setCopy as any
652
+ }
653
+
654
+ const copy: { [key: string]: any } = {}
655
+ for (const key in obj) {
656
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
657
+ copy[key] = deepCopy((obj as any)[key])
658
+ }
659
+ }
660
+ return copy as T
661
+ }