material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

DropDownMenu is disabled using dynamic MenuItems

Open AleffHenrique opened this issue 10 years ago • 8 comments

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>
         );
}

AleffHenrique avatar Jan 13 '16 10:01 AleffHenrique

+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"

AlexYegupov avatar Jan 13 '16 11:01 AlexYegupov

@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.

ahmadferdous avatar Jan 13 '16 20:01 ahmadferdous

@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 avatar Jan 13 '16 20:01 ahmadferdous

@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 avatar Jan 14 '16 00:01 AleffHenrique

@AleffHenrique I guess you can close this issue as it's already resolved.

ahmadferdous avatar Jan 14 '16 02:01 ahmadferdous

@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.

AleffHenrique avatar Jan 14 '16 08:01 AleffHenrique

@AlexYegupov Could you install this plugin and check if your example work? I did it and It is working right now!

AleffHenrique avatar Jan 15 '16 02:01 AleffHenrique

@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

ahmadferdous avatar Jan 15 '16 04:01 ahmadferdous