Collapse 折叠面板

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

何时使用

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

代码演示

折叠面板

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

Collapse折叠面板 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Collapse } from 'choerodon-ui';
  4. const Panel = Collapse.Panel;
  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. ReactDOM.render(
  14. <Collapse defaultActiveKey={['1']} onChange={callback}>
  15. <Panel header="This is panel header 1" key="1">
  16. <p>{text}</p>
  17. </Panel>
  18. <Panel header="This is panel header 2" key="2">
  19. <p>{text}</p>
  20. </Panel>
  21. <Panel header="This is panel header 3" key="3" disabled>
  22. <p>{text}</p>
  23. </Panel>
  24. </Collapse>,
  25. document.getElementById('container'),
  26. );

手风琴

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

Collapse折叠面板 - 图2

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

面板嵌套

嵌套折叠面板。

Collapse折叠面板 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Collapse } from 'choerodon-ui';
  4. const Panel = Collapse.Panel;
  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. ReactDOM.render(
  14. <Collapse onChange={callback}>
  15. <Panel header="This is panel header 1" key="1">
  16. <Collapse defaultActiveKey="1">
  17. <Panel header="This is panel nest panel" key="1">
  18. <p>{text}</p>
  19. </Panel>
  20. </Collapse>
  21. </Panel>
  22. <Panel header="This is panel header 2" key="2">
  23. <p>{text}</p>
  24. </Panel>
  25. <Panel header="This is panel header 3" key="3">
  26. <p>{text}</p>
  27. </Panel>
  28. </Collapse>,
  29. document.getElementById('container'),
  30. );

自定义面板

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

Collapse折叠面板 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Collapse } from 'choerodon-ui';
  4. const Panel = Collapse.Panel;
  5. const text = `
  6. A dog is a type of domesticated animal.
  7. Known for its loyalty and faithfulness,
  8. it can be found as a welcome guest in many households across the world.
  9. `;
  10. const customPanelStyle = {
  11. background: '#f7f7f7',
  12. borderRadius: 4,
  13. marginBottom: 24,
  14. border: 0,
  15. overflow: 'hidden',
  16. };
  17. ReactDOM.render(
  18. <Collapse bordered={false} defaultActiveKey={['1']}>
  19. <Panel header="This is panel header 1" key="1" style={customPanelStyle}>
  20. <p>{text}</p>
  21. </Panel>
  22. <Panel header="This is panel header 2" key="2" style={customPanelStyle}>
  23. <p>{text}</p>
  24. </Panel>
  25. <Panel header="This is panel header 3" key="3" style={customPanelStyle}>
  26. <p>{text}</p>
  27. </Panel>
  28. </Collapse>,
  29. document.getElementById('container'),
  30. );

隐藏箭头

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

Collapse折叠面板 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Collapse } from 'choerodon-ui';
  4. const Panel = Collapse.Panel;
  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. ReactDOM.render(
  14. <Collapse defaultActiveKey={['1']} onChange={callback}>
  15. <Panel header="This is panel header with arrow icon" key="1">
  16. <p>{text}</p>
  17. </Panel>
  18. <Panel showArrow={false} header="This is panel header with no arrow icon" key="2">
  19. <p>{text}</p>
  20. </Panel>
  21. </Collapse>,
  22. document.getElementById('container'),
  23. );

简洁风格

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

Collapse折叠面板 - 图6

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

自定义图标

自定义图标、位置、点击触发区域。

Collapse折叠面板 - 图7

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Collapse } from 'choerodon-ui';
  4. const Panel = Collapse.Panel;
  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. ReactDOM.render(
  14. <Collapse
  15. defaultActiveKey={['1']}
  16. onChange={callback}
  17. trigger="icon"
  18. expandIconPosition="right"
  19. expandIcon="text"
  20. >
  21. <Panel header="This is panel header 1" key="1">
  22. <p>{text}</p>
  23. </Panel>
  24. <Panel header="This is panel header 2" key="2">
  25. <p>{text}</p>
  26. </Panel>
  27. <Panel header="This is panel header 3" key="3" disabled>
  28. <p>{text}</p>
  29. </Panel>
  30. </Collapse>,
  31. document.getElementById('container'),
  32. );

API

Collapse

参数说明类型默认值
activeKey当前激活 tab 面板的 keystring[]|string默认无,accordion模式下默认第一个元素
defaultActiveKey初始化选中面板的 keystring
onChange切换面板的回调Function
bordered带边框风格的折叠面板booleantrue
accordion手风琴模式booleanfalse
expandIcon自定义切换图标(panelProps) => ReactNode | text(预置icon + 展开收起文字)
expandIconPosition设置图标位置left | rightleft
destroyInactivePanel销毁折叠隐藏的面板booleanfalse
trigger切换面板的触发位置header | iconheader

Collapse.Panel

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