@flowgram.ai/form 0.1.0-alpha.4 → 0.1.0-alpha.6
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 +103 -37
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.mts +30 -7
- package/dist/index.d.ts +30 -7
- package/dist/index.js +103 -37
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/esm/index.js
CHANGED
|
@@ -36,14 +36,7 @@ function shallowSetIn(obj, path, value) {
|
|
|
36
36
|
if ((i === 0 ? obj : resVal)[pathArray[i]] === value) {
|
|
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) {
|
|
@@ -201,18 +194,15 @@ 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
|
-
function isFieldWarning(f) {
|
|
211
|
-
if (f.level === "warning" /* Warning */) {
|
|
212
|
-
return true;
|
|
213
|
-
}
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
206
|
var ValidateTrigger = /* @__PURE__ */ ((ValidateTrigger2) => {
|
|
217
207
|
ValidateTrigger2["onChange"] = "onChange";
|
|
218
208
|
ValidateTrigger2["onBlur"] = "onBlur";
|
|
@@ -309,14 +299,14 @@ var Path = class _Path {
|
|
|
309
299
|
replaceParent(parent, newParent) {
|
|
310
300
|
if (parent.value.length > this.value.length) {
|
|
311
301
|
throw new Error(
|
|
312
|
-
`[Form] Error in Path.
|
|
302
|
+
`[Form] Error in Path.replaceParent: invalid parent param: ${parent}, parent length should not greater than current length.`
|
|
313
303
|
);
|
|
314
304
|
}
|
|
315
305
|
const rest = [];
|
|
316
306
|
for (let i = 0; i < this.value.length; i++) {
|
|
317
307
|
if (i < parent.value.length && parent.value[i] !== this.value[i]) {
|
|
318
308
|
throw new Error(
|
|
319
|
-
`[Form] Error in Path.
|
|
309
|
+
`[Form] Error in Path.replaceParent: invalid parent param: '${parent}' is not a parent of '${this.toString()}'`
|
|
320
310
|
);
|
|
321
311
|
}
|
|
322
312
|
if (i >= parent.value.length) {
|
|
@@ -329,7 +319,7 @@ var Path = class _Path {
|
|
|
329
319
|
|
|
330
320
|
// src/core/utils.ts
|
|
331
321
|
function updateFeedbacksName(feedbacks, name) {
|
|
332
|
-
return feedbacks.map((f) => ({
|
|
322
|
+
return (feedbacks || []).map((f) => ({
|
|
333
323
|
...f,
|
|
334
324
|
name
|
|
335
325
|
}));
|
|
@@ -390,7 +380,7 @@ var FieldEventUtils;
|
|
|
390
380
|
FieldEventUtils2.shouldTriggerFieldChangeEvent = shouldTriggerFieldChangeEvent;
|
|
391
381
|
function shouldTriggerFieldValidateWhenChange(payload, fieldName) {
|
|
392
382
|
const { name: changedName, options } = payload;
|
|
393
|
-
if (options?.action === "array-splice") {
|
|
383
|
+
if (options?.action === "array-splice" || options?.action === "array-swap") {
|
|
394
384
|
return fieldName === changedName;
|
|
395
385
|
}
|
|
396
386
|
return FieldEventUtils2.shouldTriggerFieldChangeEvent(payload, fieldName);
|
|
@@ -478,7 +468,8 @@ function toForm(model) {
|
|
|
478
468
|
},
|
|
479
469
|
state: toFormState(model.state),
|
|
480
470
|
getValueIn: (name) => model.getValueIn(name),
|
|
481
|
-
setValueIn: (name, value) => model.setValueIn(name, value)
|
|
471
|
+
setValueIn: (name, value) => model.setValueIn(name, value),
|
|
472
|
+
validate: model.validate.bind(model)
|
|
482
473
|
};
|
|
483
474
|
Object.defineProperty(res, "_formModel", {
|
|
484
475
|
enumerable: false,
|
|
@@ -614,7 +605,12 @@ function toFieldArray(model) {
|
|
|
614
605
|
},
|
|
615
606
|
map: (cb) => model.map((f, index) => cb(toField(f), index)),
|
|
616
607
|
append: (value) => toField(model.append(value)),
|
|
608
|
+
/**
|
|
609
|
+
* @deprecated: use remove instead
|
|
610
|
+
* @param index
|
|
611
|
+
*/
|
|
617
612
|
delete: (index) => model.delete(index),
|
|
613
|
+
remove: (index) => model.delete(index),
|
|
618
614
|
swap: (from, to) => model.swap(from, to),
|
|
619
615
|
move: (from, to) => model.move(from, to)
|
|
620
616
|
};
|
|
@@ -925,21 +921,19 @@ var FieldModel = class {
|
|
|
925
921
|
this.form.onValidateEmitter.fire(this.form.state);
|
|
926
922
|
}
|
|
927
923
|
async _runAsyncValidate() {
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
const
|
|
931
|
-
if (!
|
|
924
|
+
let errors = [];
|
|
925
|
+
let warnings = [];
|
|
926
|
+
const results = await this.form.validateIn(this.name);
|
|
927
|
+
if (!results?.length) {
|
|
932
928
|
return {};
|
|
933
929
|
} else {
|
|
934
|
-
const
|
|
935
|
-
if (!
|
|
930
|
+
const feedbacks = results.map((result) => toFeedback(result, this.name)).filter(Boolean);
|
|
931
|
+
if (!feedbacks?.length) {
|
|
936
932
|
return {};
|
|
937
933
|
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
errors.push(feedback);
|
|
942
|
-
}
|
|
934
|
+
const groupedFeedbacks = groupBy(feedbacks, "level");
|
|
935
|
+
warnings = warnings.concat(groupedFeedbacks["warning" /* Warning */]);
|
|
936
|
+
errors = errors.concat(groupedFeedbacks["error" /* Error */]);
|
|
943
937
|
}
|
|
944
938
|
return { errors, warnings };
|
|
945
939
|
}
|
|
@@ -1020,7 +1014,7 @@ var FieldArrayModel = class extends FieldModel {
|
|
|
1020
1014
|
return newElemField;
|
|
1021
1015
|
}
|
|
1022
1016
|
/**
|
|
1023
|
-
*
|
|
1017
|
+
* Delete the element in given index and delete the corresponding FieldModel as well
|
|
1024
1018
|
* @param index
|
|
1025
1019
|
*/
|
|
1026
1020
|
delete(index) {
|
|
@@ -1101,12 +1095,49 @@ var FieldArrayModel = class extends FieldModel {
|
|
|
1101
1095
|
`[Form]: FieldArrayModel.swap Error: invalid params 'form' and 'to', form=${from} to=${to}. expect the value between 0 to ${length - 1}`
|
|
1102
1096
|
);
|
|
1103
1097
|
}
|
|
1098
|
+
const oldFormValues = this.form.values;
|
|
1104
1099
|
const tempValue = [...this.value];
|
|
1105
1100
|
const fromValue = tempValue[from];
|
|
1106
1101
|
const toValue = tempValue[to];
|
|
1107
1102
|
tempValue[to] = fromValue;
|
|
1108
1103
|
tempValue[from] = toValue;
|
|
1109
|
-
this.form.
|
|
1104
|
+
this.form.store.setIn(this.path, tempValue);
|
|
1105
|
+
this.form.fireOnFormValuesChange({
|
|
1106
|
+
values: this.form.values,
|
|
1107
|
+
prevValues: oldFormValues,
|
|
1108
|
+
name: this.name,
|
|
1109
|
+
options: {
|
|
1110
|
+
action: "array-swap",
|
|
1111
|
+
indexes: [from, to]
|
|
1112
|
+
}
|
|
1113
|
+
});
|
|
1114
|
+
const newFieldMap = new Map(this.form.fieldMap);
|
|
1115
|
+
const fromFields = this.findAllFieldsAt(from);
|
|
1116
|
+
const toFields = this.findAllFieldsAt(to);
|
|
1117
|
+
const fromRootPath = this.getPathAt(from);
|
|
1118
|
+
const toRootPath = this.getPathAt(to);
|
|
1119
|
+
const leafFieldsModified = [];
|
|
1120
|
+
fromFields.forEach((f) => {
|
|
1121
|
+
const newName = f.path.replaceParent(fromRootPath, toRootPath).toString();
|
|
1122
|
+
f.name = newName;
|
|
1123
|
+
if (!f.children.length) {
|
|
1124
|
+
f.updateNameForLeafState(newName);
|
|
1125
|
+
leafFieldsModified.push(f);
|
|
1126
|
+
}
|
|
1127
|
+
newFieldMap.set(newName, f);
|
|
1128
|
+
});
|
|
1129
|
+
toFields.forEach((f) => {
|
|
1130
|
+
const newName = f.path.replaceParent(toRootPath, fromRootPath).toString();
|
|
1131
|
+
f.name = newName;
|
|
1132
|
+
if (!f.children.length) {
|
|
1133
|
+
f.updateNameForLeafState(newName);
|
|
1134
|
+
}
|
|
1135
|
+
newFieldMap.set(newName, f);
|
|
1136
|
+
leafFieldsModified.push(f);
|
|
1137
|
+
});
|
|
1138
|
+
this.form.fieldMap = newFieldMap;
|
|
1139
|
+
leafFieldsModified.forEach((f) => f.bubbleState());
|
|
1140
|
+
this.form.alignStateWithFieldMap();
|
|
1110
1141
|
}
|
|
1111
1142
|
move(from, to) {
|
|
1112
1143
|
if (!this.value) {
|
|
@@ -1123,6 +1154,41 @@ var FieldArrayModel = class extends FieldModel {
|
|
|
1123
1154
|
tempValue.splice(to, 0, fromValue);
|
|
1124
1155
|
this.form.setValueIn(this.name, tempValue);
|
|
1125
1156
|
}
|
|
1157
|
+
insertAt(index, value) {
|
|
1158
|
+
if (!this.value) {
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
if (index < 0 || index > this.value.length) {
|
|
1162
|
+
throw new Error(`[Form]: FieldArrayModel.insertAt Error: index exceeds array boundary`);
|
|
1163
|
+
}
|
|
1164
|
+
const tempValue = [...this.value];
|
|
1165
|
+
tempValue.splice(index, 0, value);
|
|
1166
|
+
this.form.setValueIn(this.name, tempValue);
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* get element path at given index
|
|
1170
|
+
* @param index
|
|
1171
|
+
* @protected
|
|
1172
|
+
*/
|
|
1173
|
+
getPathAt(index) {
|
|
1174
|
+
return this.path.concat(index);
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* find all fields including child and grandchild fields at given index.
|
|
1178
|
+
* @param index
|
|
1179
|
+
* @protected
|
|
1180
|
+
*/
|
|
1181
|
+
findAllFieldsAt(index) {
|
|
1182
|
+
const rootPath = this.getPathAt(index);
|
|
1183
|
+
const rootPathString = rootPath.toString();
|
|
1184
|
+
const res = this.form.fieldMap.get(rootPathString) ? [this.form.fieldMap.get(rootPathString)] : [];
|
|
1185
|
+
this.form.fieldMap.forEach((field, fieldName) => {
|
|
1186
|
+
if (rootPath.isChildOrGrandChild(fieldName)) {
|
|
1187
|
+
res.push(field);
|
|
1188
|
+
}
|
|
1189
|
+
});
|
|
1190
|
+
return res;
|
|
1191
|
+
}
|
|
1126
1192
|
};
|
|
1127
1193
|
|
|
1128
1194
|
// src/core/form-model.ts
|
|
@@ -1286,10 +1352,10 @@ var FormModel = class {
|
|
|
1286
1352
|
if (!this._options.validate) {
|
|
1287
1353
|
return;
|
|
1288
1354
|
}
|
|
1289
|
-
const
|
|
1355
|
+
const validateKeys = Object.keys(this._options.validate).filter(
|
|
1290
1356
|
(pattern) => Glob.isMatch(pattern, name)
|
|
1291
1357
|
);
|
|
1292
|
-
|
|
1358
|
+
const validatePromises = validateKeys.map(async (validateKey) => {
|
|
1293
1359
|
const validate = this._options.validate[validateKey];
|
|
1294
1360
|
return validate({
|
|
1295
1361
|
value: this.getValueIn(name),
|
|
@@ -1297,7 +1363,8 @@ var FormModel = class {
|
|
|
1297
1363
|
context: this.context,
|
|
1298
1364
|
name
|
|
1299
1365
|
});
|
|
1300
|
-
}
|
|
1366
|
+
});
|
|
1367
|
+
return Promise.all(validatePromises);
|
|
1301
1368
|
}
|
|
1302
1369
|
async validate() {
|
|
1303
1370
|
if (!this._options.validate) {
|
|
@@ -1319,7 +1386,6 @@ var FormModel = class {
|
|
|
1319
1386
|
const errors = feedbackToFieldErrorsOrWarnings(path, feedback);
|
|
1320
1387
|
const warnings = feedbackToFieldErrorsOrWarnings(path, feedback);
|
|
1321
1388
|
if (field) {
|
|
1322
|
-
debugger;
|
|
1323
1389
|
field.state.errors = errors;
|
|
1324
1390
|
field.state.warnings = warnings;
|
|
1325
1391
|
field.state.invalid = hasError(errors);
|
|
@@ -1445,7 +1511,7 @@ function FieldArray({
|
|
|
1445
1511
|
const formModel = useFormModel();
|
|
1446
1512
|
const fieldModel = React4.useMemo(
|
|
1447
1513
|
() => formModel.getField(name) || formModel.createFieldArray(name),
|
|
1448
|
-
[]
|
|
1514
|
+
[name]
|
|
1449
1515
|
);
|
|
1450
1516
|
const field = React4.useMemo(() => toFieldArray(fieldModel), [fieldModel]);
|
|
1451
1517
|
const refresh = useRefresh3();
|