List 清單
把一組結構相近的內容按行排列,每一行用 HuiListItem。可以帶整個清單的頭部、底部,以及每一項自己的標題和附加資訊。
引入
import { HuiList, HuiListItem } from '@nscode/handy-ui'
基礎用法
HuiList 裡放若干個 HuiListItem,每一項的內容寫在預設插槽裡。滑鼠移到清單項上時有淺色底。
<script setup lang="ts">
import { HuiList, HuiListItem } from '@nscode/handy-ui'
</script>
<template>
<HuiList>
<HuiListItem>寫本周周報</HuiListItem>
<HuiListItem>評審訂單頁設計稿</HuiListItem>
<HuiListItem>回復客戶郵件</HuiListItem>
</HuiList>
</template>
帶邊框
border 給整個清單加一圈邊框和圓角。上面是預設的不帶邊框,下面加了 border:
<template>
<HuiSpace direction="vertical" fill>
<HuiList header="不帶邊框">
<HuiListItem>寫本周周報</HuiListItem>
<HuiListItem>評審訂單頁設計稿</HuiListItem>
</HuiList>
<HuiList header="border" border>
<HuiListItem>寫本周周報</HuiListItem>
<HuiListItem>評審訂單頁設計稿</HuiListItem>
</HuiList>
</HuiSpace>
</template>
頭部和底部
header、footer 傳文字,顯示在清單的最上面和最下面。
<template>
<HuiList header="待辦事項" footer="共 3 項" border>
<HuiListItem>寫本周周報</HuiListItem>
<HuiListItem>評審訂單頁設計稿</HuiListItem>
<HuiListItem>回復客戶郵件</HuiListItem>
</HuiList>
</template>
要放按鈕等其他內容時用 header、footer 插槽,寫了插槽就不再顯示對應的 prop。
<script setup lang="ts">
import { HuiList, HuiListItem, HuiButton } from '@nscode/handy-ui'
</script>
<template>
<HuiList border>
<template #header>
<div style="display: flex; justify-content: space-between; align-items: center">
<span>待辦事項</span>
<HuiButton size="small" type="primary" icon="plus">新增</HuiButton>
</div>
</template>
<HuiListItem>寫本周周報</HuiListItem>
<HuiListItem>評審訂單頁設計稿</HuiListItem>
<template #footer>
<HuiButton type="primary" variant="link" href="https://example.com" target="_blank">
查看全部
</HuiButton>
</template>
</HuiList>
</template>
清單項的標題和附加資訊
HuiListItem 也有 header、footer:標題顯示在內容上面,附加資訊用小一號的字顯示在內容下面。同樣可以換成插槽。
<script setup lang="ts">
import { HuiButton, HuiList, HuiListItem, HuiSpace, HuiTag } from '@nscode/handy-ui'
</script>
<template>
<HuiList border>
<HuiListItem header="版本 1.2.0 發布" footer="2026-03-01">
新增表格固定列,修復分頁頁碼顯示的問題。
</HuiListItem>
<HuiListItem footer="2026-02-15">
<template #header>
<HuiSpace :size="8">
<strong>版本 1.1.0 發布</strong>
<HuiTag size="small" type="success">穩定版</HuiTag>
</HuiSpace>
</template>
新增時間線元件。
</HuiListItem>
<HuiListItem header="版本 1.0.0 發布">
第一個正式版本。
<template #footer>
<HuiSpace :size="8">
<span>2026-01-10</span>
<HuiButton size="small" variant="link" type="primary">查看更新說明</HuiButton>
</HuiSpace>
</template>
</HuiListItem>
</HuiList>
</template>
尺寸
size 可選 large、default、small,同時影響頭部、底部和每個清單項的內距和字級。內距是 20、16、12px;文字 large、default 都是 14px(large 的頭部標題是 16px),small 是 12px。不寫 size 時跟隨 ConfigProvider,都沒有時是 default。
<template>
<HuiSpace direction="vertical" fill>
<HuiList size="large" header="large" footer="共 2 項" border>
<HuiListItem header="寫本周周報" footer="今天 18:00 前">周五下班前發給主管</HuiListItem>
<HuiListItem>評審訂單頁設計稿</HuiListItem>
</HuiList>
<HuiList header="default" footer="共 2 項" border>
<HuiListItem header="寫本周周報" footer="今天 18:00 前">周五下班前發給主管</HuiListItem>
<HuiListItem>評審訂單頁設計稿</HuiListItem>
</HuiList>
<HuiList size="small" header="small" footer="共 2 項" border>
<HuiListItem header="寫本周周報" footer="今天 18:00 前">周五下班前發給主管</HuiListItem>
<HuiListItem>評審訂單頁設計稿</HuiListItem>
</HuiList>
</HuiSpace>
</template>
載入中
loading 為 true 時在清單項上蓋一層半透明的遮罩並顯示載入圖示,這時清單項不能點擊。頭部和底部不受影響。載入中還沒有清單項時不顯示空態,載入結束仍沒有資料才顯示。下面可以用開關直接切換,也可以點“刷新”模擬請求 1.5 秒:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiList, HuiListItem, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const loading = ref(true)
const batch = ref(1)
const messages = ['張偉評論了你的文章', '王芳給你分配了任務', '系統將於今晚 23:00 維護']
const refresh = () => {
loading.value = true
window.setTimeout(() => {
batch.value++
loading.value = false
}, 1500)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiSwitch v-model="loading" active-text="loading" />
<HuiButton icon="refresh" :loading="loading" @click="refresh">刷新</HuiButton>
</HuiSpace>
<HuiList :loading="loading" header="最新消息" :footer="`第 ${batch} 次刷新`" border>
<HuiListItem v-for="text in messages" :key="text">{{ text }}</HuiListItem>
</HuiList>
</HuiSpace>
</template>
點擊清單項
HuiListItem 沒有 click 事件,直接在它上面寫 @click 就行(會落到它的根元素上)。下面點一項把它設為目前項,用自己的類名加上選中底色:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiList, HuiListItem } from '@nscode/handy-ui'
const contacts = [
{ id: 1, name: '张伟', dept: '研发部' },
{ id: 2, name: '王芳', dept: '市场部' },
{ id: 3, name: '李娜', dept: '财务部' }
]
const activeId = ref(1)
</script>
<template>
<HuiList class="contacts" border>
<HuiListItem
v-for="item in contacts"
:key="item.id"
:header="item.name"
:class="{ 'is-active': item.id === activeId }"
style="cursor: pointer"
@click="activeId = item.id"
>
{{ item.dept }}
</HuiListItem>
<template #footer>目前選中:{{ contacts.find((c) => c.id === activeId)?.name }}</template>
</HuiList>
</template>
<style scoped>
.contacts .is-active,
.contacts .is-active:hover {
background-color: var(--hui-color-primary-bg);
}
</style>
呈現陣列
清單項一般用 v-for 產生。元件沒有空狀態,陣列為空時自行在預設插槽裡寫提示,例如放一個 HuiEmpty。清除和刪除都先確認對象與範圍;取消、關閉或 Esc 不改資料。確認期間新增、替換或修改待辦、切換唯讀狀態或卸載示例,舊確認會失效;唯讀開關僅保護本例刪除入口,不是 HuiList 的屬性。
<script setup lang="ts">
import { onUnmounted, ref, watch } from 'vue'
import {
HuiButton,
HuiEmpty,
HuiList,
HuiListItem,
HuiMessageBox,
HuiSpace,
HuiSwitch
} from '@nscode/handy-ui'
interface Todo {
id: number
title: string
desc: string
}
const candidates = ['整理會議紀錄', '更新專案排程', '準備季度報告', '追蹤合約收款']
const items = ref<Todo[]>([
{ id: 1, title: '寫本週週報', desc: '週五下班前寄給主管' },
{ id: 2, title: '評審訂單頁設計稿', desc: '週三下午兩點,3 號會議室' }
])
const readonly = ref(false)
const pending = ref(false)
let alive = true
let revision = 0
watch([items, readonly], () => revision++, { deep: true, flush: 'sync' })
onUnmounted(() => {
alive = false
revision++
})
let nextId = 3
const add = () => {
const title = candidates[(nextId - 3) % candidates.length]
items.value.push({ id: nextId++, title, desc: '新增的待辦' })
}
const requestDelete = async (id?: number) => {
if (!alive || readonly.value || pending.value || !items.value.length) return
const target = items.value
const row = id === undefined ? undefined : target.find((item) => item.id === id)
if (id !== undefined && !row) return
const request = revision
pending.value = true
try {
await HuiMessageBox.confirm({
title: row ? '刪除待辦' : '清除全部待辦',
message: row
? `將刪除待辦「${row.title}」及其說明,僅影響這一項,不影響其他待辦。`
: `將清除目前清單的全部 ${target.length} 項待辦及其說明,不影響其他資料。`,
type: 'warning'
})
if (!alive || readonly.value || request !== revision || items.value !== target) return
items.value = row ? target.filter((item) => item !== row) : []
} catch {
// 取消、關閉或 Esc 不改清單。
} finally {
if (alive) pending.value = false
}
}
const remove = (id: number) => requestDelete(id)
const clear = () => requestDelete()
</script>
<template>
<HuiList border :footer="`共 ${items.length} 筆`">
<template #header>
<HuiSpace wrap>
<HuiButton size="small" type="primary" icon="plus" @click="add">新增</HuiButton>
<HuiButton size="small" :disabled="readonly || pending || items.length === 0" @click="clear"
>清除全部</HuiButton
>
<HuiSwitch v-model="readonly" active-text="刪除唯讀保護" />
</HuiSpace>
</template>
<HuiListItem v-for="item in items" :key="item.id" :header="item.title">
{{ item.desc }}
<template #footer>
<HuiButton
size="small"
variant="link"
type="danger"
:disabled="readonly || pending"
@click="remove(item.id)"
>
刪除
</HuiButton>
</template>
</HuiListItem>
<HuiEmpty v-if="items.length === 0" description="暫無待辦" />
</HuiList>
</template>
中繼資訊、響應式網格與巢狀回覆
頭像、說明、側邊內容和操作區域使用一致的版面配置。此例可切換三種尺寸、網格、分隔線、游標停留背景,以及內建或自訂空狀態;操作結果顯示在頁面。預覽空狀態時保留原始資料。
grid.column 接受數字或響應式斷點值,gutter 使用 px;標頭與頁尾保持整行。網格模式忽略列分隔線和游標停留背景。巢狀清單使用各自設定,不沿用外層清單的顯式選項。間距也可以用 CSS 變數 --hui-list-gutter 覆寫,寫在清單本身或任一祖先上都生效,優先於 gutter。
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiAvatar, HuiButton, HuiInput, HuiList, HuiListItem, HuiSpace } from '@nscode/handy-ui'
import type { ListGrid, ListSize } from '@nscode/handy-ui'
const size = ref<ListSize>('default')
const sizes: ListSize[] = ['small', 'default', 'large']
const gridEnabled = ref(false)
const showDivider = ref(true)
const hoverable = ref(true)
const empty = ref(false)
const customEmpty = ref(false)
const note = ref('尚未送出的備註')
const status = ref('尚未操作')
const grid = computed<ListGrid | undefined>(() =>
gridEnabled.value ? { column: { xs: 1, md: 2, xl: 3 }, gutter: 16 } : undefined
)
const avatar =
'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"%3E%3Ctext x="20" y="28" text-anchor="middle" font-size="28"%3E%E5%BC%B5%3C/text%3E%3C/svg%3E'
const members = [
{ id: 1, name: '張偉', description: '研發部 · 核准負責人', count: 3 },
{ id: 2, name: '王芳', description: '行銷部 · 專案聯絡人', count: 2 },
{ id: 3, name: '李娜', description: '財務部 · 複核負責人', count: 1 }
]
</script>
<template>
<HuiSpace class="list-features-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton
v-for="value in sizes"
:key="value"
:type="size === value ? 'primary' : 'default'"
@click="size = value"
>{{ value }}</HuiButton
>
<HuiButton @click="gridEnabled = !gridEnabled">{{
gridEnabled ? '顯示列清單' : '顯示網格'
}}</HuiButton>
<HuiButton @click="showDivider = !showDivider">分隔線:{{ showDivider }}</HuiButton>
<HuiButton @click="hoverable = !hoverable">游標停留背景:{{ hoverable }}</HuiButton>
<HuiButton @click="empty = !empty">{{ empty ? '顯示資料' : '預覽空狀態' }}</HuiButton>
<HuiButton @click="customEmpty = !customEmpty">自訂空狀態:{{ customEmpty }}</HuiButton>
</HuiSpace>
<p role="status">{{ status }}</p>
<HuiList
:size="size"
:grid="grid"
:show-divider="showDivider"
:hoverable="hoverable"
header="專案成員"
footer="切換版面或預覽空狀態時,資料與備註會保留。"
empty-text="尚無專案成員"
border
>
<HuiListItem
v-for="member in empty ? [] : members"
:key="member.id"
:header="member.name"
:description="member.description"
:avatar="member.id === 1 ? avatar : undefined"
>
<template v-if="member.id !== 1" #avatar
><HuiAvatar>{{ member.name.slice(0, 1) }}</HuiAvatar></template
>
<span>待處理 {{ member.count }} 項</span>
<HuiInput v-if="member.id === 1" v-model="note" aria-label="張偉的備註" />
<template #extra><span>在崗</span></template>
<template #actions>
<HuiButton size="small" variant="link" @click="status = `已檢視${member.name}`"
>檢視</HuiButton
>
<HuiButton size="small" variant="link" @click="status = `已聯絡${member.name}`"
>聯絡</HuiButton
>
</template>
</HuiListItem>
<template v-if="customEmpty" #empty><p>尚未指派成員,請先完成專案設定。</p></template>
</HuiList>
<HuiList header="留言與回覆" size="small" :show-divider="false" :hoverable="false" border>
<HuiListItem header="張偉" description="外層清單關閉分隔線與游標停留背景">
請補充複核意見。
<HuiList size="default">
<HuiListItem header="王芳" description="內層清單使用自己的預設設定"
>已核對,資料齊全。</HuiListItem
>
<HuiListItem header="李娜" description="內層清單使用自己的預設設定"
>同意繼續。</HuiListItem
>
</HuiList>
</HuiListItem>
</HuiList>
</HuiSpace>
</template>
HuiList Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| header | string | - | 清單頭部的文字。寫了 header 插槽時不顯示 |
| footer | string | - | 清單底部的文字。寫了 footer 插槽時不顯示 |
| loading | boolean | false | 是否顯示載入中的遮罩 |
| size | 'large' | 'default' | 'small' | - | 尺寸,影響頭部、底部和清單項的內距和字級。不寫時跟隨 ConfigProvider,都沒有時是 'default' |
| border | boolean | false | 是否顯示外框 |
| showDivider | boolean | true | 子項之間顯示分隔線,網格模式忽略 |
| hoverable | boolean | true | 游標停留時顯示子項背景,網格模式忽略 |
| emptyText | string | 隨語言 | 內建空狀態說明,顯式空字串可隱藏說明 |
| grid | ListGrid | — | 內容區的欄數與間距,標頭和頁尾保持整行 |
HuiListItem Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| header | string | - | 清單項的標題,顯示在內容上方。寫了 header 插槽時不顯示 |
| footer | string | - | 清單項的附加資訊,顯示在內容下方。寫了 footer 插槽時不顯示 |
| avatar | string | — | 頭像網址,avatar 插槽優先;small/default/large 預設為 32/40/48px |
| description | string | — | 標題下方說明,description 插槽優先 |
HuiList Slots
| 插槽 | 作用域參數 | 說明 |
|---|---|---|
| default | - | 清單內容,放 HuiListItem |
| header | - | 清單頭部 |
| footer | - | 清單底部 |
| empty | — | 整行空狀態,優先於 emptyText;顯式空插槽仍會覆蓋 |
HuiListItem Slots
| 插槽 | 作用域參數 | 說明 |
|---|---|---|
| default | - | 清單項的內容 |
| header | - | 清單項的標題 |
| footer | - | 清單項的附加資訊 |
| avatar | — | 左側頭像或圖示 |
| description | — | 標題下方的說明 |
| extra | — | 右側內容 |
| actions | — | 主體下方操作,多個操作間自動加入分隔線 |
類型
import type {
ListProps,
ListItemProps,
ListSize,
ListGrid,
ListSlots,
ListItemSlots
} from '@nscode/handy-ui'
import type { VNode } from 'vue'
type ListSize = 'large' | 'default' | 'small'
interface ListProps {
header?: string
footer?: string
loading?: boolean
size?: ListSize
border?: boolean
showDivider?: boolean
hoverable?: boolean
emptyText?: string
grid?: ListGrid
}
interface ListItemProps {
header?: string
footer?: string
avatar?: string
description?: string
}
interface ListSlots {
default?: () => VNode[]
header?: () => VNode[]
footer?: () => VNode[]
empty?: () => VNode[]
}
interface ListItemSlots {
default?: () => VNode[]
header?: () => VNode[]
footer?: () => VNode[]
avatar?: () => VNode[]
description?: () => VNode[]
extra?: () => VNode[]
actions?: () => VNode[]
}