【问题标题】:How to make an Accordion component with Reactstrap?如何使用 Reactstrap 制作 Accordion 组件?
【发布时间】:2019-10-02 03:38:36
【问题描述】:

Reactstrap 在文档中没有 Accordion 的官方样本。

使用卡片组件,您可以扩展默认折叠行为以创建手风琴。要正确实现手风琴风格,请务必使用 .accordion 作为包装器。

<div class="accordion" id="accordionExample">
  <div class="card">
    <div class="card-header" id="headingOne">
      <h2 class="mb-0">
        <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </button>
      </h2>
    </div>

    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
        on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table,
        raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="card">
    <div class="card-header" id="headingTwo">
      <h2 class="mb-0">
        <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Collapsible Group Item #2
        </button>
      </h2>
    </div>
    <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
        on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table,
        raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="card">
    <div class="card-header" id="headingThree">
      <h2 class="mb-0">
        <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          Collapsible Group Item #3
        </button>
      </h2>
    </div>
    <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird
        on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table,
        raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
</div>

是否可以拥有像 Bootstrap 4 文档示例这样的 Accordion 组件?

【问题讨论】:

标签: javascript html css reactjs reactstrap


【解决方案1】:

Reactstrap docs 是对的。您可以使用CardCollapse 组件创建手风琴。 首先,初始化你的构造函数和状态。

    constructor(props) {
      super(props);
      this.toggle = this.toggle.bind(this);
      this.state = { collapse: 0, cards: [1, 2, 3, 4, 5] };
    }

然后是您切换/折叠手风琴的方法。

    toggle(e) {
      let event = e.target.dataset.event;
      this.setState({ collapse: this.state.collapse === Number(event) ? 0 : Number(event) });
    }

还有render() 函数:

render() {
  const {cards, collapse} = this.state;
  return (
    <div className="container">
        <h3 className="page-header">Reactstrap Accordion using card component</h3>
        {cards.map(index => {
          return (
            <Card style={{ marginBottom: '1rem' }} key={index}>
              <CardHeader onClick={this.toggle} data-event={index}>Header</CardHeader>
              <Collapse isOpen={collapse === index}>
              <CardBody>Example</CardBody>
              </Collapse>
            </Card>
          )
        })}     

      </div>
  );
}

别忘了导入:

import { Collapse, CardBody, Card, CardHeader } from 'reactstrap';

Source Code

【讨论】:

  • 这应该是一条评论。请使用代码 sn-ps 扩展您的答案。请参阅 Stackoverflow.com/help/how-to-answer
  • 如何添加最新的 react-strap 代码? reactstrap.github.io/?path=/docs/…
【解决方案2】:

如果你使用 reactstrap 和 react Hooks 你可以这样做

1 从 reactstrap 导入需要的组件

import { Collapse, CardBody, Card, CardHeader} from 'reactstrap';

2- 从反应中导入 useState

import React, {useState} from 'react';

3- 在您的组件中创建您的状态

const [toggleQuestion, setToggequestion] = useState(1);//1 is the default id to be opened by default

4- 制作您的手风琴。注意:在我的情况下,我只切换主体,但您可以通过移动折叠来切换所有内容。

<Card>
    <CardHeader onClick={() => setToggequestion(1)}>
        <span className="font-weight-bold">title</span>
    </CardHeader>
    <Collapse  isOpen={toggleQuestion === 1 ? true : false}>
        <CardBody>
            example text
        </CardBody>
    </Collapse>
</Card>

<Card>
    <CardHeader onClick={() => setToggequestion(2)}>
        <span className="font-weight-bold">title 2</span>
    </CardHeader>
    <Collapse  isOpen={toggleQuestion === 2 ? true : false}>
        <CardBody>
            example text 2
        </CardBody>
    </Collapse>
</Card>

现在您可以根据需要创建任意数量,并且只需要更改 ID。在我的情况下,id 只是 1 和 2

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 2018-11-29
    • 2018-06-24
    • 1970-01-01
    • 2021-03-18
    • 2019-06-05
    • 2017-12-29
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多