How to set focus on a MenuItem in material-ui

强颜欢笑 提交于 2019-12-12 03:42:38

问题


I am trying to programmatically set focus on (activate) one of the MenuItem(s) inside the Menu component in material-ui. I can manually do it by tabbing to it but I need to do it programmatically in response to a key down event.

<Menu disableAutoFocus={true}>
   <MenuItem .../>
   <MenuItem .../>
   ...
</Menu>

回答1:


Do you mean by select the menuItem programmatically? If so, you can use the concept of 'controlled component'.

Here is the example, If this.state.selectedItem = 1, the item 'Maps' will be selected. If this.state.selectedItem = 2, the item 'Books' will be selected.

  <Menu
    selectedMenuItemStyle={ {backgroundColor: '#c00', color: '#FFFFFF'} }
    value={this.state.selectedItem}
    >
      <MenuItem primaryText="Maps" value='1'/>
      <MenuItem primaryText="Books" value='2' />
  </Menu>


来源:https://stackoverflow.com/questions/41623914/how-to-set-focus-on-a-menuitem-in-material-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!