JGuang

折叠面板

Collapse · Ant Design

交互预览
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

折叠面板

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

何时使用

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

代码演示

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

API

通用属性参考:通用属性

Collapse

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

ItemType

参数说明类型默认值版本
classNames语义化结构 classNameRecord<header | body, string>-5.21.0
collapsible是否可折叠或指定可折叠触发区域header | icon | disabled-
childrenbody 区域内容ReactNode-
extra自定义渲染每个面板右上角的内容ReactNode-
forceRender被隐藏时是否渲染 body 区域 DOM 结构booleanfalse
key对应 activeKeystring | number-
label面板标题ReactNode--
showArrow是否展示当前面板上的箭头(为 false 时,collapsible 不能设为 icon)booleantrue
styles语义化结构 styleRecord<header | body, CSSProperties>-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 结构booleanfalse
header面板标题ReactNode-
key对应 activeKeystring | number-
showArrow是否展示当前面板上的箭头(为 false 时,collapsible 不能设为 icon)booleantrue

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)

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

令牌类型原生说明
headerPaddingCSSProperties['padding']折叠面板头部内边距
headerPaddingSMCSSProperties['padding']小号折叠面板头部内边距
headerPaddingLGCSSProperties['padding']大号折叠面板头部内边距
headerBgstring折叠面板头部背景
contentPaddingCSSProperties['padding']折叠面板内容内边距
contentPaddingSMCSSProperties['padding']小号折叠面板内容内边距
contentPaddingLGCSSProperties['padding']大号折叠面板内容内边距
contentBgstring折叠面板内容背景
borderlessContentPaddingCSSProperties['padding']简约风格折叠面板的内容内边距
borderlessContentBgstring简约风格折叠面板的内容背景

声明来源: vendor/components/collapse/style/index.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;
};

原生通用属性

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

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

安装并复现示例

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 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。