简体   繁体   English

在react-native-side-menu中突出显示已查看组件上的菜单项

[英]Highlights the menu item on the viewed component in react-native-side-menu

I am using react-native-side-menu to view different component by refering to this answer . 我正在使用react-native-side-menu通过引用这个答案来查看不同的组件。 Each of the menu item when pressed will displaying the corresponding component. 按下每个菜单项将显示相应的组件。 Everything working fine except I don't know how to highlight the menu item on the current display component when the menu is opened. 一切正常,但我不知道如何在打开菜单时突出显示当前显示组件上的菜单项。

Let say I am changing the background color of the menu item as highlighted, how can toggle the style based on the viewed component? 假设我将菜单项的背景颜色更改为突出显示,如何根据查看的组件切换样式?

The menu items component: 菜单项组件:

module.exports = class Menu extends Component {
    static propTypes = {
        onItemSelected: React.PropTypes.func.isRequired,
    };

    render() {
    return (
      <ScrollView  style={styles.menu}>
       //the route is the menu items with component
       {
        routes.map((com) => {
            return(
            <Text key={com.id}
              onPress={() => this.props.onItemSelected(com)}
              style={styles.item}>
              {com.name}
            </Text>
            )
        })
      }
      </ScrollView>
    );
  }
}

The sidemenu component: sidemenu组件:

module.exports = class Main extends Component {
  constructor(props) {
    super(props);
    this.state = {
        isOpen: false,
        selectedItem: routes[0],
      };
  }

  toggle() {
    this.setState({
      isOpen: !this.state.isOpen,
    });
  }

  updateMenuState(isOpen) {
    this.setState({ isOpen, });
  }

  onMenuItemSelected = (item) => {
    this.setState({
      isOpen: false,
      selectedItem: item,
    });
  }

  render() {
    const menu = <Menu onItemSelected={this.onMenuItemSelected} />;
    let Component = this.state.selectedItem.compo  ;
    return (
      <SideMenu
        menu={menu}
        isOpen={this.state.isOpen}
        onChange={(isOpen) => this.updateMenuState(isOpen)}
        openMenuOffset={SCREEN_WIDTH * 0.4}
        >

        {Component}

        <Button style={styles.button} onPress={() => this.toggle()}>
          <Image
            source={require('./assets/menu.png')} style={{width: 35, height: 35}} />
        </Button>
      </SideMenu>
    );
  }
}

using [] on style prop allow to define an array of styles : so basically the main idea is to add a style depending on state. 在样式道具上使用[]允许定义样式数组:所以基本上主要的想法是根据状态添加样式。 let's say when an item is selected i should set it's background color to red else to white. 让我们说当选择一个项目时,我应该将它的背景颜色设置为红色,否则为白色。

So first, we should track the current selected Item, second define the backgroundColor dynamically and finally when clicking on item update the current selected item with the selected one. 首先,我们应该跟踪当前选择的项目,第二个动态定义backgroundColor,最后在点击项目时更新当前所选项目和所选项目。

module.exports = class Menu extends Component {
  constructor( props ) {
    super( props );
    this.state = {
      //Add a state to track what current selected Item
      selectedItem: routes[ 0 ].id
    };
  }

  static propTypes = {
    onItemSelected: React.PropTypes.func.isRequired,
  };

  render() {
    return (
    <ScrollView style={ styles.menu }>
      //the route is the menu items with component
      { routes.map( (com) => {
          return ( < Text
                          key={ com.id }
                          onPress={ () => {
                                      this.props.onItemSelected( com )
                                      //Change the current selected Item
                                      this.setState( {
                                        selectedItem: com.id
                                      } )
                                    } }
                          style={ [ styles.item, 
                                    { 
                                      backgroundColor: this.state.selectedItem == com.id ? 
                                      'red' : 'white' 
                                    } 
                                  ] }>
                     { com.name }
                     </Text>
          )
        } ) }
    </ScrollView>
    );
  }
}

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

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