@ebiz/designer-components 0.0.19 → 0.0.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ebiz/designer-components",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -22,7 +22,7 @@
22
22
  "@opentiny/vue-icon": "~3.20.0",
23
23
  "axios": "^1.8.1",
24
24
  "echarts": "^5.6.0",
25
- "tdesign-vue-next": "^1.11.5",
25
+ "tdesign-vue-next": "^1.12.0",
26
26
  "unplugin-auto-import": "^19.0.0",
27
27
  "unplugin-vue-components": "^28.0.0",
28
28
  "vue": "^3.5.13",
@@ -115,7 +115,7 @@ const click = async (e) => {
115
115
  emit('loading', true)
116
116
 
117
117
  // 调用前置处理并获取返回值
118
- const shouldContinue = props.onPrepare()
118
+ const shouldContinue = await Promise.resolve(props.onPrepare())
119
119
  emit('prepare', shouldContinue)
120
120
 
121
121
  // 只有当返回值明确为false时才中断,undefined或其他值都继续执行
@@ -1,6 +1,7 @@
1
1
  <template>
2
2
  <div>
3
- <slot name="content" :row="data"></slot>
3
+ <slot></slot>
4
+ <slot v-if="$slots.content" name="content" :row="data"></slot>
4
5
  </div>
5
6
  </template>
6
7
 
@@ -0,0 +1,597 @@
1
+ <template>
2
+ <div class="ebiz-auto-form">
3
+ <t-form
4
+ ref="formRef"
5
+ :data="formData"
6
+ :rules="formRules"
7
+ :label-width="labelWidth"
8
+ :label-align="labelAlign"
9
+ :layout="layout"
10
+ :colon="colon"
11
+ :show-error-message="showErrorMessage"
12
+ :reset-type="resetType"
13
+ @reset="handleReset"
14
+ @submit="handleSubmit"
15
+ @validate="handleValidate"
16
+ >
17
+ <t-form-item
18
+ v-for="field in formFields"
19
+ :key="field.name"
20
+ :name="field.name"
21
+ :label="field.label"
22
+ :rules="field.rules"
23
+ >
24
+ <!-- 文本输入框 -->
25
+ <t-input
26
+ v-if="field.componentType === 'input'"
27
+ v-model="formData[field.name]"
28
+ :placeholder="field.placeholder || `请输入${field.label}`"
29
+ :type="getInputType(field.fieldType)"
30
+ />
31
+
32
+ <!-- 长文本 -->
33
+ <t-textarea
34
+ v-else-if="field.componentType === 'textarea'"
35
+ v-model="formData[field.name]"
36
+ :placeholder="field.placeholder || `请输入${field.label}`"
37
+ />
38
+
39
+ <!-- 单选框组 -->
40
+ <t-radio-group
41
+ v-else-if="field.componentType === 'radio'"
42
+ v-model="formData[field.name]"
43
+ >
44
+ <t-radio
45
+ v-for="option in field.options"
46
+ :key="option.value"
47
+ :value="option.value"
48
+ >
49
+ {{ option.label }}
50
+ </t-radio>
51
+ </t-radio-group>
52
+
53
+ <!-- 多选框组 -->
54
+ <t-checkbox-group
55
+ v-else-if="field.componentType === 'checkbox'"
56
+ v-model="formData[field.name]"
57
+ >
58
+ <t-checkbox
59
+ v-for="option in field.options"
60
+ :key="option.value"
61
+ :value="option.value"
62
+ >
63
+ {{ option.label }}
64
+ </t-checkbox>
65
+ </t-checkbox-group>
66
+
67
+ <!-- 日期选择器 -->
68
+ <t-date-picker
69
+ v-else-if="field.componentType === 'date'"
70
+ v-model="formData[field.name]"
71
+ :placeholder="field.placeholder || `请选择${field.label}`"
72
+ :format="field.format || 'YYYY-MM-DD'"
73
+ />
74
+
75
+ <!-- 时间选择器 -->
76
+ <t-time-picker
77
+ v-else-if="field.componentType === 'time'"
78
+ v-model="formData[field.name]"
79
+ :placeholder="field.placeholder || `请选择${field.label}`"
80
+ :format="field.format || 'HH:mm:ss'"
81
+ />
82
+
83
+ <!-- 日期时间选择器 -->
84
+ <t-date-picker
85
+ v-else-if="field.componentType === 'datetime'"
86
+ v-model="formData[field.name]"
87
+ :placeholder="field.placeholder || `请选择${field.label}`"
88
+ :format="field.format || 'YYYY-MM-DD HH:mm:ss'"
89
+ enable-time-picker
90
+ />
91
+
92
+ <!-- 下拉选择器 -->
93
+ <t-select
94
+ v-else-if="field.componentType === 'select'"
95
+ v-model="formData[field.name]"
96
+ :placeholder="field.placeholder || `请选择${field.label}`"
97
+ :multiple="field.multiple"
98
+ :options="field.options"
99
+ />
100
+
101
+ <!-- 文件上传 -->
102
+ <t-upload
103
+ v-else-if="field.componentType === 'upload'"
104
+ v-model="formData[field.name]"
105
+ :placeholder="field.placeholder || `请上传${field.label}`"
106
+ :accept="field.accept"
107
+ :theme="field.theme || 'file'"
108
+ :multiple="field.multiple"
109
+ :draggable="field.draggable"
110
+ :action="uploadAction"
111
+ @success="handleUploadSuccess($event, field)"
112
+ />
113
+
114
+ <!-- 开关 -->
115
+ <t-switch
116
+ v-else-if="field.componentType === 'switch'"
117
+ v-model="formData[field.name]"
118
+ />
119
+
120
+ <!-- 数字输入框 -->
121
+ <t-input-number
122
+ v-else-if="field.componentType === 'number'"
123
+ v-model="formData[field.name]"
124
+ :placeholder="field.placeholder || `请输入${field.label}`"
125
+ :step="field.step || 1"
126
+ :min="field.min"
127
+ :max="field.max"
128
+ />
129
+
130
+ <!-- 默认为文本输入框 -->
131
+ <t-input
132
+ v-else
133
+ v-model="formData[field.name]"
134
+ :placeholder="field.placeholder || `请输入${field.label}`"
135
+ />
136
+ </t-form-item>
137
+
138
+ <!-- 子表单处理 -->
139
+ <div v-for="subEntity in subEntities" :key="subEntity.name" class="ebiz-auto-form-sub-table">
140
+ <h3>{{ subEntity.label || subEntity.name }}</h3>
141
+
142
+ <t-table
143
+ :data="formData[subEntity.name] || []"
144
+ :columns="subEntity.columns"
145
+ row-key="id"
146
+ size="small"
147
+ bordered
148
+ >
149
+ <template #operation="{ row }">
150
+ <t-space>
151
+ <t-button size="small" variant="text" @click="handleEditSubRow(subEntity.name, row)">编辑</t-button>
152
+ <t-button size="small" variant="text" theme="danger" @click="handleDeleteSubRow(subEntity.name, row)">删除</t-button>
153
+ </t-space>
154
+ </template>
155
+ </t-table>
156
+
157
+ <t-button style="margin-top: 8px;" theme="primary" variant="outline" size="small" @click="handleAddSubRow(subEntity.name)">
158
+ 添加{{ subEntity.label || subEntity.name }}
159
+ </t-button>
160
+ </div>
161
+
162
+ <div class="ebiz-auto-form-footer" v-if="showSubmitButton || showResetButton">
163
+ <t-space>
164
+ <t-button v-if="showResetButton" @click="handleReset">{{ resetButtonText }}</t-button>
165
+ <t-button v-if="showSubmitButton" theme="primary" type="submit" @click="handleSubmit">{{ submitButtonText }}</t-button>
166
+ </t-space>
167
+ </div>
168
+ </t-form>
169
+
170
+ <!-- 子表单编辑对话框 -->
171
+ <t-dialog
172
+ v-model:visible="subFormDialogVisible"
173
+ :header="subFormDialogTitle"
174
+ width="650px"
175
+ attach="body"
176
+ @confirm="handleSubFormConfirm"
177
+ @close="handleSubFormCancel"
178
+ >
179
+ <template v-if="currentSubForm">
180
+ <t-form
181
+ ref="subFormRef"
182
+ :data="subFormData"
183
+ >
184
+ <t-form-item
185
+ v-for="field in currentSubForm.fields"
186
+ :key="field.name"
187
+ :name="field.name"
188
+ :label="field.label"
189
+ >
190
+ <t-input
191
+ v-if="!field.componentType || field.componentType === 'input'"
192
+ v-model="subFormData[field.name]"
193
+ :placeholder="field.placeholder || `请输入${field.label}`"
194
+ />
195
+
196
+ <!-- 可以根据需要添加更多类型的组件 -->
197
+ </t-form-item>
198
+ </t-form>
199
+ </template>
200
+ </t-dialog>
201
+ </div>
202
+ </template>
203
+
204
+ <script>
205
+ export default {
206
+ name: "EbizAutoForm"
207
+ }
208
+ </script>
209
+
210
+ <script setup>
211
+ import { ref, reactive, computed, onMounted, watch } from 'vue';
212
+ import { Form as TForm, FormItem as TFormItem, Button as TButton, Input as TInput,
213
+ Textarea as TTextarea, RadioGroup as TRadioGroup, Radio as TRadio,
214
+ CheckboxGroup as TCheckboxGroup, Checkbox as TCheckbox, DatePicker as TDatePicker,
215
+ TimePicker as TTimePicker, Select as TSelect, Upload as TUpload, Switch as TSwitch,
216
+ InputNumber as TInputNumber, Space as TSpace, Table as TTable, Dialog as TDialog } from 'tdesign-vue-next';
217
+ import { MessagePlugin } from 'tdesign-vue-next';
218
+
219
+ const props = defineProps({
220
+ apiConfig: {
221
+ type: Object,
222
+ required: true,
223
+ default: () => ({})
224
+ },
225
+ initialData: {
226
+ type: Object,
227
+ default: () => ({})
228
+ },
229
+ labelWidth: {
230
+ type: [String, Number],
231
+ default: '120px'
232
+ },
233
+ labelAlign: {
234
+ type: String,
235
+ default: 'right',
236
+ validator: (val) => ['left', 'right', 'top'].includes(val)
237
+ },
238
+ layout: {
239
+ type: String,
240
+ default: 'vertical',
241
+ validator: (val) => ['vertical', 'inline'].includes(val)
242
+ },
243
+ colon: {
244
+ type: Boolean,
245
+ default: true
246
+ },
247
+ showErrorMessage: {
248
+ type: Boolean,
249
+ default: true
250
+ },
251
+ resetType: {
252
+ type: String,
253
+ default: 'initial',
254
+ validator: (val) => ['empty', 'initial'].includes(val)
255
+ },
256
+ showSubmitButton: {
257
+ type: Boolean,
258
+ default: true
259
+ },
260
+ showResetButton: {
261
+ type: Boolean,
262
+ default: true
263
+ },
264
+ submitButtonText: {
265
+ type: String,
266
+ default: '提交'
267
+ },
268
+ resetButtonText: {
269
+ type: String,
270
+ default: '重置'
271
+ },
272
+ uploadAction: {
273
+ type: String,
274
+ default: '/api/file/upload'
275
+ }
276
+ });
277
+
278
+ const emit = defineEmits(['submit', 'reset', 'validate', 'change', 'field-change', 'upload-success']);
279
+
280
+ // 表单引用
281
+ const formRef = ref(null);
282
+ const formData = ref({...props.initialData});
283
+ const formFields = ref([]);
284
+ const formRules = ref({});
285
+ const subEntities = ref([]);
286
+
287
+ // 子表单相关
288
+ const subFormRef = ref(null);
289
+ const subFormDialogVisible = ref(false);
290
+ const subFormDialogTitle = ref('');
291
+ const subFormData = ref({});
292
+ const currentSubForm = ref(null);
293
+ const currentSubFormIndex = ref(-1);
294
+ const currentSubFormEntityName = ref('');
295
+
296
+ // 将字段类型映射到表单组件类型
297
+ const fieldTypeToComponentMap = {
298
+ 'TEXT': 'input',
299
+ 'LONG_TEXT': 'textarea',
300
+ 'RADIO': 'radio',
301
+ 'CHECKBOX': 'checkbox',
302
+ 'ID_NUMBER': 'input',
303
+ 'ADDRESS': 'textarea',
304
+ 'VIDEO': 'upload',
305
+ 'IMAGE': 'upload',
306
+ 'DATE': 'date',
307
+ 'TIME': 'time',
308
+ 'DATETIME': 'datetime',
309
+ 'TIMESTAMP': 'datetime',
310
+ 'CITY': 'select',
311
+ 'PHONE': 'input',
312
+ 'MOBILE': 'input',
313
+ 'EMAIL': 'input',
314
+ 'FILE': 'upload',
315
+ 'TAGS': 'select',
316
+ 'GEOGRAPHY': 'input',
317
+ 'HTML_TEXT': 'textarea',
318
+ 'INT': 'number',
319
+ 'NUMBER': 'number',
320
+ 'AMOUNT': 'number',
321
+ 'BOOLEAN': 'switch',
322
+ 'JSON': 'textarea',
323
+ 'FORMULA': 'input',
324
+ 'ENUM': 'select'
325
+ };
326
+
327
+ // 根据字段类型获取 input 的 type 属性
328
+ const getInputType = (fieldType) => {
329
+ switch (fieldType) {
330
+ case 'EMAIL':
331
+ return 'email';
332
+ case 'PHONE':
333
+ case 'MOBILE':
334
+ return 'tel';
335
+ case 'ID_NUMBER':
336
+ return 'text';
337
+ case 'PASSWORD':
338
+ return 'password';
339
+ default:
340
+ return 'text';
341
+ }
342
+ };
343
+
344
+ // 监听初始数据变化
345
+ watch(() => props.initialData, (newVal) => {
346
+ formData.value = {...newVal};
347
+ }, { deep: true });
348
+
349
+ // 监听 apiConfig 变化
350
+ watch(() => props.apiConfig, () => {
351
+ fetchFormConfig();
352
+ }, { deep: true });
353
+
354
+ // 初始化表单配置
355
+ onMounted(() => {
356
+ fetchFormConfig();
357
+ });
358
+
359
+ // 从 apiConfig 获取表单配置
360
+ const fetchFormConfig = async () => {
361
+ try {
362
+ if (!props.apiConfig || !props.apiConfig.apiId) {
363
+ console.error('apiConfig 或 apiId 未提供');
364
+ return;
365
+ }
366
+
367
+ // 这里实际项目中需要发送请求获取实体字段定义
368
+ // 以下是模拟从接口获取字段定义的代码
369
+ await processEntityFields(props.apiConfig);
370
+
371
+ } catch (error) {
372
+ console.error('获取表单配置失败:', error);
373
+ MessagePlugin.error('获取表单配置失败');
374
+ }
375
+ };
376
+
377
+ // 处理实体字段
378
+ const processEntityFields = async (apiConfig) => {
379
+ try {
380
+ // 实际项目中,这里应该是调用接口获取实体字段定义
381
+ // const response = await fetchEntityFields(apiConfig);
382
+ // const fields = response.data.fields;
383
+
384
+ // 模拟字段数据
385
+ const mockFields = [
386
+ { name: 'name', label: '姓名', fieldType: 'TEXT', required: true },
387
+ { name: 'age', label: '年龄', fieldType: 'INT', required: true },
388
+ { name: 'email', label: '邮箱', fieldType: 'EMAIL', required: false },
389
+ { name: 'gender', label: '性别', fieldType: 'RADIO', required: false, options: [
390
+ { label: '男', value: 'male' },
391
+ { label: '女', value: 'female' }
392
+ ]},
393
+ { name: 'hobbies', label: '爱好', fieldType: 'CHECKBOX', required: false, options: [
394
+ { label: '阅读', value: 'reading' },
395
+ { label: '运动', value: 'sports' },
396
+ { label: '旅游', value: 'travel' },
397
+ { label: '音乐', value: 'music' }
398
+ ]},
399
+ { name: 'birthday', label: '生日', fieldType: 'DATE', required: false },
400
+ { name: 'memo', label: '备注', fieldType: 'LONG_TEXT', required: false }
401
+ ];
402
+
403
+ // 模拟子实体数据
404
+ const mockSubEntities = [
405
+ {
406
+ name: 'contacts',
407
+ label: '联系人',
408
+ type: 'subEntity',
409
+ columns: [
410
+ { colKey: 'name', title: '姓名' },
411
+ { colKey: 'phone', title: '电话' },
412
+ { colKey: 'relationship', title: '关系' },
413
+ { colKey: 'operation', title: '操作' }
414
+ ],
415
+ fields: [
416
+ { name: 'name', label: '姓名', fieldType: 'TEXT', required: true },
417
+ { name: 'phone', label: '电话', fieldType: 'PHONE', required: true },
418
+ { name: 'relationship', label: '关系', fieldType: 'TEXT', required: false }
419
+ ]
420
+ }
421
+ ];
422
+
423
+ // 初始化表单数据
424
+ const initialFormData = {...props.initialData};
425
+ const fields = [];
426
+ const rules = {};
427
+
428
+ // 处理普通字段
429
+ mockFields.forEach(field => {
430
+ // 转换字段定义为表单字段
431
+ const formField = {
432
+ name: field.name,
433
+ label: field.label,
434
+ fieldType: field.fieldType,
435
+ componentType: fieldTypeToComponentMap[field.fieldType] || 'input',
436
+ placeholder: `请输入${field.label}`,
437
+ options: field.options || [],
438
+ required: field.required
439
+ };
440
+
441
+ // 添加验证规则
442
+ if (field.required) {
443
+ rules[field.name] = [{ required: true, message: `请输入${field.label}`, type: 'error' }];
444
+ }
445
+
446
+ // 设置默认值
447
+ if (!(field.name in initialFormData)) {
448
+ if (field.fieldType === 'CHECKBOX') {
449
+ initialFormData[field.name] = [];
450
+ } else if (field.fieldType === 'BOOLEAN') {
451
+ initialFormData[field.name] = false;
452
+ } else if (field.fieldType === 'INT' || field.fieldType === 'NUMBER' || field.fieldType === 'AMOUNT') {
453
+ initialFormData[field.name] = null;
454
+ } else {
455
+ initialFormData[field.name] = '';
456
+ }
457
+ }
458
+
459
+ fields.push(formField);
460
+ });
461
+
462
+ // 处理子实体
463
+ mockSubEntities.forEach(subEntity => {
464
+ if (!(subEntity.name in initialFormData)) {
465
+ initialFormData[subEntity.name] = [];
466
+ }
467
+ });
468
+
469
+ // 更新组件状态
470
+ formFields.value = fields;
471
+ formRules.value = rules;
472
+ formData.value = initialFormData;
473
+ subEntities.value = mockSubEntities;
474
+
475
+ } catch (error) {
476
+ console.error('处理实体字段失败:', error);
477
+ MessagePlugin.error('处理实体字段失败');
478
+ }
479
+ };
480
+
481
+ // 处理表单提交
482
+ const handleSubmit = (e) => {
483
+ e.preventDefault();
484
+ formRef.value.validate().then(result => {
485
+ if (result === true) {
486
+ emit('submit', formData.value);
487
+ }
488
+ });
489
+ };
490
+
491
+ // 处理表单重置
492
+ const handleReset = (e) => {
493
+ formRef.value.reset();
494
+ emit('reset');
495
+ };
496
+
497
+ // 处理表单验证
498
+ const handleValidate = (params) => {
499
+ emit('validate', params);
500
+ };
501
+
502
+ // 处理文件上传成功
503
+ const handleUploadSuccess = (info, field) => {
504
+ emit('upload-success', info, field);
505
+ };
506
+
507
+ // 子表单相关方法
508
+ const handleAddSubRow = (entityName) => {
509
+ const entity = subEntities.value.find(item => item.name === entityName);
510
+ if (!entity) return;
511
+
512
+ currentSubForm.value = entity;
513
+ currentSubFormEntityName.value = entityName;
514
+ currentSubFormIndex.value = -1;
515
+ subFormDialogTitle.value = `添加${entity.label || entity.name}`;
516
+ subFormData.value = {};
517
+ subFormDialogVisible.value = true;
518
+ };
519
+
520
+ const handleEditSubRow = (entityName, row) => {
521
+ const entity = subEntities.value.find(item => item.name === entityName);
522
+ if (!entity) return;
523
+
524
+ const index = formData.value[entityName].findIndex(item => item.id === row.id);
525
+ if (index === -1) return;
526
+
527
+ currentSubForm.value = entity;
528
+ currentSubFormEntityName.value = entityName;
529
+ currentSubFormIndex.value = index;
530
+ subFormDialogTitle.value = `编辑${entity.label || entity.name}`;
531
+ subFormData.value = {...formData.value[entityName][index]};
532
+ subFormDialogVisible.value = true;
533
+ };
534
+
535
+ const handleDeleteSubRow = (entityName, row) => {
536
+ const index = formData.value[entityName].findIndex(item => item.id === row.id);
537
+ if (index !== -1) {
538
+ formData.value[entityName].splice(index, 1);
539
+ emit('change', formData.value);
540
+ }
541
+ };
542
+
543
+ const handleSubFormConfirm = () => {
544
+ const entityName = currentSubFormEntityName.value;
545
+ if (!entityName) return;
546
+
547
+ // 确保数组初始化
548
+ if (!formData.value[entityName]) {
549
+ formData.value[entityName] = [];
550
+ }
551
+
552
+ // 添加或更新数据
553
+ if (currentSubFormIndex.value === -1) {
554
+ // 新增
555
+ const newRow = {...subFormData.value, id: Date.now()};
556
+ formData.value[entityName].push(newRow);
557
+ } else {
558
+ // 更新
559
+ formData.value[entityName][currentSubFormIndex.value] = {...subFormData.value};
560
+ }
561
+
562
+ subFormDialogVisible.value = false;
563
+ emit('change', formData.value);
564
+ };
565
+
566
+ const handleSubFormCancel = () => {
567
+ subFormDialogVisible.value = false;
568
+ };
569
+
570
+ // 暴露方法给父组件
571
+ defineExpose({
572
+ formRef,
573
+ validate: () => formRef.value?.validate(),
574
+ reset: () => formRef.value?.reset(),
575
+ submit: handleSubmit,
576
+ getFormData: () => formData.value,
577
+ setFormData: (data) => {
578
+ formData.value = {...data};
579
+ }
580
+ });
581
+ </script>
582
+
583
+ <style lang="less" scoped>
584
+ .ebiz-auto-form {
585
+ width: 100%;
586
+
587
+ &-sub-table {
588
+ margin-bottom: 24px;
589
+ }
590
+
591
+ &-footer {
592
+ margin-top: 24px;
593
+ display: flex;
594
+ justify-content: center;
595
+ }
596
+ }
597
+ </style>