Skip to content

Commit 7463eea

Browse files
committed
Vue3 重构:REVIEW 表单构建
1 parent 2abc7ec commit 7463eea

File tree

1 file changed

+67
-72
lines changed

1 file changed

+67
-72
lines changed

src/views/infra/build/index.vue

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,99 @@
33
<el-row>
44
<el-col>
55
<div class="mb-2 float-right">
6-
<el-button size="small" @click="setJson"> 导入JSON</el-button>
7-
<el-button size="small" @click="setOption"> 导入Options</el-button>
8-
<el-button size="small" type="primary" @click="showJson">生成JSON</el-button>
9-
<el-button size="small" type="success" @click="showOption">生成Options</el-button>
6+
<el-button size="small" type="primary" @click="showJson">生成 JSON</el-button>
7+
<el-button size="small" type="success" @click="showOption">生成O ptions</el-button>
108
<el-button size="small" type="danger" @click="showTemplate">生成组件</el-button>
11-
<!-- <el-button size="small" @click="changeLocale">中英切换</el-button> -->
129
</div>
1310
</el-col>
11+
<!-- 表单设计器 -->
1412
<el-col>
1513
<fc-designer ref="designer" height="780px" />
1614
</el-col>
1715
</el-row>
18-
<Dialog :title="dialogTitle" v-model="dialogVisible" maxHeight="600">
19-
<div ref="editor" v-if="dialogVisible">
20-
<XTextButton style="float: right" :title="t('common.copy')" @click="copy(formValue)" />
21-
<el-scrollbar height="580">
22-
<div v-highlight>
23-
<code class="hljs">
24-
{{ formValue }}
25-
</code>
26-
</div>
27-
</el-scrollbar>
28-
</div>
29-
<span style="color: red" v-if="err">输入内容格式有误!</span>
30-
</Dialog>
3116
</ContentWrap>
17+
18+
<!-- 弹窗:表单预览 -->
19+
<Dialog :title="dialogTitle" v-model="dialogVisible" max-height="600">
20+
<div ref="editor" v-if="dialogVisible">
21+
<el-button style="float: right" @click="copy(formData)">
22+
{{ t('common.copy') }}
23+
</el-button>
24+
<el-scrollbar height="580">
25+
<div v-highlight>
26+
<code class="hljs">
27+
{{ formData }}
28+
</code>
29+
</div>
30+
</el-scrollbar>
31+
</div>
32+
</Dialog>
3233
</template>
3334
<script setup lang="ts" name="Build">
3435
import formCreate from '@form-create/element-ui'
3536
import { useClipboard } from '@vueuse/core'
37+
const { t } = useI18n() // 国际化
38+
const message = useMessage() // 消息
3639
37-
const { t } = useI18n()
38-
const message = useMessage()
39-
40-
const designer = ref()
41-
42-
const dialogVisible = ref(false)
43-
const dialogTitle = ref('')
44-
const err = ref(false)
45-
const type = ref(-1)
46-
const formValue = ref('')
40+
const designer = ref() // 表单设计器
41+
const dialogVisible = ref(false) // 弹窗的是否展示
42+
const dialogTitle = ref('') // 弹窗的标题
43+
const formType = ref(-1) // 表单的类型:0 - 生成 JSON;1 - 生成 Options;2 - 生成组件
44+
const formData = ref('') // 表单数据
4745
46+
/** 打开弹窗 */
4847
const openModel = (title: string) => {
4948
dialogVisible.value = true
5049
dialogTitle.value = title
5150
}
5251
53-
const setJson = () => {
54-
openModel('导入JSON--未实现')
55-
}
56-
const setOption = () => {
57-
openModel('导入Options--未实现')
58-
}
52+
/** 生成 JSON */
5953
const showJson = () => {
60-
openModel('生成JSON')
61-
type.value = 0
62-
formValue.value = designer.value.getRule()
54+
openModel('生成 JSON')
55+
formType.value = 0
56+
formData.value = designer.value.getRule()
6357
}
58+
59+
/** 生成 Options */
6460
const showOption = () => {
65-
openModel('生成Options')
66-
type.value = 1
67-
formValue.value = designer.value.getOption()
61+
openModel('生成 Options')
62+
formType.value = 1
63+
formData.value = designer.value.getOption()
6864
}
65+
66+
/** 生成组件 */
6967
const showTemplate = () => {
7068
openModel('生成组件')
71-
type.value = 2
72-
formValue.value = makeTemplate()
69+
formType.value = 2
70+
formData.value = makeTemplate()
71+
}
72+
73+
const makeTemplate = () => {
74+
const rule = designer.value.getRule()
75+
const opt = designer.value.getOption()
76+
return `<template>
77+
<form-create
78+
v-model="fapi"
79+
:rule="rule"
80+
:option="option"
81+
@submit="onSubmit"
82+
></form-create>
83+
</template>
84+
<script setup lang=ts>
85+
import formCreate from "@form-create/element-ui";
86+
const faps = ref(null)
87+
const rule = ref('')
88+
const option = ref('')
89+
const init = () => {
90+
rule.value = formCreate.parseJson('${formCreate.toJson(rule).replaceAll('\\', '\\\\')}')
91+
option.value = formCreate.parseJson('${JSON.stringify(opt)}')
92+
}
93+
const onSubmit = (formData) => {
94+
//todo 提交表单
95+
}
96+
init()
97+
<\/script>`
7398
}
74-
// const changeLocale = () => {
75-
// console.info('changeLocale')
76-
// }
7799
78100
/** 复制 **/
79101
const copy = async (text: string) => {
@@ -87,31 +109,4 @@ const copy = async (text: string) => {
87109
}
88110
}
89111
}
90-
91-
const makeTemplate = () => {
92-
const rule = designer.value.getRule()
93-
const opt = designer.value.getOption()
94-
return `<template>
95-
<form-create
96-
v-model="fapi"
97-
:rule="rule"
98-
:option="option"
99-
@submit="onSubmit"
100-
></form-create>
101-
</template>
102-
<script setup lang=ts>
103-
import formCreate from "@form-create/element-ui";
104-
const faps = ref(null)
105-
const rule = ref('')
106-
const option = ref('')
107-
const init = () => {
108-
rule.value = formCreate.parseJson('${formCreate.toJson(rule).replaceAll('\\', '\\\\')}')
109-
option.value = formCreate.parseJson('${JSON.stringify(opt)}')
110-
}
111-
const onSubmit = (formData) => {
112-
//todo 提交表单
113-
}
114-
init()
115-
<\/script>`
116-
}
117112
</script>

0 commit comments

Comments
 (0)