-
Notifications
You must be signed in to change notification settings - Fork 7
/
schema-form.vue
690 lines (682 loc) · 14.9 KB
/
schema-form.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<template>
<el-container class="schema-container">
<div class="schema-aside">
<div class="schema-aside-opt">
<el-switch class="mb-20" active-text="显示操作按钮" v-model="showActionButtonGroup" @change="onChange" />
<el-switch class="mb-20" active-text="显示重置按钮" v-model="showResetButton" @change="onChange" />
<el-switch class="mb-20" active-text="显示提交按钮" v-model="showSubmitButton" @change="onChange" />
<el-button class="mb-20" @click="setProps({ submitButtonOptions: { loading: true, text: '加载中' }})">修改提交按钮</el-button>
<el-button class="mb-20" @click="setProps({ resetButtonOptions: { text: '取消' }})">修改重置按钮</el-button>
<el-button class="mb-20" @click="setProps({ textAlign: 'right' })">修改按钮位置</el-button>
<el-button class="mb-20" @click="onGetFormModal">获取表单值</el-button>
<el-button class="mb-20" @click="onDisplay">回显 字段1</el-button>
<el-button class="mb-20" @click="onResetFields">重置表单</el-button>
<!-- <el-button class="mb-20" @click="onValidateFields">校验自定义校验规则</el-button> -->
<el-button class="mb-20" @click="validateForm">手动校验表单</el-button>
<el-button class="mb-20" @click="resetValidate">清空校验信息</el-button>
<el-button class="mb-20" @click="deleteField">删除 字段1</el-button>
<el-button class="mb-20" @click="appendField"> 往字段1后面插入字段2 </el-button>
</div>
</div>
<el-main class="schema-main">
<SchemaForm
label-position="right"
label-width="150px"
@register="register"
:rules="rules"
@submit="handleSubmit"
>
<template #f3="{ model, field }">
<el-autocomplete style="width: 100%" :fetch-suggestions="querySearchAsync" v-model="model[field]" placeholder="el-autocomplete" />
</template>
<template #add="{ field }">
<el-button v-if="Number(field) === 0" @click="add">新增</el-button>
<el-button v-if="field > 0" @click="del(field)">删除</el-button>
</template>
</SchemaForm>
</el-main>
</el-container>
</template>
<script>
import { defineComponent, h, reactive, ref, toRefs } from 'vue'
import { SchemaForm, useComponentRegister, useForm } from '../../../../packages/schema-form'
import { ElInput } from 'element-plus'
import demo from './demo.vue'
useComponentRegister('demo', demo)
const schemas = [
{
field: 'field1',
component: 'Input',
label: '字段1',
helpMessage: '这是字段1',
helpComponentProps: {
placement: 'bottom',
icon: 'el-icon-question'
// ... el-tooltip 属性
},
colProps: {
span: 8
},
componentProps: {
placeholder: '自定义placeholder',
maxlength: 4,
clearable: true,
onChange: (e) => {
console.log(e)
}
},
dynamicDisabled: ({ values }) => {
return !!values.field7
},
dynamicRules: ({ values }) => {
return values.field8 ? [{ required: true, message: '字段1必填' }] : []
}
},
{
field: 'field8',
component: 'Switch',
label: '字段1是否必填',
colProps: {
span: 8
}
},
{
field: 'field7',
component: 'Switch',
label: '是否禁用字段1',
colProps: {
span: 8
}
},
{
field: 'field2',
component: 'Input',
label: '默认值',
colProps: {
span: 8
},
defaultValue: 'defalut value',
componentProps: {
clearable: true
}
},
{
field: 'field18',
component: 'Input',
label: '自定义校验规则',
colProps: {
span: 8
},
show: ({ values }) => {
return !!values.field25
},
componentProps: {
size: 'mini',
suffixIcon: 'el-icon-edit-outline'
},
rules: [
{
required: true,
validator: async (rule, value) => {
if (!value) {
return Promise.reject('值不能为空')
}
if (value === '1') {
return Promise.reject('值不能为1')
}
return Promise.resolve()
},
trigger: 'change'
}
]
},
{
field: 'field25',
component: 'Switch',
label: '显示自定义校验规则',
defaultValue: true,
colProps: {
span: 8
}
},
{
field: 'field16',
component: 'Input',
label: '输入框前置内容',
colProps: {
span: 8
},
renderComponentContent: () =>{
return {
prepend: () => 'Http://'
}
}
},
{
field: 'field17',
component: 'Input',
label: '输入框后置内容',
colProps: {
span: 8
},
renderComponentContent: () =>{
return {
append: () => '.com'
}
}
},
{
field: 'field3',
component: 'Input',
label: '自定义渲染组的 slot',
colProps: {
span: 8
},
renderComponentContent: () => {
return {
suffix: () => '$'
}
}
},
{
field: 'field6',
label: '自定义Slot-远程搜索',
slot: 'f3',
colProps: {
span: 12
}
},
{
field: 'field11',
component: 'Input',
label: 'render方式',
colProps: {
span: 12
},
render: ({ model, field }) => {
return h(ElInput, {
placeholder: '请输入',
modelValue: model[field],
onInput: (e) => {
model[field] = e
}
})
}
},
{
field: 'field4',
component: 'Select',
label: '单选',
colProps: {
span: 8
},
componentProps: {
clearable: true,
filterable: true,
options: [
{
value: '选项1',
label: '黄金糕'
}, {
value: '选项2',
label: '双皮奶'
}, {
value: '选项3',
label: '蚵仔煎'
}, {
value: '选项4',
disabled: true,
label: '龙须面'
}, {
value: '选项5',
label: '北京烤鸭'
}
],
onChange: () => {
// 配置请求参数
// schemas.filter(it => it.field === 'field30')[0].componentProps.params.query = '2'
}
}
},
{
field: 'field30',
component: 'Select',
label: '远程下拉',
componentProps: {
filterable: true,
// api: getUserInfo,
clearable: true,
immediate: false,
params: {}, // 请求参数
optionKey: {
label: 'marketName',
value: 'marketCode'
}
},
colProps: {
span: 8
}
},
{
field: 'field29',
component: 'Cascader',
label: '级联',
colProps: {
span: 8
},
componentProps: {
clearable: true,
options: [
{
value: 1,
label: '东南',
children: [
{
value: 2,
label: '上海',
children: [
{ value: 3, label: '普陀' },
{ value: 4, label: '黄埔' },
{ value: 5, label: '徐汇' }
]
},
{
value: 7,
label: '江苏',
children: [
{ value: 8, label: '南京' },
{ value: 9, label: '苏州' },
{ value: 10, label: '无锡' }
]
},
{
value: 12,
label: '浙江',
children: [
{ value: 13, label: '杭州' },
{ value: 14, label: '宁波' },
{ value: 15, label: '嘉兴' }
]
}
]
},
{
value: 17,
label: '西北',
children: [
{
value: 18,
label: '陕西',
children: [
{ value: 19, label: '西安' },
{ value: 20, label: '延安' }
]
},
{
value: 21,
label: '新疆维吾尔族自治区',
children: [
{ value: 22, label: '乌鲁木齐' },
{ value: 23, label: '克拉玛依' }
]
}
]
}
]
}
},
{
field: 'field19',
component: 'DatePicker',
valueFormate: 'YYYY/MM/DD',
label: '日期',
colProps: {
span: 8
},
componentProps: {
type: 'date',
shortcuts: [{
text: 'Today',
value: new Date()
}, {
text: 'Yesterday',
value: (() => {
const date = new Date()
date.setTime(date.getTime() - 3600 * 1000 * 24)
return date
})()
}, {
text: 'A week ago',
value: (() => {
const date = new Date()
date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
return date
})()
}]
}
},
{
field: 'field20',
component: 'RangePicker',
label: '日期范围',
valueFormate: 'YYYY-MM-DD',
colProps: {
span: 16
},
componentProps: {
type: 'daterange',
rangeSeparator: '至',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期'
},
fieldMapToTime: ['startTime', 'endTime']
},
{
field: 'field21',
component: 'TimeSelect',
label: 'TimeSelect 时间选择',
colProps: {
span: 8
},
componentProps: {
start: '08:30',
step: '00:15',
end: '18:30'
}
},
{
field: 'field22',
component: 'TimePicker',
label: 'TimePicker 时间选择',
colProps: {
span: 16
},
componentProps: {
isRange: true,
rangeSeparator: '至',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期'
}
},
{
field: 'f1',
component: 'Input',
label: 'F1',
colProps: {
span: 12
},
componentProps: ({ formModel }) => {
return {
placeholder: '同步f2的值为f1',
onInput: (e) => {
formModel.f2 = e
}
}
}
},
{
field: 'f2',
component: 'Input',
label: 'F2-自定义labelWidth',
colProps: {
span: 12
},
labelWidth: '300px',
componentProps: {
type: 'textarea',
disabled: true
}
},
{
field: 'field5',
component: 'RadioGroup',
label: '单选框组',
colProps: {
span: 8
},
defaultValue: '选项1',
componentProps: {
options: ['选项1', '选项2']
// type: 'button',
// border: true,
// size: 'mini',
}
},
{
field: 'field26',
component: 'CheckboxGroup',
label: '多选框组',
colProps: {
span: 8
},
componentProps: {
options: [
{
label: '选项1',
value: '3'
},
{
label: '选项2',
value: '4'
}
]
}
},
{
field: 'field27',
component: 'InputNumber',
label: '计数器',
colProps: {
span: 8
}
},
{
field: 'field28',
component: 'Slider',
label: '滑块',
colProps: {
span: 8
}
},
{
field: 'field24',
component: 'Rate',
label: '评分',
colProps: {
span: 8
}
},
{
field: 'demo',
component: 'demo',
label: '自定义注册组件',
colProps: {
span: 8
}
},
{
field: 'field0a',
component: 'Input',
label: '字段0',
colProps: {
span: 12
},
},
{
field: '0',
component: 'Input',
labelWidth: '20px',
label: ' ',
colProps: {
span: 4
},
slot: 'add'
}
]
const rules = {
field16: [
{ "required": true, "message": "请输入名称" },
{ "max": 5, "message": "最多 5 个字符" }
]
}
export default defineComponent({
components: { SchemaForm },
setup () {
const [register, {
setProps,
appendSchemaByField,
removeSchemaByFiled,
getFieldsValue,
setFieldsValue,
resetFields,
validate,
validateFields,
clearValidate
}] = useForm({ schemas: schemas })
const opt = reactive({
showActionButtonGroup: true,
showResetButton: true,
showSubmitButton: true
})
const formElRef = ref(null)
const n = ref(1)
function add () {
appendSchemaByField(
{
field: `field${n.value}a`,
component: 'Input',
label: '字段' + n.value,
colProps: {
span: 12
},
},
''
)
appendSchemaByField(
{
field: `${n.value}`,
component: 'Input',
label: ' ',
labelWidth: '20px',
colProps: {
span: 4
},
slot: 'add'
},
''
)
n.value++
}
function del (field) {
removeSchemaByFiled([`field${field}a`, `field${field}b`, `${field}`])
n.value--
}
const handleSubmit = (values) => {
console.log(values)
}
function querySearchAsync (queryString, cb) {
cb([{
value: '南拳妈妈龙虾盖浇饭',
address: '普陀区金沙江路1699号鑫乐惠美食广场A13'
}])
}
function onChange () {
setProps({ ...optVal })
}
const optVal = toRefs(opt)
function onDisplay () {
let defVal = { field1: '我是回显值' }
setFieldsValue(defVal)
}
async function validateForm () {
try {
const res = await validate()
console.log('passing', res)
} catch (error) {
console.log('not passing', error)
}
}
async function onGetFormModal () {
const res = await getFieldsValue()
alert(JSON.stringify(res))
}
async function resetValidate () {
clearValidate()
}
function deleteField () {
removeSchemaByFiled('field1')
}
function appendField () {
appendSchemaByField(
{
field: 'field10',
label: '字段2',
component: 'Input',
colProps: {
span: 8
}
},
'field1'
)
}
async function onResetFields () {
await resetFields()
}
async function onValidateFields () {
await validateFields(['field18'])
}
return {
schemas,
formElRef,
handleSubmit,
querySearchAsync,
setProps,
rules,
deleteField,
appendField,
resetValidate,
...optVal,
onDisplay,
onChange,
validateForm,
add,
del,
onGetFormModal,
register,
onResetFields,
onValidateFields
}
}
})
</script>
<style lang="less" scoped>
.schema-alert {
margin: 10px 0 20px 0;
}
.schema-aside {
position: fixed;
left: 0;
height: 100%;
z-index: 1;
background: #fff;
width: 290px;
border-right: 1px solid #eee;
overflow: auto;
padding-left: 35px;
&-opt {
padding-top: 100px;
display: flex;
flex-direction: column;
align-items: flex-start;
}
.el-button+.el-button {
margin-left: 0px;
}
}
.schema-main {
margin: 80px 30px 20px 310px;
}
.mb-20 {
margin-bottom: 25px;
}
</style>