简体   繁体   中英

Dynamically expand/collapse on click of header

I have a set of items that needs to be shown in the UI, like a header and list of items under it.
There is a parent component where I am passing this data to a file that is shown below.
Based on this the parent-child layout is shown.
Now I need to expand/collapse based on the click of the header.

There is a class "open" and "close " that can be attached to the div. Based on it the it gets collapse/expands.
The point is how do it item wise

Can someone help


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

Ok let me explain it to you, here is your code

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

You can see I have taken a new property in state navBarStatus. Based on navBarStatus value I am changing CSS class which will expand/close your attached div

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM