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 }} 項;指示器支援Enter、空白和 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 點擊切換;兩者均可用Enter、空白打開,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)
}