@flowgram.ai/form 0.1.0-alpha.3 → 0.1.0-alpha.31
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/esm/index.js +180 -78
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.mts +132 -15
- package/dist/index.d.ts +132 -15
- package/dist/index.js +214 -111
- package/dist/index.js.map +1 -1
- package/package.json +15 -14
package/dist/esm/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// src/react/field.tsx
|
|
2
2
|
import * as React2 from "react";
|
|
3
|
-
import { isFunction } from "lodash";
|
|
3
|
+
import { isFunction } from "lodash-es";
|
|
4
4
|
import { DisposableCollection, useRefresh } from "@flowgram.ai/utils";
|
|
5
5
|
import { useReadonlyReactiveState } from "@flowgram.ai/reactive";
|
|
6
6
|
|
|
7
7
|
// src/utils/object.ts
|
|
8
|
-
import { clone, toPath } from "lodash";
|
|
8
|
+
import { clone, toPath } from "lodash-es";
|
|
9
9
|
var isObject = (obj) => obj !== null && typeof obj === "object";
|
|
10
10
|
var isInteger = (obj) => String(Math.floor(Number(obj))) === obj;
|
|
11
11
|
function getIn(obj, key, def, p = 0) {
|
|
@@ -33,17 +33,10 @@ function shallowSetIn(obj, path, value) {
|
|
|
33
33
|
resVal = resVal[currentPath] = isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {};
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
if ((i === 0 ? obj : resVal)[pathArray[i]] === value) {
|
|
36
|
+
if ((i === 0 ? obj : resVal)[pathArray[i]] === value && pathArray[i] in obj) {
|
|
37
37
|
return obj;
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
delete resVal[pathArray[i]];
|
|
41
|
-
} else {
|
|
42
|
-
resVal[pathArray[i]] = value;
|
|
43
|
-
}
|
|
44
|
-
if (i === 0 && value === void 0) {
|
|
45
|
-
delete res[pathArray[i]];
|
|
46
|
-
}
|
|
39
|
+
resVal[pathArray[i]] = value;
|
|
47
40
|
return res;
|
|
48
41
|
}
|
|
49
42
|
function keepValidKeys(obj, validKeys) {
|
|
@@ -66,7 +59,7 @@ function isCheckBoxEvent(e) {
|
|
|
66
59
|
}
|
|
67
60
|
|
|
68
61
|
// src/utils/glob.ts
|
|
69
|
-
import { flatten, get, isArray, isObject as isObject2 } from "lodash";
|
|
62
|
+
import { flatten, get, isArray, isObject as isObject2 } from "lodash-es";
|
|
70
63
|
var Glob;
|
|
71
64
|
((Glob2) => {
|
|
72
65
|
Glob2.DIVIDER = ".";
|
|
@@ -175,7 +168,7 @@ var Glob;
|
|
|
175
168
|
let curPaths = [];
|
|
176
169
|
let curValue = obj;
|
|
177
170
|
while (curKey) {
|
|
178
|
-
let isObject3 = typeof curValue === "object";
|
|
171
|
+
let isObject3 = typeof curValue === "object" && curValue !== null;
|
|
179
172
|
if (!isObject3) return [];
|
|
180
173
|
if (curKey === Glob2.ALL) {
|
|
181
174
|
const parentPath = curPaths.join(Glob2.DIVIDER);
|
|
@@ -201,18 +194,20 @@ var Glob;
|
|
|
201
194
|
}
|
|
202
195
|
Glob2.findMatchPaths = findMatchPaths;
|
|
203
196
|
function findMatchPathsWithEmptyValue(obj, pattern) {
|
|
197
|
+
if (!pattern.includes("*")) {
|
|
198
|
+
return [pattern];
|
|
199
|
+
}
|
|
204
200
|
return findMatchPaths(obj, pattern, true);
|
|
205
201
|
}
|
|
206
202
|
Glob2.findMatchPathsWithEmptyValue = findMatchPathsWithEmptyValue;
|
|
207
203
|
})(Glob || (Glob = {}));
|
|
208
204
|
|
|
209
205
|
// src/types/validate.ts
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
206
|
+
var FeedbackLevel = /* @__PURE__ */ ((FeedbackLevel2) => {
|
|
207
|
+
FeedbackLevel2["Error"] = "error";
|
|
208
|
+
FeedbackLevel2["Warning"] = "warning";
|
|
209
|
+
return FeedbackLevel2;
|
|
210
|
+
})(FeedbackLevel || {});
|
|
216
211
|
var ValidateTrigger = /* @__PURE__ */ ((ValidateTrigger2) => {
|
|
217
212
|
ValidateTrigger2["onChange"] = "onChange";
|
|
218
213
|
ValidateTrigger2["onBlur"] = "onBlur";
|
|
@@ -220,10 +215,10 @@ var ValidateTrigger = /* @__PURE__ */ ((ValidateTrigger2) => {
|
|
|
220
215
|
})(ValidateTrigger || {});
|
|
221
216
|
|
|
222
217
|
// src/core/utils.ts
|
|
223
|
-
import { isEmpty, isEqual } from "lodash";
|
|
218
|
+
import { isEmpty, isEqual } from "lodash-es";
|
|
224
219
|
|
|
225
220
|
// src/core/path.ts
|
|
226
|
-
import { toPath as toPath2 } from "lodash";
|
|
221
|
+
import { toPath as toPath2 } from "lodash-es";
|
|
227
222
|
var Path = class _Path {
|
|
228
223
|
constructor(path) {
|
|
229
224
|
this._path = [];
|
|
@@ -309,14 +304,14 @@ var Path = class _Path {
|
|
|
309
304
|
replaceParent(parent, newParent) {
|
|
310
305
|
if (parent.value.length > this.value.length) {
|
|
311
306
|
throw new Error(
|
|
312
|
-
`[Form] Error in Path.
|
|
307
|
+
`[Form] Error in Path.replaceParent: invalid parent param: ${parent}, parent length should not greater than current length.`
|
|
313
308
|
);
|
|
314
309
|
}
|
|
315
310
|
const rest = [];
|
|
316
311
|
for (let i = 0; i < this.value.length; i++) {
|
|
317
312
|
if (i < parent.value.length && parent.value[i] !== this.value[i]) {
|
|
318
313
|
throw new Error(
|
|
319
|
-
`[Form] Error in Path.
|
|
314
|
+
`[Form] Error in Path.replaceParent: invalid parent param: '${parent}' is not a parent of '${this.toString()}'`
|
|
320
315
|
);
|
|
321
316
|
}
|
|
322
317
|
if (i >= parent.value.length) {
|
|
@@ -329,7 +324,7 @@ var Path = class _Path {
|
|
|
329
324
|
|
|
330
325
|
// src/core/utils.ts
|
|
331
326
|
function updateFeedbacksName(feedbacks, name) {
|
|
332
|
-
return feedbacks.map((f) => ({
|
|
327
|
+
return (feedbacks || []).map((f) => ({
|
|
333
328
|
...f,
|
|
334
329
|
name
|
|
335
330
|
}));
|
|
@@ -390,7 +385,7 @@ var FieldEventUtils;
|
|
|
390
385
|
FieldEventUtils2.shouldTriggerFieldChangeEvent = shouldTriggerFieldChangeEvent;
|
|
391
386
|
function shouldTriggerFieldValidateWhenChange(payload, fieldName) {
|
|
392
387
|
const { name: changedName, options } = payload;
|
|
393
|
-
if (options?.action === "array-splice") {
|
|
388
|
+
if (options?.action === "array-splice" || options?.action === "array-swap") {
|
|
394
389
|
return fieldName === changedName;
|
|
395
390
|
}
|
|
396
391
|
return FieldEventUtils2.shouldTriggerFieldChangeEvent(payload, fieldName);
|
|
@@ -478,7 +473,8 @@ function toForm(model) {
|
|
|
478
473
|
},
|
|
479
474
|
state: toFormState(model.state),
|
|
480
475
|
getValueIn: (name) => model.getValueIn(name),
|
|
481
|
-
setValueIn: (name, value) => model.setValueIn(name, value)
|
|
476
|
+
setValueIn: (name, value) => model.setValueIn(name, value),
|
|
477
|
+
validate: model.validate.bind(model)
|
|
482
478
|
};
|
|
483
479
|
Object.defineProperty(res, "_formModel", {
|
|
484
480
|
enumerable: false,
|
|
@@ -550,6 +546,11 @@ function Field({
|
|
|
550
546
|
const formState = toFormState(formModelState);
|
|
551
547
|
const refresh = useRefresh();
|
|
552
548
|
React2.useEffect(() => {
|
|
549
|
+
if (fieldModel.disposed) {
|
|
550
|
+
refresh();
|
|
551
|
+
return () => {
|
|
552
|
+
};
|
|
553
|
+
}
|
|
553
554
|
fieldModel.renderCount = fieldModel.renderCount + 1;
|
|
554
555
|
if (!formModel.getValueIn(name) !== void 0 && defaultValue !== void 0) {
|
|
555
556
|
formModel.setInitValueIn(name, defaultValue);
|
|
@@ -590,12 +591,13 @@ function Field({
|
|
|
590
591
|
}
|
|
591
592
|
return React2.cloneElement(children, { ...field });
|
|
592
593
|
};
|
|
594
|
+
if (fieldModel.disposed) return /* @__PURE__ */ React2.createElement(React2.Fragment, null);
|
|
593
595
|
return /* @__PURE__ */ React2.createElement(FieldModelContext.Provider, { value: fieldModel }, renderInner());
|
|
594
596
|
}
|
|
595
597
|
|
|
596
598
|
// src/react/form.tsx
|
|
597
599
|
import React3, { Children, useEffect as useEffect2, useMemo as useMemo2 } from "react";
|
|
598
|
-
import { isFunction as isFunction2 } from "lodash";
|
|
600
|
+
import { isFunction as isFunction2 } from "lodash-es";
|
|
599
601
|
|
|
600
602
|
// src/core/to-field-array.ts
|
|
601
603
|
function toFieldArray(model) {
|
|
@@ -614,7 +616,12 @@ function toFieldArray(model) {
|
|
|
614
616
|
},
|
|
615
617
|
map: (cb) => model.map((f, index) => cb(toField(f), index)),
|
|
616
618
|
append: (value) => toField(model.append(value)),
|
|
619
|
+
/**
|
|
620
|
+
* @deprecated: use remove instead
|
|
621
|
+
* @param index
|
|
622
|
+
*/
|
|
617
623
|
delete: (index) => model.delete(index),
|
|
624
|
+
remove: (index) => model.delete(index),
|
|
618
625
|
swap: (from, to) => model.swap(from, to),
|
|
619
626
|
move: (from, to) => model.move(from, to)
|
|
620
627
|
};
|
|
@@ -628,7 +635,8 @@ function toFieldArray(model) {
|
|
|
628
635
|
}
|
|
629
636
|
|
|
630
637
|
// src/core/form-model.ts
|
|
631
|
-
import {
|
|
638
|
+
import { flatten as flatten2, get as get4 } from "lodash-es";
|
|
639
|
+
import { deepEqual } from "fast-equals";
|
|
632
640
|
import { Emitter as Emitter3 } from "@flowgram.ai/utils";
|
|
633
641
|
import { ReactiveState as ReactiveState2 } from "@flowgram.ai/reactive";
|
|
634
642
|
|
|
@@ -652,14 +660,7 @@ function feedbackToFieldErrorsOrWarnings(name, feedback) {
|
|
|
652
660
|
[name]: feedback ? [feedback] : []
|
|
653
661
|
};
|
|
654
662
|
}
|
|
655
|
-
var hasError = (errors) =>
|
|
656
|
-
for (let fieldErrors in errors) {
|
|
657
|
-
if (fieldErrors.length) {
|
|
658
|
-
return true;
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
return false;
|
|
662
|
-
};
|
|
663
|
+
var hasError = (errors) => Object.keys(errors).some((key) => errors[key]?.length > 0);
|
|
663
664
|
|
|
664
665
|
// src/constants.ts
|
|
665
666
|
var DEFAULT_FIELD_STATE = {
|
|
@@ -688,22 +689,19 @@ function createFieldModelState(initialState) {
|
|
|
688
689
|
}
|
|
689
690
|
|
|
690
691
|
// src/core/store.ts
|
|
691
|
-
import { get as get2 } from "lodash";
|
|
692
|
+
import { get as get2, clone as clone2, cloneDeep } from "lodash-es";
|
|
692
693
|
var Store = class {
|
|
693
694
|
get values() {
|
|
694
|
-
return this._values;
|
|
695
|
+
return clone2(this._values);
|
|
695
696
|
}
|
|
696
697
|
set values(v) {
|
|
697
|
-
this._values = v;
|
|
698
|
-
}
|
|
699
|
-
setInitialValues(values) {
|
|
700
|
-
this._values = values;
|
|
698
|
+
this._values = cloneDeep(v);
|
|
701
699
|
}
|
|
702
700
|
setIn(path, value) {
|
|
703
701
|
this._values = shallowSetIn(this._values || {}, path.toString(), value);
|
|
704
702
|
}
|
|
705
703
|
getIn(path) {
|
|
706
|
-
return get2(this.
|
|
704
|
+
return get2(this.values, path.value);
|
|
707
705
|
}
|
|
708
706
|
dispose() {
|
|
709
707
|
}
|
|
@@ -711,7 +709,7 @@ var Store = class {
|
|
|
711
709
|
|
|
712
710
|
// src/core/field-model.ts
|
|
713
711
|
import { nanoid } from "nanoid";
|
|
714
|
-
import { get as get3, groupBy, some } from "lodash";
|
|
712
|
+
import { get as get3, groupBy, some } from "lodash-es";
|
|
715
713
|
import { DisposableCollection as DisposableCollection2, Emitter } from "@flowgram.ai/utils";
|
|
716
714
|
import { ReactiveState } from "@flowgram.ai/reactive";
|
|
717
715
|
var FieldModel = class {
|
|
@@ -932,21 +930,19 @@ var FieldModel = class {
|
|
|
932
930
|
this.form.onValidateEmitter.fire(this.form.state);
|
|
933
931
|
}
|
|
934
932
|
async _runAsyncValidate() {
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
const
|
|
938
|
-
if (!
|
|
933
|
+
let errors = [];
|
|
934
|
+
let warnings = [];
|
|
935
|
+
const results = await this.form.validateIn(this.name);
|
|
936
|
+
if (!results?.length) {
|
|
939
937
|
return {};
|
|
940
938
|
} else {
|
|
941
|
-
const
|
|
942
|
-
if (!
|
|
939
|
+
const feedbacks = results.map((result) => toFeedback(result, this.name)).filter(Boolean);
|
|
940
|
+
if (!feedbacks?.length) {
|
|
943
941
|
return {};
|
|
944
942
|
}
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
errors.push(feedback);
|
|
949
|
-
}
|
|
943
|
+
const groupedFeedbacks = groupBy(feedbacks, "level");
|
|
944
|
+
warnings = warnings.concat(groupedFeedbacks["warning" /* Warning */] || []);
|
|
945
|
+
errors = errors.concat(groupedFeedbacks["error" /* Error */] || []);
|
|
950
946
|
}
|
|
951
947
|
return { errors, warnings };
|
|
952
948
|
}
|
|
@@ -960,6 +956,9 @@ var FieldModel = class {
|
|
|
960
956
|
onDispose(fn) {
|
|
961
957
|
this.toDispose.onDispose(fn);
|
|
962
958
|
}
|
|
959
|
+
get disposed() {
|
|
960
|
+
return this.toDispose.disposed;
|
|
961
|
+
}
|
|
963
962
|
};
|
|
964
963
|
|
|
965
964
|
// src/core/field-array-model.ts
|
|
@@ -983,7 +982,7 @@ var FieldArrayModel = class extends FieldModel {
|
|
|
983
982
|
const p1 = f1.path.value;
|
|
984
983
|
const p2 = f2.path.value;
|
|
985
984
|
const i1 = parseInt(p1[p1.length - 1]);
|
|
986
|
-
const i2 = parseInt(
|
|
985
|
+
const i2 = parseInt(p2[p2.length - 1]);
|
|
987
986
|
return i1 - i2;
|
|
988
987
|
});
|
|
989
988
|
}
|
|
@@ -1027,7 +1026,7 @@ var FieldArrayModel = class extends FieldModel {
|
|
|
1027
1026
|
return newElemField;
|
|
1028
1027
|
}
|
|
1029
1028
|
/**
|
|
1030
|
-
*
|
|
1029
|
+
* Delete the element in given index and delete the corresponding FieldModel as well
|
|
1031
1030
|
* @param index
|
|
1032
1031
|
*/
|
|
1033
1032
|
delete(index) {
|
|
@@ -1108,12 +1107,49 @@ var FieldArrayModel = class extends FieldModel {
|
|
|
1108
1107
|
`[Form]: FieldArrayModel.swap Error: invalid params 'form' and 'to', form=${from} to=${to}. expect the value between 0 to ${length - 1}`
|
|
1109
1108
|
);
|
|
1110
1109
|
}
|
|
1110
|
+
const oldFormValues = this.form.values;
|
|
1111
1111
|
const tempValue = [...this.value];
|
|
1112
1112
|
const fromValue = tempValue[from];
|
|
1113
1113
|
const toValue = tempValue[to];
|
|
1114
1114
|
tempValue[to] = fromValue;
|
|
1115
1115
|
tempValue[from] = toValue;
|
|
1116
|
-
this.form.
|
|
1116
|
+
this.form.store.setIn(this.path, tempValue);
|
|
1117
|
+
this.form.fireOnFormValuesChange({
|
|
1118
|
+
values: this.form.values,
|
|
1119
|
+
prevValues: oldFormValues,
|
|
1120
|
+
name: this.name,
|
|
1121
|
+
options: {
|
|
1122
|
+
action: "array-swap",
|
|
1123
|
+
indexes: [from, to]
|
|
1124
|
+
}
|
|
1125
|
+
});
|
|
1126
|
+
const newFieldMap = new Map(this.form.fieldMap);
|
|
1127
|
+
const fromFields = this.findAllFieldsAt(from);
|
|
1128
|
+
const toFields = this.findAllFieldsAt(to);
|
|
1129
|
+
const fromRootPath = this.getPathAt(from);
|
|
1130
|
+
const toRootPath = this.getPathAt(to);
|
|
1131
|
+
const leafFieldsModified = [];
|
|
1132
|
+
fromFields.forEach((f) => {
|
|
1133
|
+
const newName = f.path.replaceParent(fromRootPath, toRootPath).toString();
|
|
1134
|
+
f.name = newName;
|
|
1135
|
+
if (!f.children.length) {
|
|
1136
|
+
f.updateNameForLeafState(newName);
|
|
1137
|
+
leafFieldsModified.push(f);
|
|
1138
|
+
}
|
|
1139
|
+
newFieldMap.set(newName, f);
|
|
1140
|
+
});
|
|
1141
|
+
toFields.forEach((f) => {
|
|
1142
|
+
const newName = f.path.replaceParent(toRootPath, fromRootPath).toString();
|
|
1143
|
+
f.name = newName;
|
|
1144
|
+
if (!f.children.length) {
|
|
1145
|
+
f.updateNameForLeafState(newName);
|
|
1146
|
+
}
|
|
1147
|
+
newFieldMap.set(newName, f);
|
|
1148
|
+
leafFieldsModified.push(f);
|
|
1149
|
+
});
|
|
1150
|
+
this.form.fieldMap = newFieldMap;
|
|
1151
|
+
leafFieldsModified.forEach((f) => f.bubbleState());
|
|
1152
|
+
this.form.alignStateWithFieldMap();
|
|
1117
1153
|
}
|
|
1118
1154
|
move(from, to) {
|
|
1119
1155
|
if (!this.value) {
|
|
@@ -1130,6 +1166,41 @@ var FieldArrayModel = class extends FieldModel {
|
|
|
1130
1166
|
tempValue.splice(to, 0, fromValue);
|
|
1131
1167
|
this.form.setValueIn(this.name, tempValue);
|
|
1132
1168
|
}
|
|
1169
|
+
insertAt(index, value) {
|
|
1170
|
+
if (!this.value) {
|
|
1171
|
+
return;
|
|
1172
|
+
}
|
|
1173
|
+
if (index < 0 || index > this.value.length) {
|
|
1174
|
+
throw new Error(`[Form]: FieldArrayModel.insertAt Error: index exceeds array boundary`);
|
|
1175
|
+
}
|
|
1176
|
+
const tempValue = [...this.value];
|
|
1177
|
+
tempValue.splice(index, 0, value);
|
|
1178
|
+
this.form.setValueIn(this.name, tempValue);
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* get element path at given index
|
|
1182
|
+
* @param index
|
|
1183
|
+
* @protected
|
|
1184
|
+
*/
|
|
1185
|
+
getPathAt(index) {
|
|
1186
|
+
return this.path.concat(index);
|
|
1187
|
+
}
|
|
1188
|
+
/**
|
|
1189
|
+
* find all fields including child and grandchild fields at given index.
|
|
1190
|
+
* @param index
|
|
1191
|
+
* @protected
|
|
1192
|
+
*/
|
|
1193
|
+
findAllFieldsAt(index) {
|
|
1194
|
+
const rootPath = this.getPathAt(index);
|
|
1195
|
+
const rootPathString = rootPath.toString();
|
|
1196
|
+
const res = this.form.fieldMap.get(rootPathString) ? [this.form.fieldMap.get(rootPathString)] : [];
|
|
1197
|
+
this.form.fieldMap.forEach((field, fieldName) => {
|
|
1198
|
+
if (rootPath.isChildOrGrandChild(fieldName)) {
|
|
1199
|
+
res.push(field);
|
|
1200
|
+
}
|
|
1201
|
+
});
|
|
1202
|
+
return res;
|
|
1203
|
+
}
|
|
1133
1204
|
};
|
|
1134
1205
|
|
|
1135
1206
|
// src/core/form-model.ts
|
|
@@ -1152,6 +1223,7 @@ var FormModel = class {
|
|
|
1152
1223
|
createFormModelState()
|
|
1153
1224
|
);
|
|
1154
1225
|
this._initialized = false;
|
|
1226
|
+
this.validateDisabled = false;
|
|
1155
1227
|
}
|
|
1156
1228
|
set fieldMap(map) {
|
|
1157
1229
|
this._fieldMap = map;
|
|
@@ -1171,10 +1243,13 @@ var FormModel = class {
|
|
|
1171
1243
|
return this._options.initialValues;
|
|
1172
1244
|
}
|
|
1173
1245
|
get values() {
|
|
1174
|
-
return
|
|
1246
|
+
return this.store.values;
|
|
1175
1247
|
}
|
|
1176
1248
|
set values(v) {
|
|
1177
1249
|
const prevValues = this.values;
|
|
1250
|
+
if (deepEqual(prevValues, v)) {
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1178
1253
|
this.store.values = v;
|
|
1179
1254
|
this.fireOnFormValuesChange({
|
|
1180
1255
|
values: this.values,
|
|
@@ -1211,7 +1286,7 @@ var FormModel = class {
|
|
|
1211
1286
|
this._options = options;
|
|
1212
1287
|
if (options.initialValues) {
|
|
1213
1288
|
const prevValues = this.store.values;
|
|
1214
|
-
this.store.
|
|
1289
|
+
this.store.values = options.initialValues;
|
|
1215
1290
|
this.fireOnFormValuesInit({
|
|
1216
1291
|
values: options.initialValues,
|
|
1217
1292
|
prevValues,
|
|
@@ -1290,41 +1365,60 @@ var FormModel = class {
|
|
|
1290
1365
|
this.setValueIn(name, void 0);
|
|
1291
1366
|
}
|
|
1292
1367
|
async validateIn(name) {
|
|
1293
|
-
if (
|
|
1368
|
+
if (this.validateDisabled) return [];
|
|
1369
|
+
const validateOptions = this.getValidateOptions();
|
|
1370
|
+
if (!validateOptions) {
|
|
1294
1371
|
return;
|
|
1295
1372
|
}
|
|
1296
|
-
const
|
|
1373
|
+
const validateKeys = Object.keys(validateOptions).filter(
|
|
1297
1374
|
(pattern) => Glob.isMatch(pattern, name)
|
|
1298
1375
|
);
|
|
1299
|
-
|
|
1300
|
-
const validate =
|
|
1376
|
+
const validatePromises = validateKeys.map(async (validateKey) => {
|
|
1377
|
+
const validate = validateOptions[validateKey];
|
|
1301
1378
|
return validate({
|
|
1302
1379
|
value: this.getValueIn(name),
|
|
1303
1380
|
formValues: this.values,
|
|
1304
1381
|
context: this.context,
|
|
1305
1382
|
name
|
|
1306
1383
|
});
|
|
1384
|
+
});
|
|
1385
|
+
return Promise.all(validatePromises);
|
|
1386
|
+
}
|
|
1387
|
+
getValidateOptions() {
|
|
1388
|
+
const validate = this._options.validate;
|
|
1389
|
+
if (typeof validate === "function") {
|
|
1390
|
+
return validate(this.values, this.context);
|
|
1307
1391
|
}
|
|
1392
|
+
return validate;
|
|
1308
1393
|
}
|
|
1309
1394
|
async validate() {
|
|
1310
|
-
if (
|
|
1395
|
+
if (this.validateDisabled) return [];
|
|
1396
|
+
const validateOptions = this.getValidateOptions();
|
|
1397
|
+
if (!validateOptions) {
|
|
1311
1398
|
return [];
|
|
1312
1399
|
}
|
|
1313
|
-
const feedbacksArrPromises = Object.keys(
|
|
1314
|
-
const validate =
|
|
1315
|
-
const
|
|
1400
|
+
const feedbacksArrPromises = Object.keys(validateOptions).map(async (nameRule) => {
|
|
1401
|
+
const validate = validateOptions[nameRule];
|
|
1402
|
+
const values = this.values;
|
|
1403
|
+
const paths = Glob.findMatchPathsWithEmptyValue(values, nameRule);
|
|
1316
1404
|
return Promise.all(
|
|
1317
1405
|
paths.map(async (path) => {
|
|
1318
1406
|
const result = await validate({
|
|
1319
|
-
value: get4(
|
|
1320
|
-
formValues:
|
|
1407
|
+
value: get4(values, path),
|
|
1408
|
+
formValues: values,
|
|
1321
1409
|
context: this.context,
|
|
1322
1410
|
name: path
|
|
1323
1411
|
});
|
|
1324
1412
|
const feedback = toFeedback(result, path);
|
|
1325
1413
|
const field = this.getField(path);
|
|
1326
|
-
const errors = feedbackToFieldErrorsOrWarnings(
|
|
1327
|
-
|
|
1414
|
+
const errors = feedbackToFieldErrorsOrWarnings(
|
|
1415
|
+
path,
|
|
1416
|
+
feedback?.level === "error" /* Error */ ? feedback : void 0
|
|
1417
|
+
);
|
|
1418
|
+
const warnings = feedbackToFieldErrorsOrWarnings(
|
|
1419
|
+
path,
|
|
1420
|
+
feedback?.level === "warning" /* Warning */ ? feedback : void 0
|
|
1421
|
+
);
|
|
1328
1422
|
if (field) {
|
|
1329
1423
|
field.state.errors = errors;
|
|
1330
1424
|
field.state.warnings = warnings;
|
|
@@ -1391,6 +1485,10 @@ function createForm(options) {
|
|
|
1391
1485
|
}
|
|
1392
1486
|
|
|
1393
1487
|
// src/react/form.tsx
|
|
1488
|
+
function FormContentRender(props) {
|
|
1489
|
+
const { form, render } = props;
|
|
1490
|
+
return /* @__PURE__ */ React3.createElement(React3.Fragment, null, render({ form }));
|
|
1491
|
+
}
|
|
1394
1492
|
function Form(props) {
|
|
1395
1493
|
const { children, keepModelOnUnMount = false, control, ...restOptions } = props;
|
|
1396
1494
|
const { _formModel: formModel } = useMemo2(
|
|
@@ -1406,7 +1504,7 @@ function Form(props) {
|
|
|
1406
1504
|
[]
|
|
1407
1505
|
);
|
|
1408
1506
|
const form = useMemo2(() => toForm(formModel), [formModel]);
|
|
1409
|
-
return /* @__PURE__ */ React3.createElement(FormModelContext.Provider, { value: formModel }, children ? isFunction2(children) ?
|
|
1507
|
+
return /* @__PURE__ */ React3.createElement(FormModelContext.Provider, { value: formModel }, children ? isFunction2(children) ? /* @__PURE__ */ React3.createElement(FormContentRender, { form, render: children }) : Children.only(children) : null);
|
|
1410
1508
|
}
|
|
1411
1509
|
|
|
1412
1510
|
// src/react/use-form.ts
|
|
@@ -1438,7 +1536,7 @@ function useWatch(name) {
|
|
|
1438
1536
|
|
|
1439
1537
|
// src/react/field-array.tsx
|
|
1440
1538
|
import * as React4 from "react";
|
|
1441
|
-
import { isFunction as isFunction3 } from "lodash";
|
|
1539
|
+
import { isFunction as isFunction3 } from "lodash-es";
|
|
1442
1540
|
import { DisposableCollection as DisposableCollection3, useRefresh as useRefresh3 } from "@flowgram.ai/utils";
|
|
1443
1541
|
import { useReadonlyReactiveState as useReadonlyReactiveState2 } from "@flowgram.ai/reactive";
|
|
1444
1542
|
function FieldArray({
|
|
@@ -1449,10 +1547,7 @@ function FieldArray({
|
|
|
1449
1547
|
children
|
|
1450
1548
|
}) {
|
|
1451
1549
|
const formModel = useFormModel();
|
|
1452
|
-
const fieldModel =
|
|
1453
|
-
() => formModel.getField(name) || formModel.createFieldArray(name),
|
|
1454
|
-
[]
|
|
1455
|
-
);
|
|
1550
|
+
const fieldModel = formModel.getField(name) || formModel.createFieldArray(name);
|
|
1456
1551
|
const field = React4.useMemo(() => toFieldArray(fieldModel), [fieldModel]);
|
|
1457
1552
|
const refresh = useRefresh3();
|
|
1458
1553
|
const fieldModelState = useReadonlyReactiveState2(fieldModel.reactiveState);
|
|
@@ -1460,6 +1555,11 @@ function FieldArray({
|
|
|
1460
1555
|
const fieldState = toFieldState(fieldModelState);
|
|
1461
1556
|
const formState = React4.useMemo(() => toFormState(formModelState), [formModelState]);
|
|
1462
1557
|
React4.useEffect(() => {
|
|
1558
|
+
if (fieldModel.disposed) {
|
|
1559
|
+
refresh();
|
|
1560
|
+
return () => {
|
|
1561
|
+
};
|
|
1562
|
+
}
|
|
1463
1563
|
fieldModel.renderCount = fieldModel.renderCount + 1;
|
|
1464
1564
|
if (!formModel.getValueIn(name) !== void 0 && defaultValue !== void 0) {
|
|
1465
1565
|
formModel.setInitValueIn(name, defaultValue);
|
|
@@ -1500,6 +1600,7 @@ function FieldArray({
|
|
|
1500
1600
|
}
|
|
1501
1601
|
return /* @__PURE__ */ React4.createElement(React4.Fragment, null, "Invalid Array render");
|
|
1502
1602
|
};
|
|
1603
|
+
if (fieldModel.disposed) return /* @__PURE__ */ React4.createElement(React4.Fragment, null);
|
|
1503
1604
|
return /* @__PURE__ */ React4.createElement(FieldModelContext.Provider, { value: fieldModel }, renderInner());
|
|
1504
1605
|
}
|
|
1505
1606
|
|
|
@@ -1578,6 +1679,7 @@ function useCurrentFieldState() {
|
|
|
1578
1679
|
return useMemo4(() => toFieldState(fieldModelState), [fieldModelState]);
|
|
1579
1680
|
}
|
|
1580
1681
|
export {
|
|
1682
|
+
FeedbackLevel,
|
|
1581
1683
|
Field,
|
|
1582
1684
|
FieldArray,
|
|
1583
1685
|
FieldArrayModel,
|