@tmagic/form 1.2.0-beta.2 → 1.2.0-beta.3
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.mjs +2081 -2500
- package/dist/tmagic-form.mjs.map +1 -1
- package/dist/tmagic-form.umd.js +2104 -2524
- package/dist/tmagic-form.umd.js.map +1 -1
- package/package.json +3 -4
- package/src/Form.vue +116 -159
- package/src/FormDialog.vue +104 -126
- package/src/containers/Col.vue +22 -38
- package/src/containers/Container.vue +135 -171
- package/src/containers/Fieldset.vue +11 -7
- package/src/containers/GroupList.vue +80 -112
- package/src/containers/GroupListItem.vue +87 -124
- package/src/containers/Panel.vue +26 -49
- package/src/containers/Row.vue +18 -40
- package/src/containers/Step.vue +36 -48
- package/src/containers/Table.vue +437 -478
- package/src/containers/Tabs.vue +89 -118
- package/src/fields/Cascader.vue +88 -136
- package/src/fields/Checkbox.vue +46 -50
- package/src/fields/CheckboxGroup.vue +39 -35
- package/src/fields/ColorPicker.vue +4 -2
- package/src/fields/Date.vue +20 -26
- package/src/fields/DateTime.vue +31 -39
- package/src/fields/Daterange.vue +3 -2
- package/src/fields/DynamicField.vue +63 -77
- package/src/fields/Link.vue +65 -86
- package/src/fields/Number.vue +32 -34
- package/src/fields/RadioGroup.vue +23 -23
- package/src/fields/Select.vue +294 -310
- package/src/fields/SelectOptionGroups.vue +10 -5
- package/src/fields/SelectOptions.vue +4 -2
- package/src/fields/Switch.vue +46 -49
- package/src/fields/Text.vue +99 -107
- package/src/fields/Textarea.vue +32 -44
- package/src/fields/Time.vue +19 -30
- package/src/index.ts +22 -23
- package/src/schema.ts +45 -12
- package/types/Form.vue.d.ts +213 -111
- package/types/FormDialog.vue.d.ts +812 -155
- package/types/containers/Col.vue.d.ts +94 -40
- package/types/containers/Container.vue.d.ts +124 -65
- package/types/containers/GroupList.vue.d.ts +88 -53
- package/types/containers/GroupListItem.vue.d.ts +99 -67
- package/types/containers/Panel.vue.d.ts +92 -39
- package/types/containers/Row.vue.d.ts +94 -39
- package/types/containers/Step.vue.d.ts +104 -42
- package/types/containers/Table.vue.d.ts +157 -130
- package/types/containers/Tabs.vue.d.ts +95 -50
- package/types/fields/Cascader.vue.d.ts +97 -71
- package/types/fields/Checkbox.vue.d.ts +97 -56
- package/types/fields/CheckboxGroup.vue.d.ts +97 -53
- package/types/fields/Date.vue.d.ts +97 -54
- package/types/fields/DateTime.vue.d.ts +97 -54
- package/types/fields/DynamicField.vue.d.ts +97 -64
- package/types/fields/Link.vue.d.ts +62 -657
- package/types/fields/Number.vue.d.ts +101 -56
- package/types/fields/RadioGroup.vue.d.ts +98 -52
- package/types/fields/Select.vue.d.ts +99 -65
- package/types/fields/Switch.vue.d.ts +97 -56
- package/types/fields/Text.vue.d.ts +100 -57
- package/types/fields/Textarea.vue.d.ts +102 -60
- package/types/fields/Time.vue.d.ts +97 -55
- package/types/index.d.ts +0 -1
- package/types/schema.d.ts +37 -9
- package/src/utils/fieldProps.ts +0 -39
- package/types/utils/fieldProps.d.ts +0 -21
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
></component>
|
|
31
31
|
|
|
32
32
|
<template v-else-if="type && display">
|
|
33
|
-
<
|
|
33
|
+
<TMagicFormItem
|
|
34
34
|
:style="config.tip ? 'flex: 1' : ''"
|
|
35
35
|
:class="{ hidden: `${itemLabelWidth}` === '0' || !config.text }"
|
|
36
36
|
:prop="itemProp"
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
:rules="rule"
|
|
39
39
|
>
|
|
40
40
|
<template #label><span v-html="type === 'checkbox' ? '' : config.text"></span></template>
|
|
41
|
-
<
|
|
41
|
+
<TMagicTooltip v-if="tooltip">
|
|
42
42
|
<component
|
|
43
43
|
:key="key(config)"
|
|
44
44
|
:size="size"
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
<template #content>
|
|
54
54
|
<div v-html="tooltip"></div>
|
|
55
55
|
</template>
|
|
56
|
-
</
|
|
56
|
+
</TMagicTooltip>
|
|
57
57
|
|
|
58
58
|
<component
|
|
59
59
|
v-else
|
|
@@ -69,19 +69,19 @@
|
|
|
69
69
|
></component>
|
|
70
70
|
|
|
71
71
|
<div v-if="extra" v-html="extra" class="m-form-tip"></div>
|
|
72
|
-
</
|
|
72
|
+
</TMagicFormItem>
|
|
73
73
|
|
|
74
|
-
<
|
|
75
|
-
<
|
|
74
|
+
<TMagicTooltip v-if="config.tip" placement="left">
|
|
75
|
+
<TMagicIcon style="line-height: 40px; margin-left: 5px"><warning-filled /></TMagicIcon>
|
|
76
76
|
<template #content>
|
|
77
77
|
<div v-html="config.tip"></div>
|
|
78
78
|
</template>
|
|
79
|
-
</
|
|
79
|
+
</TMagicTooltip>
|
|
80
80
|
</template>
|
|
81
81
|
|
|
82
82
|
<template v-else-if="items && display">
|
|
83
83
|
<template v-if="name || name === 0 ? model[name] : model">
|
|
84
|
-
<
|
|
84
|
+
<Container
|
|
85
85
|
v-for="item in items"
|
|
86
86
|
:key="key(item)"
|
|
87
87
|
:model="name || name === 0 ? model[name] : model"
|
|
@@ -92,202 +92,166 @@
|
|
|
92
92
|
:label-width="itemLabelWidth"
|
|
93
93
|
:prop="itemProp"
|
|
94
94
|
@change="onChangeHandler"
|
|
95
|
-
></
|
|
95
|
+
></Container>
|
|
96
96
|
</template>
|
|
97
97
|
</template>
|
|
98
98
|
|
|
99
99
|
<div style="text-align: center" v-if="config.expand && type !== 'fieldset'">
|
|
100
|
-
<
|
|
100
|
+
<TMagicButton text @click="expandHandler">{{ expand ? '收起配置' : '展开更多配置' }}</TMagicButton>
|
|
101
101
|
</div>
|
|
102
102
|
</div>
|
|
103
103
|
</template>
|
|
104
104
|
|
|
105
|
-
<script lang="ts">
|
|
106
|
-
import { computed,
|
|
105
|
+
<script setup lang="ts">
|
|
106
|
+
import { computed, inject, ref, resolveComponent, watchEffect } from 'vue';
|
|
107
107
|
import { WarningFilled } from '@element-plus/icons-vue';
|
|
108
108
|
|
|
109
|
+
import { TMagicButton, TMagicFormItem, TMagicIcon, TMagicTooltip } from '@tmagic/design';
|
|
110
|
+
|
|
109
111
|
import { ChildConfig, ContainerCommonConfig, FormState, FormValue } from '../schema';
|
|
110
112
|
import { display as displayFunction, filterFunction, getRules } from '../utils/form';
|
|
111
113
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
114
|
+
const props = withDefaults(
|
|
115
|
+
defineProps<{
|
|
116
|
+
model: FormValue;
|
|
117
|
+
config: ChildConfig;
|
|
118
|
+
prop?: string;
|
|
119
|
+
labelWidth?: string;
|
|
120
|
+
expandMore?: boolean;
|
|
121
|
+
stepActive?: string | number;
|
|
122
|
+
size?: string;
|
|
123
|
+
}>(),
|
|
124
|
+
{
|
|
125
|
+
prop: '',
|
|
126
|
+
size: 'small',
|
|
127
|
+
expandMore: false,
|
|
128
|
+
},
|
|
129
|
+
);
|
|
120
130
|
|
|
121
|
-
|
|
122
|
-
type: [Object, Array] as PropType<FormValue>,
|
|
123
|
-
required: true,
|
|
124
|
-
},
|
|
131
|
+
const emit = defineEmits(['change']);
|
|
125
132
|
|
|
126
|
-
|
|
127
|
-
type: Object as PropType<ChildConfig>,
|
|
128
|
-
required: true,
|
|
129
|
-
},
|
|
133
|
+
const mForm = inject<FormState | undefined>('mForm');
|
|
130
134
|
|
|
131
|
-
|
|
132
|
-
type: String,
|
|
133
|
-
default: () => '',
|
|
134
|
-
},
|
|
135
|
+
const expand = ref(false);
|
|
135
136
|
|
|
136
|
-
|
|
137
|
-
type: [String, Number],
|
|
138
|
-
},
|
|
137
|
+
const name = computed(() => props.config.name || '');
|
|
139
138
|
|
|
140
|
-
|
|
141
|
-
type: String,
|
|
142
|
-
default: 'small',
|
|
143
|
-
},
|
|
144
|
-
},
|
|
139
|
+
const items = computed(() => (props.config as ContainerCommonConfig).items);
|
|
145
140
|
|
|
146
|
-
|
|
141
|
+
const itemProp = computed(() => {
|
|
142
|
+
let n: string | number = '';
|
|
143
|
+
const { names } = props.config as any;
|
|
144
|
+
if (names?.[0]) {
|
|
145
|
+
[n] = names;
|
|
146
|
+
} else if (name.value) {
|
|
147
|
+
n = name.value;
|
|
148
|
+
} else {
|
|
149
|
+
return props.prop;
|
|
150
|
+
}
|
|
151
|
+
return `${props.prop}${props.prop ? '.' : ''}${n}`;
|
|
152
|
+
});
|
|
147
153
|
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
const tagName = computed(() => {
|
|
155
|
+
const component = resolveComponent(`m-${items.value ? 'form' : 'fields'}-${type.value}`);
|
|
156
|
+
if (typeof component !== 'string') return component;
|
|
157
|
+
return 'm-fields-text';
|
|
158
|
+
});
|
|
150
159
|
|
|
151
|
-
|
|
160
|
+
const disabled = computed(() => filterFunction(mForm, props.config.disabled, props));
|
|
152
161
|
|
|
153
|
-
|
|
162
|
+
const tooltip = computed(() => filterFunction(mForm, props.config.tooltip, props));
|
|
154
163
|
|
|
155
|
-
|
|
164
|
+
const extra = computed(() => filterFunction(mForm, props.config.extra, props));
|
|
156
165
|
|
|
157
|
-
|
|
158
|
-
let n: string | number = '';
|
|
159
|
-
const { names } = props.config as any;
|
|
160
|
-
if (names?.[0]) {
|
|
161
|
-
[n] = names;
|
|
162
|
-
} else if (name.value) {
|
|
163
|
-
n = name.value;
|
|
164
|
-
} else {
|
|
165
|
-
return props.prop;
|
|
166
|
-
}
|
|
167
|
-
return `${props.prop}${props.prop ? '.' : ''}${n}`;
|
|
168
|
-
});
|
|
166
|
+
const rule = computed(() => getRules(mForm, props.config.rules, props));
|
|
169
167
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
168
|
+
const type = computed((): string => {
|
|
169
|
+
let { type } = props.config;
|
|
170
|
+
if (typeof type === 'function') {
|
|
171
|
+
type = type(mForm, {
|
|
172
|
+
model: props.model,
|
|
174
173
|
});
|
|
174
|
+
}
|
|
175
|
+
if (type === 'form') return '';
|
|
176
|
+
return type?.replace(/([A-Z])/g, '-$1').toLowerCase() || (items.value ? '' : 'text');
|
|
177
|
+
});
|
|
175
178
|
|
|
176
|
-
|
|
179
|
+
const display = computed((): boolean => {
|
|
180
|
+
if (props.config.display === 'expand') {
|
|
181
|
+
return expand.value;
|
|
182
|
+
}
|
|
177
183
|
|
|
178
|
-
|
|
184
|
+
return displayFunction(mForm, props.config.display, props);
|
|
185
|
+
});
|
|
179
186
|
|
|
180
|
-
|
|
187
|
+
const itemLabelWidth = computed(() => props.config.labelWidth || props.labelWidth);
|
|
181
188
|
|
|
182
|
-
|
|
189
|
+
watchEffect(() => {
|
|
190
|
+
expand.value = props.expandMore;
|
|
191
|
+
});
|
|
183
192
|
|
|
184
|
-
|
|
185
|
-
let { type } = props.config;
|
|
186
|
-
if (typeof type === 'function') {
|
|
187
|
-
type = type(mForm, {
|
|
188
|
-
model: props.model,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
if (type === 'form') return '';
|
|
192
|
-
return type?.replace(/([A-Z])/g, '-$1').toLowerCase() || (items.value ? '' : 'text');
|
|
193
|
-
});
|
|
193
|
+
const expandHandler = () => (expand.value = !expand.value);
|
|
194
194
|
|
|
195
|
-
|
|
196
|
-
if (props.config.display === 'expand') {
|
|
197
|
-
return expand.value;
|
|
198
|
-
}
|
|
195
|
+
const key = (config: any) => config[mForm?.keyProps];
|
|
199
196
|
|
|
200
|
-
|
|
197
|
+
const filterHandler = (filter: any, value: FormValue | number | string) => {
|
|
198
|
+
if (typeof filter === 'function') {
|
|
199
|
+
return filter(mForm, value, {
|
|
200
|
+
model: props.model,
|
|
201
|
+
values: mForm?.initValues,
|
|
202
|
+
formValue: mForm?.values,
|
|
203
|
+
prop: itemProp.value,
|
|
204
|
+
config: props.config,
|
|
201
205
|
});
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (filter === 'number') {
|
|
209
|
+
return +value;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return value;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const changeHandler = (onChange: any, value: FormValue | number | string) => {
|
|
216
|
+
if (typeof onChange === 'function') {
|
|
217
|
+
return onChange(mForm, value, {
|
|
218
|
+
model: props.model,
|
|
219
|
+
values: mForm?.initValues,
|
|
220
|
+
formValue: mForm?.values,
|
|
221
|
+
prop: itemProp.value,
|
|
222
|
+
config: props.config,
|
|
207
223
|
});
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
const trimHandler = (trim: any, value: FormValue | number | string) => {
|
|
244
|
-
if (typeof value === 'string' && trim) {
|
|
245
|
-
return value.replace(/^\s*/, '').replace(/\s*$/, '');
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
const onChangeHandler = async function (v: FormValue, key?: string) {
|
|
250
|
-
const { filter, onChange, trim, name, dynamicKey } = props.config as any;
|
|
251
|
-
let value: FormValue | number | string = v;
|
|
252
|
-
|
|
253
|
-
try {
|
|
254
|
-
value = filterHandler(filter, v);
|
|
255
|
-
value = (await changeHandler(onChange, value)) ?? value;
|
|
256
|
-
value = trimHandler(trim, value) ?? value;
|
|
257
|
-
} catch (e) {
|
|
258
|
-
console.error(e);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// field内容下包含field-link时,model===value, 这里避免循环引用
|
|
262
|
-
if ((name || name === 0) && props.model !== value && (v !== value || props.model[name] !== value)) {
|
|
263
|
-
// eslint-disable-next-line vue/no-mutating-props
|
|
264
|
-
props.model[name] = value;
|
|
265
|
-
}
|
|
266
|
-
// 动态表单类型,根据value和key参数,直接修改model
|
|
267
|
-
if (key !== undefined && dynamicKey) {
|
|
268
|
-
// eslint-disable-next-line vue/no-mutating-props
|
|
269
|
-
props.model[key] = value;
|
|
270
|
-
}
|
|
271
|
-
emit('change', props.model);
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
return {
|
|
275
|
-
expand,
|
|
276
|
-
name,
|
|
277
|
-
type,
|
|
278
|
-
disabled,
|
|
279
|
-
itemProp,
|
|
280
|
-
items,
|
|
281
|
-
display,
|
|
282
|
-
itemLabelWidth,
|
|
283
|
-
tagName,
|
|
284
|
-
rule,
|
|
285
|
-
tooltip,
|
|
286
|
-
extra,
|
|
287
|
-
key,
|
|
288
|
-
onChangeHandler,
|
|
289
|
-
expandHandler,
|
|
290
|
-
};
|
|
291
|
-
},
|
|
292
|
-
});
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const trimHandler = (trim: any, value: FormValue | number | string) => {
|
|
228
|
+
if (typeof value === 'string' && trim) {
|
|
229
|
+
return value.replace(/^\s*/, '').replace(/\s*$/, '');
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const onChangeHandler = async function (v: FormValue, key?: string) {
|
|
234
|
+
const { filter, onChange, trim, name, dynamicKey } = props.config as any;
|
|
235
|
+
let value: FormValue | number | string = v;
|
|
236
|
+
|
|
237
|
+
try {
|
|
238
|
+
value = filterHandler(filter, v);
|
|
239
|
+
value = (await changeHandler(onChange, value)) ?? value;
|
|
240
|
+
value = trimHandler(trim, value) ?? value;
|
|
241
|
+
} catch (e) {
|
|
242
|
+
console.error(e);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// field内容下包含field-link时,model===value, 这里避免循环引用
|
|
246
|
+
if ((name || name === 0) && props.model !== value && (v !== value || props.model[name] !== value)) {
|
|
247
|
+
// eslint-disable-next-line vue/no-mutating-props
|
|
248
|
+
props.model[name] = value;
|
|
249
|
+
}
|
|
250
|
+
// 动态表单类型,根据value和key参数,直接修改model
|
|
251
|
+
if (key !== undefined && dynamicKey) {
|
|
252
|
+
// eslint-disable-next-line vue/no-mutating-props
|
|
253
|
+
props.model[key] = value;
|
|
254
|
+
}
|
|
255
|
+
emit('change', props.model);
|
|
256
|
+
};
|
|
293
257
|
</script>
|
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
:style="show ? 'padding: 15px 15px 0 5px;' : 'border: 0'"
|
|
6
6
|
>
|
|
7
7
|
<component v-if="name && config.checkbox" :is="!show ? 'div' : 'legend'">
|
|
8
|
-
<
|
|
8
|
+
<TMagicCheckbox
|
|
9
9
|
v-model="model[name].value"
|
|
10
10
|
:prop="`${prop}${prop ? '.' : ''}${config.name}.value`"
|
|
11
11
|
:true-label="1"
|
|
12
12
|
:false-label="0"
|
|
13
13
|
><span v-html="config.legend"></span><span v-if="config.extra" v-html="config.extra" class="m-form-tip"></span
|
|
14
|
-
></
|
|
14
|
+
></TMagicCheckbox>
|
|
15
15
|
</component>
|
|
16
16
|
<legend v-else>
|
|
17
17
|
<span v-html="config.legend"></span>
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
<div v-if="config.schematic && show" style="display: flex">
|
|
22
22
|
<div style="flex: 1">
|
|
23
|
-
<
|
|
23
|
+
<Container
|
|
24
24
|
v-for="(item, index) in config.items"
|
|
25
25
|
:key="key(item, index)"
|
|
26
26
|
:model="name ? model[name] : model"
|
|
@@ -30,14 +30,14 @@
|
|
|
30
30
|
:labelWidth="lWidth"
|
|
31
31
|
:size="size"
|
|
32
32
|
@change="change"
|
|
33
|
-
></
|
|
33
|
+
></Container>
|
|
34
34
|
</div>
|
|
35
35
|
|
|
36
36
|
<img class="m-form-schematic" :src="config.schematic" />
|
|
37
37
|
</div>
|
|
38
38
|
|
|
39
39
|
<template v-else-if="show">
|
|
40
|
-
<
|
|
40
|
+
<Container
|
|
41
41
|
v-for="(item, index) in config.items"
|
|
42
42
|
:key="key(item, index)"
|
|
43
43
|
:model="name ? model[name] : model"
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
:labelWidth="lWidth"
|
|
48
48
|
:size="size"
|
|
49
49
|
@change="change"
|
|
50
|
-
></
|
|
50
|
+
></Container>
|
|
51
51
|
</template>
|
|
52
52
|
</fieldset>
|
|
53
53
|
</template>
|
|
@@ -55,8 +55,12 @@
|
|
|
55
55
|
<script lang="ts" setup>
|
|
56
56
|
import { computed, inject, watch } from 'vue';
|
|
57
57
|
|
|
58
|
+
import { TMagicCheckbox } from '@tmagic/design';
|
|
59
|
+
|
|
58
60
|
import { FieldsetConfig, FormState } from '../schema';
|
|
59
61
|
|
|
62
|
+
import Container from './Container.vue';
|
|
63
|
+
|
|
60
64
|
const props = withDefaults(
|
|
61
65
|
defineProps<{
|
|
62
66
|
labelWidth?: string;
|
|
@@ -89,7 +93,7 @@ const lWidth = computed(() => {
|
|
|
89
93
|
if (props.config.items) {
|
|
90
94
|
return props.config.labelWidth || props.labelWidth;
|
|
91
95
|
}
|
|
92
|
-
return props.config.labelWidth || props.labelWidth || (props.config.text ?
|
|
96
|
+
return props.config.labelWidth || props.labelWidth || (props.config.text ? undefined : '0');
|
|
93
97
|
});
|
|
94
98
|
|
|
95
99
|
const change = () => {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<span class="el-table__empty-text">暂无数据</span>
|
|
6
6
|
</div>
|
|
7
7
|
|
|
8
|
-
<
|
|
8
|
+
<MFieldsGroupListItem
|
|
9
9
|
v-else
|
|
10
10
|
v-for="(item, index) in model[name]"
|
|
11
11
|
:key="index"
|
|
@@ -19,138 +19,106 @@
|
|
|
19
19
|
@remove-item="removeHandler"
|
|
20
20
|
@swap-item="swapHandler"
|
|
21
21
|
@change="changeHandler"
|
|
22
|
-
></
|
|
22
|
+
></MFieldsGroupListItem>
|
|
23
23
|
|
|
24
|
-
<
|
|
24
|
+
<TMagicButton @click="addHandler" size="small" v-if="addable">添加组</TMagicButton>
|
|
25
25
|
|
|
26
|
-
<
|
|
26
|
+
<TMagicButton :icon="Grid" size="small" @click="toggleMode" v-if="config.enableToggleMode">切换为表格</TMagicButton>
|
|
27
27
|
</div>
|
|
28
28
|
</template>
|
|
29
29
|
|
|
30
|
-
<script lang="ts">
|
|
31
|
-
import { computed,
|
|
30
|
+
<script setup lang="ts">
|
|
31
|
+
import { computed, inject } from 'vue';
|
|
32
32
|
import { Grid } from '@element-plus/icons-vue';
|
|
33
33
|
|
|
34
|
+
import { TMagicButton } from '@tmagic/design';
|
|
35
|
+
|
|
34
36
|
import { FormState, GroupListConfig } from '../schema';
|
|
35
37
|
import { initValue } from '../utils/form';
|
|
36
38
|
|
|
37
39
|
import MFieldsGroupListItem from './GroupListItem.vue';
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
const props = defineProps<{
|
|
42
|
+
model: any;
|
|
43
|
+
config: GroupListConfig;
|
|
44
|
+
name: string;
|
|
45
|
+
labelWidth?: string;
|
|
46
|
+
prop?: string;
|
|
47
|
+
size?: string;
|
|
48
|
+
}>();
|
|
43
49
|
|
|
44
|
-
|
|
45
|
-
labelWidth: String,
|
|
50
|
+
const emit = defineEmits(['change']);
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
type: Object,
|
|
49
|
-
default: () => ({}),
|
|
50
|
-
},
|
|
52
|
+
const mForm = inject<FormState | undefined>('mForm');
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
default: () => ({}),
|
|
55
|
-
},
|
|
54
|
+
const addable = computed(() => {
|
|
55
|
+
if (!props.name) return false;
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
if (typeof props.config.addable === 'function') {
|
|
58
|
+
return props.config.addable(mForm, {
|
|
59
|
+
model: props.model[props.name],
|
|
60
|
+
formValue: mForm?.values,
|
|
61
|
+
prop: props.prop,
|
|
62
|
+
config: props.config,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
63
65
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
default: '',
|
|
67
|
-
},
|
|
68
|
-
},
|
|
66
|
+
return typeof props.config.addable === 'undefined' ? true : props.config.addable;
|
|
67
|
+
});
|
|
69
68
|
|
|
70
|
-
|
|
69
|
+
const changeHandler = () => {
|
|
70
|
+
if (!props.name) return false;
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
emit('change', props.model[props.name]);
|
|
73
|
+
};
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
const addHandler = async () => {
|
|
76
|
+
if (!props.name) return false;
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
return props.config.addable(mForm, {
|
|
80
|
-
model: props.model[props.name],
|
|
81
|
-
formValue: mForm?.values,
|
|
82
|
-
prop: props.prop,
|
|
83
|
-
config: props.config,
|
|
84
|
-
});
|
|
85
|
-
}
|
|
78
|
+
let initValues = {};
|
|
86
79
|
|
|
87
|
-
|
|
80
|
+
if (typeof props.config.defaultAdd === 'function') {
|
|
81
|
+
initValues = await props.config.defaultAdd(mForm, {
|
|
82
|
+
model: props.model[props.name],
|
|
83
|
+
formValue: mForm?.values,
|
|
84
|
+
prop: props.prop,
|
|
85
|
+
config: props.config,
|
|
88
86
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const swapHandler = (idx1: number, idx2: number) => {
|
|
128
|
-
if (!props.name) return false;
|
|
129
|
-
|
|
130
|
-
const [currRow] = props.model[props.name].splice(idx1, 1);
|
|
131
|
-
props.model[props.name].splice(idx2, 0, currRow);
|
|
132
|
-
changeHandler();
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const toggleMode = () => {
|
|
136
|
-
props.config.type = 'table';
|
|
137
|
-
props.config.groupItems = props.config.items;
|
|
138
|
-
props.config.items = (props.config.tableItems ||
|
|
139
|
-
props.config.items.map((item: any) => ({
|
|
140
|
-
...item,
|
|
141
|
-
label: item.label || item.text,
|
|
142
|
-
text: null,
|
|
143
|
-
}))) as any;
|
|
144
|
-
};
|
|
145
|
-
return {
|
|
146
|
-
Grid,
|
|
147
|
-
addable,
|
|
148
|
-
toggleMode,
|
|
149
|
-
removeHandler,
|
|
150
|
-
swapHandler,
|
|
151
|
-
changeHandler,
|
|
152
|
-
addHandler,
|
|
153
|
-
};
|
|
154
|
-
},
|
|
155
|
-
});
|
|
87
|
+
} else if (props.config.defaultAdd) {
|
|
88
|
+
initValues = props.config.defaultAdd;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const groupValue = await initValue(mForm, {
|
|
92
|
+
config: props.config.items,
|
|
93
|
+
initValues,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
props.model[props.name].push(groupValue);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const removeHandler = (index: number) => {
|
|
100
|
+
if (!props.name) return false;
|
|
101
|
+
|
|
102
|
+
props.model[props.name].splice(index, 1);
|
|
103
|
+
changeHandler();
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const swapHandler = (idx1: number, idx2: number) => {
|
|
107
|
+
if (!props.name) return false;
|
|
108
|
+
|
|
109
|
+
const [currRow] = props.model[props.name].splice(idx1, 1);
|
|
110
|
+
props.model[props.name].splice(idx2, 0, currRow);
|
|
111
|
+
changeHandler();
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const toggleMode = () => {
|
|
115
|
+
props.config.type = 'table';
|
|
116
|
+
props.config.groupItems = props.config.items;
|
|
117
|
+
props.config.items = (props.config.tableItems ||
|
|
118
|
+
props.config.items.map((item: any) => ({
|
|
119
|
+
...item,
|
|
120
|
+
label: item.label || item.text,
|
|
121
|
+
text: null,
|
|
122
|
+
}))) as any;
|
|
123
|
+
};
|
|
156
124
|
</script>
|