ShowMore 展开收起
长文本或插槽内容按行数、最大高度折叠,支持外部控制展开状态。
引入
uni-app 配置 easycom 后使用 <hui-show-more>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<template>
<view>
<hui-space direction="vertical" fill>
<hui-show-more
custom-class="hui-space__item"
:text="content"
:rows="2"
@change="(value) => (lastEvent = String(value))"
/>
<hui-show-more
v-model:expanded="expanded"
custom-class="hui-space__item"
:max-height="64"
:fade="false"
><text>{{ content }}</text></hui-show-more
>
<text class="hui-space__item">{{ expanded }} / {{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const expanded = ref(false)
const content = '长文本 / Long content. '.repeat(30)
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text | string | — | 纯文字内容;传了就忽略默认插槽 |
rows | number | 3 | 收起时显示几行;maxHeight 也传了时用 maxHeight |
maxHeight | number | string | — | 收起时的最大高度,数字按 px;只对默认插槽有效 |
expanded | boolean | undefined | v-model:expanded;受控布尔,声明成 default: undefined |
defaultExpanded | boolean | false | 非受控模式的初始展开状态。 |
expandText | string | — | 不传用 t('common.expand') |
collapseText | string | — | 不传用 t('common.collapse');传 '' 时展开后不显示收起按钮 |
fade | boolean | true | 收起时底部渐隐;text 模式有省略号,默认不渐隐 |
duration | number | 300 | 展开收起动画,ms;0 不动画 |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
update:expanded | (expanded: boolean) |
change | (expanded: boolean) |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
default | — |
toggle | { expanded, overflow, toggle } |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
toggle | (expanded?: boolean): void | 请求切换展开状态 |
remeasure | (): Promise<void> | 重新测量完整内容与折叠高度 |
类型
ShowMoreProps、ShowMoreEmits、ShowMoreExpose.
平台说明
只把 text 作为纯文本,不解释 HTML。默认插槽在微信必须放置可测量的实际内容;父容器从隐藏变为可见后调用 remeasure。无溢出不显示按钮,受控 expanded 不回写不私自展开。
设备冒烟清单.