Collapse折叠面板
可以折叠/展开的内容区域。
何时使用
对复杂区域进行分组和隐藏,保持页面的整洁。
手风琴是一种特殊的折叠面板,只允许单个内容区域展开。
代码演示

可以同时展开多个面板,这个例子默认展开了第一个。
import { Collapse } from 'antd';const { Panel } = Collapse;function callback(key) {console.log(key);}const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;ReactDOM.render(<Collapse defaultActiveKey={['1']} onChange={callback}><Panel header="This is panel header 1" key="1"><p>{text}</p></Panel><Panel header="This is panel header 2" key="2"><p>{text}</p></Panel><Panel header="This is panel header 3" key="3"><p>{text}</p></Panel></Collapse>,mountNode,);

手风琴,每次只打开一个 tab。
import { Collapse } from 'antd';const { Panel } = Collapse;const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;ReactDOM.render(<Collapse accordion><Panel header="This is panel header 1" key="1"><p>{text}</p></Panel><Panel header="This is panel header 2" key="2"><p>{text}</p></Panel><Panel header="This is panel header 3" key="3"><p>{text}</p></Panel></Collapse>,mountNode,);

嵌套折叠面板。
import { Collapse } from 'antd';const { Panel } = Collapse;function callback(key) {console.log(key);}const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;ReactDOM.render(<Collapse onChange={callback}><Panel header="This is panel header 1" key="1"><Collapse defaultActiveKey="1"><Panel header="This is panel nest panel" key="1"><p>{text}</p></Panel></Collapse></Panel><Panel header="This is panel header 2" key="2"><p>{text}</p></Panel><Panel header="This is panel header 3" key="3"><p>{text}</p></Panel></Collapse>,mountNode,);

一套没有边框的简洁样式。
import { Collapse } from 'antd';const { Panel } = Collapse;const text = (<p style={{ paddingLeft: 24 }}>A dog is a type of domesticated animal. Known for its loyalty and faithfulness, it can be foundas a welcome guest in many households across the world.</p>);ReactDOM.render(<Collapse bordered={false} defaultActiveKey={['1']}><Panel header="This is panel header 1" key="1">{text}</Panel><Panel header="This is panel header 2" key="2">{text}</Panel><Panel header="This is panel header 3" key="3">{text}</Panel></Collapse>,mountNode,);

自定义各个面板的背景色、圆角、边距和图标。
import { Collapse } from 'antd';import { CaretRightOutlined } from '@ant-design/icons';const { Panel } = Collapse;const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;ReactDOM.render(<Collapsebordered={false}defaultActiveKey={['1']}expandIcon={({ isActive }) => <CaretRightOutlined rotate={isActive ? 90 : 0} />}className="site-collapse-custom-collapse"><Panel header="This is panel header 1" key="1" className="site-collapse-custom-panel"><p>{text}</p></Panel><Panel header="This is panel header 2" key="2" className="site-collapse-custom-panel"><p>{text}</p></Panel><Panel header="This is panel header 3" key="3" className="site-collapse-custom-panel"><p>{text}</p></Panel></Collapse>,mountNode,);
[data-theme='compact'] .site-collapse-custom-collapse .site-collapse-custom-panel,.site-collapse-custom-collapse .site-collapse-custom-panel {background: #f7f7f7;border-radius: 2px;margin-bottom: 24px;border: 0px;overflow: hidden;}

你可以通过 showArrow={false} 隐藏 CollapsePanel 组件的箭头图标。
import { Collapse } from 'antd';const { Panel } = Collapse;function callback(key) {console.log(key);}const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;ReactDOM.render(<Collapse defaultActiveKey={['1']} onChange={callback}><Panel header="This is panel header with arrow icon" key="1"><p>{text}</p></Panel><Panel showArrow={false} header="This is panel header with no arrow icon" key="2"><p>{text}</p></Panel></Collapse>,mountNode,);

可以同时展开多个面板,这个例子默认展开了第一个。
import { Collapse, Select } from 'antd';import { SettingOutlined } from '@ant-design/icons';const { Panel } = Collapse;const { Option } = Select;function callback(key) {console.log(key);}const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;const genExtra = () => (<SettingOutlinedonClick={event => {// If you don't want click extra trigger collapse, you can prevent this:event.stopPropagation();}}/>);class Demo extends React.Component {state = {expandIconPosition: 'left',};onPositionChange = expandIconPosition => {this.setState({ expandIconPosition });};render() {const { expandIconPosition } = this.state;return (<><CollapsedefaultActiveKey={['1']}onChange={callback}expandIconPosition={expandIconPosition}><Panel header="This is panel header 1" key="1" extra={genExtra()}><div>{text}</div></Panel><Panel header="This is panel header 2" key="2" extra={genExtra()}><div>{text}</div></Panel><Panel header="This is panel header 3" key="3" extra={genExtra()}><div>{text}</div></Panel></Collapse><br /><span>Expand Icon Position: </span><Selectvalue={expandIconPosition}style={{ margin: '0 8px' }}onChange={this.onPositionChange}><Option value="left">left</Option><Option value="right">right</Option></Select></>);}}ReactDOM.render(<Demo />, mountNode);

将折叠面板的背景变成透明。
import { Collapse } from 'antd';const { Panel } = Collapse;const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;ReactDOM.render(<Collapse defaultActiveKey={['1']} ghost><Panel header="This is panel header 1" key="1"><p>{text}</p></Panel><Panel header="This is panel header 2" key="2"><p>{text}</p></Panel><Panel header="This is panel header 3" key="3"><p>{text}</p></Panel></Collapse>,mountNode,);

通过 collapsible 属性,可以设置面板的可折叠触发区域。
import { Collapse, Space } from 'antd';const { Panel } = Collapse;const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;ReactDOM.render(<Space direction="vertical"><Collapse collapsible="header" defaultActiveKey={['1']}><Panel header="This panel can only be collapsed by clicking text" key="1"><p>{text}</p></Panel></Collapse><Collapse collapsible="disabled"><Panel header="This panel can't be collapsed" key="1"><p>{text}</p></Panel></Collapse></Space>,mountNode,);
API
Collapse
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| accordion | 手风琴模式 | boolean | false | |
| activeKey | 当前激活 tab 面板的 key | string[] | string number[] | number | 默认无,accordion 模式下默认第一个元素 | |
| bordered | 带边框风格的折叠面板 | boolean | true | |
| collapsible | 所有子面板是否可折叠或指定可折叠触发区域 | header | disabled | - | 4.9.0 |
| defaultActiveKey | 初始化选中面板的 key | string[] | string number[] | number | - | |
| destroyInactivePanel | 销毁折叠隐藏的面板 | boolean | false | |
| expandIcon | 自定义切换图标 | (panelProps) => ReactNode | - | |
| expandIconPosition | 设置图标位置 | left | right | - | |
| ghost | 使折叠面板透明且无边框 | boolean | false | 4.4.0 |
| onChange | 切换面板的回调 | function | - |
Collapse.Panel
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| collapsible | 是否可折叠或指定可折叠触发区域 | header | disabled | - | 4.9.0 |
| extra | 自定义渲染每个面板右上角的内容 | ReactNode | - | |
| forceRender | 被隐藏时是否渲染 DOM 结构 | boolean | false | |
| header | 面板头内容 | ReactNode | - | |
| key | 对应 activeKey | string | number | - | |
| showArrow | 是否展示当前面板上的箭头 | boolean | true |