Collapse折叠面板

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

何时使用

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

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

代码演示

Collapse 折叠面板 - 图1

折叠面板

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

  1. import { Collapse } from 'choerodon-ui';
  2. const Panel = Collapse.Panel;
  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 'choerodon-ui';
  2. const Panel = Collapse.Panel;
  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 'choerodon-ui';
  2. const Panel = Collapse.Panel;
  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 'choerodon-ui';
  2. const Panel = Collapse.Panel;
  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 'choerodon-ui';
  2. const Panel = Collapse.Panel;
  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. const customPanelStyle = {
  9. background: '#f7f7f7',
  10. borderRadius: 4,
  11. marginBottom: 24,
  12. border: 0,
  13. overflow: 'hidden',
  14. };
  15. ReactDOM.render(
  16. <Collapse bordered={false} defaultActiveKey={['1']}>
  17. <Panel header="This is panel header 1" key="1" style={customPanelStyle}>
  18. <p>{text}</p>
  19. </Panel>
  20. <Panel header="This is panel header 2" key="2" style={customPanelStyle}>
  21. <p>{text}</p>
  22. </Panel>
  23. <Panel header="This is panel header 3" key="3" style={customPanelStyle}>
  24. <p>{text}</p>
  25. </Panel>
  26. </Collapse>,
  27. mountNode,
  28. );

Collapse 折叠面板 - 图6

隐藏箭头

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

  1. import { Collapse } from 'choerodon-ui';
  2. const Panel = Collapse.Panel;
  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. );

API

Collapse

参数说明类型默认值
activeKey当前激活 tab 面板的 keystring[]|string默认无,accordion模式下默认第一个元素
defaultActiveKey初始化选中面板的 keystring
onChange切换面板的回调Function

Collapse.Panel

参数说明类型默认值
disabled禁用后的面板展开与否将无法通过用户交互改变booleanfalse
header面板头内容string|ReactNode
key对应 activeKeystring
forceRender被隐藏时是否渲染 DOM 结构booleanfalse