@tmagic/form 1.7.4 → 1.7.5
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 +40 -23
- package/dist/tmagic-form.umd.cjs +42 -25
- package/package.json +4 -4
- package/src/fields/Display.vue +15 -2
- package/src/utils/form.ts +39 -25
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,9 +230,9 @@ 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]);
|
|
226
236
|
}
|
|
227
237
|
};
|
|
228
238
|
|
|
@@ -2629,15 +2639,22 @@ const _sfc_main$j = /* @__PURE__ */ defineComponent({
|
|
|
2629
2639
|
},
|
|
2630
2640
|
setup(__props) {
|
|
2631
2641
|
const props = __props;
|
|
2642
|
+
const mForm = inject("mForm");
|
|
2632
2643
|
if (props.config.initValue && props.model) {
|
|
2633
2644
|
props.model[props.name] = props.config.initValue;
|
|
2634
2645
|
}
|
|
2646
|
+
const text = computed(() => {
|
|
2647
|
+
if (props.config.displayText) {
|
|
2648
|
+
return filterFunction(mForm, props.config.displayText, props);
|
|
2649
|
+
}
|
|
2650
|
+
return props.model[props.name];
|
|
2651
|
+
});
|
|
2635
2652
|
useAddField(props.prop);
|
|
2636
2653
|
return (_ctx, _cache) => {
|
|
2637
2654
|
return __props.model ? (openBlock(), createElementBlock(
|
|
2638
2655
|
"span",
|
|
2639
2656
|
_hoisted_1$8,
|
|
2640
|
-
toDisplayString(
|
|
2657
|
+
toDisplayString(text.value),
|
|
2641
2658
|
1
|
|
2642
2659
|
/* TEXT */
|
|
2643
2660
|
)) : createCommentVNode("v-if", true);
|
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,9 +3160,9 @@
|
|
|
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]);
|
|
3156
3166
|
}
|
|
3157
3167
|
};
|
|
3158
3168
|
|
|
@@ -5559,15 +5569,22 @@
|
|
|
5559
5569
|
},
|
|
5560
5570
|
setup(__props) {
|
|
5561
5571
|
const props = __props;
|
|
5572
|
+
const mForm = vue.inject("mForm");
|
|
5562
5573
|
if (props.config.initValue && props.model) {
|
|
5563
5574
|
props.model[props.name] = props.config.initValue;
|
|
5564
5575
|
}
|
|
5576
|
+
const text = vue.computed(() => {
|
|
5577
|
+
if (props.config.displayText) {
|
|
5578
|
+
return filterFunction(mForm, props.config.displayText, props);
|
|
5579
|
+
}
|
|
5580
|
+
return props.model[props.name];
|
|
5581
|
+
});
|
|
5565
5582
|
useAddField(props.prop);
|
|
5566
5583
|
return (_ctx, _cache) => {
|
|
5567
5584
|
return __props.model ? (vue.openBlock(), vue.createElementBlock(
|
|
5568
5585
|
"span",
|
|
5569
5586
|
_hoisted_1$8,
|
|
5570
|
-
vue.toDisplayString(
|
|
5587
|
+
vue.toDisplayString(text.value),
|
|
5571
5588
|
1
|
|
5572
5589
|
/* TEXT */
|
|
5573
5590
|
)) : vue.createCommentVNode("v-if", true);
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.7.
|
|
2
|
+
"version": "1.7.5",
|
|
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/design": "1.7.
|
|
55
|
-
"@tmagic/form-schema": "1.7.
|
|
56
|
-
"@tmagic/utils": "1.7.
|
|
54
|
+
"@tmagic/design": "1.7.5",
|
|
55
|
+
"@tmagic/form-schema": "1.7.5",
|
|
56
|
+
"@tmagic/utils": "1.7.5"
|
|
57
57
|
},
|
|
58
58
|
"peerDependenciesMeta": {
|
|
59
59
|
"typescript": {
|
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,8 @@ 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]);
|
|
353
367
|
}
|
|
354
368
|
};
|