@tmagic/form 1.7.4 → 1.7.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/tmagic-form.js +54 -29
- package/dist/tmagic-form.umd.cjs +56 -30
- package/package.json +4 -4
- package/src/containers/Container.vue +2 -6
- package/src/fields/Display.vue +15 -2
- package/src/utils/form.ts +54 -25
- package/types/index.d.ts +2 -1
package/dist/tmagic-form.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineComponent, createElementBlock, openBlock, createElementVNode, createBlock, createCommentVNode, unref, withCtx, createVNode, inject, ref, computed, watchEffect, watch, resolveComponent, normalizeStyle, normalizeClass, mergeProps, resolveDynamicComponent, Fragment, renderList, createTextVNode, toDisplayString, toRaw,
|
|
1
|
+
import { readonly, defineComponent, createElementBlock, openBlock, createElementVNode, createBlock, createCommentVNode, unref, withCtx, createVNode, inject, ref, computed, watchEffect, watch, resolveComponent, normalizeStyle, normalizeClass, mergeProps, resolveDynamicComponent, Fragment, renderList, createTextVNode, toDisplayString, toRaw, withDirectives, vShow, renderSlot, getCurrentInstance, createSlots, onUnmounted, reactive, onBeforeUnmount, vModelText, useTemplateRef, provide, shallowRef, onBeforeMount, nextTick, resolveDirective, withModifiers, Teleport, h } from 'vue';
|
|
2
2
|
import { WarningFilled, CaretBottom, CaretRight, Delete, DocumentCopy, CaretTop, Position, Grid, Plus, Sort, ArrowUp, ArrowDown, FullScreen } from '@element-plus/icons-vue';
|
|
3
3
|
import { cloneDeep, isEqual, isEmpty, debounce } from 'lodash-es';
|
|
4
4
|
import { TMagicTooltip, TMagicIcon, TMagicFormItem, TMagicButton, TMagicCheckbox, TMagicCard, TMagicPopover, TMagicInputNumber, TMagicCol, TMagicRow, TMagicSteps, TMagicStep, getDesignConfig, TMagicBadge, TMagicCascader, TMagicCheckboxGroup, TMagicColorPicker, TMagicDatePicker, TMagicForm, TMagicInput, tMagicMessage, tMagicMessageBox, TMagicDialog, TMagicRadioButton, TMagicRadio, TMagicRadioGroup, TMagicSelect, TMagicSwitch, TMagicTimePicker, useZIndex, TMagicTable, TMagicUpload, TMagicPagination, TMagicDrawer, TMagicScrollbar } from '@tmagic/design';
|
|
@@ -9,14 +9,23 @@ import { createPopper } from '@popperjs/core';
|
|
|
9
9
|
import Sortable from 'sortablejs';
|
|
10
10
|
export * from '@tmagic/form-schema';
|
|
11
11
|
|
|
12
|
-
const
|
|
12
|
+
const TABLE_SELECT_TYPES = /* @__PURE__ */ new Set(["table-select", "tableSelect"]);
|
|
13
|
+
const isTableSelect = (type) => typeof type === "string" && TABLE_SELECT_TYPES.has(type);
|
|
13
14
|
const asyncLoadConfig = (value, initValue2, { asyncLoad, name, type }) => {
|
|
14
15
|
if (type === "html" && typeof asyncLoad === "object" && typeof name !== "undefined") {
|
|
15
16
|
asyncLoad.name = name;
|
|
16
17
|
value.asyncLoad = typeof initValue2.asyncLoad === "object" ? initValue2.asyncLoad : asyncLoad;
|
|
17
18
|
}
|
|
18
19
|
};
|
|
19
|
-
const
|
|
20
|
+
const MULTIPLE_VALUE_TYPES = /* @__PURE__ */ new Set([
|
|
21
|
+
"checkbox-group",
|
|
22
|
+
"checkboxGroup",
|
|
23
|
+
"table",
|
|
24
|
+
"cascader",
|
|
25
|
+
"group-list",
|
|
26
|
+
"groupList"
|
|
27
|
+
]);
|
|
28
|
+
const isMultipleValue = (type) => typeof type === "string" && MULTIPLE_VALUE_TYPES.has(type);
|
|
20
29
|
const initItemsValue = (mForm, value, initValue2, { items, name, extensible }) => {
|
|
21
30
|
if (Array.isArray(initValue2[name])) {
|
|
22
31
|
value[name] = initValue2[name].map((v, index) => createValues(mForm, items, v, value[name]?.[index]));
|
|
@@ -50,22 +59,18 @@ const initValueItem = function(mForm, item, initValue2, value) {
|
|
|
50
59
|
const { names } = item;
|
|
51
60
|
const { type, name } = item;
|
|
52
61
|
if (isTableSelect(type) && name) {
|
|
53
|
-
value[name] = initValue2[name]
|
|
62
|
+
value[name] = initValue2[name] ?? "";
|
|
54
63
|
return value;
|
|
55
64
|
}
|
|
56
65
|
asyncLoadConfig(value, initValue2, item);
|
|
57
66
|
if (name && !items && typeof initValue2?.[name] !== "undefined") {
|
|
58
67
|
if (typeof value[name] === "undefined") {
|
|
59
|
-
|
|
60
|
-
value[name] = Number(initValue2[name]);
|
|
61
|
-
} else {
|
|
62
|
-
value[name] = typeof initValue2[name] === "object" ? initValue2[name] : initValue2[name];
|
|
63
|
-
}
|
|
68
|
+
value[name] = type === "number" ? Number(initValue2[name]) : initValue2[name];
|
|
64
69
|
}
|
|
65
70
|
return value;
|
|
66
71
|
}
|
|
67
72
|
if (names) {
|
|
68
|
-
return names.forEach((n) => value[n] = initValue2[n]
|
|
73
|
+
return names.forEach((n) => value[n] = initValue2[n] ?? "");
|
|
69
74
|
}
|
|
70
75
|
if (!name) {
|
|
71
76
|
return createValues(mForm, items, initValue2, value);
|
|
@@ -91,7 +96,7 @@ const createValues = function(mForm, config = [], initValue2 = {}, value = {}) {
|
|
|
91
96
|
}
|
|
92
97
|
return value;
|
|
93
98
|
};
|
|
94
|
-
const getDefaultValue = function(mForm, { defaultValue, type, filter, multiple }) {
|
|
99
|
+
const getDefaultValue = function(mForm, { defaultValue, type, filter, multiple, names }) {
|
|
95
100
|
if (typeof defaultValue === "function") {
|
|
96
101
|
return defaultValue(mForm);
|
|
97
102
|
}
|
|
@@ -110,15 +115,18 @@ const getDefaultValue = function(mForm, { defaultValue, type, filter, multiple }
|
|
|
110
115
|
if (multiple || type === "number-range") {
|
|
111
116
|
return [];
|
|
112
117
|
}
|
|
118
|
+
if (type === "daterange" && !names) {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
113
121
|
return "";
|
|
114
122
|
};
|
|
115
123
|
const filterFunction = (mForm, config, props) => {
|
|
116
124
|
if (typeof config === "function") {
|
|
117
125
|
return config(mForm, {
|
|
118
|
-
values: mForm?.initValues || {},
|
|
119
|
-
model: props.model,
|
|
120
|
-
parent: mForm?.parentValues || {},
|
|
121
|
-
formValue: mForm?.values || props.model,
|
|
126
|
+
values: readonly(mForm?.initValues || {}),
|
|
127
|
+
model: readonly(props.model),
|
|
128
|
+
parent: readonly(mForm?.parentValues || {}),
|
|
129
|
+
formValue: readonly(mForm?.values || props.model),
|
|
122
130
|
prop: props.prop,
|
|
123
131
|
config: props.config,
|
|
124
132
|
index: props.index,
|
|
@@ -187,7 +195,7 @@ const datetimeFormatter = (v, defaultValue = "-", format = "YYYY-MM-DD HH:mm:ss"
|
|
|
187
195
|
let time;
|
|
188
196
|
if (["x", "timestamp"].includes(format)) {
|
|
189
197
|
time = dayjs(Number.isNaN(Number(v)) ? v : Number(v)).valueOf();
|
|
190
|
-
} else if (typeof v === "string" && v.includes("Z") || v
|
|
198
|
+
} else if (typeof v === "string" && v.includes("Z") || v instanceof Date) {
|
|
191
199
|
dayjs.extend(utc);
|
|
192
200
|
time = dayjs(v).utcOffset(8).format(format);
|
|
193
201
|
} else {
|
|
@@ -200,9 +208,10 @@ const datetimeFormatter = (v, defaultValue = "-", format = "YYYY-MM-DD HH:mm:ss"
|
|
|
200
208
|
}
|
|
201
209
|
return defaultValue;
|
|
202
210
|
};
|
|
203
|
-
const getDataByPage = (data = [], pagecontext, pagesize) =>
|
|
204
|
-
|
|
205
|
-
);
|
|
211
|
+
const getDataByPage = (data = [], pagecontext, pagesize) => {
|
|
212
|
+
const start = pagecontext * pagesize;
|
|
213
|
+
return data.slice(start, start + pagesize);
|
|
214
|
+
};
|
|
206
215
|
const sortArray = (data, newIndex, oldIndex, sortKey) => {
|
|
207
216
|
if (newIndex === oldIndex) {
|
|
208
217
|
return data;
|
|
@@ -210,7 +219,8 @@ const sortArray = (data, newIndex, oldIndex, sortKey) => {
|
|
|
210
219
|
if (newIndex < 0 || newIndex >= data.length || oldIndex < 0 || oldIndex >= data.length) {
|
|
211
220
|
return data;
|
|
212
221
|
}
|
|
213
|
-
const
|
|
222
|
+
const item = data[oldIndex];
|
|
223
|
+
const newData = data.toSpliced(oldIndex, 1).toSpliced(newIndex, 0, item);
|
|
214
224
|
if (sortKey) {
|
|
215
225
|
for (let i = newData.length - 1, v = 0; i >= 0; i--, v++) {
|
|
216
226
|
newData[v][sortKey] = i;
|
|
@@ -220,10 +230,22 @@ const sortArray = (data, newIndex, oldIndex, sortKey) => {
|
|
|
220
230
|
};
|
|
221
231
|
const sortChange = (data, { prop, order }) => {
|
|
222
232
|
if (order === "ascending") {
|
|
223
|
-
data
|
|
233
|
+
data.sort((a, b) => a[prop] - b[prop]);
|
|
224
234
|
} else if (order === "descending") {
|
|
225
|
-
data
|
|
235
|
+
data.sort((a, b) => b[prop] - a[prop]);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
const createObjectProp = (prop, key, name) => {
|
|
239
|
+
if (prop === "") {
|
|
240
|
+
return key;
|
|
241
|
+
}
|
|
242
|
+
const itemPath = `${prop}`.split(".");
|
|
243
|
+
if (name) {
|
|
244
|
+
if (`${itemPath[itemPath.length - 1]}` === `${name}`) {
|
|
245
|
+
return `${[...itemPath.slice(0, -1), key].join(".")}`;
|
|
246
|
+
}
|
|
226
247
|
}
|
|
248
|
+
return `${[...itemPath, key].join(".")}`;
|
|
227
249
|
};
|
|
228
250
|
|
|
229
251
|
const _hoisted_1$e = {
|
|
@@ -463,11 +485,7 @@ const _sfc_main$A = /* @__PURE__ */ defineComponent({
|
|
|
463
485
|
value = filterHandler(filter, v);
|
|
464
486
|
if (typeof onChange === "function") {
|
|
465
487
|
const setModel = (key2, value2) => {
|
|
466
|
-
|
|
467
|
-
newChangeRecords.push({ propPath: itemProp.value.replace(`${props.config.name}`, key2), value: value2 });
|
|
468
|
-
} else {
|
|
469
|
-
newChangeRecords.push({ propPath: itemProp.value, value: value2 });
|
|
470
|
-
}
|
|
488
|
+
newChangeRecords.push({ propPath: createObjectProp(itemProp.value, key2, props.config.name), value: value2 });
|
|
471
489
|
};
|
|
472
490
|
const setFormValue = (key2, value2) => {
|
|
473
491
|
newChangeRecords.push({ propPath: key2, value: value2 });
|
|
@@ -2629,15 +2647,22 @@ const _sfc_main$j = /* @__PURE__ */ defineComponent({
|
|
|
2629
2647
|
},
|
|
2630
2648
|
setup(__props) {
|
|
2631
2649
|
const props = __props;
|
|
2650
|
+
const mForm = inject("mForm");
|
|
2632
2651
|
if (props.config.initValue && props.model) {
|
|
2633
2652
|
props.model[props.name] = props.config.initValue;
|
|
2634
2653
|
}
|
|
2654
|
+
const text = computed(() => {
|
|
2655
|
+
if (props.config.displayText) {
|
|
2656
|
+
return filterFunction(mForm, props.config.displayText, props);
|
|
2657
|
+
}
|
|
2658
|
+
return props.model[props.name];
|
|
2659
|
+
});
|
|
2635
2660
|
useAddField(props.prop);
|
|
2636
2661
|
return (_ctx, _cache) => {
|
|
2637
2662
|
return __props.model ? (openBlock(), createElementBlock(
|
|
2638
2663
|
"span",
|
|
2639
2664
|
_hoisted_1$8,
|
|
2640
|
-
toDisplayString(
|
|
2665
|
+
toDisplayString(text.value),
|
|
2641
2666
|
1
|
|
2642
2667
|
/* TEXT */
|
|
2643
2668
|
)) : createCommentVNode("v-if", true);
|
|
@@ -5873,4 +5898,4 @@ const index = {
|
|
|
5873
5898
|
}
|
|
5874
5899
|
};
|
|
5875
5900
|
|
|
5876
|
-
export { _sfc_main$q as MCascader, _sfc_main$p as MCheckbox, _sfc_main$o as MCheckboxGroup, _sfc_main$n as MColorPicker, _sfc_main$A as MContainer, _sfc_main$m as MDate, _sfc_main$k as MDateTime, _sfc_main$l as MDaterange, _sfc_main$j as MDisplay, _sfc_main$i as MDynamicField, _sfc_main$z as MFieldset, _sfc_main$y as MFlexLayout, _sfc_main$g as MForm, _sfc_main as MFormBox, _sfc_main$f as MFormDialog, _sfc_main$1 as MFormDrawer, _sfc_main$w as MGroupList, _sfc_main$h as MHidden, _sfc_main$e as MLink, _sfc_main$d as MNumber, _sfc_main$c as MNumberRange, _sfc_main$v as MPanel, _sfc_main$b as MRadioGroup, _sfc_main$t as MRow, _sfc_main$a as MSelect, _sfc_main$9 as MSwitch, _sfc_main$2 as MTable, _sfc_main$r as MTabs, _sfc_main$8 as MText, _sfc_main$7 as MTextarea, _sfc_main$6 as MTime, _sfc_main$5 as MTimerange, createForm, createValues, datetimeFormatter, index as default, display, filterFunction, getDataByPage, getRules, initValue, sortArray, sortChange, useAddField };
|
|
5901
|
+
export { _sfc_main$q as MCascader, _sfc_main$p as MCheckbox, _sfc_main$o as MCheckboxGroup, _sfc_main$n as MColorPicker, _sfc_main$A as MContainer, _sfc_main$m as MDate, _sfc_main$k as MDateTime, _sfc_main$l as MDaterange, _sfc_main$j as MDisplay, _sfc_main$i as MDynamicField, _sfc_main$z as MFieldset, _sfc_main$y as MFlexLayout, _sfc_main$g as MForm, _sfc_main as MFormBox, _sfc_main$f as MFormDialog, _sfc_main$1 as MFormDrawer, _sfc_main$w as MGroupList, _sfc_main$h as MHidden, _sfc_main$e as MLink, _sfc_main$d as MNumber, _sfc_main$c as MNumberRange, _sfc_main$v as MPanel, _sfc_main$b as MRadioGroup, _sfc_main$t as MRow, _sfc_main$a as MSelect, _sfc_main$9 as MSwitch, _sfc_main$2 as MTable, _sfc_main$r as MTabs, _sfc_main$8 as MText, _sfc_main$7 as MTextarea, _sfc_main$6 as MTime, _sfc_main$5 as MTimerange, createForm, createObjectProp, createValues, datetimeFormatter, index as default, display, filterFunction, getDataByPage, getRules, initValue, sortArray, sortChange, useAddField };
|
package/dist/tmagic-form.umd.cjs
CHANGED
|
@@ -1695,7 +1695,7 @@
|
|
|
1695
1695
|
var Promise$1 = getNative(root, 'Promise');
|
|
1696
1696
|
|
|
1697
1697
|
/* Built-in method references that are verified to be native. */
|
|
1698
|
-
var Set = getNative(root, 'Set');
|
|
1698
|
+
var Set$1 = getNative(root, 'Set');
|
|
1699
1699
|
|
|
1700
1700
|
/** `Object#toString` result references. */
|
|
1701
1701
|
var mapTag$5 = '[object Map]',
|
|
@@ -1710,7 +1710,7 @@
|
|
|
1710
1710
|
var dataViewCtorString = toSource(DataView),
|
|
1711
1711
|
mapCtorString = toSource(Map$1),
|
|
1712
1712
|
promiseCtorString = toSource(Promise$1),
|
|
1713
|
-
setCtorString = toSource(Set),
|
|
1713
|
+
setCtorString = toSource(Set$1),
|
|
1714
1714
|
weakMapCtorString = toSource(WeakMap);
|
|
1715
1715
|
|
|
1716
1716
|
/**
|
|
@@ -1726,7 +1726,7 @@
|
|
|
1726
1726
|
if ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag$3) ||
|
|
1727
1727
|
(Map$1 && getTag(new Map$1) != mapTag$5) ||
|
|
1728
1728
|
(Promise$1 && getTag(Promise$1.resolve()) != promiseTag) ||
|
|
1729
|
-
(Set && getTag(new Set) != setTag$5) ||
|
|
1729
|
+
(Set$1 && getTag(new Set$1) != setTag$5) ||
|
|
1730
1730
|
(WeakMap && getTag(new WeakMap) != weakMapTag$1)) {
|
|
1731
1731
|
getTag = function(value) {
|
|
1732
1732
|
var result = baseGetTag(value),
|
|
@@ -2939,14 +2939,23 @@
|
|
|
2939
2939
|
return baseIsEqual(value, other);
|
|
2940
2940
|
}
|
|
2941
2941
|
|
|
2942
|
-
const
|
|
2942
|
+
const TABLE_SELECT_TYPES = /* @__PURE__ */ new Set(["table-select", "tableSelect"]);
|
|
2943
|
+
const isTableSelect = (type) => typeof type === "string" && TABLE_SELECT_TYPES.has(type);
|
|
2943
2944
|
const asyncLoadConfig = (value, initValue2, { asyncLoad, name, type }) => {
|
|
2944
2945
|
if (type === "html" && typeof asyncLoad === "object" && typeof name !== "undefined") {
|
|
2945
2946
|
asyncLoad.name = name;
|
|
2946
2947
|
value.asyncLoad = typeof initValue2.asyncLoad === "object" ? initValue2.asyncLoad : asyncLoad;
|
|
2947
2948
|
}
|
|
2948
2949
|
};
|
|
2949
|
-
const
|
|
2950
|
+
const MULTIPLE_VALUE_TYPES = /* @__PURE__ */ new Set([
|
|
2951
|
+
"checkbox-group",
|
|
2952
|
+
"checkboxGroup",
|
|
2953
|
+
"table",
|
|
2954
|
+
"cascader",
|
|
2955
|
+
"group-list",
|
|
2956
|
+
"groupList"
|
|
2957
|
+
]);
|
|
2958
|
+
const isMultipleValue = (type) => typeof type === "string" && MULTIPLE_VALUE_TYPES.has(type);
|
|
2950
2959
|
const initItemsValue = (mForm, value, initValue2, { items, name, extensible }) => {
|
|
2951
2960
|
if (Array.isArray(initValue2[name])) {
|
|
2952
2961
|
value[name] = initValue2[name].map((v, index) => createValues(mForm, items, v, value[name]?.[index]));
|
|
@@ -2980,22 +2989,18 @@
|
|
|
2980
2989
|
const { names } = item;
|
|
2981
2990
|
const { type, name } = item;
|
|
2982
2991
|
if (isTableSelect(type) && name) {
|
|
2983
|
-
value[name] = initValue2[name]
|
|
2992
|
+
value[name] = initValue2[name] ?? "";
|
|
2984
2993
|
return value;
|
|
2985
2994
|
}
|
|
2986
2995
|
asyncLoadConfig(value, initValue2, item);
|
|
2987
2996
|
if (name && !items && typeof initValue2?.[name] !== "undefined") {
|
|
2988
2997
|
if (typeof value[name] === "undefined") {
|
|
2989
|
-
|
|
2990
|
-
value[name] = Number(initValue2[name]);
|
|
2991
|
-
} else {
|
|
2992
|
-
value[name] = typeof initValue2[name] === "object" ? initValue2[name] : initValue2[name];
|
|
2993
|
-
}
|
|
2998
|
+
value[name] = type === "number" ? Number(initValue2[name]) : initValue2[name];
|
|
2994
2999
|
}
|
|
2995
3000
|
return value;
|
|
2996
3001
|
}
|
|
2997
3002
|
if (names) {
|
|
2998
|
-
return names.forEach((n) => value[n] = initValue2[n]
|
|
3003
|
+
return names.forEach((n) => value[n] = initValue2[n] ?? "");
|
|
2999
3004
|
}
|
|
3000
3005
|
if (!name) {
|
|
3001
3006
|
return createValues(mForm, items, initValue2, value);
|
|
@@ -3021,7 +3026,7 @@
|
|
|
3021
3026
|
}
|
|
3022
3027
|
return value;
|
|
3023
3028
|
};
|
|
3024
|
-
const getDefaultValue = function(mForm, { defaultValue, type, filter, multiple }) {
|
|
3029
|
+
const getDefaultValue = function(mForm, { defaultValue, type, filter, multiple, names }) {
|
|
3025
3030
|
if (typeof defaultValue === "function") {
|
|
3026
3031
|
return defaultValue(mForm);
|
|
3027
3032
|
}
|
|
@@ -3040,15 +3045,18 @@
|
|
|
3040
3045
|
if (multiple || type === "number-range") {
|
|
3041
3046
|
return [];
|
|
3042
3047
|
}
|
|
3048
|
+
if (type === "daterange" && !names) {
|
|
3049
|
+
return [];
|
|
3050
|
+
}
|
|
3043
3051
|
return "";
|
|
3044
3052
|
};
|
|
3045
3053
|
const filterFunction = (mForm, config, props) => {
|
|
3046
3054
|
if (typeof config === "function") {
|
|
3047
3055
|
return config(mForm, {
|
|
3048
|
-
values: mForm?.initValues || {},
|
|
3049
|
-
model: props.model,
|
|
3050
|
-
parent: mForm?.parentValues || {},
|
|
3051
|
-
formValue: mForm?.values || props.model,
|
|
3056
|
+
values: vue.readonly(mForm?.initValues || {}),
|
|
3057
|
+
model: vue.readonly(props.model),
|
|
3058
|
+
parent: vue.readonly(mForm?.parentValues || {}),
|
|
3059
|
+
formValue: vue.readonly(mForm?.values || props.model),
|
|
3052
3060
|
prop: props.prop,
|
|
3053
3061
|
config: props.config,
|
|
3054
3062
|
index: props.index,
|
|
@@ -3117,7 +3125,7 @@
|
|
|
3117
3125
|
let time;
|
|
3118
3126
|
if (["x", "timestamp"].includes(format)) {
|
|
3119
3127
|
time = dayjs(Number.isNaN(Number(v)) ? v : Number(v)).valueOf();
|
|
3120
|
-
} else if (typeof v === "string" && v.includes("Z") || v
|
|
3128
|
+
} else if (typeof v === "string" && v.includes("Z") || v instanceof Date) {
|
|
3121
3129
|
dayjs.extend(utc);
|
|
3122
3130
|
time = dayjs(v).utcOffset(8).format(format);
|
|
3123
3131
|
} else {
|
|
@@ -3130,9 +3138,10 @@
|
|
|
3130
3138
|
}
|
|
3131
3139
|
return defaultValue;
|
|
3132
3140
|
};
|
|
3133
|
-
const getDataByPage = (data = [], pagecontext, pagesize) =>
|
|
3134
|
-
|
|
3135
|
-
|
|
3141
|
+
const getDataByPage = (data = [], pagecontext, pagesize) => {
|
|
3142
|
+
const start = pagecontext * pagesize;
|
|
3143
|
+
return data.slice(start, start + pagesize);
|
|
3144
|
+
};
|
|
3136
3145
|
const sortArray = (data, newIndex, oldIndex, sortKey) => {
|
|
3137
3146
|
if (newIndex === oldIndex) {
|
|
3138
3147
|
return data;
|
|
@@ -3140,7 +3149,8 @@
|
|
|
3140
3149
|
if (newIndex < 0 || newIndex >= data.length || oldIndex < 0 || oldIndex >= data.length) {
|
|
3141
3150
|
return data;
|
|
3142
3151
|
}
|
|
3143
|
-
const
|
|
3152
|
+
const item = data[oldIndex];
|
|
3153
|
+
const newData = data.toSpliced(oldIndex, 1).toSpliced(newIndex, 0, item);
|
|
3144
3154
|
if (sortKey) {
|
|
3145
3155
|
for (let i = newData.length - 1, v = 0; i >= 0; i--, v++) {
|
|
3146
3156
|
newData[v][sortKey] = i;
|
|
@@ -3150,10 +3160,22 @@
|
|
|
3150
3160
|
};
|
|
3151
3161
|
const sortChange = (data, { prop, order }) => {
|
|
3152
3162
|
if (order === "ascending") {
|
|
3153
|
-
data
|
|
3163
|
+
data.sort((a, b) => a[prop] - b[prop]);
|
|
3154
3164
|
} else if (order === "descending") {
|
|
3155
|
-
data
|
|
3165
|
+
data.sort((a, b) => b[prop] - a[prop]);
|
|
3166
|
+
}
|
|
3167
|
+
};
|
|
3168
|
+
const createObjectProp = (prop, key, name) => {
|
|
3169
|
+
if (prop === "") {
|
|
3170
|
+
return key;
|
|
3171
|
+
}
|
|
3172
|
+
const itemPath = `${prop}`.split(".");
|
|
3173
|
+
if (name) {
|
|
3174
|
+
if (`${itemPath[itemPath.length - 1]}` === `${name}`) {
|
|
3175
|
+
return `${[...itemPath.slice(0, -1), key].join(".")}`;
|
|
3176
|
+
}
|
|
3156
3177
|
}
|
|
3178
|
+
return `${[...itemPath, key].join(".")}`;
|
|
3157
3179
|
};
|
|
3158
3180
|
|
|
3159
3181
|
const _hoisted_1$e = {
|
|
@@ -3393,11 +3415,7 @@
|
|
|
3393
3415
|
value = filterHandler(filter, v);
|
|
3394
3416
|
if (typeof onChange === "function") {
|
|
3395
3417
|
const setModel = (key2, value2) => {
|
|
3396
|
-
|
|
3397
|
-
newChangeRecords.push({ propPath: itemProp.value.replace(`${props.config.name}`, key2), value: value2 });
|
|
3398
|
-
} else {
|
|
3399
|
-
newChangeRecords.push({ propPath: itemProp.value, value: value2 });
|
|
3400
|
-
}
|
|
3418
|
+
newChangeRecords.push({ propPath: createObjectProp(itemProp.value, key2, props.config.name), value: value2 });
|
|
3401
3419
|
};
|
|
3402
3420
|
const setFormValue = (key2, value2) => {
|
|
3403
3421
|
newChangeRecords.push({ propPath: key2, value: value2 });
|
|
@@ -5559,15 +5577,22 @@
|
|
|
5559
5577
|
},
|
|
5560
5578
|
setup(__props) {
|
|
5561
5579
|
const props = __props;
|
|
5580
|
+
const mForm = vue.inject("mForm");
|
|
5562
5581
|
if (props.config.initValue && props.model) {
|
|
5563
5582
|
props.model[props.name] = props.config.initValue;
|
|
5564
5583
|
}
|
|
5584
|
+
const text = vue.computed(() => {
|
|
5585
|
+
if (props.config.displayText) {
|
|
5586
|
+
return filterFunction(mForm, props.config.displayText, props);
|
|
5587
|
+
}
|
|
5588
|
+
return props.model[props.name];
|
|
5589
|
+
});
|
|
5565
5590
|
useAddField(props.prop);
|
|
5566
5591
|
return (_ctx, _cache) => {
|
|
5567
5592
|
return __props.model ? (vue.openBlock(), vue.createElementBlock(
|
|
5568
5593
|
"span",
|
|
5569
5594
|
_hoisted_1$8,
|
|
5570
|
-
vue.toDisplayString(
|
|
5595
|
+
vue.toDisplayString(text.value),
|
|
5571
5596
|
1
|
|
5572
5597
|
/* TEXT */
|
|
5573
5598
|
)) : vue.createCommentVNode("v-if", true);
|
|
@@ -8836,6 +8861,7 @@
|
|
|
8836
8861
|
exports.MTime = _sfc_main$6;
|
|
8837
8862
|
exports.MTimerange = _sfc_main$5;
|
|
8838
8863
|
exports.createForm = createForm;
|
|
8864
|
+
exports.createObjectProp = createObjectProp;
|
|
8839
8865
|
exports.createValues = createValues;
|
|
8840
8866
|
exports.datetimeFormatter = datetimeFormatter;
|
|
8841
8867
|
exports.default = index;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.7.
|
|
2
|
+
"version": "1.7.6",
|
|
3
3
|
"name": "@tmagic/form",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": [
|
|
@@ -51,9 +51,9 @@
|
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"vue": "^3.5.24",
|
|
53
53
|
"typescript": "^5.9.3",
|
|
54
|
-
"@tmagic/
|
|
55
|
-
"@tmagic/form-schema": "1.7.
|
|
56
|
-
"@tmagic/
|
|
54
|
+
"@tmagic/utils": "1.7.6",
|
|
55
|
+
"@tmagic/form-schema": "1.7.6",
|
|
56
|
+
"@tmagic/design": "1.7.6"
|
|
57
57
|
},
|
|
58
58
|
"peerDependenciesMeta": {
|
|
59
59
|
"typescript": {
|
|
@@ -180,7 +180,7 @@ import type {
|
|
|
180
180
|
FormValue,
|
|
181
181
|
ToolTipConfigType,
|
|
182
182
|
} from '../schema';
|
|
183
|
-
import { display as displayFunction, filterFunction, getRules } from '../utils/form';
|
|
183
|
+
import { createObjectProp, display as displayFunction, filterFunction, getRules } from '../utils/form';
|
|
184
184
|
|
|
185
185
|
import FormLabel from './FormLabel.vue';
|
|
186
186
|
|
|
@@ -416,11 +416,7 @@ const onChangeHandler = async function (v: any, eventData: ContainerChangeEventD
|
|
|
416
416
|
|
|
417
417
|
if (typeof onChange === 'function') {
|
|
418
418
|
const setModel = (key: string, value: any) => {
|
|
419
|
-
|
|
420
|
-
newChangeRecords.push({ propPath: itemProp.value.replace(`${props.config.name}`, key), value });
|
|
421
|
-
} else {
|
|
422
|
-
newChangeRecords.push({ propPath: itemProp.value, value });
|
|
423
|
-
}
|
|
419
|
+
newChangeRecords.push({ propPath: createObjectProp(itemProp.value, key, props.config.name), value });
|
|
424
420
|
};
|
|
425
421
|
|
|
426
422
|
const setFormValue = (key: string, value: any) => {
|
package/src/fields/Display.vue
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<span v-if="model">{{
|
|
2
|
+
<span v-if="model">{{ text }}</span>
|
|
3
3
|
</template>
|
|
4
4
|
|
|
5
5
|
<script setup lang="ts">
|
|
6
|
-
import
|
|
6
|
+
import { computed, inject } from 'vue';
|
|
7
|
+
|
|
8
|
+
import type { DisplayConfig, FieldProps, FormState } from '../schema';
|
|
9
|
+
import { filterFunction } from '../utils/form';
|
|
7
10
|
import { useAddField } from '../utils/useAddField';
|
|
8
11
|
|
|
9
12
|
defineOptions({
|
|
@@ -12,9 +15,19 @@ defineOptions({
|
|
|
12
15
|
|
|
13
16
|
const props = defineProps<FieldProps<DisplayConfig>>();
|
|
14
17
|
|
|
18
|
+
const mForm = inject<FormState | undefined>('mForm');
|
|
19
|
+
|
|
15
20
|
if (props.config.initValue && props.model) {
|
|
16
21
|
props.model[props.name] = props.config.initValue;
|
|
17
22
|
}
|
|
18
23
|
|
|
24
|
+
const text = computed(() => {
|
|
25
|
+
if (props.config.displayText) {
|
|
26
|
+
return filterFunction<string>(mForm, props.config.displayText, props);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return props.model[props.name];
|
|
30
|
+
});
|
|
31
|
+
|
|
19
32
|
useAddField(props.prop);
|
|
20
33
|
</script>
|
package/src/utils/form.ts
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
import { readonly } from 'vue';
|
|
19
20
|
import dayjs from 'dayjs';
|
|
20
21
|
import utc from 'dayjs/plugin/utc';
|
|
21
22
|
import { cloneDeep } from 'lodash-es';
|
|
@@ -42,10 +43,11 @@ interface DefaultItem {
|
|
|
42
43
|
type: string;
|
|
43
44
|
filter: string;
|
|
44
45
|
multiple: boolean;
|
|
46
|
+
names?: string[];
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
+
const TABLE_SELECT_TYPES = new Set(['table-select', 'tableSelect']);
|
|
50
|
+
const isTableSelect = (type?: string | TypeFunction) => typeof type === 'string' && TABLE_SELECT_TYPES.has(type);
|
|
49
51
|
|
|
50
52
|
const asyncLoadConfig = (value: FormValue, initValue: FormValue, { asyncLoad, name, type }: HtmlField) => {
|
|
51
53
|
// 富文本配置了异步加载
|
|
@@ -55,9 +57,15 @@ const asyncLoadConfig = (value: FormValue, initValue: FormValue, { asyncLoad, na
|
|
|
55
57
|
}
|
|
56
58
|
};
|
|
57
59
|
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
const MULTIPLE_VALUE_TYPES = new Set([
|
|
61
|
+
'checkbox-group',
|
|
62
|
+
'checkboxGroup',
|
|
63
|
+
'table',
|
|
64
|
+
'cascader',
|
|
65
|
+
'group-list',
|
|
66
|
+
'groupList',
|
|
67
|
+
]);
|
|
68
|
+
const isMultipleValue = (type?: string | TypeFunction) => typeof type === 'string' && MULTIPLE_VALUE_TYPES.has(type);
|
|
61
69
|
|
|
62
70
|
const initItemsValue = (
|
|
63
71
|
mForm: FormState | undefined,
|
|
@@ -113,7 +121,7 @@ const initValueItem = function (
|
|
|
113
121
|
const { type, name } = item as ChildConfig;
|
|
114
122
|
|
|
115
123
|
if (isTableSelect(type) && name) {
|
|
116
|
-
value[name] = initValue[name]
|
|
124
|
+
value[name] = initValue[name] ?? '';
|
|
117
125
|
return value;
|
|
118
126
|
}
|
|
119
127
|
|
|
@@ -122,18 +130,14 @@ const initValueItem = function (
|
|
|
122
130
|
// 这种情况比较多,提前结束
|
|
123
131
|
if (name && !items && typeof initValue?.[name] !== 'undefined') {
|
|
124
132
|
if (typeof value[name] === 'undefined') {
|
|
125
|
-
|
|
126
|
-
value[name] = Number(initValue[name]);
|
|
127
|
-
} else {
|
|
128
|
-
value[name] = typeof initValue[name] === 'object' ? initValue[name] : initValue[name];
|
|
129
|
-
}
|
|
133
|
+
value[name] = type === 'number' ? Number(initValue[name]) : initValue[name];
|
|
130
134
|
}
|
|
131
135
|
|
|
132
136
|
return value;
|
|
133
137
|
}
|
|
134
138
|
|
|
135
139
|
if (names) {
|
|
136
|
-
return names.forEach((n: string) => (value[n] = initValue[n]
|
|
140
|
+
return names.forEach((n: string) => (value[n] = initValue[n] ?? ''));
|
|
137
141
|
}
|
|
138
142
|
|
|
139
143
|
if (!name) {
|
|
@@ -173,7 +177,10 @@ export const createValues = function (
|
|
|
173
177
|
return value;
|
|
174
178
|
};
|
|
175
179
|
|
|
176
|
-
const getDefaultValue = function (
|
|
180
|
+
const getDefaultValue = function (
|
|
181
|
+
mForm: FormState | undefined,
|
|
182
|
+
{ defaultValue, type, filter, multiple, names }: DefaultItem,
|
|
183
|
+
) {
|
|
177
184
|
if (typeof defaultValue === 'function') {
|
|
178
185
|
return defaultValue(mForm);
|
|
179
186
|
}
|
|
@@ -199,6 +206,10 @@ const getDefaultValue = function (mForm: FormState | undefined, { defaultValue,
|
|
|
199
206
|
return [];
|
|
200
207
|
}
|
|
201
208
|
|
|
209
|
+
if (type === 'daterange' && !names) {
|
|
210
|
+
return [];
|
|
211
|
+
}
|
|
212
|
+
|
|
202
213
|
return '';
|
|
203
214
|
};
|
|
204
215
|
|
|
@@ -209,10 +220,10 @@ export const filterFunction = <T = any>(
|
|
|
209
220
|
) => {
|
|
210
221
|
if (typeof config === 'function') {
|
|
211
222
|
return (config as FilterFunction<T>)(mForm, {
|
|
212
|
-
values: mForm?.initValues || {},
|
|
213
|
-
model: props.model,
|
|
214
|
-
parent: mForm?.parentValues || {},
|
|
215
|
-
formValue: mForm?.values || props.model,
|
|
223
|
+
values: readonly(mForm?.initValues || {}),
|
|
224
|
+
model: readonly(props.model),
|
|
225
|
+
parent: readonly(mForm?.parentValues || {}),
|
|
226
|
+
formValue: readonly(mForm?.values || props.model),
|
|
216
227
|
prop: props.prop,
|
|
217
228
|
config: props.config,
|
|
218
229
|
index: props.index,
|
|
@@ -304,7 +315,8 @@ export const datetimeFormatter = (
|
|
|
304
315
|
let time: string | number;
|
|
305
316
|
if (['x', 'timestamp'].includes(format)) {
|
|
306
317
|
time = dayjs(Number.isNaN(Number(v)) ? v : Number(v)).valueOf();
|
|
307
|
-
} else if ((typeof v === 'string' && v.includes('Z')) || v
|
|
318
|
+
} else if ((typeof v === 'string' && v.includes('Z')) || v instanceof Date) {
|
|
319
|
+
// dayjs.extend 内部有防重复机制 (plugin.$i),无需额外判断
|
|
308
320
|
dayjs.extend(utc);
|
|
309
321
|
// UTC字符串时间或Date对象格式化为北京时间
|
|
310
322
|
time = dayjs(v).utcOffset(8).format(format);
|
|
@@ -320,10 +332,10 @@ export const datetimeFormatter = (
|
|
|
320
332
|
return defaultValue;
|
|
321
333
|
};
|
|
322
334
|
|
|
323
|
-
export const getDataByPage = (data: any[] = [], pagecontext: number, pagesize: number) =>
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
335
|
+
export const getDataByPage = (data: any[] = [], pagecontext: number, pagesize: number) => {
|
|
336
|
+
const start = pagecontext * pagesize;
|
|
337
|
+
return data.slice(start, start + pagesize);
|
|
338
|
+
};
|
|
327
339
|
|
|
328
340
|
export const sortArray = (data: any[], newIndex: number, oldIndex: number, sortKey?: string) => {
|
|
329
341
|
if (newIndex === oldIndex) {
|
|
@@ -334,7 +346,9 @@ export const sortArray = (data: any[], newIndex: number, oldIndex: number, sortK
|
|
|
334
346
|
return data;
|
|
335
347
|
}
|
|
336
348
|
|
|
337
|
-
|
|
349
|
+
// 先取出要移动的元素,再使用 toSpliced 避免修改原数组
|
|
350
|
+
const item = data[oldIndex];
|
|
351
|
+
const newData = data.toSpliced(oldIndex, 1).toSpliced(newIndex, 0, item);
|
|
338
352
|
|
|
339
353
|
if (sortKey) {
|
|
340
354
|
for (let i = newData.length - 1, v = 0; i >= 0; i--, v++) {
|
|
@@ -347,8 +361,23 @@ export const sortArray = (data: any[], newIndex: number, oldIndex: number, sortK
|
|
|
347
361
|
|
|
348
362
|
export const sortChange = (data: any[], { prop, order }: SortProp) => {
|
|
349
363
|
if (order === 'ascending') {
|
|
350
|
-
data
|
|
364
|
+
data.sort((a: any, b: any) => a[prop] - b[prop]);
|
|
351
365
|
} else if (order === 'descending') {
|
|
352
|
-
data
|
|
366
|
+
data.sort((a: any, b: any) => b[prop] - a[prop]);
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
export const createObjectProp = (prop: string, key: string, name?: string | number) => {
|
|
371
|
+
if (prop === '') {
|
|
372
|
+
return key;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const itemPath = `${prop}`.split('.');
|
|
376
|
+
|
|
377
|
+
if (name) {
|
|
378
|
+
if (`${itemPath[itemPath.length - 1]}` === `${name}`) {
|
|
379
|
+
return `${[...itemPath.slice(0, -1), key].join('.')}`;
|
|
380
|
+
}
|
|
353
381
|
}
|
|
382
|
+
return `${[...itemPath, key].join('.')}`;
|
|
354
383
|
};
|
package/types/index.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ declare const datetimeFormatter: (v: string | Date, defaultValue?: string, forma
|
|
|
31
31
|
declare const getDataByPage: (data: any[] | undefined, pagecontext: number, pagesize: number) => any[];
|
|
32
32
|
declare const sortArray: (data: any[], newIndex: number, oldIndex: number, sortKey?: string) => any[];
|
|
33
33
|
declare const sortChange: (data: any[], { prop, order }: SortProp) => void;
|
|
34
|
+
declare const createObjectProp: (prop: string, key: string, name?: string | number) => string;
|
|
34
35
|
|
|
35
36
|
declare const useAddField: (prop?: string) => void;
|
|
36
37
|
|
|
@@ -1330,5 +1331,5 @@ declare const _default: {
|
|
|
1330
1331
|
install(app: App, opt?: FormInstallOptions): void;
|
|
1331
1332
|
};
|
|
1332
1333
|
|
|
1333
|
-
export { _default$2 as MCascader, _default$c as MCheckbox, _default$7 as MCheckboxGroup, _default$8 as MColorPicker, _default$s as MContainer, _default$f as MDate, _default$e as MDateTime, _default$a as MDaterange, _default$5 as MDisplay, _default$1 as MDynamicField, _default$r as MFieldset, _default$q as MFlexLayout, _default$w as MForm, _default$t as MFormBox, _default$v as MFormDialog, _default$u as MFormDrawer, _default$l as MGroupList, _default$g as MHidden, _default$4 as MLink, _default$j as MNumber, _default$i as MNumberRange, _default$p as MPanel, _default$6 as MRadioGroup, _default$o as MRow, _default$3 as MSelect, _default$b as MSwitch, _default$m as MTable, _default$n as MTabs, _default$k as MText, _default$h as MTextarea, _default$d as MTime, _default$9 as MTimerange, createForm, createValues, datetimeFormatter, _default as default, display, filterFunction, getDataByPage, getRules, initValue, sortArray, sortChange, useAddField };
|
|
1334
|
+
export { _default$2 as MCascader, _default$c as MCheckbox, _default$7 as MCheckboxGroup, _default$8 as MColorPicker, _default$s as MContainer, _default$f as MDate, _default$e as MDateTime, _default$a as MDaterange, _default$5 as MDisplay, _default$1 as MDynamicField, _default$r as MFieldset, _default$q as MFlexLayout, _default$w as MForm, _default$t as MFormBox, _default$v as MFormDialog, _default$u as MFormDrawer, _default$l as MGroupList, _default$g as MHidden, _default$4 as MLink, _default$j as MNumber, _default$i as MNumberRange, _default$p as MPanel, _default$6 as MRadioGroup, _default$o as MRow, _default$3 as MSelect, _default$b as MSwitch, _default$m as MTable, _default$n as MTabs, _default$k as MText, _default$h as MTextarea, _default$d as MTime, _default$9 as MTimerange, createForm, createObjectProp, createValues, datetimeFormatter, _default as default, display, filterFunction, getDataByPage, getRules, initValue, sortArray, sortChange, useAddField };
|
|
1334
1335
|
export type { ChangeRecord, ContainerChangeEventData, FormInstallOptions, ValidateError };
|