简体   繁体   English

切换类添加类但不删除它

[英]Toggle class adding class but not removing it

I've got got a sidebar which shows a list of menu items. 我有一个侧边栏,显示菜单项列表。 Some of those menu items have children so I have an expand button which adds a class and those child pages show via css. 这些菜单项中有一些具有子项,因此我有一个扩展按钮来添加一个类,并且这些子页面通过CSS显示。

{
    item.childPages ? <button className = { i === this.state.activeId ? "active child-nav-control" : "child-nav-control" } onClick={() => this.setActiveElement(i)}></button> : null
}

I've managed to add the class on each individual item but when I click it a second time the class isn't removed. 我设法在每个单独的项目上添加了班级,但是当我第二次单击它时,班级却没有被删除。 Can anyone see where I'm going wrong? 谁能看到我要去哪里错了?

class Sidebar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      activeId: null
    }
    this.setActiveElement = this.setActiveElement.bind(this);
  }

  setActiveElement(id){
    this.setState({activeId: id});
  }

  render(props) {
    const data = this.props.nav.contentfulNavigation.navigationItem

    return (
      <Menu>
          <ul id="mainnav">
            <li><Link className="logo" to="/"><img src={logo} alt="Shell Logo" /></Link></li>
            {data.map((item, i) =>
              <li key={i} className={item.childPages ? item.title.replace(/\s+/g, '-') + ' has-child-wrap' : item.title.replace(/\s+/g, '-')}>
                <Link partiallyActive={true} className={item.childPages ? 'has-child' : null}
                      to={item.slug}
                      activeClassName={item.slug === '/' ? null : 'active'}>
                      {item.menuTitle}
                </Link>
                {item.childPages ? <button className={i === this.state.activeId ? "active child-nav-control" : "child-nav-control"} onClick={() => this.setActiveElement(i)}></button> : null}  
                {item.childPages && <Nav nav={item.childPages} />}
              </li>
            )}
          </ul>
      </Menu>
    )
  }
}

const Nav = ({ nav }) => {
    //console.log(nav)
    return (
      <ul className="children">
        {nav.map((node) => (
          <li key={node.id}>
            <Link partiallyActive={true} activeClassName="active child" to={node.slug}>{node.menuTitle}</Link>
          </li>
        ))}
      </ul>
    )
}

export default Sidebar

To make this work you have to check in setActiveElement if the currently active id is the one that was clicked (again) and set it null in this case: 为了使这项工作有效,您必须检入setActiveElement是否当前活动的id是再次单击的ID,在这种情况下,请将其设置为null

setActiveElement(id){
    this.setState(({activeId}) => ({activeId: id === activeId ? null : id}));
}

If I've understood correctly, you want to click the button initially for it to have the 'active' class applied, and the children to show; 如果我理解正确,您首先要单击该按钮以使其应用“活动”类,并显示子级。 then if you click again you want it to collapse and the 'active' class removed? 那么,如果您再次单击,则希望它折叠并删除“活动”类?

If that's right, what you have at the moment means if you click another item, that becomes the active item and the class is removed from the previous - this only allows one 'active' item and one expanded submenu. 如果是正确的话,那么您现在所拥有的意味着如果您单击另一个项目,该项目将成为活动项目,并且该类将从上一个项目中删除-这仅允许一个“活动”项目和一个扩展子菜单。

Given that, all you need is if the clicked item is already the active item, set activeId to null instead: 鉴于此,您所需要的只是如果单击的项目已经是活动项目,则将activeId设置为null:

  setActiveElement(id){
    let newId = ( id === this.state.activeId ? null : id );
    this.setState({activeId: newId});
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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