Collapse 折叠面板
17:06说明
由容器 ux-collapse 与面板 ux-collapse-item 组合使用:容器负责手风琴/多开状态管理与全局样式下发(arrow、arrowColor、headStyle、bodyStyle、itemStyle、hoverClass),面板负责标题与内容渲染、点击切换、展开高度动画以及 change 事件。面板通过 name 标识自身,未设置时按挂载顺序自动编号;容器默认手风琴模式(accordion=true),此时最多展开一个面板。
兼容性
| 平台 | 支持情况 |
|---|---|
| App | ✅ 支持 |
| H5 | ✅ 支持 |
| 微信小程序 | ✅ 支持(展开/收起无过渡动画,直接用 auto 高度切换) |
示例
基础用法
非手风琴模式下可同时展开多个面板,点击标题切换展开/收起。
查看源码
vue
<template>
<ux-collapse :accordion="false">
<ux-collapse-item title="标题一">
<text class="body">内容一:非手风琴模式下可同时展开多个面板</text>
</ux-collapse-item>
<ux-collapse-item title="标题二">
<text class="body">内容二:点击标题切换展开/收起</text>
</ux-collapse-item>
<ux-collapse-item title="标题三">
<text class="body">内容三:互不影响,可全部展开</text>
</ux-collapse-item>
</ux-collapse>
</template>
<style scoped>
.body {
font-size: 14px;
color: #909399;
line-height: 1.6;
}
</style>手风琴模式
accordion=true 时每次只展开一个面板,展开其他面板会自动收起当前面板。
查看源码
vue
<template>
<ux-collapse accordion>
<ux-collapse-item title="标题一">
<text class="body">内容一:手风琴模式同时只能展开一个</text>
</ux-collapse-item>
<ux-collapse-item title="标题二">
<text class="body">内容二:手风琴模式同时只能展开一个</text>
</ux-collapse-item>
</ux-collapse>
</template>
<style scoped>
.body {
font-size: 14px;
color: #909399;
line-height: 1.6;
}
</style>初始展开
通过 open 属性设置某个面板初始即为展开状态。
查看源码
vue
<template>
<ux-collapse>
<ux-collapse-item title="标题一" open>
<text class="body">内容一:默认展开状态</text>
</ux-collapse-item>
<ux-collapse-item title="标题二">
<text class="body">内容二:默认收起状态</text>
</ux-collapse-item>
</ux-collapse>
</template>
<style scoped>
.body {
font-size: 14px;
color: #909399;
line-height: 1.6;
}
</style>自定义标题
通过 title 插槽自定义面板标题内容。
查看源码
vue
<template>
<ux-collapse>
<ux-collapse-item>
<template #title>
<view class="head">
<ux-icon name="star" size="15" color="#fa3534" />
<text class="head-text">自定义标题</text>
</view>
</template>
<text class="body">通过 title 插槽自定义标题内容</text>
</ux-collapse-item>
</ux-collapse>
</template>
<style scoped>
.head {
display: flex;
flex-direction: row;
align-items: center;
}
.head-text {
font-size: 15px;
color: #303133;
margin-left: 4px;
}
.body {
font-size: 14px;
color: #909399;
line-height: 1.6;
}
</style>自定义样式
通过 itemStyle、headStyle、bodyStyle 自定义面板外观,arrowColor 设置箭头颜色。
查看源码
vue
<template>
<ux-collapse
arrow-color="#2979ff"
:body-style="customBodyStyle"
:head-style="customHeadStyle"
:item-style="customItemStyle"
>
<ux-collapse-item title="自定义边框与圆角">
<text class="body">itemStyle 为每个面板添加了边框、圆角与外边距</text>
</ux-collapse-item>
<ux-collapse-item title="自定义头部与内容样式">
<text class="body">headStyle 设置了头部背景色,bodyStyle 设置了内容内边距</text>
</ux-collapse-item>
</ux-collapse>
</template>
<script setup lang="uts">
const customItemStyle: UTSJSONObject = {
'border-width': '1px',
'border-style': 'solid',
'border-color': '#ebeef5',
'border-radius': '8px',
'margin-top': '12px',
'overflow': 'hidden'
};
const customHeadStyle: UTSJSONObject = {
'background-color': '#f8f9fb',
'padding-left': '12px',
'padding-right': '12px'
};
const customBodyStyle: UTSJSONObject = {
'padding-left': '12px',
'padding-right': '12px'
};
</script>
<style scoped>
.body {
font-size: 14px;
color: #909399;
line-height: 1.6;
}
</style>禁用状态
disabled=true 的面板不可点击展开或收起。
查看源码
vue
<template>
<ux-collapse>
<ux-collapse-item title="禁用面板" disabled>
<text class="body">禁用状态下无法展开</text>
</ux-collapse-item>
</ux-collapse>
</template>
<style scoped>
.body {
font-size: 14px;
color: #909399;
line-height: 1.6;
}
</style>嵌套折叠
ux-collapse 支持嵌套:在外层面板内再放一个 ux-collapse,两层独立展开,外层高度自动适应。
查看源码
vue
<template>
<ux-collapse>
<ux-collapse-item title="外层面板(默认展开)" open>
<text class="body">外层内容:下方嵌套了一组内层折叠面板</text>
<ux-collapse :item-style="nestedItemStyle">
<ux-collapse-item title="内层面板一">
<text class="body">内层内容一:点击内层标题可独立展开,不影响外层状态</text>
</ux-collapse-item>
<ux-collapse-item title="内层面板二(默认展开)" open>
<text class="body">内层内容二:外层容器高度会随内层展开/收起自动变化</text>
</ux-collapse-item>
</ux-collapse>
</ux-collapse-item>
</ux-collapse>
</template>
<script setup lang="uts">
const nestedItemStyle: UTSJSONObject = {
'border-width': '1px',
'border-style': 'solid',
'border-color': '#ebeef5',
'border-radius': '6px',
'margin-top': '8px',
'padding-left': '12px',
'padding-right': '12px',
'background-color': '#fafafa',
'overflow': 'hidden'
};
</script>
<style scoped>
.body {
font-size: 14px;
color: #909399;
line-height: 1.6;
}
</style>API
Props
ux-collapse
| 属性 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
customStyle | 自定义根节点样式 | UTSJSONObject | - | - |
customClass | 自定义根节点样式类 | string | '' | - |
accordion | 是否手风琴模式 | boolean | true | - |
headStyle | 面板头部的样式 | UTSJSONObject | - | - |
bodyStyle | 面板主体的样式 | UTSJSONObject | - | - |
itemStyle | 每个面板的样式 | UTSJSONObject | - | - |
arrow | 是否显示右侧箭头 | boolean | true | - |
arrowColor | 箭头的颜色 | string | '' | - |
hoverClass | 标题部分按压时的样式类,"none" 为无效果 | string | 'ux-collapse--hover' | - |
ux-collapse-item
| 属性 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
customStyle | 自定义根节点样式 | UTSJSONObject | - | - |
customClass | 自定义根节点样式类 | string | '' | - |
title | 面板标题 | string | '' | - |
align | 标题的对齐方式 | string | 'left' | - |
disabled | 面板是否可以打开或收起 | boolean | false | - |
open | 面板初始状态是否打开 | boolean | false | - |
name | 唯一标识符,不设置时按挂载顺序自动编号 | string | number | '' | - |
activeStyle | 不显示箭头时,展开态叠加到标题上的活动样式 | UTSJSONObject | - | - |
index | 用于事件回调,标识被点击的面板 | string | number | '' | - |
Events
ux-collapse-item
| 事件名 | 说明 | 回调参数 | 版本 |
|---|---|---|---|
change | 某个面板被打开或收起时触发 | value: { index: string | number; name: string | number; show: boolean } | - |
Slots
ux-collapse
| 插槽名 | 说明 | 版本 |
|---|---|---|
default | 默认插槽,放置 ux-collapse-item 面板 | - |
ux-collapse-item
| 插槽名 | 说明 | 版本 |
|---|---|---|
title | 自定义标题文字(箭头仍会显示) | - |
title-all | 自定义整个头部内容(含箭头) | - |
default | 面板内容 | - |
注意事项
ux-collapse-item依赖父级ux-collapse提供上下文;脱离容器单独使用时状态不联动,点击仅派发change。- 面板唯一标识的取值优先级为
name>index> 挂载顺序,未显式设置时按顺序自动编号。 - 手风琴模式(
accordion=true)最多展开一个,非手风琴模式可多开;open只在挂载时注册初始展开态,手风琴模式下只有第一个open生效。 - 微信小程序端展开/收起无过渡动画(
transition被禁用),展开高度直接用auto;App/H5 使用测量得到的内容高度,并借助UniResizeObserver监听,嵌套或动态内容时外层高度会自动适应。 arrow、arrowColor、headStyle、bodyStyle、itemStyle、hoverClass均由容器统一下发;面板单独使用时走自身默认值(箭头默认显示、箭头色取tipsColor)。title插槽只替换标题文字,箭头仍会显示;需要连箭头一起自定义时改用title-all,它会接管整个头部。activeStyle仅在「已展开且不显示箭头」时叠加到标题样式上。change由面板派发,参数为{ index, name, show };disabled=true的面板点击被拦截,不会切换状态。