wip(VForm): VForm component development
This commit is contained in:
parent
558abb86dc
commit
2730c2b359
12
src/App.vue
12
src/App.vue
|
@ -15,15 +15,17 @@ onMounted(() => {
|
||||||
const schema = reactive<VFormSchema[]>([
|
const schema = reactive<VFormSchema[]>([
|
||||||
{
|
{
|
||||||
field: 'field1',
|
field: 'field1',
|
||||||
|
component: 'Divider',
|
||||||
|
componentProps: {
|
||||||
|
text: 'input示例'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'field2',
|
||||||
label: '字段1',
|
label: '字段1',
|
||||||
component: 'Input'
|
component: 'Input'
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
// setTimeout(() => {
|
|
||||||
// schema.push({
|
|
||||||
// field: '2'
|
|
||||||
// })
|
|
||||||
// }, 3000)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { ElForm, ElFormItem, ElRow, ElCol } from 'element-plus'
|
||||||
import { COMPONENT_MAP } from './componentMap'
|
import { COMPONENT_MAP } from './componentMap'
|
||||||
import { propTypes } from '@/utils/propTypes'
|
import { propTypes } from '@/utils/propTypes'
|
||||||
import { getSlot } from '@/utils/tsxHelper'
|
import { getSlot } from '@/utils/tsxHelper'
|
||||||
import { setTextPlaceholder } from './helper'
|
import { setTextPlaceholder, setGridProp } from './helper'
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: 'VForm',
|
name: 'VForm',
|
||||||
|
@ -43,22 +43,37 @@ export default defineComponent({
|
||||||
// 渲染包裹标签,是否使用栅格布局
|
// 渲染包裹标签,是否使用栅格布局
|
||||||
function renderWrap() {
|
function renderWrap() {
|
||||||
const content = isCol ? (
|
const content = isCol ? (
|
||||||
<ElRow gutter={20}>
|
<ElRow gutter={20}>{renderFormItemWrap()}</ElRow>
|
||||||
<ElCol>{renderFormItem()}</ElCol>
|
|
||||||
</ElRow>
|
|
||||||
) : (
|
) : (
|
||||||
renderFormItem()
|
renderFormItemWrap()
|
||||||
)
|
)
|
||||||
console.log(content)
|
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染formItem
|
// 是否要渲染el-col
|
||||||
function renderFormItem() {
|
function renderFormItemWrap() {
|
||||||
// hidden属性表示隐藏
|
// hidden属性表示隐藏,不做渲染
|
||||||
return schema
|
return schema
|
||||||
.filter((v) => !v.hidden)
|
.filter((v) => !v.hidden)
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
|
// 如果是 Divider 组件,需要自己占用一行
|
||||||
|
const isDivider = item.component === 'Divider'
|
||||||
|
const Com = COMPONENT_MAP['Divider'] as ReturnType<typeof defineComponent>
|
||||||
|
return isDivider ? (
|
||||||
|
<Com {...{ contentPosition: 'left', ...item.componentProps }}>
|
||||||
|
{item?.componentProps?.text}
|
||||||
|
</Com>
|
||||||
|
) : isCol ? (
|
||||||
|
// 如果需要栅格,需要包裹 ElCol
|
||||||
|
<ElCol {...setGridProp(item.colProps)}>{renderFormItem(item)}</ElCol>
|
||||||
|
) : (
|
||||||
|
renderFormItem(item)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染formItem
|
||||||
|
function renderFormItem(item: VFormSchema) {
|
||||||
return (
|
return (
|
||||||
<ElFormItem {...(item.formItemProps || {})} prop={item.field} label={item.label}>
|
<ElFormItem {...(item.formItemProps || {})} prop={item.field} label={item.label}>
|
||||||
{() => {
|
{() => {
|
||||||
|
@ -66,14 +81,13 @@ export default defineComponent({
|
||||||
typeof defineComponent
|
typeof defineComponent
|
||||||
>
|
>
|
||||||
return (
|
return (
|
||||||
<Com v-model={test.value} {...(autoSetPlaceholder && setTextPlaceholder(item))}>
|
<Com vModel={test.value} {...(autoSetPlaceholder && setTextPlaceholder(item))}>
|
||||||
{item.options ? renderOptions() : null}
|
{item.options ? renderOptions() : null}
|
||||||
</Com>
|
</Com>
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
</ElFormItem>
|
</ElFormItem>
|
||||||
)
|
)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染options
|
// 渲染options
|
||||||
|
|
|
@ -11,6 +11,7 @@ interface PlaceholderMoel {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param schema 对应组件数据
|
* @param schema 对应组件数据
|
||||||
|
* @returns 返回提示信息对象
|
||||||
* @description 用于自动设置placeholder
|
* @description 用于自动设置placeholder
|
||||||
*/
|
*/
|
||||||
export function setTextPlaceholder(schema: VFormSchema): PlaceholderMoel {
|
export function setTextPlaceholder(schema: VFormSchema): PlaceholderMoel {
|
||||||
|
@ -42,3 +43,23 @@ export function setTextPlaceholder(schema: VFormSchema): PlaceholderMoel {
|
||||||
}
|
}
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param col 内置栅格
|
||||||
|
* @returns 返回栅格属性
|
||||||
|
* @description 合并传入进来的栅格属性
|
||||||
|
*/
|
||||||
|
export function setGridProp(col: ColProps = {}): ColProps {
|
||||||
|
const colProps: ColProps = {
|
||||||
|
...{
|
||||||
|
xs: 24,
|
||||||
|
sm: 12,
|
||||||
|
md: 12,
|
||||||
|
lg: 12,
|
||||||
|
xl: 8
|
||||||
|
},
|
||||||
|
...col
|
||||||
|
}
|
||||||
|
return colProps
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue