@flowgram.ai/form 0.1.14 → 0.1.16

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 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
- if (value === void 0) {
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) {
@@ -309,14 +302,14 @@ var Path = class _Path {
309
302
  replaceParent(parent, newParent) {
310
303
  if (parent.value.length > this.value.length) {
311
304
  throw new Error(
312
- `[Form] Error in Path.getChildSuffixByParent: invalid parent param: ${parent}, parent length should not greater than current length.`
305
+ `[Form] Error in Path.replaceParent: invalid parent param: ${parent}, parent length should not greater than current length.`
313
306
  );
314
307
  }
315
308
  const rest = [];
316
309
  for (let i = 0; i < this.value.length; i++) {
317
310
  if (i < parent.value.length && parent.value[i] !== this.value[i]) {
318
311
  throw new Error(
319
- `[Form] Error in Path.getChildSuffixByParent: invalid parent param: ${parent}`
312
+ `[Form] Error in Path.replaceParent: invalid parent param: '${parent}' is not a parent of '${this.toString()}'`
320
313
  );
321
314
  }
322
315
  if (i >= parent.value.length) {
@@ -329,7 +322,7 @@ var Path = class _Path {
329
322
 
330
323
  // src/core/utils.ts
331
324
  function updateFeedbacksName(feedbacks, name) {
332
- return feedbacks.map((f) => ({
325
+ return (feedbacks || []).map((f) => ({
333
326
  ...f,
334
327
  name
335
328
  }));
@@ -390,7 +383,7 @@ var FieldEventUtils;
390
383
  FieldEventUtils2.shouldTriggerFieldChangeEvent = shouldTriggerFieldChangeEvent;
391
384
  function shouldTriggerFieldValidateWhenChange(payload, fieldName) {
392
385
  const { name: changedName, options } = payload;
393
- if (options?.action === "array-splice") {
386
+ if (options?.action === "array-splice" || options?.action === "array-swap") {
394
387
  return fieldName === changedName;
395
388
  }
396
389
  return FieldEventUtils2.shouldTriggerFieldChangeEvent(payload, fieldName);
@@ -478,7 +471,8 @@ function toForm(model) {
478
471
  },
479
472
  state: toFormState(model.state),
480
473
  getValueIn: (name) => model.getValueIn(name),
481
- setValueIn: (name, value) => model.setValueIn(name, value)
474
+ setValueIn: (name, value) => model.setValueIn(name, value),
475
+ validate: model.validate.bind(model)
482
476
  };
483
477
  Object.defineProperty(res, "_formModel", {
484
478
  enumerable: false,
@@ -614,7 +608,12 @@ function toFieldArray(model) {
614
608
  },
615
609
  map: (cb) => model.map((f, index) => cb(toField(f), index)),
616
610
  append: (value) => toField(model.append(value)),
611
+ /**
612
+ * @deprecated: use remove instead
613
+ * @param index
614
+ */
617
615
  delete: (index) => model.delete(index),
616
+ remove: (index) => model.delete(index),
618
617
  swap: (from, to) => model.swap(from, to),
619
618
  move: (from, to) => model.move(from, to)
620
619
  };
@@ -1020,7 +1019,7 @@ var FieldArrayModel = class extends FieldModel {
1020
1019
  return newElemField;
1021
1020
  }
1022
1021
  /**
1023
- * 删除数组项,该操作会删除数组项的值并销毁数组项的Field模型
1022
+ * Delete the element in given index and delete the corresponding FieldModel as well
1024
1023
  * @param index
1025
1024
  */
1026
1025
  delete(index) {
@@ -1101,12 +1100,49 @@ var FieldArrayModel = class extends FieldModel {
1101
1100
  `[Form]: FieldArrayModel.swap Error: invalid params 'form' and 'to', form=${from} to=${to}. expect the value between 0 to ${length - 1}`
1102
1101
  );
1103
1102
  }
1103
+ const oldFormValues = this.form.values;
1104
1104
  const tempValue = [...this.value];
1105
1105
  const fromValue = tempValue[from];
1106
1106
  const toValue = tempValue[to];
1107
1107
  tempValue[to] = fromValue;
1108
1108
  tempValue[from] = toValue;
1109
- this.form.setValueIn(this.name, tempValue);
1109
+ this.form.store.setIn(this.path, tempValue);
1110
+ this.form.fireOnFormValuesChange({
1111
+ values: this.form.values,
1112
+ prevValues: oldFormValues,
1113
+ name: this.name,
1114
+ options: {
1115
+ action: "array-swap",
1116
+ indexes: [from, to]
1117
+ }
1118
+ });
1119
+ const newFieldMap = new Map(this.form.fieldMap);
1120
+ const fromFields = this.findAllFieldsAt(from);
1121
+ const toFields = this.findAllFieldsAt(to);
1122
+ const fromRootPath = this.getPathAt(from);
1123
+ const toRootPath = this.getPathAt(to);
1124
+ const leafFieldsModified = [];
1125
+ fromFields.forEach((f) => {
1126
+ const newName = f.path.replaceParent(fromRootPath, toRootPath).toString();
1127
+ f.name = newName;
1128
+ if (!f.children.length) {
1129
+ f.updateNameForLeafState(newName);
1130
+ leafFieldsModified.push(f);
1131
+ }
1132
+ newFieldMap.set(newName, f);
1133
+ });
1134
+ toFields.forEach((f) => {
1135
+ const newName = f.path.replaceParent(toRootPath, fromRootPath).toString();
1136
+ f.name = newName;
1137
+ if (!f.children.length) {
1138
+ f.updateNameForLeafState(newName);
1139
+ }
1140
+ newFieldMap.set(newName, f);
1141
+ leafFieldsModified.push(f);
1142
+ });
1143
+ this.form.fieldMap = newFieldMap;
1144
+ leafFieldsModified.forEach((f) => f.bubbleState());
1145
+ this.form.alignStateWithFieldMap();
1110
1146
  }
1111
1147
  move(from, to) {
1112
1148
  if (!this.value) {
@@ -1123,6 +1159,41 @@ var FieldArrayModel = class extends FieldModel {
1123
1159
  tempValue.splice(to, 0, fromValue);
1124
1160
  this.form.setValueIn(this.name, tempValue);
1125
1161
  }
1162
+ insertAt(index, value) {
1163
+ if (!this.value) {
1164
+ return;
1165
+ }
1166
+ if (index < 0 || index > this.value.length) {
1167
+ throw new Error(`[Form]: FieldArrayModel.insertAt Error: index exceeds array boundary`);
1168
+ }
1169
+ const tempValue = [...this.value];
1170
+ tempValue.splice(index, 0, value);
1171
+ this.form.setValueIn(this.name, tempValue);
1172
+ }
1173
+ /**
1174
+ * get element path at given index
1175
+ * @param index
1176
+ * @protected
1177
+ */
1178
+ getPathAt(index) {
1179
+ return this.path.concat(index);
1180
+ }
1181
+ /**
1182
+ * find all fields including child and grandchild fields at given index.
1183
+ * @param index
1184
+ * @protected
1185
+ */
1186
+ findAllFieldsAt(index) {
1187
+ const rootPath = this.getPathAt(index);
1188
+ const rootPathString = rootPath.toString();
1189
+ const res = this.form.fieldMap.get(rootPathString) ? [this.form.fieldMap.get(rootPathString)] : [];
1190
+ this.form.fieldMap.forEach((field, fieldName) => {
1191
+ if (rootPath.isChildOrGrandChild(fieldName)) {
1192
+ res.push(field);
1193
+ }
1194
+ });
1195
+ return res;
1196
+ }
1126
1197
  };
1127
1198
 
1128
1199
  // src/core/form-model.ts
@@ -1444,7 +1515,7 @@ function FieldArray({
1444
1515
  const formModel = useFormModel();
1445
1516
  const fieldModel = React4.useMemo(
1446
1517
  () => formModel.getField(name) || formModel.createFieldArray(name),
1447
- []
1518
+ [name]
1448
1519
  );
1449
1520
  const field = React4.useMemo(() => toFieldArray(fieldModel), [fieldModel]);
1450
1521
  const refresh = useRefresh3();