Collapse折叠面板

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

何时使用

  • 对复杂区域进行分组和隐藏,保持页面的整洁。

  • 手风琴 是一种特殊的折叠面板,只允许单个内容区域展开。

代码演示

Collapse折叠面板 - 图1

折叠面板

可以同时展开多个面板,这个例子默认展开了第一个。

  1. import { Collapse } from 'antd';
  2. const { Panel } = Collapse;
  3. function callback(key) {
  4. console.log(key);
  5. }
  6. const text = `
  7. A dog is a type of domesticated animal.
  8. Known for its loyalty and faithfulness,
  9. it can be found as a welcome guest in many households across the world.
  10. `;
  11. ReactDOM.render(
  12. <Collapse defaultActiveKey={['1']} onChange={callback}>
  13. <Panel header="This is panel header 1" key="1">
  14. <p>{text}</p>
  15. </Panel>
  16. <Panel header="This is panel header 2" key="2">
  17. <p>{text}</p>
  18. </Panel>
  19. <Panel header="This is panel header 3" key="3">
  20. <p>{text}</p>
  21. </Panel>
  22. </Collapse>,
  23. mountNode,
  24. );

Collapse折叠面板 - 图2

手风琴

手风琴,每次只打开一个 tab。

  1. import { Collapse } from 'antd';
  2. const { Panel } = Collapse;
  3. const text = `
  4. A dog is a type of domesticated animal.
  5. Known for its loyalty and faithfulness,
  6. it can be found as a welcome guest in many households across the world.
  7. `;
  8. ReactDOM.render(
  9. <Collapse accordion>
  10. <Panel header="This is panel header 1" key="1">
  11. <p>{text}</p>
  12. </Panel>
  13. <Panel header="This is panel header 2" key="2">
  14. <p>{text}</p>
  15. </Panel>
  16. <Panel header="This is panel header 3" key="3">
  17. <p>{text}</p>
  18. </Panel>
  19. </Collapse>,
  20. mountNode,
  21. );

Collapse折叠面板 - 图3

面板嵌套

嵌套折叠面板。

  1. import { Collapse } from 'antd';
  2. const { Panel } = Collapse;
  3. function callback(key) {
  4. console.log(key);
  5. }
  6. const text = `
  7. A dog is a type of domesticated animal.
  8. Known for its loyalty and faithfulness,
  9. it can be found as a welcome guest in many households across the world.
  10. `;
  11. ReactDOM.render(
  12. <Collapse onChange={callback}>
  13. <Panel header="This is panel header 1" key="1">
  14. <Collapse defaultActiveKey="1">
  15. <Panel header="This is panel nest panel" key="1">
  16. <p>{text}</p>
  17. </Panel>
  18. </Collapse>
  19. </Panel>
  20. <Panel header="This is panel header 2" key="2">
  21. <p>{text}</p>
  22. </Panel>
  23. <Panel header="This is panel header 3" key="3">
  24. <p>{text}</p>
  25. </Panel>
  26. </Collapse>,
  27. mountNode,
  28. );

Collapse折叠面板 - 图4

简洁风格

一套没有边框的简洁样式。

  1. import { Collapse } from 'antd';
  2. const { Panel } = Collapse;
  3. const text = (
  4. <p style={{ paddingLeft: 24 }}>
  5. A dog is a type of domesticated animal. Known for its loyalty and faithfulness, it can be found
  6. as a welcome guest in many households across the world.
  7. </p>
  8. );
  9. ReactDOM.render(
  10. <Collapse bordered={false} defaultActiveKey={['1']}>
  11. <Panel header="This is panel header 1" key="1">
  12. {text}
  13. </Panel>
  14. <Panel header="This is panel header 2" key="2">
  15. {text}
  16. </Panel>
  17. <Panel header="This is panel header 3" key="3">
  18. {text}
  19. </Panel>
  20. </Collapse>,
  21. mountNode,
  22. );

Collapse折叠面板 - 图5

自定义面板

自定义各个面板的背景色、圆角、边距和图标。

  1. import { Collapse } from 'antd';
  2. import { CaretRightOutlined } from '@ant-design/icons';
  3. const { Panel } = Collapse;
  4. const text = `
  5. A dog is a type of domesticated animal.
  6. Known for its loyalty and faithfulness,
  7. it can be found as a welcome guest in many households across the world.
  8. `;
  9. ReactDOM.render(
  10. <Collapse
  11. bordered={false}
  12. defaultActiveKey={['1']}
  13. expandIcon={({ isActive }) => <CaretRightOutlined rotate={isActive ? 90 : 0} />}
  14. className="site-collapse-custom-collapse"
  15. >
  16. <Panel header="This is panel header 1" key="1" className="site-collapse-custom-panel">
  17. <p>{text}</p>
  18. </Panel>
  19. <Panel header="This is panel header 2" key="2" className="site-collapse-custom-panel">
  20. <p>{text}</p>
  21. </Panel>
  22. <Panel header="This is panel header 3" key="3" className="site-collapse-custom-panel">
  23. <p>{text}</p>
  24. </Panel>
  25. </Collapse>,
  26. mountNode,
  27. );
  1. [data-theme='compact'] .site-collapse-custom-collapse .site-collapse-custom-panel,
  2. .site-collapse-custom-collapse .site-collapse-custom-panel {
  3. margin-bottom: 24px;
  4. overflow: hidden;
  5. background: #f7f7f7;
  6. border: 0px;
  7. border-radius: 2px;
  8. }

Collapse折叠面板 - 图6

隐藏箭头

你可以通过 showArrow={false} 隐藏 CollapsePanel 组件的箭头图标。

  1. import { Collapse } from 'antd';
  2. const { Panel } = Collapse;
  3. function callback(key) {
  4. console.log(key);
  5. }
  6. const text = `
  7. A dog is a type of domesticated animal.
  8. Known for its loyalty and faithfulness,
  9. it can be found as a welcome guest in many households across the world.
  10. `;
  11. ReactDOM.render(
  12. <Collapse defaultActiveKey={['1']} onChange={callback}>
  13. <Panel header="This is panel header with arrow icon" key="1">
  14. <p>{text}</p>
  15. </Panel>
  16. <Panel showArrow={false} header="This is panel header with no arrow icon" key="2">
  17. <p>{text}</p>
  18. </Panel>
  19. </Collapse>,
  20. mountNode,
  21. );

Collapse折叠面板 - 图7

额外节点

可以同时展开多个面板,这个例子默认展开了第一个。

  1. import { Collapse, Select } from 'antd';
  2. import { SettingOutlined } from '@ant-design/icons';
  3. const { Panel } = Collapse;
  4. const { Option } = Select;
  5. function callback(key) {
  6. console.log(key);
  7. }
  8. const text = `
  9. A dog is a type of domesticated animal.
  10. Known for its loyalty and faithfulness,
  11. it can be found as a welcome guest in many households across the world.
  12. `;
  13. const genExtra = () => (
  14. <SettingOutlined
  15. onClick={event => {
  16. // If you don't want click extra trigger collapse, you can prevent this:
  17. event.stopPropagation();
  18. }}
  19. />
  20. );
  21. class Demo extends React.Component {
  22. state = {
  23. expandIconPosition: 'left',
  24. };
  25. onPositionChange = expandIconPosition => {
  26. this.setState({ expandIconPosition });
  27. };
  28. render() {
  29. const { expandIconPosition } = this.state;
  30. return (
  31. <>
  32. <Collapse
  33. defaultActiveKey={['1']}
  34. onChange={callback}
  35. expandIconPosition={expandIconPosition}
  36. >
  37. <Panel header="This is panel header 1" key="1" extra={genExtra()}>
  38. <div>{text}</div>
  39. </Panel>
  40. <Panel header="This is panel header 2" key="2" extra={genExtra()}>
  41. <div>{text}</div>
  42. </Panel>
  43. <Panel header="This is panel header 3" key="3" extra={genExtra()}>
  44. <div>{text}</div>
  45. </Panel>
  46. </Collapse>
  47. <br />
  48. <span>Expand Icon Position: </span>
  49. <Select
  50. value={expandIconPosition}
  51. style={{ margin: '0 8px' }}
  52. onChange={this.onPositionChange}
  53. >
  54. <Option value="left">left</Option>
  55. <Option value="right">right</Option>
  56. </Select>
  57. </>
  58. );
  59. }
  60. }
  61. ReactDOM.render(<Demo />, mountNode);

Collapse折叠面板 - 图8

幽灵折叠面板

将折叠面板的背景变成透明。

  1. import { Collapse } from 'antd';
  2. const { Panel } = Collapse;
  3. const text = `
  4. A dog is a type of domesticated animal.
  5. Known for its loyalty and faithfulness,
  6. it can be found as a welcome guest in many households across the world.
  7. `;
  8. ReactDOM.render(
  9. <Collapse defaultActiveKey={['1']} ghost>
  10. <Panel header="This is panel header 1" key="1">
  11. <p>{text}</p>
  12. </Panel>
  13. <Panel header="This is panel header 2" key="2">
  14. <p>{text}</p>
  15. </Panel>
  16. <Panel header="This is panel header 3" key="3">
  17. <p>{text}</p>
  18. </Panel>
  19. </Collapse>,
  20. mountNode,
  21. );

Collapse折叠面板 - 图9

可折叠触发区域

通过 collapsible 属性,可以设置面板的可折叠触发区域。

  1. import { Collapse, Space } from 'antd';
  2. const { Panel } = Collapse;
  3. const text = `
  4. A dog is a type of domesticated animal.
  5. Known for its loyalty and faithfulness,
  6. it can be found as a welcome guest in many households across the world.
  7. `;
  8. ReactDOM.render(
  9. <Space direction="vertical">
  10. <Collapse collapsible="header" defaultActiveKey={['1']}>
  11. <Panel header="This panel can only be collapsed by clicking text" key="1">
  12. <p>{text}</p>
  13. </Panel>
  14. </Collapse>
  15. <Collapse collapsible="disabled">
  16. <Panel header="This panel can't be collapsed" key="1">
  17. <p>{text}</p>
  18. </Panel>
  19. </Collapse>
  20. </Space>,
  21. mountNode,
  22. );

API

Collapse

参数说明类型默认值版本
accordion手风琴模式booleanfalse
activeKey当前激活 tab 面板的 keystring[] | string
number[] | number
默认无,accordion 模式下默认第一个元素
bordered带边框风格的折叠面板booleantrue
collapsible所有子面板是否可折叠或指定可折叠触发区域header | disabled-4.9.0
defaultActiveKey初始化选中面板的 keystring[] | string
number[] | number
-
destroyInactivePanel销毁折叠隐藏的面板booleanfalse
expandIcon自定义切换图标(panelProps) => ReactNode-
expandIconPosition设置图标位置left | right-
ghost使折叠面板透明且无边框booleanfalse4.4.0
onChange切换面板的回调function-

Collapse.Panel

参数说明类型默认值版本
collapsible是否可折叠或指定可折叠触发区域header | disabled-4.9.0
extra自定义渲染每个面板右上角的内容ReactNode-
forceRender被隐藏时是否渲染 DOM 结构booleanfalse
header面板头内容ReactNode-
key对应 activeKeystring | number-
showArrow是否展示当前面板上的箭头booleantrue