ArrayForm 组表单
基于ZForm
组件封装。
数组表单
表单类型type
传入array
,columns
中配置children(表单项)
,可以实现数组表单。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { ValidateField } from 'ideaz-element'
interface FormData {
name: string
gender: string
address: string
time: string[]
}
const formRef = ref()
const form = ref<FormData[]>([{
name: '',
gender: '',
address: '',
time: [],
}])
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
component: 'input',
field: 'name',
modifier: 'trim',
label: '姓名',
onInput: (val: string) => {
console.log(val, 'input event')
},
onChange: (val: string) => {
console.log(val, 'change event')
},
required: true,
},
{
component: 'select',
field: 'gender',
label: '性别',
onChange: (val: string) => {
console.log(val, 'change event')
},
onFocus: () => {
console.log('focus event')
},
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
onChange: (val: string) => {
console.log(val, 'change event')
},
},
{
slot: 'address',
label: '地址',
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean, fields: ValidateField) => {
console.log(form.value, fields, 'config.formData')
if (valid)
ElMessage.success('success')
else
console.log('error')
})
}
</script>
<template>
<z-form
ref="formRef"
v-model="form"
:options="options"
:columns="columns"
label-width="80px"
size="small"
type="array"
>
<template #address="{ formData }">
<el-input v-model="formData.address" />
</template>
</z-form>
<div class="mt-4 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>
内置数组表单
复杂的内置数组表单案列。想要校验表单项,可以直接调用表单validate
方法。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { ValidateField } from 'ideaz-element'
const formRef = ref()
const form = ref({
time: [],
a: [
{
name: '',
gender: '',
time: [],
},
],
b: [
{
name: '',
gender: '',
address: '',
time: [],
},
],
})
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
label: '文本1',
field: 'a',
max: 2,
children: [
{
component: 'input',
field: 'name',
label: '姓名',
onInput: (val: string) => {
console.log(val, 'input event')
},
onChange: (val: string) => {
console.log(val, 'change event')
},
required: true,
},
],
},
{
label: '文本2',
field: 'b',
extra: 'extra field',
tooltip: '提示',
children: [
{
component: 'select',
field: 'gender',
label: '性别',
onChange: (val: string) => {
console.log(val, 'change event')
},
onFocus: () => {
console.log('focus event')
},
rules: {
required: true,
},
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
onChange: (val: string) => {
console.log(val, 'change event')
},
},
{
slot: 'test',
field: 'address',
label: '地址',
},
],
},
{
label: 'input',
component: 'input',
field: 'input',
},
{
slot: 'operate',
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean, fields: ValidateField) => {
console.log(form.value, fields, 'config.formData')
if (valid)
ElMessage.success('success')
else
console.log('error')
})
}
</script>
<template>
<z-form
ref="formRef"
v-model="form"
:options="options"
:columns="columns"
:max="3"
label-width="80px"
size="default"
type="array"
>
<template #test="{ formData }">
<el-input v-model="formData.address" />
</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>
最大数量
通过max
属性设置最大数量。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { ValidateField } from 'ideaz-element'
const formRef = ref()
const formData = ref([{
name: '',
gender: '',
time: [],
}])
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
component: 'input',
field: 'name',
modifier: 'trim',
label: '姓名',
required: true,
},
{
component: 'select',
field: 'gender',
label: '性别',
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean, fields: ValidateField) => {
console.log(formData.value, fields, 'config.formData')
if (valid)
ElMessage.success('success')
else
console.log('error')
})
}
</script>
<template>
<z-form
ref="formRef"
v-model="formData"
:options="options"
:columns="columns"
:max="2"
label-width="80px"
size="small"
type="array"
/>
<div class="mt-4 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>
操作
配置action
为false
,可以关闭操作项。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { ValidateField } from 'ideaz-element'
interface FormData {
name: string
gender: string
address: string
time: string[]
}
const formRef = ref()
const form = ref<FormData[]>([{
name: '',
gender: '',
address: '',
time: [],
}])
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
component: 'input',
field: 'name',
modifier: 'trim',
label: '姓名',
required: true,
},
{
component: 'select',
field: 'gender',
label: '性别',
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
},
{
slot: 'address',
label: '地址',
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean, fields: ValidateField) => {
console.log(form.value, fields, 'config.formData')
if (valid)
ElMessage.success('success')
else
console.log('error')
})
}
function handleAdd() {
form.value.push({})
}
</script>
<template>
<z-form
ref="formRef"
v-model="form"
:options="options"
:columns="columns"
:action="false"
label-width="80px"
size="small"
type="array"
>
<template #address="{ formData }">
<el-input v-model="formData.address" />
</template>
</z-form>
<el-button class="mt-1 w-full" type="warning" @click="handleAdd">
添加
</el-button>
<div class="mt-4 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>
配置action
为插槽
或render函数
,可以自定义操作项。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import { ElIcon, ElMessage } from 'element-plus'
import { Delete } from '@element-plus/icons-vue'
import type { ValidateField } from 'ideaz-element'
interface FormData {
name: string
gender: string
address: string
time: string[]
}
const formRef = ref()
const form = ref<FormData[]>([{
name: '',
gender: '',
address: '',
time: [],
}])
const options = {
gender: [
{ label: '男', value: '1' },
{ label: '女', value: '2' },
],
}
const columns = [
{
component: 'input',
field: 'name',
modifier: 'trim',
label: '姓名',
required: true,
},
{
component: 'select',
field: 'gender',
label: '性别',
},
{
component: 'el-date-picker',
field: 'time',
label: '出生日期',
fieldProps: {
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期',
},
},
{
slot: 'address',
label: '地址',
},
]
function reset() {
formRef.value.resetFields()
}
function submit() {
formRef.value.validate((valid: boolean, fields: ValidateField) => {
console.log(form.value, fields, 'config.formData')
if (valid)
ElMessage.success('success')
else
console.log('error')
})
}
function handleDelete(index: number) {
form.value.splice(index, 1)
}
</script>
<template>
<z-form
ref="formRef"
v-model="form"
:options="options"
:columns="columns"
label-width="80px"
size="small"
type="array"
>
<template #address="{ formData }">
<el-input v-model="formData.address" />
</template>
<template #action="{ index }">
<ElIcon size="16" class="delete cursor-pointer" @click="handleDelete(index)">
<Delete />
</ElIcon>
</template>
</z-form>
<div class="mt-4 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>
<style lang="scss" scoped>
.delete {
position: absolute;
top: -8px;
right: 0;
color: red;
}
</style>
array表单属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
max | 最大表单项数量 | number | — |
action | 操作项 | boolean / () => VNode | true |