Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an Accordion component with Reactstrap?

Reactstrap does not have an official sample for Accordion in the documentation.

Using the card component, you can extend the default collapse behavior to create an accordion. To properly achieve the accordion style, be sure to use .accordion as a wrapper.

<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>

Is it possible to have Accordion component like Bootstrap 4 documentation sample?

like image 762
HamedFathi Avatar asked Dec 05 '22 09:12

HamedFathi


2 Answers

The Reactstrap docs is right. You can create an accordion using Card and Collapse components. First, initialize your constructor and state.

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

And then your method to toggle/collapse the accordion.

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

And the render() function:

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>
  );
}

And don't forget to import:

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

Source Code

like image 115
Igor Alves Avatar answered Dec 07 '22 22:12

Igor Alves


If you use reactstrap and react Hooks you can do it as follows

1 import the required components from reactstrap

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

2- import useState from react

import React, {useState} from 'react';

3- within your component create your states

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

4- Create your accordion. Note: in my case I only toggle the body but you can toggle everything by moving the collapse.

<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>

Now you can create as many as you want and only need to change the ID. In my case the id was just 1 and 2

like image 33
jerryurenaa Avatar answered Dec 07 '22 21:12

jerryurenaa