Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material ui multiple Select

this is my code : Code Sandbox

when I select an item from Select component; the value of

event.target.value

is undefined !!

What is proper implementation?

like image 844
mehdi parastar Avatar asked Nov 26 '19 09:11

mehdi parastar


3 Answers

You need to pass a value prop to MenuItem

Please refer below code

  <TextField
    classes={{ root: classes.root }}
    select
    name="userRoles"
    id="userRoles"
    variant="outlined"
    label="userRoles"
    SelectProps={{
      multiple: true,
      value: formState.userRoles,
      onChange: handleFieldChange
    }}
  >
    <MenuItem value="admin">Admin</MenuItem>
    <MenuItem value="user1">User1</MenuItem>
    <MenuItem value="user2">User2</MenuItem>
  </TextField>

Please refer to this sandbox for example

like image 60
Sumanth Madishetty Avatar answered Sep 28 '22 05:09

Sumanth Madishetty


As you can see in documentation of Multiple Select in MUI you have to iterate over each option and check if it is selected.

const handleChangeMultiple = event => {
    const { options } = event.target;
    const value = [];
    for (let i = 0, l = options.length; i < l; i += 1) {
      if (options[i].selected) {
        value.push(options[i].value);
      }
    }
    console.log(value); // selected options
};
like image 31
Jax-p Avatar answered Sep 28 '22 06:09

Jax-p


You need to pass value for each option.

<MenuItem value="Admin">Admin</MenuItem>
<MenuItem value="User1">User1</MenuItem>
<MenuItem value="User2">User2</MenuItem>
like image 41
Sunil tc Avatar answered Sep 28 '22 05:09

Sunil tc