FormCollapse
折叠面板,通常用在布局空间要求较高的表单场景
注意
该组件只适用于 Schema 场景。
Markup Schema 标题使用插槽案例
vue
<script setup lang="ts">
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import {
FormButtonGroup,
FormCollapse,
FormItem,
Input,
Submit,
} from '@silver-formily/element-plus'
import { ElButton } from 'element-plus'
import { h } from 'vue'
const { SchemaField, SchemaVoidField, SchemaStringField } = createSchemaField({
components: {
FormItem,
FormCollapse,
Input,
},
})
const form = createForm()
const formCollapse = FormCollapse.createFormCollapse()
async function log(values) {
console.log(values)
}
</script>
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaVoidField
type="void"
title="折叠面板"
x-decorator="FormItem"
x-component="FormCollapse"
:x-component-props="{ formCollapse }"
>
<SchemaVoidField
type="void"
name="tab1"
x-component="FormCollapse.Item"
:x-component-props="{ title: 'A1' }"
:x-content="{
title: '标题Tab1',
}"
>
<SchemaStringField
name="aaa"
x-decorator="FormItem"
title="AAA"
required
x-component="Input"
/>
</SchemaVoidField>
<SchemaVoidField
name="tab2"
x-component="FormCollapse.Item"
:x-component-props="{ title: 'A2' }"
:x-content="{
title: (errorLength) => h('span', `render 函数渲染的VNode, 错误数量:${errorLength ?? 0}`),
}"
>
<SchemaStringField
name="bbb"
x-decorator="FormItem"
title="BBB"
required
x-component="Input"
/>
</SchemaVoidField>
<SchemaVoidField
name="tab3"
:visible="false"
x-component="FormCollapse.Item"
:x-component-props="{ title: 'A3' }"
>
<SchemaStringField
name="ccc"
x-decorator="FormItem"
title="CCC"
required
x-component="Input"
/>
</SchemaVoidField>
</SchemaVoidField>
</SchemaField>
<FormButtonGroup align-form-item>
<ElButton
@click="
() => {
form.query('tab3').take((field) => {
field.visible = !field.visible
})
}
"
>
显示/隐藏最后一个Tab
</ElButton>
<ElButton
@click="
() => {
formCollapse.toggleActiveKey('tab2')
}
"
>
切换第二个Tab
</ElButton>
<Submit @submit="log">
提交
</Submit>
</FormButtonGroup>
</FormProvider>
</template>
<style lang="scss" scoped></style>JSON Schema 案例
vue
<script setup lang="ts">
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import {
FormButtonGroup,
FormCollapse,
FormItem,
FormLayout,
Input,
Submit,
} from '@silver-formily/element-plus'
import { ElButton } from 'element-plus'
const { SchemaField } = createSchemaField({
components: {
FormItem,
FormCollapse,
Input,
},
})
const schema = {
type: 'object',
properties: {
collapse: {
'type': 'void',
'title': '折叠面板',
'x-decorator': 'FormItem',
'x-component': 'FormCollapse',
'x-component-props': {
formCollapse: '{{formCollapse}}',
},
'properties': {
tab1: {
'type': 'void',
'x-component': 'FormCollapse.Item',
'x-component-props': {
title: 'A1',
},
'properties': {
aaa: {
'type': 'string',
'title': 'AAA',
'x-decorator': 'FormItem',
'required': true,
'x-component': 'Input',
},
},
},
tab2: {
'type': 'void',
'x-component': 'FormCollapse.Item',
'x-component-props': {
title: 'A2',
},
'properties': {
bbb: {
'type': 'string',
'title': 'BBB',
'x-decorator': 'FormItem',
'required': true,
'x-component': 'Input',
},
},
},
tab3: {
'type': 'void',
'x-component': 'FormCollapse.Item',
'x-component-props': {
title: 'A3',
},
'properties': {
ccc: {
'type': 'string',
'title': 'CCC',
'x-decorator': 'FormItem',
'required': true,
'x-component': 'Input',
},
},
},
},
},
},
}
const form = createForm()
const formCollapse = FormCollapse.createFormCollapse()
async function log(values) {
console.log(values)
}
</script>
<template>
<FormProvider :form="form">
<FormLayout :label-col="6" :wrapper-col="10">
<SchemaField :schema="schema" :scope="{ formCollapse }" />
<FormButtonGroup align-form-item>
<ElButton
@click="
() => {
form.query('tab3').take((field) => {
field.visible = !field.visible
})
}
"
>
显示/隐藏最后一个Tab
</ElButton>
<ElButton
@click="
() => {
formCollapse.toggleActiveKey('tab2')
}
"
>
切换第二个Tab
</ElButton>
<Submit @submit="log">
提交
</Submit>
</FormButtonGroup>
</FormLayout>
</FormProvider>
</template>
<style lang="scss" scoped></style>API
FormCollapse
| 属性名 | 类型 | 描述 | 默认值 |
|---|---|---|---|
| formCollapse | IFormCollapse | 传入通过 createFormCollapse/useFormCollapse 创建出来的模型 |
onChange事件已被组件内部使用,请勿占用。其余参考 https://cn.element-plus.org/zh-CN/component/collapse.html
FormCollapse.Item
参考 https://cn.element-plus.org/zh-CN/component/collapse.html#collapse-插槽
FormCollapse.createFormCollapse
ts
type ActiveKey = string | number
type ActiveKeys = string | number | Array<string | number>
interface createFormCollapse {
(defaultActiveKeys?: ActiveKeys): IFormCollpase
}
interface IFormCollapse {
// 激活主键列表
activeKeys: ActiveKeys
// 是否存在该激活主键
hasActiveKey: (key: ActiveKey) => boolean
// 设置激活主键列表
setActiveKeys: (keys: ActiveKeys) => void
// 添加激活主键
addActiveKey: (key: ActiveKey) => void
// 删除激活主键
removeActiveKey: (key: ActiveKey) => void
// 开关切换激活主键
toggleActiveKey: (key: ActiveKey) => void
}