@tanstack/form-core 1.6.2 → 1.7.0
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/FieldApi.cjs +63 -36
- package/dist/cjs/FieldApi.cjs.map +1 -1
- package/dist/cjs/FieldApi.d.cts +5 -1
- package/dist/cjs/FormApi.cjs +85 -48
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/cjs/FormApi.d.cts +22 -8
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/metaHelper.cjs +2 -1
- package/dist/cjs/metaHelper.cjs.map +1 -1
- package/dist/cjs/types.d.cts +10 -0
- package/dist/cjs/utils.cjs +30 -0
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +24 -1
- package/dist/esm/FieldApi.d.ts +5 -1
- package/dist/esm/FieldApi.js +64 -37
- package/dist/esm/FieldApi.js.map +1 -1
- package/dist/esm/FormApi.d.ts +22 -8
- package/dist/esm/FormApi.js +86 -49
- package/dist/esm/FormApi.js.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/metaHelper.js +2 -1
- package/dist/esm/metaHelper.js.map +1 -1
- package/dist/esm/types.d.ts +10 -0
- package/dist/esm/utils.d.ts +24 -1
- package/dist/esm/utils.js +30 -0
- package/dist/esm/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/FieldApi.ts +76 -32
- package/src/FormApi.ts +191 -70
- package/src/metaHelper.ts +1 -0
- package/src/types.ts +11 -0
- package/src/utils.ts +68 -1
package/dist/cjs/FormApi.cjs
CHANGED
|
@@ -32,14 +32,15 @@ class FormApi {
|
|
|
32
32
|
this.options = {};
|
|
33
33
|
this.fieldInfo = {};
|
|
34
34
|
this.prevTransformArray = [];
|
|
35
|
-
this.cumulativeFieldsErrorMap = {};
|
|
36
35
|
this.mount = () => {
|
|
36
|
+
var _a2, _b;
|
|
37
37
|
const cleanupFieldMetaDerived = this.fieldMetaDerived.mount();
|
|
38
38
|
const cleanupStoreDerived = this.store.mount();
|
|
39
39
|
const cleanup = () => {
|
|
40
40
|
cleanupFieldMetaDerived();
|
|
41
41
|
cleanupStoreDerived();
|
|
42
42
|
};
|
|
43
|
+
(_b = (_a2 = this.options.listeners) == null ? void 0 : _a2.onMount) == null ? void 0 : _b.call(_a2, { formApi: this });
|
|
43
44
|
const { onMount } = this.options.validators || {};
|
|
44
45
|
if (!onMount) return cleanup;
|
|
45
46
|
this.validateSync("mount");
|
|
@@ -149,7 +150,7 @@ class FormApi {
|
|
|
149
150
|
let hasErrored = false;
|
|
150
151
|
const currentValidationErrorMap = {};
|
|
151
152
|
store.batch(() => {
|
|
152
|
-
var _a2;
|
|
153
|
+
var _a2, _b;
|
|
153
154
|
for (const validateObj of validates) {
|
|
154
155
|
if (!validateObj.validate) continue;
|
|
155
156
|
const rawError = this.runValidator({
|
|
@@ -163,44 +164,49 @@ class FormApi {
|
|
|
163
164
|
});
|
|
164
165
|
const { formError, fieldErrors } = normalizeError(rawError);
|
|
165
166
|
const errorMapKey = getErrorMapKey(validateObj.cause);
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const newErrorMap = {
|
|
170
|
-
...oldErrorMap,
|
|
171
|
-
[errorMapKey]: fieldError
|
|
172
|
-
};
|
|
173
|
-
currentValidationErrorMap[field] = newErrorMap;
|
|
174
|
-
this.cumulativeFieldsErrorMap[field] = newErrorMap;
|
|
175
|
-
const fieldMeta = this.getFieldMeta(field);
|
|
176
|
-
if (fieldMeta && fieldMeta.errorMap[errorMapKey] !== fieldError) {
|
|
177
|
-
this.setFieldMeta(field, (prev) => ({
|
|
178
|
-
...prev,
|
|
179
|
-
errorMap: {
|
|
180
|
-
...prev.errorMap,
|
|
181
|
-
[errorMapKey]: fieldError
|
|
182
|
-
}
|
|
183
|
-
}));
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
for (const field of Object.keys(this.cumulativeFieldsErrorMap)) {
|
|
167
|
+
for (const field of Object.keys(
|
|
168
|
+
this.state.fieldMeta
|
|
169
|
+
)) {
|
|
188
170
|
const fieldMeta = this.getFieldMeta(field);
|
|
189
|
-
if (
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
171
|
+
if (!fieldMeta) continue;
|
|
172
|
+
const {
|
|
173
|
+
errorMap: currentErrorMap,
|
|
174
|
+
errorSourceMap: currentErrorMapSource
|
|
175
|
+
} = fieldMeta;
|
|
176
|
+
const newFormValidatorError = fieldErrors == null ? void 0 : fieldErrors[field];
|
|
177
|
+
const { newErrorValue, newSource } = utils.determineFormLevelErrorSourceAndValue({
|
|
178
|
+
newFormValidatorError,
|
|
179
|
+
isPreviousErrorFromFormValidator: (
|
|
180
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
181
|
+
(currentErrorMapSource == null ? void 0 : currentErrorMapSource[errorMapKey]) === "form"
|
|
182
|
+
),
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
184
|
+
previousErrorValue: currentErrorMap == null ? void 0 : currentErrorMap[errorMapKey]
|
|
185
|
+
});
|
|
186
|
+
if (newSource === "form") {
|
|
187
|
+
currentValidationErrorMap[field] = {
|
|
188
|
+
...currentValidationErrorMap[field],
|
|
189
|
+
[errorMapKey]: newFormValidatorError
|
|
193
190
|
};
|
|
191
|
+
}
|
|
192
|
+
if (
|
|
193
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
194
|
+
(currentErrorMap == null ? void 0 : currentErrorMap[errorMapKey]) !== newErrorValue
|
|
195
|
+
) {
|
|
194
196
|
this.setFieldMeta(field, (prev) => ({
|
|
195
197
|
...prev,
|
|
196
198
|
errorMap: {
|
|
197
199
|
...prev.errorMap,
|
|
198
|
-
[errorMapKey]:
|
|
200
|
+
[errorMapKey]: newErrorValue
|
|
201
|
+
},
|
|
202
|
+
errorSourceMap: {
|
|
203
|
+
...prev.errorSourceMap,
|
|
204
|
+
[errorMapKey]: newSource
|
|
199
205
|
}
|
|
200
206
|
}));
|
|
201
207
|
}
|
|
202
208
|
}
|
|
203
|
-
if (this.state.errorMap[errorMapKey] !== formError) {
|
|
209
|
+
if (((_a2 = this.state.errorMap) == null ? void 0 : _a2[errorMapKey]) !== formError) {
|
|
204
210
|
this.baseStore.setState((prev) => ({
|
|
205
211
|
...prev,
|
|
206
212
|
errorMap: {
|
|
@@ -214,7 +220,10 @@ class FormApi {
|
|
|
214
220
|
}
|
|
215
221
|
}
|
|
216
222
|
const submitErrKey = getErrorMapKey("submit");
|
|
217
|
-
if (
|
|
223
|
+
if (
|
|
224
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
225
|
+
((_b = this.state.errorMap) == null ? void 0 : _b[submitErrKey]) && cause !== "submit" && !hasErrored
|
|
226
|
+
) {
|
|
218
227
|
this.baseStore.setState((prev) => ({
|
|
219
228
|
...prev,
|
|
220
229
|
errorMap: {
|
|
@@ -232,7 +241,7 @@ class FormApi {
|
|
|
232
241
|
this.baseStore.setState((prev) => ({ ...prev, isFormValidating: true }));
|
|
233
242
|
}
|
|
234
243
|
const promises = [];
|
|
235
|
-
let
|
|
244
|
+
let fieldErrorsFromFormValidators;
|
|
236
245
|
for (const validateObj of validates) {
|
|
237
246
|
if (!validateObj.validate) continue;
|
|
238
247
|
const key = getErrorMapKey(validateObj.cause);
|
|
@@ -272,21 +281,46 @@ class FormApi {
|
|
|
272
281
|
}
|
|
273
282
|
const { formError, fieldErrors: fieldErrorsFromNormalizeError } = normalizeError(rawError);
|
|
274
283
|
if (fieldErrorsFromNormalizeError) {
|
|
275
|
-
|
|
284
|
+
fieldErrorsFromFormValidators = fieldErrorsFromFormValidators ? {
|
|
285
|
+
...fieldErrorsFromFormValidators,
|
|
286
|
+
...fieldErrorsFromNormalizeError
|
|
287
|
+
} : fieldErrorsFromNormalizeError;
|
|
276
288
|
}
|
|
277
289
|
const errorMapKey = getErrorMapKey(validateObj.cause);
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
+
for (const field of Object.keys(
|
|
291
|
+
this.state.fieldMeta
|
|
292
|
+
)) {
|
|
293
|
+
const fieldMeta = this.getFieldMeta(field);
|
|
294
|
+
if (!fieldMeta) continue;
|
|
295
|
+
const {
|
|
296
|
+
errorMap: currentErrorMap,
|
|
297
|
+
errorSourceMap: currentErrorMapSource
|
|
298
|
+
} = fieldMeta;
|
|
299
|
+
const newFormValidatorError = fieldErrorsFromFormValidators == null ? void 0 : fieldErrorsFromFormValidators[field];
|
|
300
|
+
const { newErrorValue, newSource } = utils.determineFormLevelErrorSourceAndValue({
|
|
301
|
+
newFormValidatorError,
|
|
302
|
+
isPreviousErrorFromFormValidator: (
|
|
303
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
304
|
+
(currentErrorMapSource == null ? void 0 : currentErrorMapSource[errorMapKey]) === "form"
|
|
305
|
+
),
|
|
306
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
307
|
+
previousErrorValue: currentErrorMap == null ? void 0 : currentErrorMap[errorMapKey]
|
|
308
|
+
});
|
|
309
|
+
if (
|
|
310
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
311
|
+
(currentErrorMap == null ? void 0 : currentErrorMap[errorMapKey]) !== newErrorValue
|
|
312
|
+
) {
|
|
313
|
+
this.setFieldMeta(field, (prev) => ({
|
|
314
|
+
...prev,
|
|
315
|
+
errorMap: {
|
|
316
|
+
...prev.errorMap,
|
|
317
|
+
[errorMapKey]: newErrorValue
|
|
318
|
+
},
|
|
319
|
+
errorSourceMap: {
|
|
320
|
+
...prev.errorSourceMap,
|
|
321
|
+
[errorMapKey]: newSource
|
|
322
|
+
}
|
|
323
|
+
}));
|
|
290
324
|
}
|
|
291
325
|
}
|
|
292
326
|
this.baseStore.setState((prev) => ({
|
|
@@ -296,7 +330,9 @@ class FormApi {
|
|
|
296
330
|
[errorMapKey]: formError
|
|
297
331
|
}
|
|
298
332
|
}));
|
|
299
|
-
resolve(
|
|
333
|
+
resolve(
|
|
334
|
+
fieldErrorsFromFormValidators ? { fieldErrors: fieldErrorsFromFormValidators, errorMapKey } : void 0
|
|
335
|
+
);
|
|
300
336
|
})
|
|
301
337
|
);
|
|
302
338
|
}
|
|
@@ -700,7 +736,7 @@ class FormApi {
|
|
|
700
736
|
return props.validate(props.value);
|
|
701
737
|
}
|
|
702
738
|
async handleSubmit(submitMeta) {
|
|
703
|
-
var _a, _b, _c, _d, _e, _f;
|
|
739
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
704
740
|
this.baseStore.setState((old) => ({
|
|
705
741
|
...old,
|
|
706
742
|
// Submission attempts mark the form as not submitted
|
|
@@ -754,8 +790,9 @@ class FormApi {
|
|
|
754
790
|
}
|
|
755
791
|
);
|
|
756
792
|
});
|
|
793
|
+
(_f = (_e = this.options.listeners) == null ? void 0 : _e.onSubmit) == null ? void 0 : _f.call(_e, { formApi: this });
|
|
757
794
|
try {
|
|
758
|
-
await ((
|
|
795
|
+
await ((_h = (_g = this.options).onSubmit) == null ? void 0 : _h.call(_g, {
|
|
759
796
|
value: this.state.values,
|
|
760
797
|
formApi: this,
|
|
761
798
|
meta: submitMeta ?? this.options.onSubmitMeta
|