@tmagic/form 1.8.0-beta.2 → 1.8.0-beta.4

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.
@@ -52,8 +52,9 @@ var Container_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defi
52
52
  const name = computed(() => props.config.name || "");
53
53
  const showDiff = computed(() => {
54
54
  if (!props.isCompare) return false;
55
- const curValue = name.value ? props.model[name.value] : props.model;
56
- const lastValue = name.value ? props.lastValues[name.value] : props.lastValues;
55
+ if (!name.value) return false;
56
+ const curValue = props.model[name.value];
57
+ const lastValue = props.lastValues[name.value];
57
58
  const customShowDiff = diffConfig.showDiff;
58
59
  if (typeof customShowDiff === "function") return Boolean(customShowDiff({
59
60
  curValue,
@@ -1,3 +1,4 @@
1
+ import { filterFunction } from "../utils/form.js";
1
2
  import Container_default from "./Container.js";
2
3
  import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, inject, normalizeStyle, openBlock, renderList, resolveDynamicComponent, unref, withCtx } from "vue";
3
4
  import { TMagicCheckbox } from "@tmagic/design";
@@ -36,6 +37,7 @@ var Fieldset_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defin
36
37
  const emit = __emit;
37
38
  const mForm = inject("mForm");
38
39
  const name = computed(() => props.config.name || "");
40
+ const legend = computed(() => filterFunction(mForm, props.config.legend, props));
39
41
  const checkboxName = computed(() => {
40
42
  if (typeof props.config.checkbox === "object" && typeof props.config.checkbox.name === "string") return props.config.checkbox.name;
41
43
  return "value";
@@ -75,7 +77,7 @@ var Fieldset_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defin
75
77
  "false-value": checkboxFalseValue.value,
76
78
  "onUpdate:modelValue": valueChangeHandler
77
79
  }, {
78
- default: withCtx(() => [createElementVNode("span", { innerHTML: __props.config.legend }, null, 8, _hoisted_1), __props.config.extra ? (openBlock(), createElementBlock("span", {
80
+ default: withCtx(() => [createElementVNode("span", { innerHTML: legend.value }, null, 8, _hoisted_1), __props.config.extra ? (openBlock(), createElementBlock("span", {
79
81
  key: 0,
80
82
  innerHTML: __props.config.extra,
81
83
  class: "m-form-tip"
@@ -88,7 +90,7 @@ var Fieldset_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defin
88
90
  "false-value"
89
91
  ])]),
90
92
  _: 1
91
- })) : (openBlock(), createElementBlock("legend", _hoisted_3, [createElementVNode("span", { innerHTML: __props.config.legend }, null, 8, _hoisted_4), __props.config.extra ? (openBlock(), createElementBlock("span", {
93
+ })) : (openBlock(), createElementBlock("legend", _hoisted_3, [createElementVNode("span", { innerHTML: legend.value }, null, 8, _hoisted_4), __props.config.extra ? (openBlock(), createElementBlock("span", {
92
94
  key: 0,
93
95
  innerHTML: __props.config.extra,
94
96
  class: "m-form-tip"
@@ -21,10 +21,17 @@ import { createApp, defineComponent, h, nextTick, ref, watch } from "vue";
21
21
  * } catch (e) {
22
22
  * console.error(e);
23
23
  * }
24
+ *
25
+ * // 需要同时获取变更记录时:
26
+ * const { values, changeRecords } = await submitForm({
27
+ * config: [...],
28
+ * initValues: { name: 'foo' },
29
+ * returnChangeRecords: true,
30
+ * });
24
31
  * ```
25
32
  */
26
33
  var submitForm = (options) => {
27
- const { native, appContext, timeout = 1e4, ...formProps } = options;
34
+ const { native, appContext, timeout = 1e4, returnChangeRecords, ...formProps } = options;
28
35
  return new Promise((resolve, reject) => {
29
36
  const container = document.createElement("div");
30
37
  container.style.display = "none";
@@ -40,7 +47,12 @@ var submitForm = (options) => {
40
47
  stop();
41
48
  try {
42
49
  await nextTick();
43
- resolve(await formRef.value.submitForm(native));
50
+ const changeRecords = [...formRef.value.changeRecords ?? []];
51
+ const result = await formRef.value.submitForm(native);
52
+ resolve(returnChangeRecords ? {
53
+ values: result,
54
+ changeRecords
55
+ } : result);
44
56
  } catch (err) {
45
57
  reject(err);
46
58
  } finally {
@@ -2930,8 +2930,9 @@
2930
2930
  const name = (0, vue.computed)(() => props.config.name || "");
2931
2931
  const showDiff = (0, vue.computed)(() => {
2932
2932
  if (!props.isCompare) return false;
2933
- const curValue = name.value ? props.model[name.value] : props.model;
2934
- const lastValue = name.value ? props.lastValues[name.value] : props.lastValues;
2933
+ if (!name.value) return false;
2934
+ const curValue = props.model[name.value];
2935
+ const lastValue = props.lastValues[name.value];
2935
2936
  const customShowDiff = diffConfig.showDiff;
2936
2937
  if (typeof customShowDiff === "function") return Boolean(customShowDiff({
2937
2938
  curValue,
@@ -3748,10 +3749,17 @@
3748
3749
  * } catch (e) {
3749
3750
  * console.error(e);
3750
3751
  * }
3752
+ *
3753
+ * // 需要同时获取变更记录时:
3754
+ * const { values, changeRecords } = await submitForm({
3755
+ * config: [...],
3756
+ * initValues: { name: 'foo' },
3757
+ * returnChangeRecords: true,
3758
+ * });
3751
3759
  * ```
3752
3760
  */
3753
3761
  var submitForm = (options) => {
3754
- const { native, appContext, timeout = 1e4, ...formProps } = options;
3762
+ const { native, appContext, timeout = 1e4, returnChangeRecords, ...formProps } = options;
3755
3763
  return new Promise((resolve, reject) => {
3756
3764
  const container = document.createElement("div");
3757
3765
  container.style.display = "none";
@@ -3767,7 +3775,12 @@
3767
3775
  stop();
3768
3776
  try {
3769
3777
  await (0, vue.nextTick)();
3770
- resolve(await formRef.value.submitForm(native));
3778
+ const changeRecords = [...formRef.value.changeRecords ?? []];
3779
+ const result = await formRef.value.submitForm(native);
3780
+ resolve(returnChangeRecords ? {
3781
+ values: result,
3782
+ changeRecords
3783
+ } : result);
3771
3784
  } catch (err) {
3772
3785
  reject(err);
3773
3786
  } finally {
@@ -4366,6 +4379,7 @@
4366
4379
  const emit = __emit;
4367
4380
  const mForm = (0, vue.inject)("mForm");
4368
4381
  const name = (0, vue.computed)(() => props.config.name || "");
4382
+ const legend = (0, vue.computed)(() => filterFunction(mForm, props.config.legend, props));
4369
4383
  const checkboxName = (0, vue.computed)(() => {
4370
4384
  if (typeof props.config.checkbox === "object" && typeof props.config.checkbox.name === "string") return props.config.checkbox.name;
4371
4385
  return "value";
@@ -4405,7 +4419,7 @@
4405
4419
  "false-value": checkboxFalseValue.value,
4406
4420
  "onUpdate:modelValue": valueChangeHandler
4407
4421
  }, {
4408
- default: (0, vue.withCtx)(() => [(0, vue.createElementVNode)("span", { innerHTML: __props.config.legend }, null, 8, _hoisted_1$11), __props.config.extra ? ((0, vue.openBlock)(), (0, vue.createElementBlock)("span", {
4422
+ default: (0, vue.withCtx)(() => [(0, vue.createElementVNode)("span", { innerHTML: legend.value }, null, 8, _hoisted_1$11), __props.config.extra ? ((0, vue.openBlock)(), (0, vue.createElementBlock)("span", {
4409
4423
  key: 0,
4410
4424
  innerHTML: __props.config.extra,
4411
4425
  class: "m-form-tip"
@@ -4418,7 +4432,7 @@
4418
4432
  "false-value"
4419
4433
  ])]),
4420
4434
  _: 1
4421
- })) : ((0, vue.openBlock)(), (0, vue.createElementBlock)("legend", _hoisted_3$5, [(0, vue.createElementVNode)("span", { innerHTML: __props.config.legend }, null, 8, _hoisted_4$3), __props.config.extra ? ((0, vue.openBlock)(), (0, vue.createElementBlock)("span", {
4435
+ })) : ((0, vue.openBlock)(), (0, vue.createElementBlock)("legend", _hoisted_3$5, [(0, vue.createElementVNode)("span", { innerHTML: legend.value }, null, 8, _hoisted_4$3), __props.config.extra ? ((0, vue.openBlock)(), (0, vue.createElementBlock)("span", {
4422
4436
  key: 0,
4423
4437
  innerHTML: __props.config.extra,
4424
4438
  class: "m-form-tip"
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.8.0-beta.2",
2
+ "version": "1.8.0-beta.4",
3
3
  "name": "@tmagic/form",
4
4
  "type": "module",
5
5
  "sideEffects": [
@@ -52,9 +52,9 @@
52
52
  "peerDependencies": {
53
53
  "vue": "^3.5.34",
54
54
  "typescript": "^6.0.3",
55
- "@tmagic/design": "1.8.0-beta.2",
56
- "@tmagic/form-schema": "1.8.0-beta.2",
57
- "@tmagic/utils": "1.8.0-beta.2"
55
+ "@tmagic/design": "1.8.0-beta.4",
56
+ "@tmagic/form-schema": "1.8.0-beta.4",
57
+ "@tmagic/utils": "1.8.0-beta.4"
58
58
  },
59
59
  "peerDependenciesMeta": {
60
60
  "typescript": {
@@ -307,8 +307,10 @@ const name = computed(() => props.config.name || '');
307
307
  // 便于业务侧自定义"语义上相等"的特殊场景(例如空字符串与空 hook 结构)。
308
308
  const showDiff = computed(() => {
309
309
  if (!props.isCompare) return false;
310
- const curValue = name.value ? props.model[name.value] : props.model;
311
- const lastValue = name.value ? props.lastValues[name.value] : props.lastValues;
310
+ if (!name.value) return false;
311
+
312
+ const curValue = props.model[name.value];
313
+ const lastValue = props.lastValues[name.value];
312
314
 
313
315
  const customShowDiff = diffConfig.showDiff;
314
316
  if (typeof customShowDiff === 'function') {
@@ -7,11 +7,11 @@
7
7
  :true-value="checkboxTrueValue"
8
8
  :false-value="checkboxFalseValue"
9
9
  @update:modelValue="valueChangeHandler"
10
- ><span v-html="config.legend"></span><span v-if="config.extra" v-html="config.extra" class="m-form-tip"></span
10
+ ><span v-html="legend"></span><span v-if="config.extra" v-html="config.extra" class="m-form-tip"></span
11
11
  ></TMagicCheckbox>
12
12
  </component>
13
13
  <legend v-else>
14
- <span v-html="config.legend"></span>
14
+ <span v-html="legend"></span>
15
15
  <span v-if="config.extra" v-html="config.extra" class="m-form-tip"></span>
16
16
  </legend>
17
17
 
@@ -63,6 +63,7 @@ import { computed, inject } from 'vue';
63
63
  import { TMagicCheckbox } from '@tmagic/design';
64
64
 
65
65
  import { ContainerChangeEventData, FieldsetConfig, FormState } from '../schema';
66
+ import { filterFunction } from '../utils/form';
66
67
 
67
68
  import Container from './Container.vue';
68
69
 
@@ -99,6 +100,8 @@ const mForm = inject<FormState | undefined>('mForm');
99
100
 
100
101
  const name = computed(() => props.config.name || '');
101
102
 
103
+ const legend = computed(() => filterFunction<string>(mForm, props.config.legend, props));
104
+
102
105
  const checkboxName = computed(() => {
103
106
  if (typeof props.config.checkbox === 'object' && typeof props.config.checkbox.name === 'string') {
104
107
  return props.config.checkbox.name;
package/src/submitForm.ts CHANGED
@@ -19,7 +19,7 @@
19
19
  import { type AppContext, type Component, createApp, defineComponent, h, nextTick, ref, watch } from 'vue';
20
20
 
21
21
  import Form from './Form.vue';
22
- import type { FormConfig, FormState } from './schema';
22
+ import type { ChangeRecord, FormConfig, FormState } from './schema';
23
23
 
24
24
  // #region SubmitFormOptions
25
25
  /**
@@ -48,6 +48,11 @@ export interface SubmitFormOptions {
48
48
  extendState?: (_state: FormState) => Record<string, any> | Promise<Record<string, any>>;
49
49
  /** 透传给 Form.submitForm 的参数:是否直接返回原始响应式 values */
50
50
  native?: boolean;
51
+ /**
52
+ * 是否在 resolve 结果中携带 changeRecords(变更记录)。
53
+ * 开启后 resolve 的结果为 `{ values, changeRecords }`,否则仅 resolve values。
54
+ */
55
+ returnChangeRecords?: boolean;
51
56
  /**
52
57
  * 父级应用上下文,用于继承全局组件、指令、provide 等。
53
58
  * 通常通过 `app._context` 或 `getCurrentInstance()?.appContext` 获取。
@@ -58,6 +63,18 @@ export interface SubmitFormOptions {
58
63
  }
59
64
  // #endregion SubmitFormOptions
60
65
 
66
+ // #region SubmitFormResult
67
+ /**
68
+ * 开启 `returnChangeRecords` 时 submitForm 的返回结果
69
+ */
70
+ export interface SubmitFormResult {
71
+ /** 校验通过后的表单值 */
72
+ values: any;
73
+ /** 表单变更记录 */
74
+ changeRecords: ChangeRecord[];
75
+ }
76
+ // #endregion SubmitFormResult
77
+
61
78
  /**
62
79
  * 以命令式方式调用 Form.vue 完成一次表单校验/提交。
63
80
  *
@@ -78,10 +95,17 @@ export interface SubmitFormOptions {
78
95
  * } catch (e) {
79
96
  * console.error(e);
80
97
  * }
98
+ *
99
+ * // 需要同时获取变更记录时:
100
+ * const { values, changeRecords } = await submitForm({
101
+ * config: [...],
102
+ * initValues: { name: 'foo' },
103
+ * returnChangeRecords: true,
104
+ * });
81
105
  * ```
82
106
  */
83
107
  export const submitForm = (options: SubmitFormOptions): Promise<any> => {
84
- const { native, appContext, timeout = 10000, ...formProps } = options;
108
+ const { native, appContext, timeout = 10000, returnChangeRecords, ...formProps } = options;
85
109
 
86
110
  return new Promise((resolve, reject) => {
87
111
  const container = document.createElement('div');
@@ -105,8 +129,10 @@ export const submitForm = (options: SubmitFormOptions): Promise<any> => {
105
129
  try {
106
130
  // 等待子组件(FormItem 等)完成首次渲染,确保 validate 能拿到所有字段
107
131
  await nextTick();
132
+ // submitForm 校验通过后会清空 changeRecords,需在调用前先做快照
133
+ const changeRecords: ChangeRecord[] = [...(formRef.value.changeRecords ?? [])];
108
134
  const result = await formRef.value.submitForm(native);
109
- resolve(result);
135
+ resolve(returnChangeRecords ? { values: result, changeRecords } : result);
110
136
  } catch (err) {
111
137
  reject(err);
112
138
  } finally {
package/types/index.d.ts CHANGED
@@ -90,6 +90,11 @@ interface SubmitFormOptions {
90
90
  extendState?: (_state: schema_d_exports.FormState) => Record<string, any> | Promise<Record<string, any>>;
91
91
  /** 透传给 Form.submitForm 的参数:是否直接返回原始响应式 values */
92
92
  native?: boolean;
93
+ /**
94
+ * 是否在 resolve 结果中携带 changeRecords(变更记录)。
95
+ * 开启后 resolve 的结果为 `{ values, changeRecords }`,否则仅 resolve values。
96
+ */
97
+ returnChangeRecords?: boolean;
93
98
  /**
94
99
  * 父级应用上下文,用于继承全局组件、指令、provide 等。
95
100
  * 通常通过 `app._context` 或 `getCurrentInstance()?.appContext` 获取。
@@ -98,6 +103,15 @@ interface SubmitFormOptions {
98
103
  /** 等待表单初始化的最长时间(毫秒),超时将以错误 reject。默认 10000ms */
99
104
  timeout?: number;
100
105
  }
106
+ /**
107
+ * 开启 `returnChangeRecords` 时 submitForm 的返回结果
108
+ */
109
+ interface SubmitFormResult {
110
+ /** 校验通过后的表单值 */
111
+ values: any;
112
+ /** 表单变更记录 */
113
+ changeRecords: ChangeRecord[];
114
+ }
101
115
  /**
102
116
  * 以命令式方式调用 Form.vue 完成一次表单校验/提交。
103
117
  *
@@ -118,6 +132,13 @@ interface SubmitFormOptions {
118
132
  * } catch (e) {
119
133
  * console.error(e);
120
134
  * }
135
+ *
136
+ * // 需要同时获取变更记录时:
137
+ * const { values, changeRecords } = await submitForm({
138
+ * config: [...],
139
+ * initValues: { name: 'foo' },
140
+ * returnChangeRecords: true,
141
+ * });
121
142
  * ```
122
143
  */
123
144
  declare const submitForm: (options: SubmitFormOptions) => Promise<any>;
@@ -1897,8 +1918,8 @@ declare const _default$31: {
1897
1918
  install(app: App, opt?: FormInstallOptions): void;
1898
1919
  };
1899
1920
  declare namespace index_d_exports {
1900
- export { ChangeRecord, ContainerChangeEventData, FORM_DIFF_CONFIG_KEY, FormDiffConfig, FormInstallOptions, FormLabelSlotProps, FormSlots, _default as MCascader, _default$1 as MCheckbox, _default$2 as MCheckboxGroup, _default$3 as MColorPicker, _default$4 as MContainer, _default$5 as MDate, _default$6 as MDateTime, _default$7 as MDaterange, _default$8 as MDisplay, _default$9 as MDynamicField, _default$10 as MFieldset, _default$11 as MFlexLayout, _default$12 as MForm, _default$13 as MFormBox, _default$14 as MFormDialog, _default$15 as MFormDrawer, _default$16 as MGroupList, _default$17 as MHidden, _default$18 as MLink, _default$19 as MNumber, _default$20 as MNumberRange, _default$21 as MPanel, _default$22 as MRadioGroup, _default$23 as MRow, _default$24 as MSelect, _default$25 as MSwitch, _default$16 as MTable, _default$16 as MTableGroupList, _default$26 as MTabs, _default$27 as MText, _default$28 as MTextarea, _default$29 as MTime, _default$30 as MTimerange, SubmitFormOptions, ValidateError, createForm, createObjectProp, createValues, datetimeFormatter, _default$31 as default, deleteField as deleteFormField, display, filterFunction, getDataByPage, getField as getFormField, getRules, initValue, registerField as registerFormField, sortArray, sortChange, submitForm, useAddField };
1921
+ export { ChangeRecord, ContainerChangeEventData, FORM_DIFF_CONFIG_KEY, FormDiffConfig, FormInstallOptions, FormLabelSlotProps, FormSlots, _default as MCascader, _default$1 as MCheckbox, _default$2 as MCheckboxGroup, _default$3 as MColorPicker, _default$4 as MContainer, _default$5 as MDate, _default$6 as MDateTime, _default$7 as MDaterange, _default$8 as MDisplay, _default$9 as MDynamicField, _default$10 as MFieldset, _default$11 as MFlexLayout, _default$12 as MForm, _default$13 as MFormBox, _default$14 as MFormDialog, _default$15 as MFormDrawer, _default$16 as MGroupList, _default$17 as MHidden, _default$18 as MLink, _default$19 as MNumber, _default$20 as MNumberRange, _default$21 as MPanel, _default$22 as MRadioGroup, _default$23 as MRow, _default$24 as MSelect, _default$25 as MSwitch, _default$16 as MTable, _default$16 as MTableGroupList, _default$26 as MTabs, _default$27 as MText, _default$28 as MTextarea, _default$29 as MTime, _default$30 as MTimerange, SubmitFormOptions, SubmitFormResult, ValidateError, createForm, createObjectProp, createValues, datetimeFormatter, _default$31 as default, deleteField as deleteFormField, display, filterFunction, getDataByPage, getField as getFormField, getRules, initValue, registerField as registerFormField, sortArray, sortChange, submitForm, useAddField };
1901
1922
  }
1902
1923
  declare const createForm: <T extends [] = []>(config: schema_d_exports.FormConfig | T) => schema_d_exports.FormConfig | T;
1903
1924
  //#endregion
1904
- export { ChangeRecord, ContainerChangeEventData, FORM_DIFF_CONFIG_KEY, FormDiffConfig, type FormInstallOptions, FormLabelSlotProps, FormSlots, _default as MCascader, _default$1 as MCheckbox, _default$2 as MCheckboxGroup, _default$3 as MColorPicker, _default$4 as MContainer, _default$5 as MDate, _default$6 as MDateTime, _default$7 as MDaterange, _default$8 as MDisplay, _default$9 as MDynamicField, _default$10 as MFieldset, _default$11 as MFlexLayout, _default$12 as MForm, _default$13 as MFormBox, _default$14 as MFormDialog, _default$15 as MFormDrawer, _default$16 as MGroupList, _default$16 as MTable, _default$16 as MTableGroupList, _default$17 as MHidden, _default$18 as MLink, _default$19 as MNumber, _default$20 as MNumberRange, _default$21 as MPanel, _default$22 as MRadioGroup, _default$23 as MRow, _default$24 as MSelect, _default$25 as MSwitch, _default$26 as MTabs, _default$27 as MText, _default$28 as MTextarea, _default$29 as MTime, _default$30 as MTimerange, SubmitFormOptions, ValidateError, createForm, createObjectProp, createValues, datetimeFormatter, _default$31 as default, deleteField as deleteFormField, display, filterFunction, getDataByPage, getField as getFormField, getRules, initValue, registerField as registerFormField, sortArray, sortChange, submitForm, useAddField };
1925
+ export { ChangeRecord, ContainerChangeEventData, FORM_DIFF_CONFIG_KEY, FormDiffConfig, type FormInstallOptions, FormLabelSlotProps, FormSlots, _default as MCascader, _default$1 as MCheckbox, _default$2 as MCheckboxGroup, _default$3 as MColorPicker, _default$4 as MContainer, _default$5 as MDate, _default$6 as MDateTime, _default$7 as MDaterange, _default$8 as MDisplay, _default$9 as MDynamicField, _default$10 as MFieldset, _default$11 as MFlexLayout, _default$12 as MForm, _default$13 as MFormBox, _default$14 as MFormDialog, _default$15 as MFormDrawer, _default$16 as MGroupList, _default$16 as MTable, _default$16 as MTableGroupList, _default$17 as MHidden, _default$18 as MLink, _default$19 as MNumber, _default$20 as MNumberRange, _default$21 as MPanel, _default$22 as MRadioGroup, _default$23 as MRow, _default$24 as MSelect, _default$25 as MSwitch, _default$26 as MTabs, _default$27 as MText, _default$28 as MTextarea, _default$29 as MTime, _default$30 as MTimerange, SubmitFormOptions, SubmitFormResult, ValidateError, createForm, createObjectProp, createValues, datetimeFormatter, _default$31 as default, deleteField as deleteFormField, display, filterFunction, getDataByPage, getField as getFormField, getRules, initValue, registerField as registerFormField, sortArray, sortChange, submitForm, useAddField };