# ArrayCollapse
折叠面板,对于每行字段数量较多,联动较多的场景比较适合使用 ArrayCollapse
注意:该组件只适用于 Schema 场景
# Markup Schema 案例
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaArrayField
name="string_array"
:maxItems="3"
x-decorator="FormItem"
x-component="ArrayCollapse"
:x-component-props="{
accordion: true,
defaultOpenPanelCount: 3,
}"
>
<SchemaVoidField
x-component="ArrayCollapse.CollapsePanel"
:x-component-props="{
header: '字符串数组',
}"
>
<SchemaVoidField x-component="ArrayCollapse.Index" />
<SchemaStringField
name="input"
x-decorator="FormItem"
title="Input"
required
x-component="Input"
/>
<SchemaVoidField x-component="ArrayCollapse.Remove" />
<SchemaVoidField x-component="ArrayCollapse.MoveUp" />
<SchemaVoidField x-component="ArrayCollapse.MoveDown" />
</SchemaVoidField>
<SchemaVoidField
x-component="ArrayCollapse.Addition"
title="添加条目"
/>
</SchemaArrayField>
<SchemaArrayField
name="array"
:maxItems="3"
x-decorator="FormItem"
x-component="ArrayCollapse"
>
<SchemaObjectField
x-component="ArrayCollapse.Item"
:x-component-props="{
header: '对象数组',
}"
>
<SchemaVoidField x-component="ArrayCollapse.Index" />
<SchemaStringField
name="input"
x-decorator="FormItem"
title="Input"
required
x-component="Input"
/>
<SchemaVoidField x-component="ArrayCollapse.Remove" />
<SchemaVoidField x-component="ArrayCollapse.MoveUp" />
<SchemaVoidField x-component="ArrayCollapse.MoveDown" />
</SchemaObjectField>
<SchemaVoidField
x-component="ArrayCollapse.Addition"
title="添加条目"
/>
</SchemaArrayField>
<SchemaArrayField
name="string_array_unshift"
:maxItems="3"
x-decorator="FormItem"
x-component="ArrayCollapse"
:x-component-props="{
defaultOpenPanelCount: 8,
}"
>
<SchemaVoidField
x-component="ArrayCollapse.Item"
:x-component-props="{
header: '字符串数组',
}"
>
<SchemaVoidField x-component="ArrayCollapse.Index" />
<SchemaStringField
name="input"
x-decorator="FormItem"
title="Input"
required
x-component="Input"
/>
<SchemaVoidField x-component="ArrayCollapse.Remove" />
<SchemaVoidField x-component="ArrayCollapse.MoveUp" />
<SchemaVoidField x-component="ArrayCollapse.MoveDown" />
</SchemaVoidField>
<SchemaVoidField
x-component="ArrayCollapse.Addition"
title="添加条目(unshift)"
:x-component-props="{
method: 'unshift',
}"
/>
</SchemaArrayField>
</SchemaField>
<FormButtonGroup>
<Button
@click="
() => {
form.setInitialValues({
array: Array.from({ length: 10 }).map(() => ({
input: 'default value',
})),
string_array: Array.from({ length: 10 }).map(
() => 'default value'
),
string_array_unshift: Array.from({ length: 10 }).map(
() => 'default value'
),
})
}
"
>
加载默认数据
</Button>
<Submit @submit="log">提交</Submit>
</FormButtonGroup>
</FormProvider>
</template>
<script>
import { createForm } from '@formily/core'
import { FormProvider, createSchemaField } from '@formily/vue'
import {
FormItem,
FormButtonGroup,
Submit,
Input,
ArrayCollapse,
} from '@formily/antdv'
import { Button } from 'ant-design-vue'
const SchemaField = createSchemaField({
components: {
FormItem,
Input,
ArrayCollapse,
},
})
export default {
components: {
FormProvider,
FormButtonGroup,
Button,
Submit,
...SchemaField,
},
data() {
const form = createForm()
return {
form,
}
},
methods: {
log(values) {
console.log(values)
},
},
}
</script>
<style lang="scss" scoped></style>
# JSON Schema 案例
<template>
<FormProvider :form="form">
<SchemaField :schema="schema" />
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm } from '@formily/core'
import { FormProvider, createSchemaField } from '@formily/vue'
import {
FormItem,
FormButtonGroup,
Submit,
Input,
ArrayCollapse,
} from '@formily/antdv'
import { Button } from 'ant-design-vue'
const SchemaField = createSchemaField({
components: {
FormItem,
Input,
ArrayCollapse,
},
})
export default {
components: {
FormProvider,
FormButtonGroup,
Button,
Submit,
...SchemaField,
},
data() {
const form = createForm()
const schema = {
type: 'object',
properties: {
string_array: {
type: 'array',
'x-component': 'ArrayCollapse',
maxItems: 3,
'x-decorator': 'FormItem',
items: {
type: 'object',
'x-component': 'ArrayCollapse.CollapsePanel',
'x-component-props': {
header: '字符串数组',
},
properties: {
index: {
type: 'void',
'x-component': 'ArrayCollapse.Index',
},
input: {
type: 'string',
'x-decorator': 'FormItem',
title: 'Input',
required: true,
'x-component': 'Input',
},
remove: {
type: 'void',
'x-component': 'ArrayCollapse.Remove',
},
moveUp: {
type: 'void',
'x-component': 'ArrayCollapse.MoveUp',
},
moveDown: {
type: 'void',
'x-component': 'ArrayCollapse.MoveDown',
},
},
},
properties: {
addition: {
type: 'void',
title: '添加条目',
'x-component': 'ArrayCollapse.Addition',
},
},
},
array: {
type: 'array',
'x-component': 'ArrayCollapse',
maxItems: 3,
'x-decorator': 'FormItem',
items: {
type: 'object',
'x-component': 'ArrayCollapse.CollapsePanel',
'x-component-props': {
header: '对象数组',
},
properties: {
index: {
type: 'void',
'x-component': 'ArrayCollapse.Index',
},
input: {
type: 'string',
'x-decorator': 'FormItem',
title: 'Input',
required: true,
'x-component': 'Input',
},
remove: {
type: 'void',
'x-component': 'ArrayCollapse.Remove',
},
moveUp: {
type: 'void',
'x-component': 'ArrayCollapse.MoveUp',
},
moveDown: {
type: 'void',
'x-component': 'ArrayCollapse.MoveDown',
},
},
},
properties: {
addition: {
type: 'void',
title: '添加条目',
'x-component': 'ArrayCollapse.Addition',
},
},
},
array_unshift: {
type: 'array',
'x-component': 'ArrayCollapse',
maxItems: 3,
'x-decorator': 'FormItem',
items: {
type: 'object',
'x-component': 'ArrayCollapse.CollapsePanel',
'x-component-props': {
header: '对象数组',
},
properties: {
index: {
type: 'void',
'x-component': 'ArrayCollapse.Index',
},
input: {
type: 'string',
'x-decorator': 'FormItem',
title: 'Input',
required: true,
'x-component': 'Input',
},
remove: {
type: 'void',
'x-component': 'ArrayCollapse.Remove',
},
moveUp: {
type: 'void',
'x-component': 'ArrayCollapse.MoveUp',
},
moveDown: {
type: 'void',
'x-component': 'ArrayCollapse.MoveDown',
},
},
},
properties: {
addition: {
type: 'void',
title: '添加条目(unshift)',
'x-component': 'ArrayCollapse.Addition',
'x-component-props': {
method: 'unshift',
},
},
},
},
},
}
return {
form,
schema,
}
},
methods: {
log(values) {
console.log(values)
},
},
}
</script>
<style lang="scss" scoped></style>
# Effects 联动案例
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaArrayField
name="array"
:maxItems="3"
x-component="ArrayCollapse"
x-decorator="FormItem"
:x-component-props="{
title: '对象数组',
}"
>
<SchemaObjectField
x-component="ArrayCollapse.CollapsePanel"
x-decorator="FormItem"
:x-component-props="{
header: '对象数组',
}"
>
<SchemaVoidField x-component="ArrayCollapse.Index" />
<SchemaStringField
name="aa"
x-decorator="FormItem"
title="AA"
required
description="AA输入123时隐藏BB"
x-component="Input"
/>
<SchemaStringField
name="bb"
x-decorator="FormItem"
title="BB"
required
x-component="Input"
/>
<SchemaStringField
name="cc"
x-decorator="FormItem"
title="CC"
required
description="CC输入123时隐藏DD"
x-component="Input"
/>
<SchemaStringField
name="dd"
x-decorator="FormItem"
title="DD"
required
x-component="Input"
/>
<SchemaVoidField x-component="ArrayCollapse.Remove" />
<SchemaVoidField x-component="ArrayCollapse.MoveUp" />
<SchemaVoidField x-component="ArrayCollapse.MoveDown" />
</SchemaObjectField>
<SchemaVoidField
x-component="ArrayCollapse.Addition"
title="添加条目"
/>
</SchemaArrayField>
</SchemaField>
<Submit @submit="log">提交</Submit>
</FormProvider>
</template>
<script>
import { createForm, onFieldChange, onFieldReact } from '@formily/core'
import { FormProvider, createSchemaField } from '@formily/vue'
import {
FormItem,
FormButtonGroup,
Submit,
Input,
ArrayCollapse,
} from '@formily/antdv'
import { Button } from 'ant-design-vue'
const SchemaField = createSchemaField({
components: {
FormItem,
Input,
ArrayCollapse,
},
})
export default {
components: {
FormProvider,
FormButtonGroup,
Button,
Submit,
...SchemaField,
},
data() {
const form = createForm({
effects: () => {
//主动联动模式
onFieldChange('array.*.aa', ['value'], (field, form) => {
form.setFieldState(field.query('.bb'), (state) => {
state.visible = field.value != '123'
})
})
//被动联动模式
onFieldReact('array.*.dd', (field) => {
field.visible = field.query('.cc').get('value') != '123'
})
},
})
return {
form,
}
},
methods: {
log(values) {
console.log(values)
},
},
}
</script>
<style lang="scss" scoped></style>
# JSON Schema 联动案例
# API
# ArrayCollapse
参考 https://antdv.com/components/collapse-cn/ (opens new window)
# ArrayCollapse.Item
参考 https://antdv.com/components/collapse-cn/ (opens new window)
# ArrayCollapse.Addition
添加按钮
扩展属性
属性名 | 类型 | 描述 | 默认值 |
---|---|---|---|
title | string | 文案 | |
method | 'push' | 'unshift' | 添加方式 | 'push' |
defaultValue | any | 默认值 |
其余参考 https://antdv.com/components/button-cn/ (opens new window)
注意:title 属性可以接收 Field 模型中的 title 映射,也就是在 Field 上传 title 也是生效的
# ArrayCollapse.Remove
删除按钮
属性名 | 类型 | 描述 | 默认值 |
---|---|---|---|
title | string | 文案 |
其余参考 https://antdv.com/components/icon-cn/ (opens new window)
注意:title 属性可以接收 Field 模型中的 title 映射,也就是在 Field 上传 title 也是生效的
# ArrayCollapse.MoveDown
下移按钮
属性名 | 类型 | 描述 | 默认值 |
---|---|---|---|
title | string | 文案 |
其余参考 https://antdv.com/components/icon-cn/ (opens new window)
注意:title 属性可以接收 Field 模型中的 title 映射,也就是在 Field 上传 title 也是生效的
# ArrayCollapse.MoveUp
上移按钮
属性名 | 类型 | 描述 | 默认值 |
---|---|---|---|
title | string | 文案 |
其余参考 https://antdv.com/components/icon-cn/ (opens new window)
注意:title 属性可以接收 Field 模型中的 title 映射,也就是在 Field 上传 title 也是生效的
# ArrayCollapse.Index
索引渲染器
无属性
# ArrayCollapse.useIndex
读取当前渲染行索引的 Hook
# ArrayCollapse.useRecord
读取当前渲染记录的 Hook
← ArrayCards ArrayItems →