Listy
虚拟列表、分组、粘性标题和帖子导航。
何时使用
Listy 显示带有虚拟化、组和粘性标题的长列表。它适用于帖子、联系人和分组结果流。对于一组列和表的操作,使用 Table; Listy 周围的标头、加载和分页由应用程序收集。
Listy CSS 前缀 显示全局 ConfigProvider.prefixCls 和本地 prefixCls,无需重置状态。 Listy 是该包的一个独立组件。该前缀适用于原始模式和 virtual 模式、组和滚动条。更改前缀时,行和滚动将被保留;模式切换单独显示。
数据和虚拟窗口
items 包含源对象。 rowKey 是必需的:字段名称或稳定的键获取器,无需回退到索引。 itemRender — 带有对象及其在原始 items 中的索引的 Svelte 片段。
| 问题 | 设置 | 示例 |
|---|---|---|
| 输出一小组 | items、rowKey、itemRender |
短套装 |
| 限制 DOM 行数 | virtual,正height |
虚拟化 |
| 组和图钉标题 | group、sticky |
团体 |
| 显示不同高度 | DOM 测量 | 内容丰富 |
| 滚动时添加数据 | onScroll |
加载中 |
本地 virtual 优先于 ConfigProvider.virtual。窗口需要一个正 height;如果没有它或 height=0,则显示所有行。虚拟模式在安装后测量内容并在调整大小时重新计算窗口。编辑文本时不要更改 rowKey:该键保留其标识和测量结果。
组和软件滚动
group.key 计算组密钥,title-snippet 获取该密钥和源对象。各个 items 中的组按照首次出现的顺序收集; itemRender 保留条目的原始索引。sticky 将标题粘贴在可滚动区域内。 如果
bind:ref 在挂载后提供 scrollTo。该方法接受像素偏移、top/left 对象、记录 key 或 groupKey。align 选择 top/bottom/auto,offset 添加缩进。在正常模式下,组的目标是其部分,在虚拟模式下,它是标题行。
onScroll 的 currentTarget 是常规列表的根或虚拟列表的持有者。加载时,检查请求的状态、滚动的剩余部分以及下一部分的可用性。示例添加本地数据;网络取消和错误处理必须由应用程序实现。加载按钮保留对相同操作的访问,而无需到达滚动的确切末端。
内容、主题和限制
空不会自动出现。过滤、无结果的消息、header/footer、loading 和分页排列在列表周围。 搜索 明确地显示了这样的空状态。
重新排列是 items 所有者的逻辑。 排序 对桌面和按钮使用原生拖动;未添加外部 React dnd-kit 的传感器和触摸行为。 Ant Design 6.7 中的 rowHoverable 和 scrollWidth 尚未在公共 Svelte API 中使用。
ConfigProvider.listy 设置 root/item/groupHeader 的通用 class/style 和语义设置。回调在默认值之前获取初始道具。 itemPaddingBlock/itemPaddingInline 组件设计令牌(design tokens)通过 theme.components.Listy 应用; Semantic styles allow you to refine a specific area.任意 prefixCls 会更改类,但不会生成相应的内联 CSS。
该行自己的标记控制可用的名称、链接和按钮。虚拟化不是查找:不可见的条目可能不在 DOM 中。不要将列表冒充为没有选择模型和键盘的成熟列表框。
示例
短名单
items、必需的 rowKey 和通过 itemRender 的自定义行标记。
<script lang="ts">
import { Listy } from '@woodger/antd';
const items = [
{ id: 1, title: "文档" }, { id: 2, title: "示例" },
{ id: 3, title: "版本" }, { id: 4, title: "支持" }
];
</script>
<Listy items={(items)} rowKey="id" role="list" aria-label={("入围名单")}>
{#snippet itemRender(item, index)}
<div role="listitem" aria-posinset={index + 1} aria-setsize={items.length}>
{(index + 1)}. {(item.title)}
</div>
{/snippet}
</Listy>
虚拟窗口
一千个条目、正 height、virtual 并通过 ref.scrollTo 过渡到按键。
<script lang="ts">
import { Button, Listy, type ListyRef } from '@woodger/antd';
const items = Array.from({ length: 1000 }, (_, index) => ({ key: index + 1 }));
let ref = $state<ListyRef>();
</script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
</style>
<div class="controls">
<Button onclick={() => ref?.scrollTo({ key: 1000, align: 'bottom' })}>
{("至条目 1000")}
</Button>
<Button onclick={() => ref?.scrollTo(0)}>{("到开始")}</Button>
</div>
<Listy items={(items)} rowKey="key" height={240} virtual bind:ref aria-label={("虚拟列表")}>
{#snippet itemRender(item)}{("记录 ")}{(item.key)}{/snippet}
</Listy>
组和粘性标题
group.key、title 片段、sticky 和程序化过渡到正常和虚拟模式下的组。
<script lang="ts">
import { Button, Checkbox, Listy, type ListyRef } from '@woodger/antd';
const departments = ["设计", "开发", "支持"];
const items = departments.flatMap((department, group) => Array.from({ length: 12 }, (
_,
index
) => ({
id: group * 12 + index + 1,
title: `参加者 ${group * 12 + index + 1}`,
department
})));
let ref = $state<ListyRef>();
let virtual = $state(true);
</script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
</style>
{#snippet title(key: string, records: typeof items)}
<strong>{(key)} · {(records.length)}</strong>
{/snippet}
<div class="controls">
<Checkbox bind:checked={virtual}>{("虚拟群组")}</Checkbox>
<Button onclick={() => ref?.scrollTo({ groupKey: "支持", align: 'top' })}>
{("转至“支持”组")}
</Button>
<Button onclick={() => ref?.scrollTo(0)}>{("到第一组")}</Button>
</div>
<Listy
items={(items)}
rowKey="id"
group={{ key: item => item.department, title }}
height={260}
sticky
{virtual}
bind:ref
aria-label={("团体名单")}
>
{#snippet itemRender(item)}{(item.title)}{/snippet}
</Listy>
不同高度的线条
附加文本会改变行高;虚拟窗口遵循 DOM 尺寸。
<script lang="ts">
import { Avatar, Button, Listy } from '@woodger/antd';
const items = [
{ id: 1, name: "安娜", text: "更新了文档。" },
{
id: 2,
name: "鲍里斯",
text: "邀请您讨论示例并测试组件在窄屏幕上的行为。"
},
{ id: 3, name: "信仰", text: "为任务添加了评论。" },
{ id: 4, name: "格列布", text: "准备了新版本。" },
{
id: 5,
name: "达莉亚",
text: "我检查了键盘、字段标签并在关闭窗口后返回焦点。"
},
{ id: 6, name: "叶戈尔", text: "更新了示例。" }
];
let expanded = $state<ReadonlySet<number>>(new Set());
function toggle(id: number) {
const next = new Set(expanded);
if (next.has(id)) next.delete(id);
else next.add(id);
expanded = next;
}
</script>
<style>
.row {
display: flex;
align-items: flex-start;
gap: 12px;
}
.content {
flex: 1;
min-width: 0;
}
p {
margin: 8px 0;
line-height: 1.6;
}
</style>
<Listy items={(items)} rowKey="id" height={280} virtual aria-label={("不同高度的消息")}>
{#snippet itemRender(item)}
<div class="row">
<Avatar>{(item.name[0])}</Avatar>
<div class="content">
<strong>{item.name}</strong>
<p>{(item.text)}</p>
<Button size="small" aria-expanded={expanded.has(item.id)} onclick={() => toggle(item.id)}>
{(expanded.has(item.id) ? "坍塌" : "更多详情")}: {item.name}
</Button>
{#if expanded.has(item.id)}
<p>
{("附加信息会更改行高。 Listy 对其进行测量并更新相邻条目的位置。")}
</p>
{/if}
</div>
</div>
{/snippet}
</Listy>
更改顺序
通过 native 拖动和移动按钮适应拖动排序; React dnd-kit 未连接。
<script lang="ts">
import { Button, Listy } from '@woodger/antd';
let items = $state([
{ id: 1, title: "文档" }, { id: 2, title: "示例" },
{ id: 3, title: "版本" }, { id: 4, title: "支持" }
]);
let dragged = $state<number>();
function reorder(from: number, to: number) {
const start = items.findIndex(item => item.id === from);
const end = items.findIndex(item => item.id === to);
if (start < 0 || end < 0 || start === end) {
return;
}
const next = [...items];
const [item] = next.splice(start, 1);
next.splice(end, 0, item);
items = next;
}
function move(id: number, offset: number) {
const index = items.findIndex(item => item.id === id);
const target = items[index + offset];
if (target) reorder(id, target.id);
}
function start(event: DragEvent, id: number) {
dragged = id;
event.dataTransfer?.setData('text/plain', String(id));
}
function drop(event: DragEvent, id: number) {
event.preventDefault();
if (dragged !== undefined) reorder(dragged, id);
dragged = undefined;
}
</script>
<style>
output {
display: block;
margin-block-start: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
strong { flex: 1 1 120px; }
</style>
<Listy items={(items)} rowKey="id" role="list" aria-label={("可排序列表")}>
{#snippet itemRender(item, index)}
<div
class="row" role="listitem" draggable="true"
ondragstart={event => start(event, item.id)}
ondragend={() => { dragged = undefined; }}
ondragover={event => event.preventDefault()}
ondrop={event => drop(event, item.id)}
>
<strong>{(item.title)}</strong>
<Button
size="small"
aria-label={("向上: " + String((item.title)) + "")}
disabled={index === 0}
onclick={() => move(item.id, -1)}
>↑</Button>
<Button size="small" aria-label={("向下: " + String((item.title)) + "")} disabled={index
=== items.length - 1}
onclick={() => move(item.id, 1)}>↓</Button>
</div>
{/snippet}
</Listy>
<output
aria-label={("订单列表")}
>{items.map(item => item.title).join(', ')}</output>
滚动加载
onScroll 添加下一个本地部分。该按钮通过键盘执行相同的操作。
<script lang="ts">
import { Button, Listy, type ListyRef } from '@woodger/antd';
const records = Array.from({ length: 80 }, (_, index) => ({ id: index + 1 }));
let count = $state(20);
const items = $derived(records.slice(0, count));
let ref = $state<ListyRef>();
function loadMore() {
count = Math.min(count + 20, records.length);
}
function onScroll(event: Event & { currentTarget: HTMLDivElement }) {
const node = event.currentTarget;
if (node.scrollHeight - node.clientHeight - node.scrollTop < 32) loadMore();
}
</script>
<style>
.actions {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-top: 16px;
}
</style>
<Listy
items={(items)}
rowKey="id"
height={240}
virtual
{onScroll}
bind:ref
aria-label={("部分列表")}
>
{#snippet itemRender(item)}{("材料 ")}{(item.id)}{/snippet}
</Listy>
<div class="actions">
<Button
disabled={count === records.length}
onclick={loadMore}
>{("加载更多")}</Button>
<Button onclick={() => ref?.scrollTo({ key: count, align: 'bottom' })}>
{("在本部分的最后")}
</Button>
<output aria-label={("部分列表")}>{("已上传: ")}{count}{(" 从 ")}{(records.length)}</output>
</div>
语义 DOM 和 ConfigProvider
语义 styles 对象和函数,常见的 ConfigProvider.listy 和插槽突出显示。
<script lang="ts">
import { Checkbox, ConfigProvider, Listy } from '@woodger/antd';
const items = [
{ id: 1, department: "设计", title: "安娜" },
{ id: 2, department: "设计", title: "鲍里斯" },
{ id: 3, department: "开发", title: "信仰" }
];
let highlight = $state(false);
</script>
<style>
.examples {
display: grid;
gap: 24px;
margin-top: 20px;
}
.examples :global(.listy-slot) {
outline: 1px dashed var(--ant-color-primary);
outline-offset: -1px;
}
</style>
{#snippet title(key: string, records: typeof items)}{(key)} · {(records.length)}{/snippet}
<Checkbox bind:checked={highlight}>{("突出显示插槽列表")}</Checkbox>
<ConfigProvider listy={{ styles: { item: 'padding-block:10px' } }}>
<div class="examples">
<Listy
items={(items)} rowKey="id" group={{ key: item => item.department, title }}
classNames={highlight
? { root: 'listy-slot', item: 'listy-slot', groupHeader: 'listy-slot' }
: {}}
styles={{
root: `
border: 1px solid var(--ant-color-border);
border-radius: 8px;
overflow: hidden;
`,
groupHeader: 'color:var(--ant-color-primary-text)',
item: 'padding-inline:20px'
}}
>
{#snippet itemRender(item)}{(item.title)}{/snippet}
</Listy>
<Listy
items={(items)}
rowKey="id"
sticky
group={{ key: item => item.department, title }}
classNames={() => highlight ? { root: 'listy-slot', groupHeader: 'listy-slot' } : {}}
styles={({ props }) => ({
groupHeader: props.sticky
? 'font-weight:700'
: 'font-weight:400',
item: 'padding-inline:24px'
})}
>
{#snippet itemRender(item)}{(item.title)}{/snippet}
</Listy>
</div>
</ConfigProvider>
<script lang="ts">
import { Button, Empty, Input, Listy } from '@woodger/antd';
const records = [
{ id: 1, name: "安娜" }, { id: 2, name: "鲍里斯" }, { id: 3, name: "信仰" }
];
let query = $state<string | undefined>('');
const items = $derived(records.filter(item =>
item.name.toLowerCase().includes((query ?? '').trim().toLowerCase())
));
</script>
<style>
output {
display: block;
margin-block-start: 12px;
}
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 20px;
}
.field { width: 200px; }
</style>
<div class="controls">
<div class="field"><Input aria-label={("搜索列表")} bind:value={query} /></div>
<Button onclick={() => { query = ''; }}>{("重置搜索")}</Button>
</div>
{#if items.length}
<Listy items={(items)} rowKey="id" aria-label={("列表搜索结果")}>
{#snippet itemRender(item)}{item.name}{/snippet}
</Listy>
{:else}
<Empty image="simple" description={("没有匹配项")} />
{/if}
<output aria-label={("列表搜索结果")}>{("成立: ")}{(items.length)}</output>
审核名单
保存示例:200条记录,分组,转到最后一条记录并返回开头。
<script lang="ts">
import { Button, Listy, type ListyRef } from '@woodger/antd';
const items = Array.from({ length: 200 }, (_, index) => ({
id: index + 1,
title: `记录 ${String(index + 1).padStart(3, '0')}`,
group: `团体 ${Math.floor(index / 50) + 1}`
}));
let ref = $state<ListyRef>();
let grouped = $state(true);
</script>
<style>
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
.controls label {
display: flex;
align-items: center;
gap: 8px;
}
.list {
max-width: 640px;
border: 1px solid var(--ant-color-border-secondary);
border-radius: 8px;
overflow: hidden;
}
.row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-height: 44px;
}
.row span {
color: var(--ant-color-text-secondary);
font-size: 12px;
}
</style>
{#snippet title(key: string, entries: typeof items)}<strong>{(key)} · {(entries.length)}{(" 记录")}</strong>{/snippet}
<div class="controls">
<label><input type="checkbox" bind:checked={grouped} />{(" 带有粘性标题的组")}</label>
<Button onclick={() => ref?.scrollTo({ key: 200, align: 'bottom' })}>
{("至条目 200")}
</Button>
<Button onclick={() => ref?.scrollTo({ top: 0 })}>{("到列表的开头")}</Button>
</div>
<div class="list">
<Listy
items={(items)}
rowKey="id"
height={260}
virtual
sticky
group={grouped ? { key: item => item.group, title } : undefined}
bind:ref
aria-label={("虚拟条目列表")}
>
{#snippet itemRender(item)}
<div
class="row"
><strong>{(item.title)}</strong><span>№ {(item.id)}</span></div>
{/snippet}
</Listy>
</div>
Listy 是该包的一个独立组件。该前缀适用于原始模式和虚拟模式、组和滚动条。更改前缀时,行和滚动将被保留;模式切换单独显示。
Listy CSS 前缀
通用命名空间ConfigProvider和本地 prefixCls;当名称更改时,组件的状态将被保留。
<script lang="ts">
import { Checkbox, ConfigProvider, Listy, Input } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
const prefix = $derived(custom ? 'suite' : 'ant');
const localPrefix = $derived(local ? 'project-listy' : '');
let virtual = $state(false);
const items = Array.from({ length: 40 }, (_, index) => ({
id: index,
title: `线 ${index + 1}`
}));
</script>
<style>
.example,
.prefix-stage {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("Listy套件通用前缀")}</Checkbox>
<Checkbox bind:checked={local}>{("Listy本地前缀")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'listy-theme' } }}>
<div class="prefix-stage">
<Checkbox bind:checked={virtual}>{("带前缀的虚拟 Listy")}</Checkbox>
<Listy prefixCls={localPrefix} items={(items)} rowKey="id" {virtual} height={200}
aria-label={("带前缀的 Listy")} role="list">
{#snippet itemRender(item)}
<div role="listitem" style="padding:8px">
<Input defaultValue={item.title} aria-label={("草稿 " + String((item.title)) + " 带前缀")} />
</div>
{/snippet}
</Listy>
</div>
</ConfigProvider>
<output
aria-label={("Listy CSS 前缀")}
>{("一般的: ")}{prefix}{(";当地的: ")}{(localPrefix
|| ("来自提供商"))}</output>
<p>
{("Listy 是该包的一个独立组件。该前缀适用于原始模式和虚拟模式、组和滚动条。更改前缀时,行和滚动将被保留;模式切换单独显示。")}
</p>
</div>
API
针对八个 ant.design 6.7.0 主题和基于 6.6.8 的公共 Svelte API 进行了验证。
scrollWidth 和 rowHoverable,在 6.7.0 中引入,尚未转移。 Props 的完整定义位于包中。
| 主题 | 示例或限制 |
|---|---|
| Basic | 钥匙和自己的标记。 |
| Virtual scrolling | 数千条记录的窗口。 |
| Horizontal scrolling | scrollWidth 是在 ant.design 6.7.0 中添加的,在当前基于 6.6.8 的 Svelte API 中缺少。 |
| Grouping and sticky headers | 普通列表、virtual 列表和分组的 scrollTo 方法。 |
| Rich content | 不同高度的测量线。 |
| Drag sorting | 本机拖动和按钮。与 React dnd-kit 的集成未移植。 |
| Infinite loading | onScroll,本地部分和 virtual 部分。 |
| Custom semantic dom styling | 对象、功能和上下文设置。 |
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 组件及其部件的完整 CSS 前缀。非空本地 prop 优先;空字符串和 undefined 继承ConfigProvider。主题的命名空间是单独设置的。 | string | ConfigProvider.prefixCls + "-listy" |
items | 源记录。 | readonly T[] | [] |
rowKey | 钥匙稳定;没有自动回退到索引。 | keyof T | (item) => ListyKey | 必需的 |
itemRender | 字符串的内容。该索引指的是原始 items。 | Snippet<[T, number]> | 必需的 |
height | 可滚动区域高度;虚拟窗口需要一个正值。 | number | — |
virtual | 仅将部分线路安装在给定 height。 | boolean | 上下文/错误 |
group | key 计算组,title 是标题的Snippet。 | ListyGroup<T, K> | — |
sticky | 冻结当前组的标题。 | boolean | false |
onScroll | currentTarget - 原始 mode 下的根或 virtual mode 下的内部持有者。 onscroll 是一个兼容的别名。 | native UIEvent handler | — |
styles | CSS 行 root/item/groupHeader;带有原始 props 的对象或函数。 | ListySemantic | — |
classNames | 类 root/item/groupHeader;对象或函数。 | ListySemantic | — |
nativeElement | bind:nativeElement - 根 div;在 virtual mode 下,它包裹着支架。 | HTMLDivElement | 安装后 |
ref | bind:ref 提供 scrollTo;卸载时绑定被清除。 | ListyRef | 安装后 |
Group
key 计算组的字符串、数字或bigint 键。具有相同键的记录将按照组中第一次出现的顺序在所有 items 中进行合并。title -
Snippet<[groupKey, items]>。 itemRender 索引保留原始条目的索引。
ref.scrollTo
| 争论 | 行动 |
|---|---|
number | 垂直偏移(以像素为单位)。 |
{ top?, left? } | 绝对位置;当前包中未实现水平虚拟窗口。 |
{ key, align?, offset? } | 通过 rowKey 记录;align:top、bottom 或 auto。 |
{ groupKey, align?, offset? } | virtual mode 下的组头,原始 mode 下的组部分。 |
Semantic DOM
Listy 使用 div 容器; itemRender 标记属于应用程序。一个简单的示例显示 list/列表项和条目号。行中的链接、字段和按钮保留本机语义和 Tab 键顺序。
| Slot | 目的 |
|---|---|
root | 列表的根容器。 |
item | 记录容器:填充、分隔符和悬停。 |
groupHeader | 组标题,包括固定副本。 |
ConfigProvider.listy 指定通用 class/styles 和语义 style。当地含义补充并阐明了上下文。该函数在应用默认值之前接收初始属性。打开示例中的突出显示以查看槽的边界。
注册令牌
itemPaddingBlock 和 itemPaddingInline 组件标记通过以下方式应用
theme.components.Listy。语义 styles 允许您指定局部缩进。任意 prefixCls 会更改类,但不会生成相应的 CSS。
| 代币 | 目的 |
|---|---|
itemPaddingBlock | 垂直记录缩进。 |
itemPaddingInline | 水平记录缩进。 |
| 代币 | 意义 |
|---|---|
colorText | rgba(0,0,0,0.88) |
colorTextDescription | rgba(0,0,0,0.45) |
colorBgContainer | #ffffff |
colorSplit | rgba(5,5,5,0.06) |
controlItemBgHover | rgba(0,0,0,0.04) |
fontSize | 14 |
lineHeight | 1.5714285714285714 |
fontWeightStrong | 600 |
FAQ
为什么 virtual 输出所有行?
检查内容物的正 height 和长度。如果没有高度或零,虚拟窗口不会限制列表。
loading、分页和空在哪里?
它们是由 Listy 周围的应用程序添加的。空的 items 本身不会创建 Empty。
不同高度的线如何工作?
挂载后,列表会测量 DOM。当内容更改时保留 rowKey。
拖动排序已经完全转移了吗?
否。该示例是使用 native 拖动和按钮进行改编的;其中不包括外部 React 传感器。