@tanstack/form-core 1.16.0 → 1.18.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 +48 -40
- package/dist/cjs/FieldApi.cjs.map +1 -1
- package/dist/cjs/FieldApi.d.cts +33 -30
- package/dist/cjs/FieldGroupApi.cjs.map +1 -1
- package/dist/cjs/FieldGroupApi.d.cts +6 -6
- package/dist/cjs/FormApi.cjs +82 -69
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/cjs/FormApi.d.cts +45 -35
- package/dist/cjs/ValidationLogic.cjs +106 -0
- package/dist/cjs/ValidationLogic.cjs.map +1 -0
- package/dist/cjs/ValidationLogic.d.cts +47 -0
- package/dist/cjs/formOptions.cjs.map +1 -1
- package/dist/cjs/formOptions.d.cts +1 -1
- package/dist/cjs/index.cjs +3 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/mergeForm.cjs.map +1 -1
- package/dist/cjs/mergeForm.d.cts +1 -1
- package/dist/cjs/metaHelper.cjs.map +1 -1
- package/dist/cjs/metaHelper.d.cts +1 -1
- package/dist/cjs/types.d.cts +6 -3
- package/dist/cjs/utils.cjs +51 -63
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +11 -5
- package/dist/esm/FieldApi.d.ts +33 -30
- package/dist/esm/FieldApi.js +48 -40
- package/dist/esm/FieldApi.js.map +1 -1
- package/dist/esm/FieldGroupApi.d.ts +6 -6
- package/dist/esm/FieldGroupApi.js.map +1 -1
- package/dist/esm/FormApi.d.ts +45 -35
- package/dist/esm/FormApi.js +82 -69
- package/dist/esm/FormApi.js.map +1 -1
- package/dist/esm/ValidationLogic.d.ts +47 -0
- package/dist/esm/ValidationLogic.js +106 -0
- package/dist/esm/ValidationLogic.js.map +1 -0
- package/dist/esm/formOptions.d.ts +1 -1
- package/dist/esm/formOptions.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/mergeForm.d.ts +1 -1
- package/dist/esm/mergeForm.js.map +1 -1
- package/dist/esm/metaHelper.d.ts +1 -1
- package/dist/esm/metaHelper.js.map +1 -1
- package/dist/esm/types.d.ts +6 -3
- package/dist/esm/utils.d.ts +11 -5
- package/dist/esm/utils.js +51 -63
- package/dist/esm/utils.js.map +1 -1
- package/package.json +16 -3
- package/src/FieldApi.ts +185 -14
- package/src/FieldGroupApi.ts +14 -0
- package/src/FormApi.ts +139 -3
- package/src/ValidationLogic.ts +200 -0
- package/src/formOptions.ts +1 -1
- package/src/index.ts +1 -0
- package/src/mergeForm.ts +16 -1
- package/src/metaHelper.ts +4 -0
- package/src/types.ts +17 -1
- package/src/utils.ts +159 -109
package/dist/cjs/FormApi.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const store = require("@tanstack/store");
|
|
4
4
|
const utils = require("./utils.cjs");
|
|
5
|
+
const ValidationLogic = require("./ValidationLogic.cjs");
|
|
5
6
|
const standardSchemaValidator = require("./standardSchemaValidator.cjs");
|
|
6
7
|
const metaHelper = require("./metaHelper.cjs");
|
|
7
8
|
function getDefaultFormState(defaultState) {
|
|
@@ -19,7 +20,8 @@ function getDefaultFormState(defaultState) {
|
|
|
19
20
|
onBlur: void 0,
|
|
20
21
|
onSubmit: void 0,
|
|
21
22
|
onMount: void 0,
|
|
22
|
-
onServer: void 0
|
|
23
|
+
onServer: void 0,
|
|
24
|
+
onDynamic: void 0
|
|
23
25
|
}
|
|
24
26
|
};
|
|
25
27
|
}
|
|
@@ -28,32 +30,29 @@ class FormApi {
|
|
|
28
30
|
* Constructs a new `FormApi` instance with the given form options.
|
|
29
31
|
*/
|
|
30
32
|
constructor(opts) {
|
|
31
|
-
var _a;
|
|
32
33
|
this.options = {};
|
|
33
34
|
this.fieldInfo = {};
|
|
34
35
|
this.prevTransformArray = [];
|
|
35
36
|
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
|
-
|
|
43
|
+
this.options.listeners?.onMount?.({ formApi: this });
|
|
44
44
|
const { onMount } = this.options.validators || {};
|
|
45
45
|
if (!onMount) return cleanup;
|
|
46
46
|
this.validateSync("mount");
|
|
47
47
|
return cleanup;
|
|
48
48
|
};
|
|
49
49
|
this.update = (options) => {
|
|
50
|
-
var _a2, _b;
|
|
51
50
|
if (!options) return;
|
|
52
51
|
const oldOptions = this.options;
|
|
53
52
|
this.options = options;
|
|
54
|
-
const shouldUpdateReeval = !!
|
|
53
|
+
const shouldUpdateReeval = !!options.transform?.deps?.some(
|
|
55
54
|
(val, i) => val !== this.prevTransformArray[i]
|
|
56
|
-
)
|
|
55
|
+
);
|
|
57
56
|
const shouldUpdateValues = options.defaultValues && !utils.evaluate(options.defaultValues, oldOptions.defaultValues) && !this.state.isTouched;
|
|
58
57
|
const shouldUpdateState = !utils.evaluate(options.defaultState, oldOptions.defaultState) && !this.state.isTouched;
|
|
59
58
|
if (!shouldUpdateValues && !shouldUpdateState && !shouldUpdateReeval) return;
|
|
@@ -76,21 +75,18 @@ class FormApi {
|
|
|
76
75
|
this.reset = (values, opts2) => {
|
|
77
76
|
const { fieldMeta: currentFieldMeta } = this.state;
|
|
78
77
|
const fieldMetaBase = this.resetFieldMeta(currentFieldMeta);
|
|
79
|
-
if (values && !
|
|
78
|
+
if (values && !opts2?.keepDefaultValues) {
|
|
80
79
|
this.options = {
|
|
81
80
|
...this.options,
|
|
82
81
|
defaultValues: values
|
|
83
82
|
};
|
|
84
83
|
}
|
|
85
84
|
this.baseStore.setState(
|
|
86
|
-
() => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
fieldMetaBase
|
|
92
|
-
});
|
|
93
|
-
}
|
|
85
|
+
() => getDefaultFormState({
|
|
86
|
+
...this.options.defaultState,
|
|
87
|
+
values: values ?? this.options.defaultValues ?? this.options.defaultState?.values,
|
|
88
|
+
fieldMetaBase
|
|
89
|
+
})
|
|
94
90
|
);
|
|
95
91
|
};
|
|
96
92
|
this.validateAllFields = async (cause) => {
|
|
@@ -137,8 +133,7 @@ class FormApi {
|
|
|
137
133
|
return fieldErrorMapMap.flat();
|
|
138
134
|
};
|
|
139
135
|
this.validateField = (field, cause) => {
|
|
140
|
-
|
|
141
|
-
const fieldInstance = (_a2 = this.fieldInfo[field]) == null ? void 0 : _a2.instance;
|
|
136
|
+
const fieldInstance = this.fieldInfo[field]?.instance;
|
|
142
137
|
if (!fieldInstance) return [];
|
|
143
138
|
if (!fieldInstance.state.meta.isTouched) {
|
|
144
139
|
fieldInstance.setMeta((prev) => ({ ...prev, isTouched: true }));
|
|
@@ -146,11 +141,14 @@ class FormApi {
|
|
|
146
141
|
return fieldInstance.validate(cause);
|
|
147
142
|
};
|
|
148
143
|
this.validateSync = (cause) => {
|
|
149
|
-
const validates = utils.getSyncValidatorArray(cause,
|
|
144
|
+
const validates = utils.getSyncValidatorArray(cause, {
|
|
145
|
+
...this.options,
|
|
146
|
+
form: this,
|
|
147
|
+
validationLogic: this.options.validationLogic || ValidationLogic.defaultValidationLogic
|
|
148
|
+
});
|
|
150
149
|
let hasErrored = false;
|
|
151
150
|
const currentValidationErrorMap = {};
|
|
152
151
|
store.batch(() => {
|
|
153
|
-
var _a2, _b;
|
|
154
152
|
for (const validateObj of validates) {
|
|
155
153
|
if (!validateObj.validate) continue;
|
|
156
154
|
const rawError = this.runValidator({
|
|
@@ -173,15 +171,15 @@ class FormApi {
|
|
|
173
171
|
errorMap: currentErrorMap,
|
|
174
172
|
errorSourceMap: currentErrorMapSource
|
|
175
173
|
} = fieldMeta;
|
|
176
|
-
const newFormValidatorError = fieldErrors
|
|
174
|
+
const newFormValidatorError = fieldErrors?.[field];
|
|
177
175
|
const { newErrorValue, newSource } = utils.determineFormLevelErrorSourceAndValue({
|
|
178
176
|
newFormValidatorError,
|
|
179
177
|
isPreviousErrorFromFormValidator: (
|
|
180
178
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
181
|
-
|
|
179
|
+
currentErrorMapSource?.[errorMapKey] === "form"
|
|
182
180
|
),
|
|
183
181
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
184
|
-
previousErrorValue: currentErrorMap
|
|
182
|
+
previousErrorValue: currentErrorMap?.[errorMapKey]
|
|
185
183
|
});
|
|
186
184
|
if (newSource === "form") {
|
|
187
185
|
currentValidationErrorMap[field] = {
|
|
@@ -191,7 +189,7 @@ class FormApi {
|
|
|
191
189
|
}
|
|
192
190
|
if (
|
|
193
191
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
194
|
-
|
|
192
|
+
currentErrorMap?.[errorMapKey] !== newErrorValue
|
|
195
193
|
) {
|
|
196
194
|
this.setFieldMeta(field, (prev) => ({
|
|
197
195
|
...prev,
|
|
@@ -206,7 +204,7 @@ class FormApi {
|
|
|
206
204
|
}));
|
|
207
205
|
}
|
|
208
206
|
}
|
|
209
|
-
if (
|
|
207
|
+
if (this.state.errorMap?.[errorMapKey] !== formError) {
|
|
210
208
|
this.baseStore.setState((prev) => ({
|
|
211
209
|
...prev,
|
|
212
210
|
errorMap: {
|
|
@@ -222,7 +220,7 @@ class FormApi {
|
|
|
222
220
|
const submitErrKey = getErrorMapKey("submit");
|
|
223
221
|
if (
|
|
224
222
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
225
|
-
|
|
223
|
+
this.state.errorMap?.[submitErrKey] && cause !== "submit" && !hasErrored
|
|
226
224
|
) {
|
|
227
225
|
this.baseStore.setState((prev) => ({
|
|
228
226
|
...prev,
|
|
@@ -232,11 +230,28 @@ class FormApi {
|
|
|
232
230
|
}
|
|
233
231
|
}));
|
|
234
232
|
}
|
|
233
|
+
const serverErrKey = getErrorMapKey("server");
|
|
234
|
+
if (
|
|
235
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
236
|
+
this.state.errorMap?.[serverErrKey] && cause !== "server" && !hasErrored
|
|
237
|
+
) {
|
|
238
|
+
this.baseStore.setState((prev) => ({
|
|
239
|
+
...prev,
|
|
240
|
+
errorMap: {
|
|
241
|
+
...prev.errorMap,
|
|
242
|
+
[serverErrKey]: void 0
|
|
243
|
+
}
|
|
244
|
+
}));
|
|
245
|
+
}
|
|
235
246
|
});
|
|
236
247
|
return { hasErrored, fieldsErrorMap: currentValidationErrorMap };
|
|
237
248
|
};
|
|
238
249
|
this.validateAsync = async (cause) => {
|
|
239
|
-
const validates = utils.getAsyncValidatorArray(cause,
|
|
250
|
+
const validates = utils.getAsyncValidatorArray(cause, {
|
|
251
|
+
...this.options,
|
|
252
|
+
form: this,
|
|
253
|
+
validationLogic: this.options.validationLogic || ValidationLogic.defaultValidationLogic
|
|
254
|
+
});
|
|
240
255
|
if (!this.state.isFormValidating) {
|
|
241
256
|
this.baseStore.setState((prev) => ({ ...prev, isFormValidating: true }));
|
|
242
257
|
}
|
|
@@ -246,7 +261,7 @@ class FormApi {
|
|
|
246
261
|
if (!validateObj.validate) continue;
|
|
247
262
|
const key = getErrorMapKey(validateObj.cause);
|
|
248
263
|
const fieldValidatorMeta = this.state.validationMetaMap[key];
|
|
249
|
-
fieldValidatorMeta
|
|
264
|
+
fieldValidatorMeta?.lastAbortController.abort();
|
|
250
265
|
const controller = new AbortController();
|
|
251
266
|
this.state.validationMetaMap[key] = {
|
|
252
267
|
lastAbortController: controller
|
|
@@ -296,19 +311,19 @@ class FormApi {
|
|
|
296
311
|
errorMap: currentErrorMap,
|
|
297
312
|
errorSourceMap: currentErrorMapSource
|
|
298
313
|
} = fieldMeta;
|
|
299
|
-
const newFormValidatorError = fieldErrorsFromFormValidators
|
|
314
|
+
const newFormValidatorError = fieldErrorsFromFormValidators?.[field];
|
|
300
315
|
const { newErrorValue, newSource } = utils.determineFormLevelErrorSourceAndValue({
|
|
301
316
|
newFormValidatorError,
|
|
302
317
|
isPreviousErrorFromFormValidator: (
|
|
303
318
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
304
|
-
|
|
319
|
+
currentErrorMapSource?.[errorMapKey] === "form"
|
|
305
320
|
),
|
|
306
321
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
307
|
-
previousErrorValue: currentErrorMap
|
|
322
|
+
previousErrorValue: currentErrorMap?.[errorMapKey]
|
|
308
323
|
});
|
|
309
324
|
if (
|
|
310
325
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
311
|
-
|
|
326
|
+
currentErrorMap?.[errorMapKey] !== newErrorValue
|
|
312
327
|
) {
|
|
313
328
|
this.setFieldMeta(field, (prev) => ({
|
|
314
329
|
...prev,
|
|
@@ -341,7 +356,7 @@ class FormApi {
|
|
|
341
356
|
if (promises.length) {
|
|
342
357
|
results = await Promise.all(promises);
|
|
343
358
|
for (const fieldValidationResult of results) {
|
|
344
|
-
if (fieldValidationResult
|
|
359
|
+
if (fieldValidationResult?.fieldErrors) {
|
|
345
360
|
const { errorMapKey } = fieldValidationResult;
|
|
346
361
|
for (const [field, fieldError] of Object.entries(
|
|
347
362
|
fieldValidationResult.fieldErrors
|
|
@@ -374,17 +389,17 @@ class FormApi {
|
|
|
374
389
|
return this.state.fieldMeta[field];
|
|
375
390
|
};
|
|
376
391
|
this.getFieldInfo = (field) => {
|
|
377
|
-
|
|
378
|
-
return (_a2 = this.fieldInfo)[field] || (_a2[field] = {
|
|
392
|
+
return this.fieldInfo[field] ||= {
|
|
379
393
|
instance: null,
|
|
380
394
|
validationMetaMap: {
|
|
381
395
|
onChange: void 0,
|
|
382
396
|
onBlur: void 0,
|
|
383
397
|
onSubmit: void 0,
|
|
384
398
|
onMount: void 0,
|
|
385
|
-
onServer: void 0
|
|
399
|
+
onServer: void 0,
|
|
400
|
+
onDynamic: void 0
|
|
386
401
|
}
|
|
387
|
-
}
|
|
402
|
+
};
|
|
388
403
|
};
|
|
389
404
|
this.setFieldMeta = (field, updater) => {
|
|
390
405
|
this.baseStore.setState((prev) => {
|
|
@@ -411,7 +426,7 @@ class FormApi {
|
|
|
411
426
|
);
|
|
412
427
|
};
|
|
413
428
|
this.setFieldValue = (field, updater, opts2) => {
|
|
414
|
-
const dontUpdateMeta =
|
|
429
|
+
const dontUpdateMeta = opts2?.dontUpdateMeta ?? false;
|
|
415
430
|
store.batch(() => {
|
|
416
431
|
if (!dontUpdateMeta) {
|
|
417
432
|
this.setFieldMeta(field, (prev) => ({
|
|
@@ -420,7 +435,7 @@ class FormApi {
|
|
|
420
435
|
isDirty: true,
|
|
421
436
|
errorMap: {
|
|
422
437
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
423
|
-
...prev
|
|
438
|
+
...prev?.errorMap,
|
|
424
439
|
onMount: void 0
|
|
425
440
|
}
|
|
426
441
|
}));
|
|
@@ -594,16 +609,15 @@ class FormApi {
|
|
|
594
609
|
};
|
|
595
610
|
this.baseStore = new store.Store(
|
|
596
611
|
getDefaultFormState({
|
|
597
|
-
...opts
|
|
598
|
-
values:
|
|
612
|
+
...opts?.defaultState,
|
|
613
|
+
values: opts?.defaultValues ?? opts?.defaultState?.values
|
|
599
614
|
})
|
|
600
615
|
);
|
|
601
616
|
this.fieldMetaDerived = new store.Derived({
|
|
602
617
|
deps: [this.baseStore],
|
|
603
618
|
fn: ({ prevDepVals, currDepVals, prevVal: _prevVal }) => {
|
|
604
|
-
var _a2, _b, _c;
|
|
605
619
|
const prevVal = _prevVal;
|
|
606
|
-
const prevBaseStore = prevDepVals
|
|
620
|
+
const prevBaseStore = prevDepVals?.[0];
|
|
607
621
|
const currBaseStore = currDepVals[0];
|
|
608
622
|
let originalMetaCount = 0;
|
|
609
623
|
const fieldMeta = {};
|
|
@@ -611,17 +625,17 @@ class FormApi {
|
|
|
611
625
|
currBaseStore.fieldMetaBase
|
|
612
626
|
)) {
|
|
613
627
|
const currBaseMeta = currBaseStore.fieldMetaBase[fieldName];
|
|
614
|
-
const prevBaseMeta = prevBaseStore
|
|
615
|
-
const prevFieldInfo = prevVal
|
|
628
|
+
const prevBaseMeta = prevBaseStore?.fieldMetaBase[fieldName];
|
|
629
|
+
const prevFieldInfo = prevVal?.[fieldName];
|
|
616
630
|
const curFieldVal = utils.getBy(currBaseStore.values, fieldName);
|
|
617
|
-
let fieldErrors = prevFieldInfo
|
|
631
|
+
let fieldErrors = prevFieldInfo?.errors;
|
|
618
632
|
if (!prevBaseMeta || currBaseMeta.errorMap !== prevBaseMeta.errorMap) {
|
|
619
633
|
fieldErrors = Object.values(currBaseMeta.errorMap ?? {}).filter(
|
|
620
634
|
(val) => val !== void 0
|
|
621
635
|
);
|
|
622
|
-
const fieldInstance =
|
|
636
|
+
const fieldInstance = this.getFieldInfo(fieldName)?.instance;
|
|
623
637
|
if (fieldInstance && !fieldInstance.options.disableErrorFlat) {
|
|
624
|
-
fieldErrors = fieldErrors
|
|
638
|
+
fieldErrors = fieldErrors?.flat(
|
|
625
639
|
1
|
|
626
640
|
);
|
|
627
641
|
}
|
|
@@ -634,7 +648,7 @@ class FormApi {
|
|
|
634
648
|
) || utils.evaluate(
|
|
635
649
|
curFieldVal,
|
|
636
650
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
637
|
-
|
|
651
|
+
this.getFieldInfo(fieldName)?.instance?.options.defaultValue
|
|
638
652
|
);
|
|
639
653
|
if (prevFieldInfo && prevFieldInfo.isPristine === isFieldPristine && prevFieldInfo.isValid === isFieldValid && prevFieldInfo.isDefaultValue === isDefaultValue && prevFieldInfo.errors === fieldErrors && currBaseMeta === prevBaseMeta) {
|
|
640
654
|
fieldMeta[fieldName] = prevFieldInfo;
|
|
@@ -659,9 +673,8 @@ class FormApi {
|
|
|
659
673
|
this.store = new store.Derived({
|
|
660
674
|
deps: [this.baseStore, this.fieldMetaDerived],
|
|
661
675
|
fn: ({ prevDepVals, currDepVals, prevVal: _prevVal }) => {
|
|
662
|
-
var _a2, _b, _c, _d;
|
|
663
676
|
const prevVal = _prevVal;
|
|
664
|
-
const prevBaseStore = prevDepVals
|
|
677
|
+
const prevBaseStore = prevDepVals?.[0];
|
|
665
678
|
const currBaseStore = currDepVals[0];
|
|
666
679
|
const currFieldMeta = currDepVals[1];
|
|
667
680
|
const fieldMetaValues = Object.values(currFieldMeta).filter(
|
|
@@ -678,20 +691,17 @@ class FormApi {
|
|
|
678
691
|
);
|
|
679
692
|
const shouldInvalidateOnMount = (
|
|
680
693
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
681
|
-
isTouched &&
|
|
694
|
+
isTouched && currBaseStore.errorMap?.onMount
|
|
682
695
|
);
|
|
683
696
|
const isDirty = fieldMetaValues.some((field) => field.isDirty);
|
|
684
697
|
const isPristine = !isDirty;
|
|
685
698
|
const hasOnMountError = Boolean(
|
|
686
699
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
687
|
-
|
|
688
|
-
fieldMetaValues.some((f) =>
|
|
689
|
-
var _a3;
|
|
690
|
-
return (_a3 = f == null ? void 0 : f.errorMap) == null ? void 0 : _a3.onMount;
|
|
691
|
-
})
|
|
700
|
+
currBaseStore.errorMap?.onMount || // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
701
|
+
fieldMetaValues.some((f) => f?.errorMap?.onMount)
|
|
692
702
|
);
|
|
693
703
|
const isValidating = !!isFieldsValidating;
|
|
694
|
-
let errors =
|
|
704
|
+
let errors = prevVal?.errors ?? [];
|
|
695
705
|
if (!prevBaseStore || currBaseStore.errorMap !== prevBaseStore.errorMap) {
|
|
696
706
|
errors = Object.values(currBaseStore.errorMap).reduce((prev, curr) => {
|
|
697
707
|
if (curr === void 0) return prev;
|
|
@@ -733,11 +743,11 @@ class FormApi {
|
|
|
733
743
|
isDefaultValue,
|
|
734
744
|
isDirty
|
|
735
745
|
};
|
|
736
|
-
const transformArray =
|
|
746
|
+
const transformArray = this.options.transform?.deps ?? [];
|
|
737
747
|
const shouldTransform = transformArray.length !== this.prevTransformArray.length || transformArray.some((val, i) => val !== this.prevTransformArray[i]);
|
|
738
748
|
if (shouldTransform) {
|
|
739
749
|
const newObj = Object.assign({}, this, { state });
|
|
740
|
-
|
|
750
|
+
this.options.transform?.fn(newObj);
|
|
741
751
|
state = newObj.state;
|
|
742
752
|
this.prevTransformArray = transformArray;
|
|
743
753
|
}
|
|
@@ -750,6 +760,9 @@ class FormApi {
|
|
|
750
760
|
get state() {
|
|
751
761
|
return this.store.state;
|
|
752
762
|
}
|
|
763
|
+
get formId() {
|
|
764
|
+
return this.options.formId;
|
|
765
|
+
}
|
|
753
766
|
/**
|
|
754
767
|
* @private
|
|
755
768
|
*/
|
|
@@ -763,7 +776,6 @@ class FormApi {
|
|
|
763
776
|
return props.validate(props.value);
|
|
764
777
|
}
|
|
765
778
|
async handleSubmit(submitMeta) {
|
|
766
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
767
779
|
this.baseStore.setState((old) => ({
|
|
768
780
|
...old,
|
|
769
781
|
// Submission attempts mark the form as not submitted
|
|
@@ -792,7 +804,7 @@ class FormApi {
|
|
|
792
804
|
await this.validateAllFields("submit");
|
|
793
805
|
if (!this.state.isFieldsValid) {
|
|
794
806
|
done();
|
|
795
|
-
|
|
807
|
+
this.options.onSubmitInvalid?.({
|
|
796
808
|
value: this.state.values,
|
|
797
809
|
formApi: this,
|
|
798
810
|
meta: submitMetaArg
|
|
@@ -802,7 +814,7 @@ class FormApi {
|
|
|
802
814
|
await this.validate("submit");
|
|
803
815
|
if (!this.state.isValid) {
|
|
804
816
|
done();
|
|
805
|
-
|
|
817
|
+
this.options.onSubmitInvalid?.({
|
|
806
818
|
value: this.state.values,
|
|
807
819
|
formApi: this,
|
|
808
820
|
meta: submitMetaArg
|
|
@@ -812,21 +824,20 @@ class FormApi {
|
|
|
812
824
|
store.batch(() => {
|
|
813
825
|
void Object.values(this.fieldInfo).forEach(
|
|
814
826
|
(field) => {
|
|
815
|
-
|
|
816
|
-
(_c2 = (_b2 = (_a2 = field.instance) == null ? void 0 : _a2.options.listeners) == null ? void 0 : _b2.onSubmit) == null ? void 0 : _c2.call(_b2, {
|
|
827
|
+
field.instance?.options.listeners?.onSubmit?.({
|
|
817
828
|
value: field.instance.state.value,
|
|
818
829
|
fieldApi: field.instance
|
|
819
830
|
});
|
|
820
831
|
}
|
|
821
832
|
);
|
|
822
833
|
});
|
|
823
|
-
|
|
834
|
+
this.options.listeners?.onSubmit?.({ formApi: this, meta: submitMetaArg });
|
|
824
835
|
try {
|
|
825
|
-
await
|
|
836
|
+
await this.options.onSubmit?.({
|
|
826
837
|
value: this.state.values,
|
|
827
838
|
formApi: this,
|
|
828
839
|
meta: submitMetaArg
|
|
829
|
-
})
|
|
840
|
+
});
|
|
830
841
|
store.batch(() => {
|
|
831
842
|
this.baseStore.setState((prev) => ({
|
|
832
843
|
...prev,
|
|
@@ -864,7 +875,7 @@ class FormApi {
|
|
|
864
875
|
...prev,
|
|
865
876
|
errorMap: {
|
|
866
877
|
...prev.errorMap,
|
|
867
|
-
[errorMapKey]: fieldErrors
|
|
878
|
+
[errorMapKey]: fieldErrors?.[fieldName]
|
|
868
879
|
},
|
|
869
880
|
errorSourceMap: {
|
|
870
881
|
...prev.errorSourceMap,
|
|
@@ -913,6 +924,8 @@ function getErrorMapKey(cause) {
|
|
|
913
924
|
return "onMount";
|
|
914
925
|
case "server":
|
|
915
926
|
return "onServer";
|
|
927
|
+
case "dynamic":
|
|
928
|
+
return "onDynamic";
|
|
916
929
|
case "change":
|
|
917
930
|
default:
|
|
918
931
|
return "onChange";
|