@tmagic/form 1.4.7 → 1.4.8

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.
Files changed (60) hide show
  1. package/dist/tmagic-form.js +54 -108
  2. package/dist/tmagic-form.umd.cjs +54 -108
  3. package/package.json +9 -5
  4. package/src/Form.vue +0 -201
  5. package/src/FormBox.vue +0 -120
  6. package/src/FormDialog.vue +0 -173
  7. package/src/FormDrawer.vue +0 -159
  8. package/src/containers/Col.vue +0 -52
  9. package/src/containers/Container.vue +0 -408
  10. package/src/containers/Fieldset.vue +0 -124
  11. package/src/containers/GroupList.vue +0 -139
  12. package/src/containers/GroupListItem.vue +0 -135
  13. package/src/containers/Panel.vue +0 -99
  14. package/src/containers/Row.vue +0 -54
  15. package/src/containers/Step.vue +0 -82
  16. package/src/containers/Table.vue +0 -648
  17. package/src/containers/Tabs.vue +0 -226
  18. package/src/fields/Cascader.vue +0 -131
  19. package/src/fields/Checkbox.vue +0 -58
  20. package/src/fields/CheckboxGroup.vue +0 -43
  21. package/src/fields/ColorPicker.vue +0 -30
  22. package/src/fields/Date.vue +0 -38
  23. package/src/fields/DateTime.vue +0 -47
  24. package/src/fields/Daterange.vue +0 -99
  25. package/src/fields/Display.vue +0 -21
  26. package/src/fields/DynamicField.vue +0 -90
  27. package/src/fields/Hidden.vue +0 -16
  28. package/src/fields/Link.vue +0 -86
  29. package/src/fields/Number.vue +0 -49
  30. package/src/fields/NumberRange.vue +0 -50
  31. package/src/fields/RadioGroup.vue +0 -28
  32. package/src/fields/Select.vue +0 -449
  33. package/src/fields/Switch.vue +0 -59
  34. package/src/fields/Text.vue +0 -170
  35. package/src/fields/Textarea.vue +0 -46
  36. package/src/fields/Time.vue +0 -34
  37. package/src/fields/Timerange.vue +0 -76
  38. package/src/index.ts +0 -139
  39. package/src/schema.ts +0 -757
  40. package/src/shims-vue.d.ts +0 -6
  41. package/src/theme/date-time.scss +0 -7
  42. package/src/theme/fieldset.scss +0 -28
  43. package/src/theme/form-box.scss +0 -13
  44. package/src/theme/form-dialog.scss +0 -13
  45. package/src/theme/form-drawer.scss +0 -11
  46. package/src/theme/form.scss +0 -43
  47. package/src/theme/group-list.scss +0 -23
  48. package/src/theme/index.scss +0 -14
  49. package/src/theme/link.scss +0 -3
  50. package/src/theme/number-range.scss +0 -8
  51. package/src/theme/panel.scss +0 -24
  52. package/src/theme/select.scss +0 -3
  53. package/src/theme/table.scss +0 -70
  54. package/src/theme/tabs.scss +0 -27
  55. package/src/theme/text.scss +0 -6
  56. package/src/utils/config.ts +0 -27
  57. package/src/utils/containerProps.ts +0 -50
  58. package/src/utils/form.ts +0 -268
  59. package/src/utils/useAddField.ts +0 -40
  60. package/tsconfig.build.json +0 -13
@@ -152,8 +152,7 @@
152
152
  });
153
153
  };
154
154
  const initValue = async (mForm, { initValues, config }) => {
155
- if (!Array.isArray(config))
156
- throw new Error("config应该为数组");
155
+ if (!Array.isArray(config)) throw new Error("config应该为数组");
157
156
  let valuesTmp = createValues(mForm, config, vue.toRaw(initValues), {});
158
157
  const [firstForm] = config;
159
158
  if (firstForm && typeof firstForm.onInitValue === "function") {
@@ -207,8 +206,7 @@
207
206
  const expand = vue.ref(false);
208
207
  const name = vue.computed(() => props.config.name || "");
209
208
  const showDiff = vue.computed(() => {
210
- if (!props.isCompare)
211
- return false;
209
+ if (!props.isCompare) return false;
212
210
  const curValue = name.value ? props.model[name.value] : props.model;
213
211
  const lastValue = name.value ? props.lastValues[name.value] : props.lastValues;
214
212
  return !lodashEs.isEqual(curValue, lastValue);
@@ -235,10 +233,8 @@
235
233
  const type = vue.computed(() => {
236
234
  let { type: type2 } = props.config;
237
235
  type2 = type2 && filterFunction(mForm, type2, props);
238
- if (type2 === "form")
239
- return "";
240
- if (type2 === "container")
241
- return "";
236
+ if (type2 === "form") return "";
237
+ if (type2 === "container") return "";
242
238
  return type2?.replace(/([A-Z])/g, "-$1").toLowerCase() || (items.value ? "" : "text");
243
239
  });
244
240
  const display$1 = vue.computed(() => {
@@ -255,10 +251,8 @@
255
251
  vue.watch(
256
252
  showDiff,
257
253
  (showDiff2) => {
258
- if (type.value === "hidden")
259
- return;
260
- if (items.value && !text.value && type.value && display$1.value)
261
- return;
254
+ if (type.value === "hidden") return;
255
+ if (items.value && !text.value && type.value && display$1.value) return;
262
256
  if (display$1.value && showDiff2 && type.value) {
263
257
  emit("addDiffCount");
264
258
  }
@@ -794,8 +788,7 @@
794
788
  const changeOrder = (offset = 0) => emit("swap-item", props.index, props.index + offset);
795
789
  const movable = () => {
796
790
  const { movable: movable2 } = props.config;
797
- if (movable2 === void 0)
798
- return true;
791
+ if (movable2 === void 0) return true;
799
792
  if (typeof movable2 === "function") {
800
793
  return movable2(mForm, props.index || 0, props.model, props.groupModel);
801
794
  }
@@ -926,8 +919,7 @@
926
919
  const emit = __emit;
927
920
  const mForm = vue.inject("mForm");
928
921
  const addable = vue.computed(() => {
929
- if (!props.name)
930
- return false;
922
+ if (!props.name) return false;
931
923
  if (typeof props.config.addable === "function") {
932
924
  return props.config.addable(mForm, {
933
925
  model: props.model[props.name],
@@ -939,13 +931,11 @@
939
931
  return typeof props.config.addable === "undefined" ? true : props.config.addable;
940
932
  });
941
933
  const changeHandler = () => {
942
- if (!props.name)
943
- return false;
934
+ if (!props.name) return false;
944
935
  emit("change", props.model[props.name]);
945
936
  };
946
937
  const addHandler = async () => {
947
- if (!props.name)
948
- return false;
938
+ if (!props.name) return false;
949
939
  let initValues = {};
950
940
  if (typeof props.config.defaultAdd === "function") {
951
941
  initValues = await props.config.defaultAdd(mForm, {
@@ -964,14 +954,12 @@
964
954
  props.model[props.name].push(groupValue);
965
955
  };
966
956
  const removeHandler = (index) => {
967
- if (!props.name)
968
- return false;
957
+ if (!props.name) return false;
969
958
  props.model[props.name].splice(index, 1);
970
959
  changeHandler();
971
960
  };
972
961
  const swapHandler = (idx1, idx2) => {
973
- if (!props.name)
974
- return false;
962
+ if (!props.name) return false;
975
963
  const [currRow] = props.model[props.name].splice(idx1, 1);
976
964
  props.model[props.name].splice(idx2, 0, currRow);
977
965
  changeHandler();
@@ -1408,10 +1396,8 @@
1408
1396
  if (tBodyEl) {
1409
1397
  const sortable = Sortable.create(tBodyEl, {
1410
1398
  onEnd: ({ newIndex, oldIndex }) => {
1411
- if (typeof newIndex === "undefined")
1412
- return;
1413
- if (typeof oldIndex === "undefined")
1414
- return;
1399
+ if (typeof newIndex === "undefined") return;
1400
+ if (typeof oldIndex === "undefined") return;
1415
1401
  swapArray(newIndex, oldIndex);
1416
1402
  emit("change", props.model[modelName.value]);
1417
1403
  foreUpdate();
@@ -1441,8 +1427,7 @@
1441
1427
  formValue: mForm?.values,
1442
1428
  prop: props.prop
1443
1429
  });
1444
- if (!beforeCheckRes)
1445
- return;
1430
+ if (!beforeCheckRes) return;
1446
1431
  }
1447
1432
  const columns = props.config.items;
1448
1433
  const enumValues = props.config.enum || [];
@@ -1547,8 +1532,7 @@
1547
1532
  return fuc;
1548
1533
  };
1549
1534
  const removeHandler = (index) => {
1550
- if (props.disabled)
1551
- return;
1535
+ if (props.disabled) return;
1552
1536
  props.model[modelName.value].splice(index, 1);
1553
1537
  emit("change", props.model[modelName.value]);
1554
1538
  };
@@ -1655,8 +1639,7 @@
1655
1639
  };
1656
1640
  const toggleMode = () => {
1657
1641
  const calcLabelWidth = (label) => {
1658
- if (!label)
1659
- return "0px";
1642
+ if (!label) return "0px";
1660
1643
  const zhLength = label.match(/[^\x00-\xff]/g)?.length || 0;
1661
1644
  const chLength = label.length - zhLength;
1662
1645
  return `${Math.max(chLength * 8 + zhLength * 20, 80)}px`;
@@ -1676,8 +1659,7 @@
1676
1659
  });
1677
1660
  };
1678
1661
  const toggleFullscreen = () => {
1679
- if (!mTable.value)
1680
- return;
1662
+ if (!mTable.value) return;
1681
1663
  if (isFullscreen.value) {
1682
1664
  mTable.value.classList.remove("fixed");
1683
1665
  isFullscreen.value = false;
@@ -1992,12 +1974,9 @@
1992
1974
  const getActive = (mForm2, props2, activeTabName2) => {
1993
1975
  const { config, model, prop } = props2;
1994
1976
  const { active } = config;
1995
- if (typeof active === "function")
1996
- return active(mForm2, { model, formValue: mForm2?.values, prop });
1997
- if (+activeTabName2 >= props2.config.items.length)
1998
- return "0";
1999
- if (typeof active !== "undefined")
2000
- return active;
1977
+ if (typeof active === "function") return active(mForm2, { model, formValue: mForm2?.values, prop });
1978
+ if (+activeTabName2 >= props2.config.items.length) return "0";
1979
+ if (typeof active !== "undefined") return active;
2001
1980
  return "0";
2002
1981
  };
2003
1982
  const tabClick = (mForm2, tab, props2) => {
@@ -2018,8 +1997,7 @@
2018
1997
  const diffCount = vue.ref({});
2019
1998
  const tabs = vue.computed(() => {
2020
1999
  if (props.config.dynamic) {
2021
- if (!props.config.name)
2022
- throw new Error("dynamic tab 必须配置name");
2000
+ if (!props.config.name) throw new Error("dynamic tab 必须配置name");
2023
2001
  return props.model[props.config.name] || [];
2024
2002
  }
2025
2003
  return props.config.items.filter((item) => display(mForm, item.display, props));
@@ -2036,8 +2014,7 @@
2036
2014
  const tabItems = (tab) => props.config.dynamic ? props.config.items : tab.items;
2037
2015
  const tabClickHandler = (tab) => tabClick(mForm, tab, props);
2038
2016
  const onTabAdd = () => {
2039
- if (!props.config.name)
2040
- throw new Error("dynamic tab 必须配置name");
2017
+ if (!props.config.name) throw new Error("dynamic tab 必须配置name");
2041
2018
  if (typeof props.config.onTabAdd === "function") {
2042
2019
  props.config.onTabAdd(mForm, {
2043
2020
  model: props.model,
@@ -2053,8 +2030,7 @@
2053
2030
  mForm?.$emit("field-change", props.prop, props.model[props.config.name]);
2054
2031
  };
2055
2032
  const onTabRemove = (tabName) => {
2056
- if (!props.config.name)
2057
- throw new Error("dynamic tab 必须配置name");
2033
+ if (!props.config.name) throw new Error("dynamic tab 必须配置name");
2058
2034
  if (typeof props.config.onTabRemove === "function") {
2059
2035
  props.config.onTabRemove(mForm, tabName, {
2060
2036
  model: props.model,
@@ -2157,8 +2133,7 @@
2157
2133
  const getConfig = (key) => $MAGIC_FORM[key];
2158
2134
 
2159
2135
  const useAddField = (prop) => {
2160
- if (!prop)
2161
- return;
2136
+ if (!prop) return;
2162
2137
  const mForm = vue.inject("mForm");
2163
2138
  const instance = vue.getCurrentInstance();
2164
2139
  vue.watch(
@@ -2222,8 +2197,7 @@
2222
2197
  const setRemoteOptions = async function() {
2223
2198
  const { config } = props;
2224
2199
  const { option } = config;
2225
- if (!option)
2226
- return;
2200
+ if (!option) return;
2227
2201
  let { body } = option;
2228
2202
  const postOptions = {
2229
2203
  url: option.url,
@@ -2268,8 +2242,7 @@
2268
2242
  });
2269
2243
  }
2270
2244
  const changeHandler = () => {
2271
- if (!tMagicCascader.value)
2272
- return;
2245
+ if (!tMagicCascader.value) return;
2273
2246
  tMagicCascader.value.setQuery("");
2274
2247
  tMagicCascader.value.setPreviousQuery(null);
2275
2248
  emit("change", props.model[props.name]);
@@ -2391,10 +2364,8 @@
2391
2364
  };
2392
2365
  const mForm = vue.inject("mForm");
2393
2366
  const options = vue.computed(() => {
2394
- if (Array.isArray(props.config.options))
2395
- return props.config.options;
2396
- if (typeof props.config.options === "function")
2397
- return filterFunction(mForm, props.config.options, props);
2367
+ if (Array.isArray(props.config.options)) return props.config.options;
2368
+ if (typeof props.config.options === "function") return filterFunction(mForm, props.config.options, props);
2398
2369
  return [];
2399
2370
  });
2400
2371
  return (_ctx, _cache) => {
@@ -2532,12 +2503,9 @@
2532
2503
  if (!value.value) {
2533
2504
  value.value = [];
2534
2505
  }
2535
- if (!start || !end)
2536
- value.value = [];
2537
- if (start !== preStart)
2538
- value.value[0] = new Date(start);
2539
- if (end !== preEnd)
2540
- value.value[1] = new Date(end);
2506
+ if (!start || !end) value.value = [];
2507
+ if (start !== preStart) value.value[0] = new Date(start);
2508
+ if (end !== preEnd) value.value[1] = new Date(end);
2541
2509
  },
2542
2510
  {
2543
2511
  immediate: true
@@ -2547,8 +2515,7 @@
2547
2515
  vue.watch(
2548
2516
  () => props.model[props.name],
2549
2517
  (start, preStart) => {
2550
- if (start !== preStart)
2551
- value.value = start.map((item) => item ? new Date(item) : void 0);
2518
+ if (start !== preStart) value.value = start.map((item) => item ? new Date(item) : void 0);
2552
2519
  },
2553
2520
  {
2554
2521
  immediate: true
@@ -2574,8 +2541,7 @@
2574
2541
  emit(
2575
2542
  "change",
2576
2543
  value2.map((item) => {
2577
- if (item)
2578
- return utils.datetimeFormatter(item, "");
2544
+ if (item) return utils.datetimeFormatter(item, "");
2579
2545
  return void 0;
2580
2546
  })
2581
2547
  );
@@ -2713,14 +2679,12 @@
2713
2679
  value: {}
2714
2680
  });
2715
2681
  const changeFieldMap = async () => {
2716
- if (typeof props.config.returnFields !== "function" || !props.model)
2717
- return;
2682
+ if (typeof props.config.returnFields !== "function" || !props.model) return;
2718
2683
  const fields = await props.config.returnFields(props.config, props.model, request);
2719
2684
  fieldMap.value = {};
2720
2685
  fieldLabelMap.value = {};
2721
2686
  fields.forEach((v) => {
2722
- if (typeof v !== "object" || v.name === void 0)
2723
- return;
2687
+ if (typeof v !== "object" || v.name === void 0) return;
2724
2688
  let oldVal = props.model?.[v.name] || "";
2725
2689
  if (!oldVal && v.defaultValue !== void 0) {
2726
2690
  oldVal = v.defaultValue;
@@ -2924,12 +2888,9 @@
2924
2888
  const error = [];
2925
2889
  Object.entries(invalidFields).forEach(([, ValidateError]) => {
2926
2890
  ValidateError.forEach(({ field, message }) => {
2927
- if (field && message)
2928
- error.push(`${field} -> ${message}`);
2929
- if (field && !message)
2930
- error.push(`${field} -> 出现错误`);
2931
- if (!field && message)
2932
- error.push(`${message}`);
2891
+ if (field && message) error.push(`${field} -> ${message}`);
2892
+ if (field && !message) error.push(`${field} -> 出现错误`);
2893
+ if (!field && message) error.push(`${message}`);
2933
2894
  });
2934
2895
  });
2935
2896
  throw new Error(error.join("<br>"));
@@ -3452,8 +3413,7 @@
3452
3413
  const emit = __emit;
3453
3414
  const optionComponent = design.getConfig("components")?.option;
3454
3415
  const optionGroupComponent = design.getConfig("components")?.optionGroup;
3455
- if (!props.model)
3456
- throw new Error("不能没有model");
3416
+ if (!props.model) throw new Error("不能没有model");
3457
3417
  useAddField(props.prop);
3458
3418
  const tMagicSelect = vue.ref();
3459
3419
  const mForm = vue.inject("mForm");
@@ -3488,16 +3448,14 @@
3488
3448
  }));
3489
3449
  };
3490
3450
  const getOptions = async () => {
3491
- if (!props.model)
3492
- return [];
3451
+ if (!props.model) return [];
3493
3452
  if (localOptions.value.length) {
3494
3453
  return localOptions.value;
3495
3454
  }
3496
3455
  loading.value = true;
3497
3456
  let items = [];
3498
3457
  const { option } = props.config;
3499
- if (!option)
3500
- return [];
3458
+ if (!option) return [];
3501
3459
  const { root = "", totalKey = "total" } = option;
3502
3460
  let { body = {}, url } = option;
3503
3461
  if (typeof url === "function") {
@@ -3570,8 +3528,7 @@
3570
3528
  return pgIndex.value === 0 ? selectedOptions.concat(items) : options.value.concat(items);
3571
3529
  };
3572
3530
  const getInitLocalOption = async () => {
3573
- if (!props.model)
3574
- return [];
3531
+ if (!props.model) return [];
3575
3532
  const value = props.model[props.name];
3576
3533
  const { config } = props;
3577
3534
  localOptions.value = await getOptions();
@@ -3592,11 +3549,9 @@
3592
3549
  return localOptions.value.filter((item) => equalValue(item.value, value));
3593
3550
  };
3594
3551
  const getInitOption = async () => {
3595
- if (!props.model)
3596
- return [];
3552
+ if (!props.model) return [];
3597
3553
  const { option } = props.config;
3598
- if (!option)
3599
- return [];
3554
+ if (!option) return [];
3600
3555
  const { root = "", initRoot = "" } = option;
3601
3556
  let { initBody = {} } = option;
3602
3557
  let options2 = [];
@@ -3679,8 +3634,7 @@
3679
3634
  });
3680
3635
  } else if (props.config.option) {
3681
3636
  vue.onBeforeMount(() => {
3682
- if (!props.model)
3683
- return;
3637
+ if (!props.model) return;
3684
3638
  const v = props.model[props.name];
3685
3639
  if (Array.isArray(v) ? v.length : typeof v !== "undefined") {
3686
3640
  getInitOption().then((data) => {
@@ -3724,10 +3678,8 @@
3724
3678
  emit("change", value);
3725
3679
  };
3726
3680
  const visibleHandler = async (visible) => {
3727
- if (!visible)
3728
- return;
3729
- if (!props.config.remote)
3730
- return;
3681
+ if (!visible) return;
3682
+ if (!props.config.remote) return;
3731
3683
  if (query.value && tMagicSelect.value) {
3732
3684
  tMagicSelect.value.setQuery(query.value);
3733
3685
  tMagicSelect.value.setPreviousQuery(query.value);
@@ -3954,8 +3906,7 @@
3954
3906
  mForm?.$emit("field-input", props.prop, v);
3955
3907
  };
3956
3908
  const buttonClickHandler = () => {
3957
- if (!appendConfig.value)
3958
- return;
3909
+ if (!appendConfig.value) return;
3959
3910
  if (typeof appendConfig.value.handler === "function") {
3960
3911
  appendConfig.value.handler(mForm, {
3961
3912
  model: props.model,
@@ -3964,10 +3915,8 @@
3964
3915
  }
3965
3916
  };
3966
3917
  const keyUpHandler = ($event) => {
3967
- if (!props.model)
3968
- return;
3969
- if (!props.name)
3970
- return;
3918
+ if (!props.model) return;
3919
+ if (!props.name) return;
3971
3920
  const arrowUp = $event.key === "ArrowUp";
3972
3921
  const arrowDown = $event.key === "ArrowDown";
3973
3922
  if (!arrowUp && !arrowDown) {
@@ -4197,12 +4146,9 @@
4197
4146
  if (!value.value) {
4198
4147
  value.value = [];
4199
4148
  }
4200
- if (!start || !end)
4201
- value.value = [];
4202
- if (start !== preStart)
4203
- value.value[0] = start;
4204
- if (end !== preEnd)
4205
- value.value[1] = end;
4149
+ if (!start || !end) value.value = [];
4150
+ if (start !== preStart) value.value[0] = start;
4151
+ if (end !== preEnd) value.value[1] = end;
4206
4152
  },
4207
4153
  {
4208
4154
  immediate: true
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.7",
2
+ "version": "1.4.8",
3
3
  "name": "@tmagic/form",
4
4
  "type": "module",
5
5
  "sideEffects": [
@@ -22,6 +22,10 @@
22
22
  },
23
23
  "./*": "./*"
24
24
  },
25
+ "files": [
26
+ "dist",
27
+ "types"
28
+ ],
25
29
  "license": "Apache-2.0",
26
30
  "engines": {
27
31
  "node": ">=18"
@@ -44,14 +48,14 @@
44
48
  "@vue/test-utils": "^2.4.6",
45
49
  "rimraf": "^3.0.2",
46
50
  "sass": "^1.77.0",
47
- "vite": "^5.2.11",
48
- "vue-tsc": "^2.0.16"
51
+ "vite": "^5.3.1",
52
+ "vue-tsc": "^2.0.19"
49
53
  },
50
54
  "peerDependencies": {
51
55
  "vue": "^3.4.27",
52
56
  "typescript": "*",
53
- "@tmagic/design": "1.4.7",
54
- "@tmagic/utils": "1.4.7"
57
+ "@tmagic/design": "1.4.8",
58
+ "@tmagic/utils": "1.4.8"
55
59
  },
56
60
  "peerDependenciesMeta": {
57
61
  "typescript": {
package/src/Form.vue DELETED
@@ -1,201 +0,0 @@
1
- <template>
2
- <TMagicForm
3
- class="m-form"
4
- ref="tMagicForm"
5
- :model="values"
6
- :label-width="labelWidth"
7
- :style="`height: ${height}`"
8
- :inline="inline"
9
- :label-position="labelPosition"
10
- >
11
- <template v-if="initialized && Array.isArray(config)">
12
- <Container
13
- v-for="(item, index) in config"
14
- :disabled="disabled"
15
- :key="item[keyProp] ?? index"
16
- :config="item"
17
- :model="values"
18
- :last-values="lastValuesProcessed"
19
- :is-compare="isCompare"
20
- :label-width="item.labelWidth || labelWidth"
21
- :step-active="stepActive"
22
- :size="size"
23
- @change="changeHandler"
24
- ></Container>
25
- </template>
26
- </TMagicForm>
27
- </template>
28
-
29
- <script setup lang="ts">
30
- import { provide, reactive, ref, toRaw, watch, watchEffect } from 'vue';
31
- import { cloneDeep, isEqual } from 'lodash-es';
32
-
33
- import { TMagicForm } from '@tmagic/design';
34
-
35
- import Container from './containers/Container.vue';
36
- import { getConfig } from './utils/config';
37
- import { initValue } from './utils/form';
38
- import type { FormConfig, FormState, FormValue, ValidateError } from './schema';
39
-
40
- defineOptions({
41
- name: 'MForm',
42
- });
43
-
44
- const props = withDefaults(
45
- defineProps<{
46
- /** 表单配置 */
47
- config: FormConfig;
48
- /** 表单值 */
49
- initValues: Record<string, any>;
50
- /** 需对比的值(开启对比模式时传入) */
51
- lastValues?: Record<string, any>;
52
- /** 是否开启对比模式 */
53
- isCompare?: boolean;
54
- parentValues?: Record<string, any>;
55
- labelWidth?: string;
56
- disabled?: boolean;
57
- height?: string;
58
- stepActive?: string | number;
59
- size?: 'small' | 'default' | 'large';
60
- inline?: boolean;
61
- labelPosition?: string;
62
- keyProp?: string;
63
- popperClass?: string;
64
- extendState?: (state: FormState) => Record<string, any> | Promise<Record<string, any>>;
65
- }>(),
66
- {
67
- config: () => [],
68
- initValues: () => ({}),
69
- lastValues: () => ({}),
70
- isCompare: false,
71
- parentValues: () => ({}),
72
- labelWidth: '200px',
73
- disabled: false,
74
- height: 'auto',
75
- stepActive: 1,
76
- inline: false,
77
- labelPosition: 'right',
78
- keyProp: '__key',
79
- },
80
- );
81
-
82
- const emit = defineEmits(['change', 'error', 'field-input', 'field-change']);
83
-
84
- const tMagicForm = ref<InstanceType<typeof TMagicForm>>();
85
- const initialized = ref(false);
86
- const values = ref<FormValue>({});
87
- const lastValuesProcessed = ref<FormValue>({});
88
- const fields = new Map<string, any>();
89
-
90
- const requestFuc = getConfig('request') as Function;
91
-
92
- const formState: FormState = reactive<FormState>({
93
- keyProp: props.keyProp,
94
- popperClass: props.popperClass,
95
- config: props.config,
96
- initValues: props.initValues,
97
- isCompare: props.isCompare,
98
- lastValues: props.lastValues,
99
- parentValues: props.parentValues,
100
- values,
101
- lastValuesProcessed,
102
- $emit: emit as (event: string, ...args: any[]) => void,
103
- fields,
104
- setField: (prop: string, field: any) => fields.set(prop, field),
105
- getField: (prop: string) => fields.get(prop),
106
- deleteField: (prop: string) => fields.delete(prop),
107
- post: (options: any) => {
108
- if (requestFuc) {
109
- return requestFuc({
110
- ...options,
111
- method: 'POST',
112
- });
113
- }
114
- },
115
- });
116
-
117
- watchEffect(async () => {
118
- formState.initValues = props.initValues;
119
- formState.lastValues = props.lastValues;
120
- formState.isCompare = props.isCompare;
121
- formState.config = props.config;
122
- formState.keyProp = props.keyProp;
123
- formState.popperClass = props.popperClass;
124
- formState.parentValues = props.parentValues;
125
-
126
- if (typeof props.extendState === 'function') {
127
- const state = (await props.extendState(formState)) || {};
128
- Object.entries(state).forEach(([key, value]) => {
129
- formState[key] = value;
130
- });
131
- }
132
- });
133
-
134
- provide('mForm', formState);
135
-
136
- watch(
137
- [() => props.config, () => props.initValues],
138
- ([config], [preConfig]) => {
139
- if (!isEqual(toRaw(config), toRaw(preConfig))) {
140
- initialized.value = false;
141
- }
142
-
143
- initValue(formState, {
144
- initValues: props.initValues,
145
- config: props.config,
146
- }).then((value) => {
147
- values.value = value;
148
- // 非对比模式,初始化完成
149
- initialized.value = !props.isCompare;
150
- });
151
-
152
- if (props.isCompare) {
153
- // 对比模式下初始化待对比的表单值
154
- initValue(formState, {
155
- initValues: props.lastValues,
156
- config: props.config,
157
- }).then((value) => {
158
- lastValuesProcessed.value = value;
159
- initialized.value = true;
160
- });
161
- }
162
- },
163
- { immediate: true },
164
- );
165
-
166
- const changeHandler = () => {
167
- emit('change', values.value);
168
- };
169
-
170
- defineExpose({
171
- values,
172
- lastValuesProcessed,
173
- formState,
174
- initialized,
175
-
176
- changeHandler,
177
-
178
- resetForm: () => tMagicForm.value?.resetFields(),
179
-
180
- submitForm: async (native?: boolean): Promise<any> => {
181
- try {
182
- await tMagicForm.value?.validate();
183
- return native ? values.value : cloneDeep(toRaw(values.value));
184
- } catch (invalidFields: any) {
185
- emit('error', invalidFields);
186
-
187
- const error: string[] = [];
188
-
189
- Object.entries(invalidFields).forEach(([, ValidateError]) => {
190
- (ValidateError as ValidateError[]).forEach(({ field, message }) => {
191
- if (field && message) error.push(`${field} -> ${message}`);
192
- if (field && !message) error.push(`${field} -> 出现错误`);
193
- if (!field && message) error.push(`${message}`);
194
- });
195
- });
196
-
197
- throw new Error(error.join('<br>'));
198
- }
199
- },
200
- });
201
- </script>