简体   繁体   English

React Material-UI 下拉菜单不起作用

[英]React Material-UI dropdown menu not working

I am creating a simple icon drop-down menu using Material UI.我正在使用 Material UI 创建一个简单的图标下拉菜单。 But after rendering the glyph appears and no MenuItems are shown after clicking on the it.但是在渲染字形后出现并且单击它后没有显示任何MenuItems Here is the code -这是代码 -

import { IconMenu, IconButton } from 'material-ui' ;
const MoreVertIcon = require('material-ui/lib/svg-icons/navigation/more-vert');
const MenuItem = require('material-ui/lib/menus/menu-item');

const PostCard = React.createClass({
    render: function() {
        let button = (
                <IconButton
                    touch={true}
                    tooltip='Click to see menu.'
                    tooltipPosition='bottom-left'>
                    <MoreVertIcon />
                </IconButton>
            );
        return (
            <IconMenu iconButtonElement={button}>
                <MenuItem primaryText="Refresh" />
                <MenuItem primaryText="Send feedback" />
                <MenuItem primaryText="Settings" />
                <MenuItem primaryText="Help" />
                <MenuItem primaryText="Sign out" />
            </IconMenu>     
        );
    }
});

I had a similar issue.我有一个类似的问题。 Solution was to to add this somewhere in the app.解决方案是在应用程序的某个地方添加它。 I'm not sure if it matters where but I added it at a higher-level as possible:我不确定这是否重要,但我尽可能在更高级别添加它:

var injectTapEventPlugin = require("react-tap-event-plugin");
injectTapEventPlugin();

Just wanted to add the reason to add injectTapEventPlugin.只是想补充一下添加injectTapEventPlugin的原因。

According to 300ms tap delay, gone away By Jake Archibald根据300ms tap 延迟,消失 作者:Jake Archibald

Browsers stopped waiting 300ms for double taps and react's onClick doesnt give us the proper handle.浏览器不再为双击等待 300 毫秒,而 react 的 onClick 没有给我们正确的处理。

and According to react-tap-event-plugin git page并根据react-tap-event-plugin git page

Facebook is not planning on supporting tap events (#436) because browsers are fixing/removing the click delay. Facebook 不打算支持点击事件 (#436),因为浏览器正在修复/删除点击延迟。 Unfortunately it will take a lot of time before all mobile browsers (including iOS' UIWebView) will and can be updated.不幸的是,所有移动浏览器(包括 iOS 的 UIWebView)都需要很长时间才能更新。

Thats why we need to inject the plugin.这就是为什么我们需要注入插件。 About the proper solution, I decided to add the package and inject it in the App's constructor (The higer level I have).关于正确的解决方案,我决定添加包并将其注入应用程序的构造函数(我拥有的更高级别)。

The import:进口:

import injectTapEventPlugin from 'react-tap-event-plugin';

And inside the constructor:在构造函数内部:

injectTapEventPlugin();

In my case I have to add injectTapEventPlugin as well as change handler.就我而言,我必须添加 injectTapEventPlugin 以及更改处理程序。

var injectTapEventPlugin = require("react-tap-event-plugin");
const DropDownMenu = require('material-ui/lib/drop-down-menu');

...

injectTapEventPlugin(); // in constructor 

...

  handleChange(event, selectedIndex, menuItem) {
    this.setState({menu: event.target.value });
  }

...
  // in render
  let menuItems = [
       { payload: '0', text: 'Mon - Sun' },
       { payload: '1', text: 'Mon - Sat' },
       { payload: '2', text: 'Mon - Fri' },
       { payload: '3', text: 'Mon - Fri / Mon - Thu' },
    ];
...
  // in render return

  <DropDownMenu menuItems={menuItems} selectedIndex={this.state.menu} onChange={this.handleChange} /> 

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

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