I'm trying to create a Select component with a series of custom items that are mapped through a list. Each item has a specific type and, based on that type, the menu item will have a certain MUI icon. I created a specific component for managing the whole Select component and another specific component to display each item, its value and its icon. The problem is that the onChange function is not triggered whenever I click on one of the items. What could the problem be?
I'm attaching a code of what I'm doing.
const myTypes = {
TYPE_1: "Type 1",
TYPE_2: "Type 2",
TYPE_3: "Type 3",
};
function TypeSelect(props) {
const classes = useStyles();
const [state, setState] = useState(myTypes.TYPE_1);
const onChangeType = (e) => {
setState(e.target.value);
};
return (
<Select
id="select-type"
value={state}
onChange={onChangeType}
>
{Object.keys(myTypes).map((type) => (
<TypeSelectMenuItem
type={myTypes[type]}
key={type}
/>
))}
</Select>
);
}
function TypeSelectMenuItem({ type }) {
const renderIcon = () => {
switch (type) {
case myTypes.TYPE_1:
return <ShortTextIcon />; // Material-UI icon
case myTypes.TYPE_2:
return <SubjectIcon />; // Material-UI icon
case myTypes.TYPE_3:
return <RadioButtonCheckedIcon />; // Material-UI icon
default:
return <Fragment />;
}
};
return (
<MenuItem value={type} >
<ListItemIcon>{renderIcon()}</ListItemIcon>
<ListItemText primary={type} />
</MenuItem>
);
}