# 折叠面板

- 页面: https://jiguang132.com/zh/resources/ant-design/docs/collapse
- 资源: Ant Design (https://jiguang132.com/zh/resources/ant-design.md)
- 全部指南: https://jiguang132.com/zh/resources/ant-design/docs.md

可以折叠/展开的内容区域。


## 何时使用 {#when-to-use}

- 对复杂区域进行分组和隐藏，保持页面的整洁。
- `手风琴` 是一种特殊的折叠面板，只允许单个内容区域展开。

## 代码演示 {#examples}


- 折叠面板 · `basic`
- 面板尺寸 · `size`
- 手风琴 · `accordion`
- 面板嵌套 · `mix`
- 简洁风格 · `borderless`
- 自定义面板 · `custom`
- 面板图标 · `icon`
- 隐藏箭头 · `noarrow`
- 额外节点 · `extra`
- 幽灵折叠面板 · `ghost`
- 可折叠触发区域 · `collapsible`
- 自定义语义结构的样式和类 · `style-class`


## API

通用属性参考：[通用属性](#jguang-common-props)

### Collapse

| 参数 | 说明 | 类型 | 默认值 | 版本 | [全局配置](/components/config-provider#component-config) |
| --- | --- | --- | --- | --- | --- |
| accordion | 手风琴模式 | boolean | false |  | × |
| activeKey | 当前激活 tab 面板的 key | string\[] \| string <br/> number\[] \| number | [手风琴模式](#collapse-demo-accordion)下默认第一个元素 |  | × |
| bordered | 带边框风格的折叠面板 | boolean | true |  | × |
| classNames | 用于自定义组件内部各语义化结构的 class，支持对象或函数 | Record<[SemanticDOM](#semantic-dom), string> \| (info: { props })=> Record<[SemanticDOM](#semantic-dom), string> | - |  | 6.0.0 |
| collapsible | 所有子面板是否可折叠或指定可折叠触发区域 | `header` \| `icon` \| `disabled` | - | 4.9.0 | × |
| defaultActiveKey | 初始化选中面板的 key | string\[] \| string<br/> number\[] \| number | - |  | × |
| ~~destroyInactivePanel~~ | 销毁折叠隐藏的面板 | boolean | false |  | × |
| destroyOnHidden | 销毁折叠隐藏的面板 | boolean | false | 5.25.0 | × |
| expandIcon | 自定义切换图标 | (panelProps) => ReactNode | - |  | 5.15.0 |
| expandIconPlacement | 设置图标位置 | `start` \| `end` | `start` | - | × |
| ~~expandIconPosition~~ | 设置图标位置，请使用 `expandIconPlacement` 替换 | `start` \| `end` | - | 4.21.0 | × |
| ghost | 使折叠面板透明且无边框 | boolean | false | 4.4.0 | × |
| size | 设置折叠面板大小 | `large` \| `medium` \| `small` | `medium` | 5.2.0 | × |
| styles | 用于自定义组件内部各语义化结构的行内 style，支持对象或函数 | Record<[SemanticDOM](#semantic-dom), CSSProperties> \| (info: { props })=> Record<[SemanticDOM](#semantic-dom), CSSProperties> | - |  | 6.0.0 |
| onChange | 切换面板的回调 | function | - |  | × |
| items | 折叠项目内容 | [ItemType](#itemtype) | - | 5.6.0 | × |

### ItemType

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| classNames | 语义化结构 className | [`Record<header \| body, string>`](#semantic-dom) | - | 5.21.0 |
| collapsible | 是否可折叠或指定可折叠触发区域 | `header` \| `icon` \| `disabled` | - |  |
| children | body 区域内容 | ReactNode | - |  |
| extra | 自定义渲染每个面板右上角的内容 | ReactNode | - |  |
| forceRender | 被隐藏时是否渲染 body 区域 DOM 结构 | boolean | false |  |
| key | 对应 activeKey | string \| number | - |  |
| label | 面板标题 | ReactNode | - | - |
| showArrow | 是否展示当前面板上的箭头（为 false 时，collapsible 不能设为 icon） | boolean | true |  |
| styles | 语义化结构 style | [`Record<header \| body, CSSProperties>`](#semantic-dom) | - | 5.21.0 |

### Collapse.Panel


:::warning{title=已废弃}
版本 >= 5.6.0 时请使用 items 方式配置面板。
:::

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| collapsible | 是否可折叠或指定可折叠触发区域 | `header` \| `icon` \| `disabled` | - | 4.9.0 (icon: 4.24.0) |
| extra | 自定义渲染每个面板右上角的内容 | ReactNode | - |  |
| forceRender | 被隐藏时是否渲染 body 区域 DOM 结构 | boolean | false |  |
| header | 面板标题 | ReactNode | - |  |
| key | 对应 activeKey | string \| number | - |  |
| showArrow | 是否展示当前面板上的箭头（为 false 时，collapsible 不能设为 icon） | boolean | true |  |

## Semantic DOM

| 语义节点 | 原生说明 | 版本 |
| --- | --- | --- |
| `root` | 根元素，包含折叠面板的边框、圆角、背景色等容器样式，控制面板的整体布局和外观 | 6.0.0 |
| `header` | 头部元素，包含flex布局、内边距、颜色、行高、光标样式、过渡动画等面板头部的交互和样式 | 5.21.0 |
| `icon` | 图标元素，包含字体大小、过渡动画、旋转变换等展开收起箭头的样式和动效 | 6.0.0 |
| `title` | 标题元素，包含flex自适应布局、右边距等标题文字的布局和排版样式 | 6.0.0 |
| `body` | 内容元素，包含内边距、颜色、背景色等面板内容区域的展示样式 | 5.21.0 |

语义说明来源: `vendor/components/collapse/demo/_semantic.tsx`

## 主题变量（Design Token）{#design-token}

类型、继承关系和原生令牌声明保持固定源码原文；默认值由原生主题计算，并可能受 ConfigProvider 覆盖。本表不伪造静态默认值。

| 令牌 | 类型 | 原生说明 |
| --- | --- | --- |
| `headerPadding` | `CSSProperties['padding']` | 折叠面板头部内边距 |
| `headerPaddingSM` | `CSSProperties['padding']` | 小号折叠面板头部内边距 |
| `headerPaddingLG` | `CSSProperties['padding']` | 大号折叠面板头部内边距 |
| `headerBg` | `string` | 折叠面板头部背景 |
| `contentPadding` | `CSSProperties['padding']` | 折叠面板内容内边距 |
| `contentPaddingSM` | `CSSProperties['padding']` | 小号折叠面板内容内边距 |
| `contentPaddingLG` | `CSSProperties['padding']` | 大号折叠面板内容内边距 |
| `contentBg` | `string` | 折叠面板内容背景 |
| `borderlessContentPadding` | `CSSProperties['padding']` | 简约风格折叠面板的内容内边距 |
| `borderlessContentBg` | `string` | 简约风格折叠面板的内容背景 |

声明来源: `vendor/components/collapse/style/index.ts`

```ts
import type { CSSProperties } from 'react';

import type { CSSObject } from '@ant-design/cssinjs';

import { unit } from '@ant-design/cssinjs';

import { genFocusStyle, resetComponent, resetIcon } from '../../style';

import { genCollapseMotion } from '../../style/motion';

import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';

import { genStyleHooks, mergeToken } from '../../theme/internal';

/** Component only token. Which will handle additional calculation of alias token */
export interface ComponentToken {
  // Component token here
  /**
   * @desc 折叠面板头部内边距
   * @descEN Padding of header
   */
  headerPadding: CSSProperties['padding'];
  /**
   * @desc 小号折叠面板头部内边距
   * @descEN Padding of small header
   */
  headerPaddingSM: CSSProperties['padding'];
  /**
   * @desc 大号折叠面板头部内边距
   * @descEN Padding of large header
   */
  headerPaddingLG: CSSProperties['padding'];
  /**
   * @desc 折叠面板头部背景
   * @descEN Background of header
   */
  headerBg: string;
  /**
   * @desc 折叠面板内容内边距
   * @descEN Padding of content
   */
  contentPadding: CSSProperties['padding'];
  /**
   * @desc 小号折叠面板内容内边距
   * @descEN Padding of small content
   */
  contentPaddingSM: CSSProperties['padding'];
  /**
   * @desc 大号折叠面板内容内边距
   * @descEN Padding of large content
   */
  contentPaddingLG: CSSProperties['padding'];
  /**
   * @desc 折叠面板内容背景
   * @descEN Background of content
   */
  contentBg: string;
  /**
   * @desc 简约风格折叠面板的内容内边距
   * @descEN Padding of content in borderless style
   */
  borderlessContentPadding: CSSProperties['padding'];
  /**
   * @desc 简约风格折叠面板的内容背景
   * @descEN Background of content in borderless style
   */
  borderlessContentBg: string;
}

type CollapseToken = FullToken<'Collapse'> & {
  /**
   * @desc 折叠面板边框圆角
   * @descEN Border radius of collapse panel
   */
  collapsePanelBorderRadius: number;
};
```


## 原生通用属性 {#jguang-common-props}


> Tips: 以下通用属性适用于 antd 大部分组件，不支持的组件会单独说明。

| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| style | 自定义样式 | CSSProperties | - |
| className | 自定义类名 | string | - |
| rootClassName | 添加在组件最外层的 className | string | - |
| autoFocus | 自动获取焦点，仅对表单类、链接、交互容器等可聚焦元素生效 | boolean | false |


## 安装并复现示例

```bash
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23
```

使用 ConfigProvider 和原生 App，并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示，不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。

## 原生类型参考

源码查看器包含未修改的原生实现和以下声明文件，继承类型仍位于原生包中。代码中的英文注释保留为源码参考。

- `vendor/components/collapse/Collapse.tsx`
- `vendor/components/collapse/CollapsePanel.tsx`
- `vendor/components/collapse/style/index.ts`

## 来源与许可

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

保留原生中英文正文与可执行示例；官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可，另列依赖的许可证也需保留。上传预览禁止向外发起请求，不提供后端服务。

---

JGuang 全部资源索引: https://jiguang132.com/llms.txt
