GroupForm 组表单
基于ZForm
组件和ElDivider
组件封装。
常规使用
表单类型type
传入group
,columns
中配置children(表单项)
,可以实现分组表单。
<!-- eslint-disable no-console -->
<!-- eslint-disable no-alert -->
<script lang="ts" setup>
import { ref } from 'vue'
const formRef = ref()
const formData = ref({
name: '',
gender: '',
time: [],
})
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
label: '文本1',
children: [
{
component: 'input',
field: 'name',
label: '姓名',
required: true,
},
],
},
{
label: '文本2',
children: [
{
component: 'select',
field: 'gender',
label: '性别',
extra: '性别选择',
required: true,
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
},
],
},
{
slot: 'operate',
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean) => {
if (valid) {
alert('submit!')
console.log(formData.value, 'config.formData')
}
else {
console.log('error submit!!')
return false
}
})
}
</script>
<template>
<z-form
ref="formRef"
v-model="formData"
:options="options"
:columns="columns"
label-width="80px"
size="small"
type="group"
>
<template #operate>
<div class="w-full flex">
<el-button class="w-full" @click="reset">
重置
</el-button>
<el-button type="primary" class="w-full" @click="submit">
提交
</el-button>
</div>
</template>
</z-form>
</template>
分割线配置
column
中配置borderStyle
实现分割线样式修改,支持'none' | 'solid' | 'hidden' | 'dashed' | ...
。
<!-- eslint-disable no-console -->
<!-- eslint-disable no-alert -->
<script lang="ts" setup>
import { ref } from 'vue'
const formRef = ref()
const formData = ref({
name: '',
gender: '',
time: [],
})
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
label: '文本1',
borderStyle: 'dashed',
children: [
{
component: 'input',
field: 'name',
label: '姓名',
},
],
},
{
label: '文本2',
borderStyle: 'hidden',
children: [
{
component: 'select',
field: 'gender',
label: '性别',
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
},
],
},
{
slot: 'operate',
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean) => {
if (valid) {
alert('submit!')
console.log(formData.value, 'config.formData')
}
else {
console.log('error submit!!')
return false
}
})
}
</script>
<template>
<z-form
ref="formRef"
v-model="formData"
:options="options"
:columns="columns"
label-width="80px"
size="small"
type="group"
>
<template #operate>
<div class="w-full flex">
<el-button class="w-full" @click="reset">
重置
</el-button>
<el-button type="primary" class="w-full" @click="submit">
提交
</el-button>
</div>
</template>
</z-form>
</template>
文案位置
column
中contentPosition
属性配置分割线文案位置,支持'left' | 'right' | 'center'
。
<!-- eslint-disable no-console -->
<!-- eslint-disable no-alert -->
<script lang="ts" setup>
import { ref } from 'vue'
const formRef = ref()
const formData = ref({
name: '',
gender: '',
time: [],
})
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
label: '文本1',
children: [
{
component: 'input',
field: 'name',
label: '姓名',
},
],
},
{
label: '文本2',
contentPosition: 'center',
children: [
{
component: 'select',
field: 'gender',
label: '性别',
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
},
],
},
{
slot: 'operate',
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean) => {
if (valid) {
alert('submit!')
console.log(formData.value, 'config.formData')
}
else {
console.log('error submit!!')
return false
}
})
}
</script>
<template>
<z-form
ref="formRef"
v-model="formData"
:options="options"
:columns="columns"
label-width="80px"
size="small"
type="group"
content-position="left"
>
<template #operate>
<div class="w-full flex">
<el-button class="w-full" @click="reset">
重置
</el-button>
<el-button type="primary" class="w-full" @click="submit">
提交
</el-button>
</div>
</template>
</z-form>
</template>
文案自定义
label
除了支持字符串,还支持h函数
、render函数
和slot(需带slot字符,无视大小写)
。
<!-- eslint-disable no-console -->
<!-- eslint-disable no-alert -->
<script lang="ts" setup>
import { h, ref } from 'vue'
const formRef = ref()
const formData = ref({
name: '',
gender: '',
time: [],
})
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
label: () => h('span', '文本1'),
children: [
{
component: 'input',
field: 'name',
label: '姓名',
rules: {
required: true,
},
},
],
},
{
label: 'labelSlot',
children: [
{
component: 'select',
field: 'gender',
label: '性别',
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
},
],
},
{
slot: 'operate',
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean) => {
if (valid) {
alert('submit!')
console.log(formData.value, 'config.formData')
}
else {
console.log('error submit!!')
return false
}
})
}
</script>
<template>
<z-form
ref="formRef"
v-model="formData"
:options="options"
:columns="columns"
label-width="80px"
size="small"
type="group"
>
<template #labelSlot>
文案2
</template>
<template #operate>
<div class="w-full flex">
<el-button class="w-full" @click="reset">
重置
</el-button>
<el-button type="primary" class="w-full" @click="submit">
提交
</el-button>
</div>
</template>
</z-form>
</template>
表单属性
请查阅ZForm
组件常规使用文档。
column属性(特有)
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
label | 分割线内容 | string / () => VNode | — |
border-style | 设置分隔符样式 | none / solid / hidden / dashed / ... | solid |
content-position | 自定义分隔线内容的位置 | left / right / center | center |
children | 当前步骤中的表单项(属性都为通常column 属性) | array | — |