首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用反身带制作手风琴组件?

如何用反身带制作手风琴组件?
EN

Stack Overflow用户
提问于 2019-05-15 04:29:03
回答 2查看 10.6K关注 0票数 5

在文档中,反应带没有手风琴的正式示例。

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

代码语言:javascript
复制
<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文档样例这样的手风琴组件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-09 00:35:31

反应带文档是对的。您可以使用CardCollapse组件创建手风琴。首先,初始化构造函数和状态。

代码语言:javascript
复制
    constructor(props) {
      super(props);
      this.toggle = this.toggle.bind(this);
      this.state = { collapse: 0, cards: [1, 2, 3, 4, 5] };
    }

然后你的方法来切换/折叠手风琴。

代码语言:javascript
复制
    toggle(e) {
      let event = e.target.dataset.event;
      this.setState({ collapse: this.state.collapse === Number(event) ? 0 : Number(event) });
    }

render()函数:

代码语言:javascript
复制
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>
  );
}

别忘了进口:

代码语言:javascript
复制
import { Collapse, CardBody, Card, CardHeader } from 'reactstrap';

源代码

票数 11
EN

Stack Overflow用户

发布于 2020-07-30 21:15:27

如果你使用反应带和反应钩子,你可以这样做如下

从reactstrap导入所需的组件

代码语言:javascript
复制
import { Collapse, CardBody, Card, CardHeader} from 'reactstrap';

2-从react进口useState

代码语言:javascript
复制
import React, {useState} from 'react';

3-在组件中创建状态

代码语言:javascript
复制
const [toggleQuestion, setToggequestion] = useState(1);//1 is the default id to be opened by default

创造你的手风琴。注意:在我的例子中,我只切换身体,但是你可以通过移动崩溃来切换所有的东西。

代码语言:javascript
复制
<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。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56141786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档