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,39 +43,53 @@ 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) => {
return ( // Divider
<ElFormItem {...(item.formItemProps || {})} prop={item.field} label={item.label}> const isDivider = item.component === 'Divider'
{() => { const Com = COMPONENT_MAP['Divider'] as ReturnType<typeof defineComponent>
const Com = COMPONENT_MAP[item.component as string] as ReturnType< return isDivider ? (
typeof defineComponent <Com {...{ contentPosition: 'left', ...item.componentProps }}>
> {item?.componentProps?.text}
return ( </Com>
<Com v-model={test.value} {...(autoSetPlaceholder && setTextPlaceholder(item))}> ) : isCol ? (
{item.options ? renderOptions() : null} // ElCol
</Com> <ElCol {...setGridProp(item.colProps)}>{renderFormItem(item)}</ElCol>
) ) : (
}} renderFormItem(item)
</ElFormItem>
) )
}) })
} }
// formItem
function renderFormItem(item: VFormSchema) {
return (
<ElFormItem {...(item.formItemProps || {})} prop={item.field} label={item.label}>
{() => {
const Com = COMPONENT_MAP[item.component as string] as ReturnType<
typeof defineComponent
>
return (
<Com vModel={test.value} {...(autoSetPlaceholder && setTextPlaceholder(item))}>
{item.options ? renderOptions() : null}
</Com>
)
}}
</ElFormItem>
)
}
// options // options
function renderOptions() { function renderOptions() {
// const optionsMap = ['Radio', 'Checkbox', 'Select'] // const optionsMap = ['Radio', 'Checkbox', 'Select']

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
}