【问题标题】:how to expand only one collapse panel in ant-design如何在ant-design中只展开一个折叠面板
【发布时间】:2021-03-30 07:55:38
【问题描述】:

我使用 Ant-design 折叠作为手风琴,我的要求是当我加载页面时默认第一个面板应该打开,并且当我点击其他面板时,无论面板已经打开应该关闭并且只有点击的面板应该展开.有人可以帮我解决这个问题吗?

我正在使用 Ant-design 折叠:

https://ant.design/components/collapse/

问候,

【问题讨论】:

    标签: reactjs antd


    【解决方案1】:

    您需要使用“accordion”和“defaultActiveKey”属性。比如:

    import { Collapse } from 'antd';
    
    const { Panel } = Collapse;
    
    const text = `
      A dog is a type of domesticated animal.
      Known for its loyalty and faithfulness,
      it can be found as a welcome guest in many households across the world.
    `;
    
    ReactDOM.render(
      <Collapse accordion defaultActiveKey={['1']}>
        <Panel header="This is panel header 1" key="1">
          <p>{text}</p>
        </Panel>
        <Panel header="This is panel header 2" key="2">
          <p>{text}</p>
        </Panel>
        <Panel header="This is panel header 3" key="3">
          <p>{text}</p>
        </Panel>
      </Collapse>,
      mountNode,
    );
    

    【讨论】:

    • 很好的回答!只是一点点,如果他想让他的第一个面板成为默认面板,它应该是 defaultActiveKey={['0']},并且不需要在 Panels 上指定 keys 属性
    • 取决于手风琴项的键。在以下示例中,键从 1 开始。
    • 现场示例:codesandbox.io/s/wt6g8
    猜你喜欢
    • 2018-06-09
    • 2018-07-23
    • 2016-02-28
    • 2016-04-17
    • 2019-01-09
    • 1970-01-01
    • 2016-06-20
    • 2022-01-23
    • 2015-10-28
    相关资源
    最近更新 更多