DropDownMenu is disabled using dynamic MenuItems
I followed the DropDownMenu's docs to code a custom dropdown component but it does't display all MenuItems options:
import React from 'react';
import { DropDownMenu, MenuItem } from "material-ui";
let provinces = ["Pernambuco", "São Paulo", "Rio de Janeiro", "Rio Grande do Norte"];
export default class ProvinceDropDown extends React.Component {
constructor() {
super(props);
this.state = {
province: 0
};
}
_onProvinceSelected(e, index, value) {
e.preventDefault();
this.setState({province: index});
}
_createProvinceMenuItems() {
let menuItems = [];
for (let province of provinces) {
let itemIndex = provinces.indexOf(province);
let item = (
<MenuItem
value={itemIndex}
key={`key-${province}`}
primaryText={province} />
);
menuItems.push(item);
}
return menuItems;
}
render() {
return (
<DropDownMenu
value={this.state.province}
onChange={this._onProvinceSelected.bind(this)}
style={{width: "40%", fontSize: "20px"}} >
{ this._createProvinceMenuItems() }
</DropDownMenu>
);
}
+1 much simpler first example from http://www.material-ui.com/#/components/dropdown-menu doesn't work too - it's dropdown is disabled. "version": "0.14.2"
@AleffHenrique
In _createProvinceMenuItems function, what happens when you remove value={itemIndex} from MenuItem?
<MenuItem
value={itemIndex}
key={`key-${province}`}
primaryText={province} />
I encountered a similar issue with my own code. When I added value to MenuItem, the drop-down menu didn't show. Without value, it works.
@AleffHenrique
While fixing my own issue, I think I figured out the problem in your code as well. In _createProvinceMenuItems function, you refer to this.state.provinces. But you did not include it in component's state in the constructor. This solved the problem:
constructor(props) {
super(props);
this.state = {
provinces: provinces,
province: 0,
};
}
@ahmadferdous Sorry I just forgot to edit it cause in my real application I am doing what you said. I updated the code here :)
@AleffHenrique I guess you can close this issue as it's already resolved.
@ahmadferdous
It is not resolved, what I said was when I copied and paste the code from my app I forgot to remove {this.state.provinces} and use the local variable provinces
But in both approaches the dropdown is still disabled.
@AlexYegupov Could you install this plugin and check if your example work? I did it and It is working right now!
@AleffHenrique
react-tap-event-plugin is currently a requirement for using material-ui. Details is available in the installation page: http://www.material-ui.com/#/get-started/installation