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?
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
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
};
You need to pass value for each option.
<MenuItem value="Admin">Admin</MenuItem>
<MenuItem value="User1">User1</MenuItem>
<MenuItem value="User2">User2</MenuItem>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With