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 生成。空数组、空 Fragment、注释及空白文本会显示内置空态。下面用“预览空态”切换展示,原数组仍然保留。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiList, HuiListItem, HuiSpace } 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 号会议室' }
])
let nextId = 3
const previewEmpty = ref(false)
const add = () => {
const title = candidates[(nextId - 3) % candidates.length]
items.value.push({ id: nextId++, title, desc: '新加的待办' })
}
</script>
<template>
<HuiList border :footer="`原数据共 ${items.length} 条`" empty-text="暂无待办">
<template #header>
<HuiSpace wrap>
<HuiButton size="small" type="primary" icon="plus" @click="add">新增</HuiButton>
<HuiButton size="small" @click="previewEmpty = !previewEmpty">{{
previewEmpty ? '显示原数据' : '预览空态'
}}</HuiButton>
</HuiSpace>
</template>
<HuiListItem v-for="item in previewEmpty ? [] : items" :key="item.id" :header="item.title">
{{ item.desc }}
</HuiListItem>
</HuiList>
</template>
元信息、响应式网格与嵌套回复
头像、说明、侧边和操作区域统一排版。可切换三种尺寸、网格、分隔线、悬停和默认/自定义空态;操作结果显示在页面。空态预览保留原数据。
grid.column 接受数字或响应式断点值,gutter 的单位是 px,不传时为 16px;头部和底部保持整行。网格模式不显示分隔线和悬停底色。嵌套的列表用各自的设置,不沿用外层列表显式写的选项。间距也可以用 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%A0%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'"
:class="`list-size-${value}`"
@click="size = value"
>{{ value }}</HuiButton
>
<HuiButton class="list-toggle-grid" @click="gridEnabled = !gridEnabled">{{
gridEnabled ? '切换行列表' : '切换网格'
}}</HuiButton>
<HuiButton class="list-toggle-divider" @click="showDivider = !showDivider"
>分隔线:{{ showDivider }}</HuiButton
>
<HuiButton class="list-toggle-hover" @click="hoverable = !hoverable"
>悬停:{{ hoverable }}</HuiButton
>
<HuiButton class="list-toggle-empty" @click="empty = !empty">{{
empty ? '显示原数据' : '预览空态'
}}</HuiButton>
<HuiButton class="list-toggle-custom-empty" @click="customEmpty = !customEmpty"
>自定义空态:{{ customEmpty }}</HuiButton
>
</HuiSpace>
<p class="list-features-status" role="status">{{ status }}</p>
<HuiList
class="list-features-primary"
: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"
class="list-member-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 class="list-custom-empty">尚未分配成员,请先完成项目设置</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 ListGrid {
column?: number | Partial<Record<'xs' | 'sm' | 'md' | 'lg' | 'xl', number>>
gutter?: number
}
interface ListSlots {
default?: () => VNode[]
header?: () => VNode[]
footer?: () => VNode[]
empty?: () => VNode[]
}
interface ListItemSlots extends Omit<ListSlots, 'empty'> {
avatar?: () => VNode[]
description?: () => VNode[]
extra?: () => VNode[]
actions?: () => VNode[]
}