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" disabled>
  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. .site-collapse-custom-collapse .site-collapse-custom-panel {
  2. background: #f7f7f7;
  3. border-radius: 2px;
  4. margin-bottom: 24px;
  5. border: 0px;
  6. overflow: hidden;
  7. }

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. <div>
  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 value={expandIconPosition} onChange={this.onPositionChange}>
  50. <Option value="left">left</Option>
  51. <Option value="right">right</Option>
  52. </Select>
  53. </div>
  54. );
  55. }
  56. }
  57. ReactDOM.render(<Demo />, mountNode);

API

Collapse

参数说明类型默认值版本
activeKey当前激活 tab 面板的 keystring[]|string| number[]|number默认无,accordion 模式下默认第一个元素
defaultActiveKey初始化选中面板的 keystring[]|string| number[]|number
bordered带边框风格的折叠面板booleantrue
accordion手风琴模式booleanfalse
onChange切换面板的回调Function
expandIcon自定义切换图标(panelProps) => ReactNode-
expandIconPosition设置图标位置left | right-
destroyInactivePanel销毁折叠隐藏的面板booleanfalse

Collapse.Panel

参数说明类型默认值版本
disabled禁用后的面板展开与否将无法通过用户交互改变booleanfalse
forceRender被隐藏时是否渲染 DOM 结构booleanfalse
header面板头内容string|ReactNode
key对应 activeKeystring|number
showArrow是否展示当前面板上的箭头booleantrue
extra自定义渲染每个面板右上角的内容ReactNode-