• Collapse折叠面板
    • 何时使用
    • 代码演示
    • API
      • Collapse
      • Collapse.Panel

    Collapse折叠面板

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

    何时使用

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

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

    代码演示

    Collapse 折叠面板 - 图1

    折叠面板

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

    1. import { Collapse } from 'antd';
    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 'antd';
    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 'antd';
    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 'antd';
    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, Icon } from 'antd';
    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
    17. bordered={false}
    18. defaultActiveKey={['1']}
    19. expandIcon={({ isActive }) => <Icon type="caret-right" rotate={isActive ? 90 : 0} />}
    20. >
    21. <Panel header="This is panel header 1" key="1" style={customPanelStyle}>
    22. <p>{text}</p>
    23. </Panel>
    24. <Panel header="This is panel header 2" key="2" style={customPanelStyle}>
    25. <p>{text}</p>
    26. </Panel>
    27. <Panel header="This is panel header 3" key="3" style={customPanelStyle}>
    28. <p>{text}</p>
    29. </Panel>
    30. </Collapse>,
    31. mountNode,
    32. );

    Collapse 折叠面板 - 图6

    隐藏箭头

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

    1. import { Collapse } from 'antd';
    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. );

    Collapse 折叠面板 - 图7

    额外节点

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

    1. import { Collapse, Icon, Select } from 'antd';
    2. const { Panel } = Collapse;
    3. const { Option } = Select;
    4. function callback(key) {
    5. console.log(key);
    6. }
    7. const text = `
    8. A dog is a type of domesticated animal.
    9. Known for its loyalty and faithfulness,
    10. it can be found as a welcome guest in many households across the world.
    11. `;
    12. const genExtra = () => (
    13. <Icon
    14. type="setting"
    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. Expand Icon Position:{' '}
    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默认无,accordion 模式下默认第一个元素
    defaultActiveKey初始化选中面板的 keystring
    bordered带边框风格的折叠面板booleantrue
    accordion手风琴模式booleanfalse
    onChange切换面板的回调Function
    expandIcon自定义切换图标(panelProps) => ReactNode-
    expandIconPosition设置图标位置: left, rightleft-3.17.0
    destroyInactivePanel销毁折叠隐藏的面板booleanfalse

    Collapse.Panel

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