2017-11-08 6 views
0

아이콘을 클릭하면 열리는 사이드 메뉴가 있으며 페이지를 클릭하거나 메뉴의 항목을 클릭하면 닫힙니다. 자물쇠 아이콘을 클릭하면 메뉴 항목이나 페이지를 클릭해도 메뉴가 닫히지 않으므로 자물쇠를 구현하려고합니다.메뉴 반응을 잠그거나 막지 않기

잠긴 아이콘에서 잠기지 않은 아이콘으로 변경 아이콘을 설정할 수 있었지만 닫기를 중지하는 기능에 문제가 있습니다. 어떤 도움이나 제안이 굉장 할 것입니다. https://codesandbox.io/s/pw6q46jp20

class SideBar extends Component { 
 
    constructor(props) { 
 
    super(props); 
 
    this.state = { 
 
     category: props.category, 
 
     currentPage: { 
 
     backgroundColor: '#383838', 
 
     color: '#A9A9A9', 
 
     }, 
 
     menuLocked: false, 
 
     menuOpen: false, 
 
     selectedIcon: null, 
 
    }; 
 
    } 
 
    handleIconClick = (category) => { 
 
    this.setState({ 
 
     category, 
 
     menuOpen: true, 
 
     selectedIcon: category, 
 
    }); 
 
    }; 
 
    handleMenuItemClick = (category) => { 
 
    this.setState({ 
 
     menuOpen: false, 
 
     selectedIcon: '', 
 
     currentPage: category, 
 
    }); 
 
    } 
 
    handlePageClick =() => { 
 
    this.setState({ 
 
     menuOpen: false, 
 
    }); 
 
    } 
 
    handleMenuLock =() => { 
 
    this.setState({ menuLocked: !this.state.menuLocked }); 
 
    } 
 
    render() { 
 
    return (
 
     <div> 
 
     <TopBar {...this.props} /> 
 
     <Container> 
 
      <SideNav> 
 
      <ListWrapper> 
 
       <IconList 
 
       currentPage={this.state.currentPage} 
 
       openMenu={this.handleIconClick} 
 
       selectedIcon={this.state.selectedIcon} 
 
       visible={this.state.menuOpen} 
 
       /> 
 
      </ListWrapper> 
 
      </SideNav> 
 
      <PageContentWrapper> 
 
      <Sidebar.Pushable> 
 
       <PushMenu 
 
       category={this.state.category} 
 
       closeOnItemClick={this.handleMenuItemClick} 
 
       handleMenuLock={this.handleMenuLock} 
 
       menuLocked={this.state.menuLocked} 
 
       params={this.props.params} 
 
       visible={this.state.menuOpen} 
 
       /> 
 
       <Sidebar.Pusher 
 
       onClick={this.handlePageClick}> 
 
       <Segment basic> 
 
        {this.props.children || <Home />} 
 
       </Segment> 
 
       </Sidebar.Pusher> 
 
      </Sidebar.Pushable> 
 
      </PageContentWrapper> 
 
     </Container> 
 
     </div> 
 
    ); 
 
    } 
 
} 
 

 
SideBar.propTypes = { 
 
    children: PropTypes.any, 
 
    params: PropTypes.any, 
 
    category: PropTypes.any, 
 
}; 
 

 
export default SideBar;

class PushMenu extends React.Component { 
 
    render() { 
 
    const category = sideNavData.lookupCategory(this.props.category); 
 
    const menuLockButton = !this.props.menuLocked 
 
     ? <Icon 
 
     link={true} 
 
     name='unlock' 
 
     size='big' 
 
     /> 
 
     : <Icon 
 
     link={true} 
 
     name='lock' 
 
     size='big' 
 
     />; 
 
    return (
 
     <Sidebar 
 
     className='Push-menu' 
 
     animation='push' 
 
     width='thin' 
 
     visible={this.props.visible} 
 
     > 
 
     <Header>{this.props.category}</Header> 
 
     <Linebreak /> 
 
     { 
 
      Object.keys(category).map((subCategory, index) => { 
 
      return (
 
       <div key={index}> 
 
       <SubcategoryHeader>{subCategory}</SubcategoryHeader> 
 
       <List> 
 
        { 
 
        Object.keys(category[subCategory]).map((item, index) => (
 
         <li key={index}> 
 
         <ListLink> 
 
          <Link 
 
          to={`/${this.props.category}/${subCategory}/${category[subCategory][item].name}`} 
 
          activeStyle={{ color: '#FFFFFF' }} 
 
          onClick={() => { 
 
           this.props.closeOnItemClick(this.props.category); 
 
          }} 
 
          > 
 
          {category[subCategory][item].name} 
 
          </Link> 
 
         </ListLink> 
 
         </li> 
 
        )) 
 
        } 
 
       </List> 
 
       <LockButtonWrapper onClick={this.props.handleMenuLock}> 
 
        {menuLockButton} 
 
       </LockButtonWrapper> 
 
       </div> 
 
      ); 
 
      }) 
 
     } 
 
     </Sidebar> 
 
    ); 
 
    } 
 
} 
 

 
PushMenu.propTypes = { 
 
    category: PropTypes.any, 
 
    closeOnItemClick: PropTypes.func, 
 
    currentPage: PropTypes.func, 
 
    handleMenuLock: PropTypes.func, 
 
    menuLocked: PropTypes.bool, 
 
    visible: PropTypes.bool, 
 
}; 
 

 
export default PushMenu;

답변

1

handlePageClick()의 내부는 this.state.menuLocked 사실이 있는지 확인을해야한다. 그렇다면 상태에있는 menuOpen의 값을 변경하지 마십시오. 새 값을 설정 menuLocked의 이전 값을 사용하는 handleMenuLock()에 보조 노트로

handlePageClick =() => { 
    if (!this.state.menuLocked) { 
    this.setState({ 
     menuOpen: false, 
    }); 
    } 
} 

. setState은 비동기식이므로 실제로 저장하는 시간에 따라 menuLocked 값이 변경되어 원하는 결과를 얻지 못할 수도 있습니다. 따라서 다음과 같은 함수를 전달해야합니다.

handleMenuLock =() => { 
    this.setState((prevState) => ({ menuLocked: !prevState.menuLocked })) 
} 

이렇게하면 실제로 값이 전환됩니다. 이 방식으로 setState을 사용하는 문서는 here입니다.

+0

감사합니다. – StuffedPoblano

1

: 아래 (부모 및 잠금 버튼으로 메뉴) 여기 전체보기를위한 코드 샌드 박스라는 두 구성 요소 this.state.menuLocked이 false 인 경우에만 handlePageClick 메서드를 setState로 변경할 수 있습니다.

handlePageClick =() => { 
 
    if (!this.state.menuLocked) { 
 
     this.setState({ 
 
     menuOpen: false, 
 
     }); 
 
    } 
 
    }

관련 문제