OverflowList 溢出列表
一行显示标签、成员或操作摘要,空间不足时收起部分项,用数量指示器查看余下内容。
引入
ts
import { HuiOverflowList } from '@nscode/handy-ui'
import type { OverflowListProps } from '@nscode/handy-ui'
可调整宽度的标签行
拖动右下角改变容器宽度。change 只在隐藏数量改变时触发;内容都能放下时不显示指示器。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOverflowList, HuiSpace, HuiTag } from '@nscode/handy-ui'
const items = ['需求分析', '页面设计', '接口开发', '联调验收', '部署上线', '运行维护']
const hidden = ref(0)
</script>
<template>
<HuiSpace direction="vertical" fill>
<div class="resizable">
<div style="display: flex; align-items: center; gap: 8px; min-width: 0">
<strong style="flex: none">标签</strong>
<HuiOverflowList :items="items" @change="hidden = $event">
<template #item="{ item }"
><HuiTag>{{ item }}</HuiTag></template
>
</HuiOverflowList>
</div>
</div>
<span>当前收起 {{ hidden }} 项;指示器支持回车、空格和 Esc。</span>
</HuiSpace>
</template>
<style scoped>
.resizable {
flex: none;
width: 320px;
max-width: 100%;
min-width: 120px;
padding: 12px;
resize: horizontal;
overflow: auto;
border: 1px solid var(--hui-border-color);
}
</style>
收起方向与原始下标
from="end" 保留前面的项,指示器在末尾;from="start" 保留后面的项,指示器在开头。item 插槽的 index 始终是原数组下标。用 item-key 提供稳定唯一标识,避免数据重排后组件状态串项。
vue
<script setup lang="ts">
import { HuiOverflowList, HuiSpace, HuiTag } from '@nscode/handy-ui'
const items = [
{ id: 'a', name: '项目管理' },
{ id: 'b', name: '任务分配' },
{ id: 'c', name: '进度跟踪' },
{ id: 'd', name: '结果归档' }
]
const keyOf = (item: { id: string }) => item.id
</script>
<template>
<HuiSpace direction="vertical" fill>
<div style="flex: none; width: 240px; max-width: 100%">
<p>end,字段名 key</p>
<HuiOverflowList :items="items" item-key="id" from="end">
<template #item="{ item, index }"
><HuiTag>{{ index }} · {{ item.name }}</HuiTag></template
>
</HuiOverflowList>
</div>
<div style="flex: none; width: 240px; max-width: 100%">
<p>start,函数 key</p>
<HuiOverflowList :items="items" :item-key="keyOf" from="start">
<template #item="{ item, index }"
><HuiTag>{{ index }} · {{ item.name }}</HuiTag></template
>
</HuiOverflowList>
</div>
</HuiSpace>
</template>
最少显示数量与间距
min 优先于可用宽度:即使放不下也保留指定项数,超出根容器的部分会裁剪。需要完整查看时,为外部容器提供足够宽度。gap 数字及数字字符串按 px;也可以写 rem、em、calc(...) 等单个 CSS 长度。
vue
<script setup lang="ts">
import { HuiOverflowList, HuiSpace, HuiTag } from '@nscode/handy-ui'
const items = ['基础资料', '计划安排', '执行记录', '统计分析']
const gaps = [0, 8, '1rem', 'calc(1rem + 4px)']
</script>
<template>
<HuiSpace direction="vertical" fill>
<div style="flex: none; width: 140px; padding-bottom: 8px; overflow: auto">
<p>min=2,超出部分裁剪</p>
<HuiOverflowList :items="items" :min="2">
<template #item="{ item }"
><HuiTag>{{ item }}</HuiTag></template
>
</HuiOverflowList>
</div>
<div v-for="gap in gaps" :key="String(gap)" style="flex: none; width: 260px; max-width: 100%">
<p>gap={{ gap }}</p>
<HuiOverflowList :items="items" :gap="gap">
<template #item="{ item }"
><HuiTag>{{ item }}</HuiTag></template
>
</HuiOverflowList>
</div>
</HuiSpace>
</template>
查看方式与自定义插槽
hover 鼠标悬停查看,click 点击切换;两者均可用回车、空格打开,Esc 关闭。none 只显示数量,不提供按钮语义或浮层。overflow 插槽只放展示内容,不要再嵌套按钮、链接等交互控件。popover 可完整替换被收起项的列表。
vue
<script setup lang="ts">
import { HuiOverflowList, HuiSpace, HuiTag } from '@nscode/handy-ui'
import type { OverflowListTrigger } from '@nscode/handy-ui'
const triggers: OverflowListTrigger[] = ['hover', 'click', 'none']
const items = ['系统管理', '权限管理', '操作审计', '运行监控', '消息通知']
</script>
<template>
<HuiSpace direction="vertical" fill>
<div
v-for="trigger in triggers"
:key="trigger"
style="flex: none; width: 240px; max-width: 100%"
>
<p>{{ trigger }}</p>
<HuiOverflowList :items="items" :overflow-trigger="trigger">
<template #item="{ item }"
><HuiTag>{{ item }}</HuiTag></template
>
</HuiOverflowList>
</div>
<div style="flex: none; width: 240px; max-width: 100%">
<p>自定义指示器与浮层</p>
<HuiOverflowList :items="items" overflow-trigger="click">
<template #item="{ item }"
><HuiTag type="success">{{ item }}</HuiTag></template
>
<template #overflow="{ count }"
><HuiTag type="warning">另外 {{ count }} 项</HuiTag></template
>
<template #popover="{ items: hidden }">
<HuiSpace direction="vertical"
><strong>已收起的功能</strong><span>{{ hidden.join('、') }}</span></HuiSpace
>
</template>
</HuiOverflowList>
</div>
</HuiSpace>
</template>
表格单元格与动态数据
组件观察容器和每个量尺子项,标签文字、图片尺寸或字体改变时重新计算。一帧内多次通知合并为一次量宽。
vue
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOverflowList, HuiSpace, HuiTable, HuiTag } from '@nscode/handy-ui'
import type { TableColumnConfig } from '@nscode/handy-ui'
const rows = ref([
{ id: 1, name: '张三', roles: ['资料维护', '计划编制', '审批处理', '数据导出'] },
{ id: 2, name: '李四', roles: ['查询统计', '日志审计'] }
])
const columns: TableColumnConfig[] = [
{ prop: 'name', label: '姓名', width: 100 },
{ prop: 'roles', label: '权限' }
]
const append = () => rows.value[0].roles.push(`扩展权限 ${rows.value[0].roles.length + 1}`)
</script>
<template>
<HuiSpace direction="vertical" fill style="flex: none; max-width: 460px">
<HuiButton @click="append">追加第一行权限</HuiButton>
<HuiTable :data="rows" :columns="columns" border>
<template #roles="{ row }">
<HuiOverflowList :items="row.roles" overflow-trigger="click">
<template #item="{ item }"
><HuiTag size="small">{{ item }}</HuiTag></template
>
</HuiOverflowList>
</template>
</HuiTable>
</HuiSpace>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| items | T[] | 必填 | 原始数据 |
| min | number | 0 | 最少显示项数;非负整数,超出项数时显示全部;小数向下取整 |
| from | OverflowListFrom | 'end' | 从末尾或开头收起 |
| gap | number | string | 8 | 数字及数字字符串按 px,或单个 CSS 长度 |
| overflowTrigger | OverflowListTrigger | 'hover' | hover、click 或 none |
| itemKey | string | ((item: T, index: number) => string | number) | 原始下标 | 稳定唯一标识 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
| change | (hiddenCount: number) | 隐藏数量变化;恢复全部显示时为 0;初始无隐藏不触发 |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
| item | { item: T, index: number } | 单项;index 为原始下标;默认直接显示数据 |
| overflow | { items: T[], count: number } | 隐藏项及数量;默认小号标签 +N;只放展示内容 |
| popover | { items: T[] } | 隐藏项浮层;默认复用 item 插槽竖排 |
方法
无公开方法。尺寸和数据更新后自动重新测量。
类型
ts
import type {
OverflowListProps,
OverflowListFrom,
OverflowListTrigger,
OverflowListItemSlotProps,
OverflowListOverflowSlotProps,
OverflowListPopoverSlotProps
} from '@nscode/handy-ui'
type OverflowListFrom = 'start' | 'end'
type OverflowListTrigger = 'hover' | 'click' | 'none'
interface OverflowListProps<T = unknown> {
items: T[]
min?: number
from?: OverflowListFrom
gap?: number | string
overflowTrigger?: OverflowListTrigger
itemKey?: string | ((item: T, index: number) => string | number)
}