如何在react中创建折叠展开列表

IT技术 javascript reactjs
2021-05-01 20:04:34

我尝试React (v 16.5)使用以下标准创建折叠和展开侧边菜单-

在页面加载第一项(通告)将在展开视图中。任何一项都可以一次展开,例如,如果用户单击第二项(规格),则第一项将折叠。我还需要在折叠/展开事务期间使用一些 CSS 动画,例如平滑地向下/向上每个项目的正文部分并更改箭头图标。我的方法是在每个项目 ( sidebar-nav-menu-item) 上动态添加/删除 CSS 类,例如 -

sidebar-nav-menu-item item-active

因此,当一个项目处于展开视图时,它应该像上面的类一样,并item-active在它处于折叠视图时移除默认情况下,div当项目处于折叠模式时,body (sidebar-nav-menu-item-body) 应该通过 CSS 隐藏。

import React, { Component } from 'react';

className SidebarNavs extends React.Component{
    constructor(props) {
       super(props);
    }

    render() {
        return(
            <div className="sidebar-nav">
                <div className="sidebar-nav-menu">

                    <div className="sidebar-nav-menu-item" data-id="circulars">
                        <div className="sidebar-nav-menu-item-head" onClick={this.handleExpandCollaps}>
                            <div className="sidebar-nav-menu-item-head-title">Circulars</div>
                            <div className="sidebar-nav-menu-item-head-help">
                                <button type="button" className="btn-help" onClick={this.moreInfoClick}>View more info</button>
                            </div>
                            <div className="sidebar-nav-menu-item-head-icon">
                                <i className="fa fa-caret-down" aria-hidden="true"></i>
                            </div>
                        </div>
                        <div className="sidebar-nav-menu-item-body">BODY CONTENT HERE</div>
                    </div>

                    <div className="sidebar-nav-menu-item" data-id="specifications">
                        <div className="sidebar-nav-menu-item-head" onClick={this.handleExpandCollaps}>
                            <div className="sidebar-nav-menu-item-head-title">Specifications</div>
                            <div className="sidebar-nav-menu-item-head-help">
                                <button type="button" className="btn-help" onClick={this.moreInfoClick}>View more info</button>
                            </div>
                            <div className="sidebar-nav-menu-item-head-icon">
                                <i className="fa fa-caret-down" aria-hidden="true"></i>
                            </div>
                        </div>
                        <div className="sidebar-nav-menu-item-body">BODY CONTENT HERE</div>
                    </div>


                    <div className="sidebar-nav-menu-item"  data-id="wo">
                        <div className="sidebar-nav-menu-item-head" onClick={this.handleExpandCollaps}>
                            <div className="sidebar-nav-menu-item-head-title">Work Orders</div>
                            <div className="sidebar-nav-menu-item-head-help">
                                <button type="button" className="btn-help" onClick={this.moreInfoClick}>View more info</button>
                            </div>
                            <div className="sidebar-nav-menu-item-head-icon">
                                <i className="fa fa-caret-down" aria-hidden="true"></i>
                            </div>
                        </div>
                        <div className="sidebar-nav-menu-item-body">BODY CONTENT HERE</div>
                    </div>

                </div>
            </div>
        )
    }
}

export default SidebarNavs;

CSS:

.sidebar-nav-menu-item{
   display:block;
}
.sidebar-nav-menu-item-body{
   display:none;
}
.sidebar-nav-menu-item.item-active .sidebar-nav-menu-item-body{
   display:block;
}
2个回答

您应该使用状态变量来显示您的可折叠项目处于活动状态/非活动状态。

我稍微修改了您的代码以使其符合您的要求。

class App extends Component {
  constructor() {
    super();
    this.state = {
        activeCollapse: 'circulars'
    };
  }

  handleExpandCollaps = (name) => {
    if (this.state.activeCollapse === name) {
        this.setState({ activeCollapse: '' })
    } else {
        this.setState({ activeCollapse: name })
    }
  }

  moreInfoClick = (e) => {
    e.stopPropagation();
    console.log("clicked");
  }
  render() {
    return (
      <div>
        <div className="sidebar-nav">
          <div className="sidebar-nav-menu">

            <div className={`sidebar-nav-menu-item ${this.state.activeCollapse === "circulars" ? 'item-active' : ''}`} onClick={() => this.handleExpandCollaps("circulars")} data-id="circulars" >
              <div className="sidebar-nav-menu-item-head">
                <span className="sidebar-nav-menu-item-head-title">Circulars</span>
                <span className="sidebar-nav-menu-item-head-help">
                  <button type="button" className="btn-help" onClick={this.moreInfoClick}>View more info</button>
                </span>
              </div>
              <div className="sidebar-nav-menu-item-body">BODY CONTENT HERE</div>
            </div>

            <div className={`sidebar-nav-menu-item ${this.state.activeCollapse === "specifications" ? 'item-active' : ''}`} onClick={() => this.handleExpandCollaps("specifications")} data-id="specifications">
              <div className="sidebar-nav-menu-item-head">
                <span className="sidebar-nav-menu-item-head-title">Specifications</span>
                <span className="sidebar-nav-menu-item-head-help">
                  <button type="button" className="btn-help" onClick={this.moreInfoClick}>View more info</button>
                </span>
              </div>
              <div className="sidebar-nav-menu-item-body">BODY CONTENT HERE</div>
            </div>


            <div className={`sidebar-nav-menu-item ${this.state.activeCollapse === "wo" ? 'item-active' : ''}`} onClick={() => this.handleExpandCollaps("wo")} data-id="wo">
              <div className="sidebar-nav-menu-item-head">
                <span className="sidebar-nav-menu-item-head-title">Work Orders</span>
                <span className="sidebar-nav-menu-item-head-help">
                  <button type="button" className="btn-help" onClick={this.moreInfoClick}>View more info</button>
                </span>
              </div>
              <div className="sidebar-nav-menu-item-body">BODY CONTENT HERE</div>
            </div>

          </div>
        </div>
      </div>
    );
  }
}

注:我已经使用CSSfont-awesome图标。希望你已经添加font-awesome

演示

要做到这一点,我会使用React.useState,因为它是一个控制和动画的小状态,我会使用 CSS:

该组件将如下所示:

function SidebarNavs() {
  const [activeItem, setActiveItem] = React.useState(1);

  return (
    <div className="sidebar-nav">
      <div className="sidebar-nav-menu">
        <SidebarItem
          title="Circulars"
          setActiveItem={setActiveItem}
          index={1}
          activeItem={activeItem}
        >
          Sidebar Content Here
        </SidebarItem>

        <SidebarItem
          title="Specifications"
          setActiveItem={setActiveItem}
          index={2}
          activeItem={activeItem}
        >
          Sidebar Content Here
        </SidebarItem>

        <SidebarItem
          title="Specifications"
          setActiveItem={setActiveItem}
          index={3}
          activeItem={activeItem}
        >
          Work Orders
        </SidebarItem>
      </div>
    </div>
  );
}

function SidebarItem({ title, children, setActiveItem, activeItem, index }) {
  const expanded = activeItem === index;
  const cls = "sidebar-nav-menu-item " + (expanded ? "item-active" : "");
  return (
    <div className={cls}>
      <div className="sidebar-nav-menu-item-head">
        <div className="sidebar-nav-menu-item-head-title">{title}</div>
        <div className="sidebar-nav-menu-item-head-help">
          <button
            type="button"
            className="btn-help"
            onClick={() => setActiveItem(index)}
          >
            View more info
          </button>
        </div>
        <div className="sidebar-nav-menu-item-head-icon">
          <i className="fa fa-caret-down" aria-hidden="true" />
        </div>
      </div>
      <div className="sidebar-nav-menu-item-body">{children}</div>
    </div>
  );
}

CSS 看起来像这样:

.sidebar-nav-menu-item {
  border: 1px solid #CCC;
  margin-bottom: 20px;
}
.sidebar-nav-menu-item .sidebar-nav-menu-item-body {
  overflow: hidden;
  max-height: 0;
  transition: all linear 0.5s;
}

.sidebar-nav-menu-item.item-active .sidebar-nav-menu-item-body {
  max-height: 100px;
  transition: all linear 0.5s 0.3s;
}