TUI

0.3.7
Collapse 折叠面板
用于展示可折叠的内容区域,支持平滑展开与收起动画,支持自定义折叠高度和动态内容更新。
基础用法
点击按钮切换折叠状态,默认折叠高度为 0,动画时长 300ms。
这是基础折叠面板内容,支持任意自定义内容。
示例代码
vue
<template>
  <TsButton @click="isExpandBasic = !isExpandBasic">{{ isExpandBasic ? '收起' : '展开' }}</TsButton>
  <TsCollapse v-model="isExpandBasic">
    <div style="padding: 16px; background: #f5f5f5;">
      这是基础折叠面板内容,支持任意自定义内容。
    </div>
  </TsCollapse>
</template>

<script setup>
  import { ref } from 'vue';
  const isExpandBasic = ref(true);
</script>
自定义折叠高度
折叠时保留 40px 高度,保持折叠面板依然可见部分内容。
这里是带有自定义折叠高度的内容。
示例代码
vue
<template>
  <TsButton @click="isExpandCustomHeight = !isExpandCustomHeight">{{ isExpandCustomHeight ? '收起' : '展开' }}</TsButton>
  <TsCollapse v-model="isExpandCustomHeight" :collapsedHeight="40" :transitionDuration="300">
    <div style="padding: 16px; background: #e0f7fa;">
      这里是带有自定义折叠高度的内容。
    </div>
  </TsCollapse>
</template>

<script setup>
  import { ref } from 'vue';
  const isExpandCustomHeight = ref(false);
</script>
动态内容更新展开高度
动态增加内容时, 通过forceOnExpand 属性来控制是否强制触发回流更新展开高度。
  • 列表项 1
  • 列表项 2
  • 列表项 3
示例代码
vue
<template>
  <TsButton @click="isExpandDynamic = !isExpandDynamic">{{ isExpandDynamic ? '收起' : '展开' }}</TsButton>
  <TsButton @click="addItem" style="margin-left:8px;">添加内容</TsButton>
  <TsCollapse ref="dynamicCollapseRef" v-model="isExpandDynamic" :collapsedHeight="0" :transitionDuration="300">
    <div style="padding: 16px; background: #f3e5f5;">
      <ul>
        <li v-for="(item, idx) in dynamicList" :key="idx">{{ item }}</li>
      </ul>
    </div>
  </TsCollapse>
</template>

<script setup>
  import { ref } from 'vue';
  const isExpandDynamic = ref(false);
  const dynamicList = ref(['列表项 1', '列表项 2', '列表项 3']);

  function addItem() {
    dynamicList.value.push(`列表项 ${dynamicList.value.length + 1}`);
    // 需要手动调用更新高度
    dynamicCollapseRef.value?.expandAndUpdateHeight();
  }
</script>
自定义插槽内容
通过默认插槽传入复杂结构,折叠面板可以承载任意组件或元素。

标题

这里是通过插槽传入的复杂内容,支持多元素嵌套。

示例代码
vue
<template>
  <TsButton @click="isExpandSlot = !isExpandSlot">{{ isExpandSlot ? '收起' : '展开' }}</TsButton>
  <TsCollapse v-model="isExpandSlot" :collapsedHeight="20" :transitionDuration="500">
    <div style="padding: 16px; background: #fff3e0;">
      <h3>标题</h3>
      <p>这里是通过插槽传入的复杂内容,支持多元素嵌套。</p>
      <TsButton @click="alert('按钮内部点击事件')">内部按钮</TsButton>
    </div>
  </TsCollapse>
</template>

<script setup>
  import { ref } from 'vue';
  const isExpandSlot = ref(false);
</script>
属性 Props
属性名
说明
类型
可选值
默认值
版本说明
modelValue
是否展开,支持使用 v-model 双向绑定
Boolean
-
true
collapsedHeight
折叠状态下面板的高度(px)
Number
-
0
transitionDuration
折叠/展开动画过渡时间(毫秒)
Number
-
300
v-model:forceOnExpand
展开状态下更新折叠面板高度
Number
-
300
插槽 Slots
插槽名
说明
default
自定义折叠面板内容
方法 Functions
方法名
说明
参数
返回值
toggleCollapse
切换折叠展开状态
-
-
getCollapseHeight
获取当前容器的实时高度(px)
-
Number
updateExpandedHeight
更新展开状态的高度,仅在展开状态下生效
-
Number
expandAndUpdateHeight
展开并更新该状态下高度
-
-