@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.
@@ -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
- if (fieldErrors) {
167
- for (const [field, fieldError] of Object.entries(fieldErrors)) {
168
- const oldErrorMap = this.cumulativeFieldsErrorMap[field] || {};
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 ((fieldMeta == null ? void 0 : fieldMeta.errorMap[errorMapKey]) && !((_a2 = currentValidationErrorMap[field]) == null ? void 0 : _a2[errorMapKey])) {
190
- this.cumulativeFieldsErrorMap[field] = {
191
- ...this.cumulativeFieldsErrorMap[field],
192
- [errorMapKey]: void 0
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]: void 0
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 (this.state.errorMap[submitErrKey] && cause !== "submit" && !hasErrored) {
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 fieldErrors;
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
- fieldErrors = fieldErrors ? { ...fieldErrors, ...fieldErrorsFromNormalizeError } : fieldErrorsFromNormalizeError;
284
+ fieldErrorsFromFormValidators = fieldErrorsFromFormValidators ? {
285
+ ...fieldErrorsFromFormValidators,
286
+ ...fieldErrorsFromNormalizeError
287
+ } : fieldErrorsFromNormalizeError;
276
288
  }
277
289
  const errorMapKey = getErrorMapKey(validateObj.cause);
278
- if (fieldErrors) {
279
- for (const [field, fieldError] of Object.entries(fieldErrors)) {
280
- const fieldMeta = this.getFieldMeta(field);
281
- if (fieldMeta && fieldMeta.errorMap[errorMapKey] !== fieldError) {
282
- this.setFieldMeta(field, (prev) => ({
283
- ...prev,
284
- errorMap: {
285
- ...prev.errorMap,
286
- [errorMapKey]: fieldError
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(fieldErrors ? { fieldErrors, errorMapKey } : void 0);
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 ((_f = (_e = this.options).onSubmit) == null ? void 0 : _f.call(_e, {
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