Skip to content

Commit 9d7cc67

Browse files
committedOct 28, 2020
feat(schema generator): 完成schema生成器 input 组件配置化
1 parent 93e344e commit 9d7cc67

File tree

5 files changed

+146
-37
lines changed

5 files changed

+146
-37
lines changed
 

‎packages/demo/src/schema-generator/views/editor/Editor.vue

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@
5555
</div>
5656
<div :class="$style.rightForm">
5757
<div v-if="curEditorItem" :class="$style.configForm">
58-
<h3>表单配置</h3>
5958
<div style="padding: 20px 0;">
6059
<VueJsonFrom
6160
v-model="editorValue"
6261
:schema="curEditorItem.componentPack.propsSchema"
62+
:form-props="{
63+
labelPosition: 'right',
64+
labelWidth: '110px'
65+
}"
6366
:form-footer="{
6467
show: false
6568
}"

‎packages/demo/src/schema-generator/views/editor/common/editorData.js

+57
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,60 @@ export function generateEditorItem(toolItem) {
3939
id
4040
};
4141
}
42+
43+
// editor item 转出为 SchemaField 的数据结构
44+
45+
export function formatFormConfig(key, value) {
46+
switch (key) {
47+
48+
case 'labelWidth':
49+
return `${value * 4}px`;
50+
51+
default: {
52+
return value;
53+
}
54+
55+
}
56+
}
57+
58+
export function editorItem2SchemaFieldProps(editorItem, formData) {
59+
const baseValue = editorItem.componentValue.baseValue;
60+
61+
// baseValue
62+
const { default: defaultValue, uiOptions } = Object.keys(editorItem.componentValue.baseValue).reduce((preVal, curVal) => {
63+
if (curVal === 'default') {
64+
preVal.default = baseValue[curVal];
65+
} else if (baseValue[curVal]) {
66+
preVal.uiOptions = preVal.uiOptions || {};
67+
console.log(formatFormConfig(curVal, baseValue[curVal]));
68+
preVal.uiOptions[curVal] = formatFormConfig(curVal, baseValue[curVal]);
69+
}
70+
71+
return preVal;
72+
}, {});
73+
74+
// options
75+
76+
77+
// rules
78+
79+
const schema = {
80+
...editorItem.componentPack.viewSchema,
81+
default: defaultValue,
82+
};
83+
84+
return {
85+
rootSchema: schema,
86+
schema,
87+
rootFormData: formData,
88+
curNodePath: editorItem.componentValue.property || '',
89+
uiSchema: {
90+
'ui:options': {
91+
...uiOptions,
92+
...editorItem.componentValue.options,
93+
...editorItem.componentValue.rules
94+
},
95+
}
96+
};
97+
98+
}

‎packages/demo/src/schema-generator/views/editor/components/ViewComponentWrap.vue

+4-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[$style.active]: editorItem.isEdit,
55
js_viewComponentWrap: true
66
}"
7-
@click="handelClickView"
7+
@click="handleClickView"
88
>
99
<span :class="$style.formProperty"> {{ attrs.curNodePath }}</span>
1010
<div v-if="editorItem.isEdit" :class="$style.editBar">
@@ -45,6 +45,7 @@
4545

4646
<script>
4747
import { SchemaField } from '@lljj/vue-json-schema-form';
48+
import { editorItem2SchemaFieldProps } from '../common/editorData';
4849
4950
export default {
5051
name: 'ViewComponentWrap',
@@ -63,40 +64,15 @@
6364
},
6465
computed: {
6566
attrs() {
66-
const baseValue = this.editorItem.componentValue.baseValue;
67-
const { default: defaultValue, uiOptions } = Object.keys(this.editorItem.componentValue.baseValue).reduce((preVal, curVal) => {
68-
if (curVal === 'default') {
69-
preVal.default = baseValue[curVal];
70-
} else if (baseValue[curVal]) {
71-
preVal.uiOptions = preVal.uiOptions || {};
72-
preVal.uiOptions[curVal] = baseValue[curVal];
73-
}
74-
75-
return preVal;
76-
}, {});
77-
78-
const schema = {
79-
...this.editorItem.componentPack.viewSchema,
80-
default: defaultValue,
81-
};
82-
83-
return {
84-
rootSchema: schema,
85-
schema,
86-
rootFormData: this.formData,
87-
curNodePath: this.editorItem.componentValue.property || '',
88-
uiSchema: {
89-
'ui:options': uiOptions,
90-
}
91-
};
67+
return editorItem2SchemaFieldProps(this.editorItem, this.formData);
9268
}
9369
},
9470
beforeDestroy() {
9571
this.hideEditForm();
9672
},
9773
methods: {
9874
// 点击只能打开,并且打开状态下只能执行一次
99-
handelClickView(e) {
75+
handleClickView(e) {
10076
// 阻止浏览器默认事件
10177
e.preventDefault();
10278
if (!this.editorItem.isEdit) this.showEditForm();

‎packages/demo/src/schema-generator/views/editor/viewComponents/Input/index.js

+56-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,60 @@ const viewSchema = {
1010
};
1111
export default {
1212
viewSchema,
13-
propsSchema: genSchema()
13+
propsSchema: genSchema({
14+
options: {
15+
type: 'object',
16+
title: '选项',
17+
required: [],
18+
properties: {
19+
placeholder: {
20+
type: 'string',
21+
title: '输入占位文本',
22+
default: '请输入'
23+
},
24+
clearable: {
25+
title: '显示清空按钮',
26+
type: 'boolean',
27+
default: false
28+
},
29+
showWordLimit: {
30+
title: '字数统计',
31+
type: 'boolean',
32+
default: false
33+
},
34+
showPassword: {
35+
title: '密码框',
36+
type: 'boolean',
37+
default: false
38+
},
39+
type: {
40+
type: 'string',
41+
title: '输入框类型',
42+
enum: [
43+
'text',
44+
'textarea'
45+
],
46+
enumNames: [
47+
'输入框 Input',
48+
'文字域 Textarea'
49+
]
50+
},
51+
}
52+
},
53+
rules: {
54+
type: 'object',
55+
title: '数据校验',
56+
required: [],
57+
properties: {
58+
maxlength: {
59+
title: '最大长度',
60+
type: 'number'
61+
},
62+
minlength: {
63+
title: '最小长度',
64+
type: 'number'
65+
}
66+
}
67+
}
68+
})
1469
};

‎packages/demo/src/schema-generator/views/editor/viewComponents/genSchema.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,41 @@
22
* Created by Liu.Jun on 2020/10/26 18:24.
33
*/
44

5+
import { formatFormConfig } from '../common/editorData';
6+
57
const baseValue = {
6-
title: '基础Form label配置',
8+
title: '基础配置',
79
type: 'object',
810
properties: {
911
title: {
1012
title: '标题',
11-
type: 'string'
13+
type: 'string',
14+
'ui:placeholder': '请输入表单项标题',
15+
'err:required': '请输入标题'
1216
},
1317
description: {
1418
title: '描述',
15-
type: 'string'
19+
type: 'string',
20+
'ui:options': {
21+
placeholder: '请输入表单项描述,支持输入html',
22+
type: 'textarea',
23+
rows: 3,
24+
}
1625
},
1726
labelWidth: {
1827
title: '标签宽度',
19-
type: 'number'
28+
type: 'number',
29+
'ui:widget': 'ElSlider',
30+
'ui:options': {
31+
formatTooltip(val) {
32+
return formatFormConfig('labelWidth', val);
33+
}
34+
}
2035
},
2136
default: {
2237
title: '默认值',
23-
type: 'string'
38+
type: 'string',
39+
'ui:placeholder': '请输入默认值'
2440
},
2541
disabled: {
2642
title: '禁用',
@@ -32,11 +48,13 @@ const baseValue = {
3248

3349
export default schema => ({
3450
type: 'object',
35-
required: [],
51+
required: ['property'],
3652
properties: {
3753
property: {
3854
title: '属性名',
39-
type: 'string'
55+
type: 'string',
56+
'ui:placeholder': '请输入属性名',
57+
'err:required': '属性名必填'
4058
},
4159
baseValue,
4260
...schema

0 commit comments

Comments
 (0)