Skip to content

Collapse 折叠面板

介绍

将一组内容放置在多个折叠面板中,点击面板的标题可以展开或收缩其内容。

引入

ts
import { IBestCollapse, IBestCollapseItem } from "@ibestservices/ibest-ui";

代码演示

基础用法

基础用法

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State activeName: string[] = ['1']
  @State groupId: string = 'group'
  build() {
    IBestCollapse({groupId: this.groupId, activeName: this.activeName}){
      IBestCollapseItem({
        groupId: this.groupId,
        name: '1',
        index: 0,
        title: '标题1'
      }){
        Text('代码是写出来给人看的,附带能在机器上运行。').fontSize(14).fontColor("#969799")
      }
      IBestCollapseItem({
        groupId: this.groupId,
        name: '2',
        index: 1,
        title: '标题2'
      }){
        Text('技术无非就是那些开发它的人的共同灵魂。').fontSize(14).fontColor("#969799")
      }
      IBestCollapseItem({
        groupId: this.groupId,
        name: '3',
        index: 2,
        title: '标题3'
      }){
        Text('在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。').fontSize(14).fontColor("#969799")
      }
    }
  }
}

手风琴

手风琴

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State activeName: string = '1'
  @State groupId: string = 'group'
  build() {
    IBestCollapse({groupId: this.groupId, activeName: this.activeName, accordion: true}){
      IBestCollapseItem({
        groupId: this.groupId,
        name: '1',
        index: 0,
        title: '标题1'
      }){
        Text('代码是写出来给人看的,附带能在机器上运行。').fontSize(14).fontColor("#969799")
      }
      IBestCollapseItem({
        groupId: this.groupId,
        name: '2',
        index: 1,
        title: '标题2'
      }){
        Text('技术无非就是那些开发它的人的共同灵魂。').fontSize(14).fontColor("#969799")
      }
      IBestCollapseItem({
        groupId: this.groupId,
        name: '3',
        index: 2,
        title: '标题3'
      }){
        Text('在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。').fontSize(14).fontColor("#969799")
      }
    }
  }
}

禁用状态

禁用状态

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State activeName: string[] = ['2']
  @State groupId: string = 'group'
  build() {
    IBestCollapse({groupId: this.groupId, activeName: this.activeName}){
      IBestCollapseItem({
        groupId: this.groupId,
        name: '1',
        index: 0,
        title: '标题1'
      }){
        Text('代码是写出来给人看的,附带能在机器上运行。').fontSize(14).fontColor("#969799")
      }
      IBestCollapseItem({
        groupId: this.groupId,
        name: '2',
        index: 1,
        title: '标题2',
        disabled: true
      }){
        Text('技术无非就是那些开发它的人的共同灵魂。').fontSize(14).fontColor("#969799")
      }
      IBestCollapseItem({
        groupId: this.groupId,
        name: '3',
        index: 2,
        title: '标题3'
      }){
        Text('在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。').fontSize(14).fontColor("#969799")
      }
    }
  }
}

自定义标题

自定义标题

点我查看代码
ts
@Entry
@Component
struct DemoPage {
  @State activeName: string[] = ['1']
  @State groupId: string = 'group'
  build() {
    IBestCollapse({groupId: this.groupId, activeName: this.activeName}){
      IBestCollapseItem({
        groupId: this.groupId,
        name: '1',
        index: 0,
        icon: $r("app.media.icon_like"),
        title: '标题1',
        value: "右侧内容"
      }){
        Text('代码是写出来给人看的,附带能在机器上运行。').fontSize(14).fontColor("#969799")
      }
    }
  }
}

实例方法

实例方法

点我查看代码
ts
import { IBestCollapseController } from '@ibestservices/ibest-ui';

@Entry
@Component
struct DemoPage {
  @State activeName: string[] = ['2']
  @State groupId: string = 'group'
  private controller: IBestCollapseController = new IBestCollapseController()
  build() {
    Column{
      IBestCollapse({
        groupId: this.groupId,
        activeName: this.activeName,
        controller: this.controller
      }){
        IBestCollapseItem({
          groupId: this.groupId,
          name: '1',
          index: 0,
          title: '标题1'
        }){
          Text('代码是写出来给人看的,附带能在机器上运行。').fontSize(14).fontColor("#969799")
        }
        IBestCollapseItem({
          groupId: this.groupId,
          name: '2',
          index: 1,
          title: '标题2',
          disabled: true
        }){
          Text('技术无非就是那些开发它的人的共同灵魂。').fontSize(14).fontColor("#969799")
        }
        IBestCollapseItem({
          groupId: this.groupId,
          name: '3',
          index: 2,
          title: '标题3'
        }){
          Text('在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。').fontSize(14).fontColor("#969799")
        }
      }
      Column(){
        Row({space: 16}){
          IBestButton({
            type: "primary",
            text: "全部展开",
            buttonSize: "small",
            onClickBtn: () => {
              this.controller.toggleAll(true)
            }
          })
          IBestButton({
            type: "primary",
            text: "全部收起",
            buttonSize: "small",
            onClickBtn: () => {
              this.controller.toggleAll(false)
            }
          })
          IBestButton({
            type: "primary",
            text: "切换",
            buttonSize: "small",
            onClickBtn: () => {
              this.controller.toggleAll()
            }
          })
        }
        .padding({top: 16, left: 16})
        Row({space: 16}){
          IBestButton({
            type: "primary",
            text: "跳过禁用展开",
            buttonSize: "small",
            onClickBtn: () => {
              this.controller.toggleAll({
                expanded: true,
                skipDisabled: true
              })
            }
          })
          IBestButton({
            type: "primary",
            text: "跳过禁用收起",
            buttonSize: "small",
            onClickBtn: () => {
              this.controller.toggleAll({
                expanded: false,
                skipDisabled: true
              })
            }
          })
          IBestButton({
            type: "primary",
            text: "跳过禁用切换",
            buttonSize: "small",
            onClickBtn: () => {
              this.controller.toggleAll({
                skipDisabled: true
              })
            }
          })
        }
        .padding({top: 16, left: 16})
      }
    }
  }
}

API

IBestCollapse @Props

参数说明类型默认值
groupId分组id, 需保证全局唯一性string''
activeName当前展开面板的name手风琴模式:string
非手风琴模式:string | string[]
[]
accordion是否开启手风琴模式booleanfalse
isShowBorder是否显示外边框booleantrue
controller实例方法IBestCollapseController-

IBestCollapse 插槽

插槽名说明类型
defaultBuilder默认内容CustomBuilder

IBestCollapse Events

事件名说明回调参数
onChange面板状态变化回调name: string | string[]

IBestCollapseController 实例方法

方法名说明参数默认值
toggleAll切换所有面板状态, 传 true 为全部展开,false 为全部收起,不传参为全部切换option?: boolean | ToggleOptionundefined

ToggleOption 数据类型

参数说明类型默认值
expanded是否展开, 传 true 为全部展开,false 为全部收起,不传参为全部切换booleanundefined
skipDisabled是否跳过禁用的选项booleanfalse

IBestCollapseItem @Props

参数说明类型默认值
groupId分组id, 与IBestCollapse groupId 一致string''
name唯一标识符string''
index索引number''
icon标题栏左侧图标ResourceStr''
iconColor标题栏左侧图标颜色ResourceColor#323233
iconSize选项图标string | number16
title标题文字ResourceStr''
titleColor标题文字颜色ResourceColor#323233
titleFontSize标题文字大小string | number14
value标题右侧文字ResourceStr''
valueColor标题右侧文字颜色ResourceColor#969799
valueFontSize标题右侧文字大小string | number14
isShowBorder是否显示内边框booleantrue
disabled是否禁用booleanfalse
readOnly是否只读booleanfalse

IBestCollapseItem 插槽

插槽名说明类型
defaultBuilder默认内容CustomBuilder