material-ui Select Box not showing selection

shawleigh17

I have a material-ui select box that is populated with a state variable. No matter what I have tried, I cannot get the value to actually show when I select an option. Can anyone tell me why? It keeps just giving me a blank bar. I even took an example from another code sandbox and copied it almost exactly. One thing I did notice is that my event.target.value is always undefined, and I am not sure why. So I just use value, instead, in my handleChange function. Any help is greatly appreciated! This has been driving me crazy.

Code Sandbox: https://codesandbox.io/s/jnyq16279v

Code:

import React from 'react';
import MenuItem from 'material-ui/MenuItem';
import Select from 'material-ui/SelectField';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';

export default class KKSelect extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            selectOptions: [
                {
                    value: "Image",
                    id: "1"
                },
                {
                    value: "Integer",
                    id: "2"
                },
                {
                    value: "Decimal",
                    id: "3"
                },
                {
                    value: "Boolean",
                    id: "4"
                },
                {
                    value: "Text",
                    id: "5"
                }
            ],
            selectedValue: ""
        };
    }

    renderSelectOptions = () => {
        return this.state.selectOptions.map((dt, i) => {
            return (
                <MenuItem key={i} value={dt.id}>
                    {dt.value}
                </MenuItem>
            );
        });
    }

    handleChange = (event, value) => {
        this.setState({ selectedValue: value });
    };

    render() {
        return (
            <MuiThemeProvider>

                <Select
                    value={this.state.selectedValue}
                    onChange={this.handleChange}
                >
                    {this.renderSelectOptions()}
                </Select>

            </MuiThemeProvider>
        );
    }
}
Vikky

First of all, you are using material-ui version 0.20.1 - docs for that version is here: https://v0.material-ui.com/#/components/select-field, but it's recommended to use v1 (https://material-ui.com/getting-started/installation/).

For version 0.20.1, there are few problems with your code:

First: renderSelectOptions: {dt.value} should be assigned to MenuItem primaryText

renderSelectOptions = () => {
        return this.state.selectOptions.map((dt, i) => {
            return (
                <MenuItem key={i} value={dt.id}>
                    {dt.value}
                </MenuItem>
            );
        });
    }

like this:

renderSelectOptions = () => {
    return this.state.selectOptions.map((dt, i) => (
      <MenuItem key={dt.id} value={dt.id} primaryText={dt.value} />
    ));
  };

And second - handle change has event, index and value arguments, so your value is acctually index - not value.

handleChange = (event, value) => {
    this.setState({ selectedValue: value });
};

Should be:

  handleChange = (event, index, value) => {
    this.setState({ selectedValue: value });
  };

Check out working version for material-ui version 0.20.1 here: https://codesandbox.io/s/9q3v1746jy

P.S. If you are using material-ui version 1.2.1, I made working example for that version too, you can check it out here: https://codesandbox.io/s/jjvrnokkv3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Material Select Box showing multiple selection

Material UI Selected Option Not Showing in Select Box

Selected option not showing in Material UI select field box

Material UI Select not showing options

Dialog Overlap in Date Selection Box, Material UI

Material UI Select Multiple Selection in Array

Redux-form Material-UI dropdown not showing dropdown, selection

Material select box not showing binded value with ngModel - Angular2

Angular/material ui - programatically select mat-selection-list

How to set default value in material-UI select box in react?

Remove blue outline from Select box React Material UI

React material-ui : Selection box over ListItemButton do not fit the cornerRadius

How to make Vue material select box showing option when the default value is empty?

<select> selection not showing on Windows computers

Material UI Select in Typescript

Material UI Select in Typescript

Material ui multiple Select

Material-UI - Tooltip on Select/MenuItem component stays displayed after selection

Excel ActiveX Combo Box not Showing Selection

laravel select box values showing

Datatables - showing/hiding with select box

Showing a default value for a select control using react and material-ui control

Angular material dialog box is not showing properly

populate the second select box based on the selection of first select box

Show hide select box and select box option based on selection

how to change UI depending on combo box selection

Enabling dropdowns according to selection in Material UI

Radio icon disappears on selection - using Material UI

Set Material UI Select width?