@flowgram.ai/form 0.1.14 → 0.1.15

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);
@@ -1101,12 +1094,49 @@ var FieldArrayModel = class extends FieldModel {
1101
1094
  `[Form]: FieldArrayModel.swap Error: invalid params 'form' and 'to', form=${from} to=${to}. expect the value between 0 to ${length - 1}`
1102
1095
  );
1103
1096
  }
1097
+ const oldFormValues = this.form.values;
1104
1098
  const tempValue = [...this.value];
1105
1099
  const fromValue = tempValue[from];
1106
1100
  const toValue = tempValue[to];
1107
1101
  tempValue[to] = fromValue;
1108
1102
  tempValue[from] = toValue;
1109
- this.form.setValueIn(this.name, tempValue);
1103
+ this.form.store.setIn(this.path, tempValue);
1104
+ this.form.fireOnFormValuesChange({
1105
+ values: this.form.values,
1106
+ prevValues: oldFormValues,
1107
+ name: this.name,
1108
+ options: {
1109
+ action: "array-swap",
1110
+ indexes: [from, to]
1111
+ }
1112
+ });
1113
+ const newFieldMap = new Map(this.form.fieldMap);
1114
+ const fromFields = this.findAllFieldsAt(from);
1115
+ const toFields = this.findAllFieldsAt(to);
1116
+ const fromRootPath = this.getPathAt(from);
1117
+ const toRootPath = this.getPathAt(to);
1118
+ const leafFieldsModified = [];
1119
+ fromFields.forEach((f) => {
1120
+ const newName = f.path.replaceParent(fromRootPath, toRootPath).toString();
1121
+ f.name = newName;
1122
+ if (!f.children.length) {
1123
+ f.updateNameForLeafState(newName);
1124
+ leafFieldsModified.push(f);
1125
+ }
1126
+ newFieldMap.set(newName, f);
1127
+ });
1128
+ toFields.forEach((f) => {
1129
+ const newName = f.path.replaceParent(toRootPath, fromRootPath).toString();
1130
+ f.name = newName;
1131
+ if (!f.children.length) {
1132
+ f.updateNameForLeafState(newName);
1133
+ }
1134
+ newFieldMap.set(newName, f);
1135
+ leafFieldsModified.push(f);
1136
+ });
1137
+ this.form.fieldMap = newFieldMap;
1138
+ leafFieldsModified.forEach((f) => f.bubbleState());
1139
+ this.form.alignStateWithFieldMap();
1110
1140
  }
1111
1141
  move(from, to) {
1112
1142
  if (!this.value) {
@@ -1123,6 +1153,41 @@ var FieldArrayModel = class extends FieldModel {
1123
1153
  tempValue.splice(to, 0, fromValue);
1124
1154
  this.form.setValueIn(this.name, tempValue);
1125
1155
  }
1156
+ insertAt(index, value) {
1157
+ if (!this.value) {
1158
+ return;
1159
+ }
1160
+ if (index < 0 || index > this.value.length) {
1161
+ throw new Error(`[Form]: FieldArrayModel.insertAt Error: index exceeds array boundary`);
1162
+ }
1163
+ const tempValue = [...this.value];
1164
+ tempValue.splice(index, 0, value);
1165
+ this.form.setValueIn(this.name, tempValue);
1166
+ }
1167
+ /**
1168
+ * get element path at given index
1169
+ * @param index
1170
+ * @protected
1171
+ */
1172
+ getPathAt(index) {
1173
+ return this.path.concat(index);
1174
+ }
1175
+ /**
1176
+ * find all fields including child and grandchild fields at given index.
1177
+ * @param index
1178
+ * @protected
1179
+ */
1180
+ findAllFieldsAt(index) {
1181
+ const rootPath = this.getPathAt(index);
1182
+ const rootPathString = rootPath.toString();
1183
+ const res = this.form.fieldMap.get(rootPathString) ? [this.form.fieldMap.get(rootPathString)] : [];
1184
+ this.form.fieldMap.forEach((field, fieldName) => {
1185
+ if (rootPath.isChildOrGrandChild(fieldName)) {
1186
+ res.push(field);
1187
+ }
1188
+ });
1189
+ return res;
1190
+ }
1126
1191
  };
1127
1192
 
1128
1193
  // src/core/form-model.ts