单击 header 时动态 expand/collapse

Dynamically expand/collapse on click of header

我有一组项目需要在 UI 中显示,例如 header 和它下面的项目列表。
有一个 parent 组件,我将此数据传递到如下所示的文件。
基于此显示 parent-child 布局。
现在我需要expand/collapse基于header的点击。

有一个class"open"和"close "可以附加到div。基于它,它得到 collapse/expands.
关键是如何明智地做到这一点

有人可以帮忙


import React from "react";
import Child from "./Child";
import Parent from "./Parent";

export default class Helper extends React.Component{
  constructor(props: any) {
    super(props);
    this.state = {
      parent:{},
      children:{},
    };
  }

  componentDidMount() {
    this.setParentValue();
    this.setChildValue();
  }


  render() {
    const { parent, children } = this.state;
    const { name } = this.props;
    return (
      <>
        <div className="an-panel expand-panel expand-close">
          <div className="an-panel-header">
            <div className="title-holder">
              <span className="toggle-icon far fa-plus-square" />
              <span className="toggle-icon far fa-minus-square" />
              <h5>{name}</h5>
            </div>
            <div className="action-holder">
              <div className="status-holder">
                <Parent
                  parent = {parent}
                  onSelect={this.handleParentClick}
                />
              </div>
            </div>
          </div>
          {children.map(({ id, name },id) => (
            <div className="an-panel-body" key={id}>
              <ul className="applications-list-holder">
                <li>
                  <div className="name">{name}</div>
                  <div className="status">
                    <Child
                      children={children}
                      onSelect={this.setChildSwitchValue}
                    />
                  </div>
                </li>
              </ul>
            </div>
          ))}
        </div>
      </>
    );
  }
}

好的,我给你解释一下,这是你的代码

    import React from "react";
import Child from "./Child";
import Parent from "./Parent";

export default class Helper extends React.Component{
    constructor(props: any) {
        super(props);
        this.state = {
            parent:{},
            children:{},
            navBarStatus: false,
        };
    }

    componentDidMount() {
        this.setParentValue();
        this.setChildValue();
    }

   changeNavBar = (e, status)=>{
        this.setState({navBarStatus: !status});
    }


    render() {
        const { parent, children } = this.state;
        const { name } = this.props;
        return (
            <>
                <div className={`an-panel expand-panel ${this.state.navBarStatus ? "expand-open" : "expand-close"}`}>
              <div className="an-panel-header" onClick={(e)=>this.changeNavBar(e, this.state.navBarStatus)}>
                <div className="title-holder">
                  <span className="toggle-icon far fa-plus-square" />
                  <span className="toggle-icon far fa-minus-square" />
                  <h5>{name}</h5>
                </div>
                <div className="action-holder">
                  <div className="status-holder">
                    <Parent
                      parent = {parent}
                      onSelect={this.handleParentClick}
                    />
                  </div>
                </div>
              </div>
              {children.map(({ id, name },id) => (
                <div className="an-panel-body" key={id}>
                  <ul className="applications-list-holder">
                    <li>
                      <div className="name">{name}</div>
                      <div className="status">
                        <ChildSetting
                          children={children}
                          onSelect={this.setChildSwitchValue}
                        />
                      </div>
                    </li>
                  </ul>
                </div>
              ))}
            </div>
          </>
        );
      }
    }

你可以看到我在状态 navBarStatus 中获取了一个新的 属性。基于 navBarStatus 值,我正在更改 CSS class 这将 expand/close 你的附件 div