优化表单配置

This commit is contained in:
Gary Fu
2023-12-31 18:44:02 +08:00
parent 7693635b45
commit 1af4c343e6
4 changed files with 64 additions and 11 deletions

View File

@@ -78,6 +78,23 @@ const modelValue = computed({
}
})
const childTypeMapping = { // 自动映射子元素类型配置的时候可以不写type
'checkbox-group': 'checkbox',
'radio-group': 'radio',
select: 'option'
}
const children = computed(() => {
const option = props.option
const result = option.children || [] // 初始化一些默认值
result.forEach(childItem => {
if (!childItem.type) {
childItem.type = childTypeMapping[option.type]
}
})
return result
})
</script>
<template>
@@ -94,9 +111,9 @@ const modelValue = computed({
:readonly="option.readonly"
@change="option.change"
>
<template v-if="option.children&&option.children.length">
<template v-if="children&&children.length">
<control-child
v-for="(childItem, index) in option.children"
v-for="(childItem, index) in children"
:key="index"
:option="childItem"
/>

View File

@@ -1,4 +1,4 @@
import { GlobalLayoutMode, GlobalLocales } from '@/consts/GlobalConstants'
import { GlobalLocales } from '@/consts/GlobalConstants'
import { useGlobalConfigStore } from '@/stores/GlobalConfigStore'
import { ref } from 'vue'
import { $i18nBundle } from '@/messages'

View File

@@ -28,12 +28,10 @@ const options = [
},
children: [{
labelKey: 'common.label.langCn',
value: GlobalLocales.CN,
type: 'option'
value: GlobalLocales.CN
}, {
labelKey: 'common.label.langEn',
value: GlobalLocales.EN,
type: 'option'
value: GlobalLocales.EN
}]
},
{
@@ -46,12 +44,10 @@ const options = [
},
children: [{
labelKey: 'common.label.layoutLeft',
value: GlobalLayoutMode.LEFT,
type: 'option'
value: GlobalLayoutMode.LEFT
}, {
labelKey: 'common.label.layoutTop',
value: GlobalLayoutMode.TOP,
type: 'option'
value: GlobalLayoutMode.TOP
}]
},
{

View File

@@ -35,6 +35,46 @@ const formOptions = [{
value: '',
placeholder: '选择出生日期',
required: true
}, {
label: '兴趣爱好',
type: 'checkbox-group',
prop: 'hobby',
value: '',
required: true,
children: [
{
label: '编程',
value: 'program'
},
{
label: '吃饭',
value: 'eat'
},
{
label: '睡觉',
value: 'sleep'
}
]
}, {
label: '性别',
type: 'radio-group',
prop: 'gender',
value: '',
required: true,
children: [
{
label: '男',
value: 'male'
},
{
label: '女',
value: 'female'
},
{
label: '保密',
value: 'unknown'
}
]
}]
const userDto = ref({
userName: '',