@tanstack/form-core 1.10.0 → 1.11.1
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.
- package/dist/cjs/FormApi.cjs +40 -7
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/cjs/FormApi.d.cts +11 -11
- package/dist/cjs/types.d.cts +5 -5
- package/dist/cjs/util-types.d.cts +7 -3
- package/dist/esm/FormApi.d.ts +11 -11
- package/dist/esm/FormApi.js +40 -7
- package/dist/esm/FormApi.js.map +1 -1
- package/dist/esm/types.d.ts +5 -5
- package/dist/esm/util-types.d.ts +7 -3
- package/package.json +1 -1
- package/src/FormApi.ts +58 -18
- package/src/types.ts +14 -4
- package/src/util-types.ts +14 -3
package/package.json
CHANGED
package/src/FormApi.ts
CHANGED
|
@@ -37,7 +37,7 @@ import type {
|
|
|
37
37
|
ValidationErrorMap,
|
|
38
38
|
ValidationErrorMapKeys,
|
|
39
39
|
} from './types'
|
|
40
|
-
import type { DeepKeys, DeepValue } from './util-types'
|
|
40
|
+
import type { DeepKeys, DeepKeysOfType, DeepValue } from './util-types'
|
|
41
41
|
import type { Updater } from './utils'
|
|
42
42
|
|
|
43
43
|
/**
|
|
@@ -513,7 +513,7 @@ export type BaseFormState<
|
|
|
513
513
|
/**
|
|
514
514
|
* The error map for the form itself.
|
|
515
515
|
*/
|
|
516
|
-
errorMap:
|
|
516
|
+
errorMap: ValidationErrorMap<
|
|
517
517
|
UnwrapFormValidateOrFn<TOnMount>,
|
|
518
518
|
UnwrapFormValidateOrFn<TOnChange>,
|
|
519
519
|
UnwrapFormAsyncValidateOrFn<TOnChangeAsync>,
|
|
@@ -1309,7 +1309,9 @@ export class FormApi<
|
|
|
1309
1309
|
/**
|
|
1310
1310
|
* Validates the children of a specified array in the form starting from a given index until the end using the correct handlers for a given validation type.
|
|
1311
1311
|
*/
|
|
1312
|
-
validateArrayFieldsStartingFrom = async <
|
|
1312
|
+
validateArrayFieldsStartingFrom = async <
|
|
1313
|
+
TField extends DeepKeysOfType<TFormData, any[]>,
|
|
1314
|
+
>(
|
|
1313
1315
|
field: TField,
|
|
1314
1316
|
index: number,
|
|
1315
1317
|
cause: ValidationCause,
|
|
@@ -1959,7 +1961,7 @@ export class FormApi<
|
|
|
1959
1961
|
/**
|
|
1960
1962
|
* Pushes a value into an array field.
|
|
1961
1963
|
*/
|
|
1962
|
-
pushFieldValue = <TField extends
|
|
1964
|
+
pushFieldValue = <TField extends DeepKeysOfType<TFormData, any[]>>(
|
|
1963
1965
|
field: TField,
|
|
1964
1966
|
value: DeepValue<TFormData, TField> extends any[]
|
|
1965
1967
|
? DeepValue<TFormData, TField>[number]
|
|
@@ -1974,7 +1976,7 @@ export class FormApi<
|
|
|
1974
1976
|
this.validateField(field, 'change')
|
|
1975
1977
|
}
|
|
1976
1978
|
|
|
1977
|
-
insertFieldValue = async <TField extends
|
|
1979
|
+
insertFieldValue = async <TField extends DeepKeysOfType<TFormData, any[]>>(
|
|
1978
1980
|
field: TField,
|
|
1979
1981
|
index: number,
|
|
1980
1982
|
value: DeepValue<TFormData, TField> extends any[]
|
|
@@ -2006,7 +2008,7 @@ export class FormApi<
|
|
|
2006
2008
|
/**
|
|
2007
2009
|
* Replaces a value into an array field at the specified index.
|
|
2008
2010
|
*/
|
|
2009
|
-
replaceFieldValue = async <TField extends
|
|
2011
|
+
replaceFieldValue = async <TField extends DeepKeysOfType<TFormData, any[]>>(
|
|
2010
2012
|
field: TField,
|
|
2011
2013
|
index: number,
|
|
2012
2014
|
value: DeepValue<TFormData, TField> extends any[]
|
|
@@ -2032,7 +2034,7 @@ export class FormApi<
|
|
|
2032
2034
|
/**
|
|
2033
2035
|
* Removes a value from an array field at the specified index.
|
|
2034
2036
|
*/
|
|
2035
|
-
removeFieldValue = async <TField extends
|
|
2037
|
+
removeFieldValue = async <TField extends DeepKeysOfType<TFormData, any[]>>(
|
|
2036
2038
|
field: TField,
|
|
2037
2039
|
index: number,
|
|
2038
2040
|
opts?: UpdateMetaOptions,
|
|
@@ -2069,7 +2071,7 @@ export class FormApi<
|
|
|
2069
2071
|
/**
|
|
2070
2072
|
* Swaps the values at the specified indices within an array field.
|
|
2071
2073
|
*/
|
|
2072
|
-
swapFieldValues = <TField extends
|
|
2074
|
+
swapFieldValues = <TField extends DeepKeysOfType<TFormData, any[]>>(
|
|
2073
2075
|
field: TField,
|
|
2074
2076
|
index1: number,
|
|
2075
2077
|
index2: number,
|
|
@@ -2098,7 +2100,7 @@ export class FormApi<
|
|
|
2098
2100
|
/**
|
|
2099
2101
|
* Moves the value at the first specified index to the second specified index within an array field.
|
|
2100
2102
|
*/
|
|
2101
|
-
moveFieldValues = <TField extends
|
|
2103
|
+
moveFieldValues = <TField extends DeepKeysOfType<TFormData, any[]>>(
|
|
2102
2104
|
field: TField,
|
|
2103
2105
|
index1: number,
|
|
2104
2106
|
index2: number,
|
|
@@ -2148,7 +2150,8 @@ export class FormApi<
|
|
|
2148
2150
|
* Updates the form's errorMap
|
|
2149
2151
|
*/
|
|
2150
2152
|
setErrorMap(
|
|
2151
|
-
errorMap:
|
|
2153
|
+
errorMap: FormValidationErrorMap<
|
|
2154
|
+
TFormData,
|
|
2152
2155
|
UnwrapFormValidateOrFn<TOnMount>,
|
|
2153
2156
|
UnwrapFormValidateOrFn<TOnChange>,
|
|
2154
2157
|
UnwrapFormAsyncValidateOrFn<TOnChangeAsync>,
|
|
@@ -2159,13 +2162,50 @@ export class FormApi<
|
|
|
2159
2162
|
UnwrapFormAsyncValidateOrFn<TOnServer>
|
|
2160
2163
|
>,
|
|
2161
2164
|
) {
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2165
|
+
batch(() => {
|
|
2166
|
+
Object.entries(errorMap).forEach(([key, value]) => {
|
|
2167
|
+
const errorMapKey = key as ValidationErrorMapKeys
|
|
2168
|
+
|
|
2169
|
+
if (isGlobalFormValidationError(value)) {
|
|
2170
|
+
const { formError, fieldErrors } = normalizeError<TFormData>(value)
|
|
2171
|
+
|
|
2172
|
+
for (const fieldName of Object.keys(
|
|
2173
|
+
this.fieldInfo,
|
|
2174
|
+
) as DeepKeys<TFormData>[]) {
|
|
2175
|
+
const fieldMeta = this.getFieldMeta(fieldName)
|
|
2176
|
+
if (!fieldMeta) continue
|
|
2177
|
+
|
|
2178
|
+
this.setFieldMeta(fieldName, (prev) => ({
|
|
2179
|
+
...prev,
|
|
2180
|
+
errorMap: {
|
|
2181
|
+
...prev.errorMap,
|
|
2182
|
+
[errorMapKey]: fieldErrors?.[fieldName],
|
|
2183
|
+
},
|
|
2184
|
+
errorSourceMap: {
|
|
2185
|
+
...prev.errorSourceMap,
|
|
2186
|
+
[errorMapKey]: 'form',
|
|
2187
|
+
},
|
|
2188
|
+
}))
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
this.baseStore.setState((prev) => ({
|
|
2192
|
+
...prev,
|
|
2193
|
+
errorMap: {
|
|
2194
|
+
...prev.errorMap,
|
|
2195
|
+
[errorMapKey]: formError,
|
|
2196
|
+
},
|
|
2197
|
+
}))
|
|
2198
|
+
} else {
|
|
2199
|
+
this.baseStore.setState((prev) => ({
|
|
2200
|
+
...prev,
|
|
2201
|
+
errorMap: {
|
|
2202
|
+
...prev.errorMap,
|
|
2203
|
+
[errorMapKey]: value,
|
|
2204
|
+
},
|
|
2205
|
+
}))
|
|
2206
|
+
}
|
|
2207
|
+
})
|
|
2208
|
+
})
|
|
2169
2209
|
}
|
|
2170
2210
|
|
|
2171
2211
|
/**
|
|
@@ -2183,7 +2223,7 @@ export class FormApi<
|
|
|
2183
2223
|
| UnwrapFormAsyncValidateOrFn<TOnSubmitAsync>
|
|
2184
2224
|
| UnwrapFormAsyncValidateOrFn<TOnServer>
|
|
2185
2225
|
>
|
|
2186
|
-
errorMap:
|
|
2226
|
+
errorMap: ValidationErrorMap<
|
|
2187
2227
|
UnwrapFormValidateOrFn<TOnMount>,
|
|
2188
2228
|
UnwrapFormValidateOrFn<TOnChange>,
|
|
2189
2229
|
UnwrapFormAsyncValidateOrFn<TOnChangeAsync>,
|
package/src/types.ts
CHANGED
|
@@ -55,6 +55,7 @@ export type ValidationErrorMapSource = {
|
|
|
55
55
|
* @private
|
|
56
56
|
*/
|
|
57
57
|
export type FormValidationErrorMap<
|
|
58
|
+
TFormData = unknown,
|
|
58
59
|
TOnMountReturn = unknown,
|
|
59
60
|
TOnChangeReturn = unknown,
|
|
60
61
|
TOnChangeAsyncReturn = unknown,
|
|
@@ -64,10 +65,19 @@ export type FormValidationErrorMap<
|
|
|
64
65
|
TOnSubmitAsyncReturn = unknown,
|
|
65
66
|
TOnServerReturn = unknown,
|
|
66
67
|
> = {
|
|
67
|
-
onMount?: TOnMountReturn
|
|
68
|
-
onChange?:
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
onMount?: TOnMountReturn | GlobalFormValidationError<TFormData>
|
|
69
|
+
onChange?:
|
|
70
|
+
| TOnChangeReturn
|
|
71
|
+
| TOnChangeAsyncReturn
|
|
72
|
+
| GlobalFormValidationError<TFormData>
|
|
73
|
+
onBlur?:
|
|
74
|
+
| TOnBlurReturn
|
|
75
|
+
| TOnBlurAsyncReturn
|
|
76
|
+
| GlobalFormValidationError<TFormData>
|
|
77
|
+
onSubmit?:
|
|
78
|
+
| TOnSubmitReturn
|
|
79
|
+
| TOnSubmitAsyncReturn
|
|
80
|
+
| GlobalFormValidationError<TFormData>
|
|
71
81
|
onServer?: TOnServerReturn
|
|
72
82
|
}
|
|
73
83
|
|
package/src/util-types.ts
CHANGED
|
@@ -19,9 +19,12 @@ type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch
|
|
|
19
19
|
*/
|
|
20
20
|
export type Narrow<A> = Try<A, [], NarrowRaw<A>>
|
|
21
21
|
|
|
22
|
-
export interface AnyDeepKeyAndValue
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
export interface AnyDeepKeyAndValue<
|
|
23
|
+
K extends string = string,
|
|
24
|
+
V extends any = any,
|
|
25
|
+
> {
|
|
26
|
+
key: K
|
|
27
|
+
value: V
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
export type ArrayAccessor<TParent extends AnyDeepKeyAndValue> =
|
|
@@ -166,3 +169,11 @@ export type DeepValue<TValue, TAccessor> = unknown extends TValue
|
|
|
166
169
|
: TAccessor extends DeepKeys<TValue>
|
|
167
170
|
? DeepRecord<TValue>[TAccessor]
|
|
168
171
|
: never
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The keys of an object or array, deeply nested and only with a value of TValue
|
|
175
|
+
*/
|
|
176
|
+
export type DeepKeysOfType<TData, TValue> = Extract<
|
|
177
|
+
DeepKeysAndValues<TData>,
|
|
178
|
+
AnyDeepKeyAndValue<string, TValue>
|
|
179
|
+
>['key']
|