wip(VForm): VForm component development

This commit is contained in:
kailong321200875 2021-12-15 21:26:14 +08:00
parent 558abb86dc
commit 2730c2b359
3 changed files with 64 additions and 27 deletions

View File

@ -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>

View File

@ -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

View File

@ -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
}