Collapse
可展开面板、手风琴、嵌套、操作、字段保留和主题配置。
何时使用
Collapse 可根据要求显示更多详细信息。当并非同时需要所有部分时,它适用于常见问题解答、设置和长格式。accordion 使一个面板打开;常规折叠允许多个。
面板和选定按键
items 指定每个面板的 label、children 和持久 key。 defaultActiveKey 定义初始状态,bind:activeKey 将打开绑定到应用程序。使用字符串表示形式对数字进行比较; onChange 总是接收 string[],包括在 accordion 中。 Ant Design 6.7 中的
| 问题 | 机会 | 示例 |
|---|---|---|
| 展开多个部分 | items、defaultActiveKey |
独立披露 |
| 保持一个面板打开 | accordion、bind:activeKey |
手风琴 |
| 附上您自己的部分 | 另一个 Collapse |
依恋 |
| 更改激活区域 | collapsible |
开放面积 |
| 管理现场保存 | destroyOnHidden |
生命周期 |
collapsible 选择 header、icon 或 disabled。该项目设置指定一般模式。 showArrow: false 隐藏箭头;这不适用于仅通过 icon 打开。 expandIcon - 带有面板数据和 isActive 的片段;本机图标不会自动旋转。
的内容、动作和生命周期
label 位于本机标题按钮内。保持被动:文本、装饰图标、签名标记。将交互式控件放置在 extra 或 children 中。 extra 是一个单独的范围,不应切换面板。
默认情况下,主体在第一次显示时创建,并在隐藏后保存。 destroyOnHidden 在关闭时将其删除,因此本地字段会重新出现。无论惰性模式如何,特定项目的 forceRender 都会创建并保存其内容。对于需要在 DOM 删除后继续存在的草稿,请将值存储在 Collapse 上方。
嵌套面板独立控制。 headingLevel 设置标题级别,默认为 3;在嵌套级别,选择下一个适当的文档级别。打开面板并不意味着验证其字段:验证仍然是表单或应用程序的责任。
外观及可用性
size 接受 small/medium/large 并继承整体尺寸。 Bordered:false去除外边框,ghost 也去除背景和边框设计。 expandIconPlacement 使用逻辑 start/end。
原生按钮提供 Enter/Space、aria-expanded 和正文连接。如果面板关闭,隐藏的主体不应该是报告错误的唯一地方。在标题中保留问题的可见指示或在检查时打开相关部分。
ConfigProvider.collapse 设置设计和通用箭头。语义标头指的是按钮,body 指的是内部内容。项目 classNames 补充公共类,item styles 覆盖公共样式。支持 CSS 字符串和对象。折叠设计令牌(design tokens)控制填充和背景。
非空本地 prefixCls 指定 Collapse 及其面板的 CSS 类。空字符串和 undefined 继承ConfigProvider.prefixCls,带有 -collapse 后缀,默认为 ant-collapse。更新前缀保留扩展、DOM 字段及其值、焦点和选择。 示例 ConfigProvider 在打开的面板中显示本地异常和编辑器。
示例
独立面板
第一个面板最初打开;您可以同时打开多个。
<script lang="ts">
import { Collapse } from '@woodger/antd';
</script>
<Collapse aria-label={("独立面板")} headingLevel={4} defaultActiveKey={[
'one'
]} items={([
{ key: 'one', label: "项目描述", children: "Svelte 5 的界面组件。" },
{ key: 'two', label: "安装", children: "连接公共 npm 包和样式。" },
{ key: 'three', label: "限制", children: "API 的组成取决于包版本。" }
])} />
方面
small、medium、large 改变标题和内容的填充。
<script lang="ts">
import { Collapse, Select } from '@woodger/antd';
let size = $state<'small' | 'medium' | 'large'>('medium');
</script>
<style>.control { margin-bottom:16px; }</style>
<div
class="control"
><Select
aria-label={("面板尺寸")}
bind:value={size}
options={([
{ value: 'small', label: "小的" }, { value: 'medium', label: "普通的" }, {
value: 'large',
label: "大的"
}
])}
/></div>
<Collapse {size} headingLevel={4} defaultActiveKey={['one']} items={([
{ key: 'one', label: "项目选项", children: "某一部分的字段和设置。" },
{ key: 'two', label: "故事", children: "更改和发布。" }
])} />
["one"]
手风琴和弦轴
仅打开一个面板; Svelte 中的 onChange 接收字符串键数组。
<script lang="ts">
import { Button, Collapse, type CollapseActiveKey } from '@woodger/antd';
let activeKey = $state<CollapseActiveKey | undefined>(['one']);
let changed = $state<string[]>(['one']);
</script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
</style>
<div class="controls"><Button onclick={() => { activeKey = ['two']; }}>
{("打开安装")}
</Button><Button onclick={() => {
activeKey = [];
}}>{("全部折叠")}</Button></div>
<Collapse
aria-label={("手风琴项目")}
headingLevel={4}
accordion
bind:activeKey
onChange={keys => { changed = keys; }}
items={([
{ key: 'one', label: "描述", children: "项目的目的。" },
{ key: 'two', label: "安装", children: "连接组件。" },
{ key: 'three', label: "考试", children: "检查可访问性和行为。" }
])}
/>
<p role="status" aria-label={("改变手风琴")}>{JSON.stringify(changed)}</p>
嵌套面板
内部面板独立控制;标题级别增加。
<script lang="ts">
import { Collapse } from '@woodger/antd';
</script>
{#snippet nested()}<Collapse
headingLevel={5}
aria-label={("项目分段")}
items={([
{ key: 'source', label: "来源", children: "组件、类型和样式。" },
{ key: 'tests', label: "测试", children: "库消费者脚本。" }
])}
/>{/snippet}
<Collapse headingLevel={4} defaultActiveKey={['project']} items={([
{ key: 'project', label: "项目", children: nested },
{ key: 'release', label: "问题", children: "验证并公布。" }
])} />
无框
bordered=false 删除外边框并更改主体设计。
<script lang="ts">
import { Collapse } from '@woodger/antd';
</script>
<Collapse headingLevel={4} bordered={false} defaultActiveKey={['one']} items={([
{ key: 'one', label: "描述", children: "没有整体框架的下拉部分。" },
{ key: 'two', label: "送货", children: "收货条款和条件。" }
])} />
定制面板设计
元素样式和 expandIcon 允许您单独设置面板样式。
<script lang="ts">
import { Collapse, type CollapseExpandIconInfo } from '@woodger/antd';
</script>
{#snippet arrow(info: CollapseExpandIconInfo)}<svg
width="14"
height="14"
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
stroke-width="2"
style={`transform:rotate(${info.isActive ? 90 : 0}deg)`}
aria-hidden="true"
><path d="m6 3 5 5-5 5" /></svg>{/snippet}
<Collapse
headingLevel={4}
bordered={false}
expandIcon={arrow}
defaultActiveKey={['one']}
items={([
{
key: 'one',
label: "项目材料",
children: "描述和文件。",
style:
`
border: 0;
margin-bottom: 16px;
border-radius: 8px;
background: var(--ant-color-fill-alter);
`,
styles: { body: 'padding:16px' }
},
{
key: 'two',
label: "讨论",
children: "团队决策。",
style: `
border: 0;
border-radius: 8px;
background: var(--ant-color-fill-alter);
`,
styles: { body: 'padding:16px' }
}
])}
/>
标题图标
原生按钮内带有 SVG 和文本的被动片段;没有嵌套控件。
<script lang="ts">
import { Collapse } from '@woodger/antd';
</script>
{#snippet title()}<span
style="
display: inline-flex;
align-items: center;
gap: 8px;
"
><svg
viewBox="0 0 20 20"
width="18"
height="18"
fill="none"
stroke="currentColor"
aria-hidden="true"
><path
d="M3 3h14v14H3zM6 7h8M6 10h8M6 13h5"
/></svg><span>{("项目文件")}</span></span>{/snippet}
<Collapse headingLevel={4} items={([{
key: 'documents',
label: title,
children: "要求、解决方案和说明。"
}])} />
无箭头
showArrow=false 隐藏图标;整个标题按钮继续工作。
<script lang="ts">
import { Collapse } from '@woodger/antd';
</script>
<Collapse headingLevel={4} items={([
{
key: 'one',
label: "无箭头部分",
showArrow: false,
children: "保留键盘控制。"
},
{ key: 'two', label: "常规款", children: "具有可见的披露指示器。" }
])} />
已保存: 0.
附加动作
extra 中的按钮执行其自己的操作,保存面板的状态。
<script lang="ts">
import { Button, Collapse } from '@woodger/antd';
let saved = $state(0);
</script>
{#snippet extra()}
<Button
size="small"
style="
max-width: 100%;
min-width: 0;
height: auto;
min-height: var(--ant-control-height-sm);
white-space: normal;
"
aria-label={("保存部分")}
onclick={() => { saved++; }}
>{("保存")}</Button>
{/snippet}
<Collapse headingLevel={4} defaultActiveKey={['one']} items={([
{ key: 'one', label: "选项", children: "右侧的操作不会折叠面板。", extra }
])} />
<p
role="status"
aria-label={("保存一个部分")}
>{("已保存: ")}{saved}.</p>
透明面板
ghost 去除面板的背景和边框;容器的背面是可见的。
<script lang="ts">
import { Collapse } from '@woodger/antd';
</script>
<style>
.surface {
padding: 16px;
background: var(--ant-color-fill-alter);
border-radius: 8px;
}
</style>
<div class="surface">
<Collapse headingLevel={4} ghost defaultActiveKey={['one']} items={([
{ key: 'one', label: "描述", children: "公共基板上的透明面板。" },
{ key: 'two', label: "细节", children: "附加信息。" }
])} />
</div>
切换区
常规标题,只有一个箭头和一个无法访问的面板。
<script lang="ts">
import { Collapse } from '@woodger/antd';
</script>
<Collapse headingLevel={4} items={([
{ key: 'header', label: "整个标题", children: "按标题打开。" },
{
key: 'icon',
label: "仅箭头",
collapsible: 'icon',
children: "箭头按钮有一个可访问的标题名称。"
},
{
key: 'disabled',
label: "无法访问的部分",
collapsible: 'disabled',
children: "无法访问的面板的内容。"
}
])} />
Semantic DOM
公共 styles 接受返回 CSS 对象的函数。items[].styles 自定义单个面板的样式。
<script lang="ts">
import { Collapse } from '@woodger/antd';
</script>
<Collapse headingLevel={4} defaultActiveKey={['one']} styles={({ props }) => ({
header: { background: props.bordered ? 'var(--ant-color-fill-alter)' : 'transparent' },
title: { fontWeight: 600 },
body: { lineHeight: 1.8 }
})} items={([
{ key: 'one', label: "总体设计", children: "标题和正文样式。" },
{
key: 'two', label: "项目设定", children: "当地的身体风格。", styles: {
body: { padding: '20px' },
title: { color: 'var(--ant-color-success-text)' }
}
}
])} />
本地话题
折叠标记设置填充和背景而不更改相邻主题。
<script lang="ts">
import { Collapse, ConfigProvider } from '@woodger/antd';
</script>
<ConfigProvider theme={{
components: {
Collapse: {
headerPadding: '14px 20px',
contentPadding: '20px',
headerBg: 'var(--ant-color-fill-alter)',
contentBg: 'var(--ant-color-bg-container)',
borderlessContentPadding: '12px 20px',
borderlessContentBg: 'var(--ant-color-fill-alter)'
}
}
}}>
<Collapse id="collapse-token-panel" headingLevel={4} defaultActiveKey={['one']} items={([
{ key: 'one', label: "本地代币", children: "自定义标题和正文缩进。" }
])} />
</ConfigProvider>
保存和删除内容
面板的高度和透明度会随动画变化。启用 destroyOnHidden 后,面板在关闭动画结束时移除输入框,重新打开时创建新的输入框。
<script lang="ts">
import { Checkbox, Collapse, Input } from '@woodger/antd';
let destroy = $state(false);
</script>
<style>.control { margin-bottom:16px; }</style>
{#snippet draft()}<Input
aria-label={("内板草案")}
defaultValue="Анна"
/>{/snippet}
<div
class="control"
><Checkbox
bind:checked={destroy}
>{("删除隐藏内容")}</Checkbox></div>
<Collapse headingLevel={4} destroyOnHidden={destroy} defaultActiveKey={['draft']} items={([
{ key: 'draft', label: "草稿", children: draft }
])} />
参赛表格
保存的审核表单:独立重置、嵌套字段和问卷提交。
<script lang="ts">
import {
Button,
Checkbox,
Collapse,
Input,
type CollapseActiveKey,
type CollapseTrigger
} from '@woodger/antd';
let activeKey = $state<CollapseActiveKey | undefined>(['contacts']);
let accordion = $state(false);
let destroyOnHidden = $state(false);
let ghost = $state(false);
let bordered = $state(true);
let collapsible = $state<CollapseTrigger>('header');
let expandIconPlacement = $state<'start' | 'end'>('start');
let revision = $state(0);
let result = $state(("折叠该部分并再次打开它 - 未保存的字段将保留在原处。"));
function save(event: SubmitEvent) {
event.preventDefault();
const data = new FormData(event.currentTarget as HTMLFormElement);
const name = data.get('contactName');
result = name === null
? ("展开“联系人”部分以填写表格。")
: ("已保存调查问卷: " + String((name || ("没有名字"))) + "。消息: " + String((data.has('news')
? ("包括")
: ("关闭"))) + ".");
}
</script>
<style>
.settings {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 24px;
}
.settings label {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
select {
padding: 4px 8px;
color: var(--ant-color-text);
background: var(--ant-color-bg-container);
border: 1px solid var(--ant-color-border);
border-radius: 6px;
font: inherit;
}
.fields {
display: grid;
gap: 16px;
max-width: 360px;
}
.field {
display: grid;
gap: 6px;
}
.footer {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
margin-top: 24px;
}
output { color: var(--ant-color-text-secondary); }
</style>
{#snippet contacts()}
{#key revision}
<div class="fields"><label class="field">{("联系人姓名")}<Input
name="contactName"
defaultValue="Анна"
/></label><label
class="field"
>{("对调查问卷的评论")}<Input
name="comment"
defaultValue="Новый участник"
/></label></div>
{/key}
{/snippet}
{#snippet preferences()}
{#key revision}
<Checkbox name="news" defaultChecked>
{("接收新闻简介")}
</Checkbox>
{/key}
{/snippet}
{#snippet extra()}
<Button
size="small"
style="
max-width: 100%;
min-width: 0;
height: auto;
min-height: var(--ant-control-height-sm);
white-space: normal;
"
onclick={() => {
revision++;
result = ("调查问卷已重置。");
}}
>{("重置个人资料")}</Button>
{/snippet}
{#snippet additional()}{#key revision}<Collapse
aria-label={("附加字段")}
headingLevel={5}
items={([
{ key: 'address', label: "地址", children: address },
{ key: 'help', label: "数据的使用方式", children: help }
])}
/>{/key}{/snippet}
{#snippet address()}<label
class="field"
>{("参加者所在城市")}<Input
name="city"
defaultValue="Москва"
/></label>{/snippet}
{#snippet help()}<p>{("这是一份演示问卷。数据保留在页面上,不会发送到服务器。")}</p>{/snippet}
<div class="settings">
<Checkbox bind:checked={accordion}>{("一个开放式分区")}</Checkbox>
<Checkbox
bind:checked={destroyOnHidden}
>{("隐藏时重置内容")}</Checkbox>
<Checkbox bind:checked={bordered}>{("显示框架")}</Checkbox>
<Checkbox bind:checked={ghost}>{("透明背景")}</Checkbox>
<label>{("切换区 ")}<select
bind:value={collapsible}
><option
value="header"
>{("标题")}</option><option
value="icon"
>{("仅箭头")}</option><option
value="disabled"
>{("残疾人")}</option></select></label>
<label>{("箭头位置 ")}<select
bind:value={expandIconPlacement}
><option
value="start"
>{("一开始")}</option><option
value="end"
>{("在最后")}</option></select></label>
</div>
<form onsubmit={save}>
<Collapse
aria-label={("参赛表格")}
headingLevel={4}
bind:activeKey
{accordion}
{destroyOnHidden}
{collapsible}
{expandIconPlacement}
{bordered}
{ghost}
items={([
{ key: 'contacts', label: "联系方式", children: contacts, extra },
{ key: 'preferences', label: "通知", children: preferences },
{ key: 'additional', label: "此外", children: additional },
{ key: 'access', label: "访问权", collapsible: 'disabled' }
])}
/>
<div
class="footer"
><Button
htmlType="submit"
type="primary"
>{("保存个人资料")}</Button><output
aria-label={("问卷结果")}
>{result}</output></div>
</form>
API
这些示例是根据当前的 ant.design 页面和已安装包的公共 API 进行检查的。 Props 的完整定义位于 @woodger/antd 中。
| 主题 | 示例或限制 |
|---|---|
| Collapse | 独立披露。 |
| Size | 三种尺寸。 |
| Accordion | 一个面板可控。 |
| Nested panel | 嵌套和标题级别。 |
| Borderless | 无外框。 |
| Custom Panel | 元素样式和自定义箭头。 |
| Panel with icon | SVG 和标题文本。 |
| No arrow | 没有箭头。 |
| Extra node | 独立行动。 |
| Ghost Collapse | 透明背衬。 |
| Collapsible | header, icon, disabled. |
| Custom semantic dom styling | 通用插槽和本地插槽。 |
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | Collapse 及其面板的完整本地 CSS 前缀。空字符串和 undefined 继承ConfigProvider.prefixCls;默认是 ant-collapse。 | string | ConfigProvider + -collapse |
items | 一系列带有标题、内容和扩展设置的面板。 | readonly CollapseItem[] | — |
activeKey | bind:activeKey:数字、字符串或列表。 | CollapseActiveKey | — |
defaultActiveKey | 非托管 bean 的初始扩展。 | CollapseActiveKey | — |
onChange | 始终是字符串键数组,包括 accordion。 | (keys: string[]) => void | — |
accordion | 只有一个打开的面板。 | boolean | false |
size | small, medium, large. | ComponentSize | 上下文/媒介 |
bordered | 通用框架。 | boolean | true |
ghost | 透明面板,无边框。 | boolean | false |
collapsible | 切换区域;项目可以阐明含义。 | header | icon | disabled | header |
expandIconPlacement | 箭头位置。 | start | end | start |
expandIcon | 自己的箭;旋转由 Snippet 设置。 | Snippet<[CollapseExpandIconInfo]> | — |
destroyOnHidden | 移除隐藏的尸体;元素的 forceRender 会保留它。 | boolean | false |
headingLevel | 本机按钮的标题级别。 | 2 | 3 | 4 | 5 | 6 | 3 |
styles | 面板各部位的 CSS 字符串和对象。支持对象或根据组件属性返回样式的函数。 | CollapseSemantic<CSSStyle> | — |
classNames | 常见的槽类。 | CollapseSemantic | — |
nativeElement | bind:nativeElement 而不是 React ref。 | HTMLDivElement | — |
CollapseItem
key 指定稳定的密钥; label 是header的被动内容,children 是body,extra 是单独的action。 collapsible、showArrow、forceRender、destroyOnHidden、styles/classNames 和 onItemClick 可用。缺失的 key 使用字符串索引;对于可变列表,请显式指定键。
Semantic DOM
Root 是一个命名组;标题使用 h2–h6 以及本机按钮和 aria-expanded/aria-controls。将交互式控件放在 extra 或 body 中,而不是放在 label 中。
插槽:root、标题、title、icon、body。 header 指的是按钮,body 指的是面板的内部内容。项目 styles 是对常见样式的补充,并优先匹配 CSS 属性。
注册令牌
theme.components.Collapse 应用十个组件令牌。 ConfigProvider.collapse 定义通用 class/style、语义槽和 expandIcon。本地道具和自定义 styles 保留优先权。
| 代币 | 目的 |
|---|---|
borderlessContentBg | 没有框架的身体背景。 |
borderlessContentPadding | 无框架的身体填充。 |
contentBg | 身体背景。 |
contentPadding | 正常的身体凹痕。 |
contentPaddingLG | 身体有大的凹痕。 |
contentPaddingSM | 身体有小凹痕。 |
headerBg | 标题背景。 |
headerPadding | 常规标题缩进。 |
headerPaddingLG | 大标题缩进。 |
headerPaddingSM | 小标题缩进。 |
| 代币 | 当前值 |
|---|---|
colorText | rgba(0,0,0,0.88) |
colorTextHeading | rgba(0,0,0,0.88) |
colorBorder | #d9d9d9 |
colorFillAlter | rgba(0,0,0,0.02) |
borderRadiusLG | 8 |
fontSize | 14 |
lineHeight | 1.5714285714285714 |
FAQ
关闭时如何保存草稿?
如果没有 destroyOnHidden,被访问的主体仍保持安装状态。为了 DOM 独立性,将数据存储在 Collapse 之上。
在哪里放置附加按钮?
extra。label 应保留标题按钮的被动内容。
为什么 onChange 在 accordion 中返回一个数组?
这是 Svelte API 契约:回调始终接收 string[]。 accordion 限制了公钥的数量。
常规尺码如何选择?
使用 medium 或一般 ConfigProvider 大小。small 和 large 改变填充。