Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI: How to renderValue display label of select tag

I'm using multiple select component of MUI, when user select it display the value of select tag to the user, but I want it display the label of select tag.

This is codesandbox link

https://codesandbox.io/embed/elated-bose-phh8xx?fontsize=14&hidenavigation=1&theme=dark

like image 232
Đức Long Avatar asked Sep 02 '25 09:09

Đức Long


2 Answers

A trick I have found is that you can called find method in Chip Lable and get that object by id then use its label

Example Code:

 <Chip key={value} label={DUMMY_DATA.find(item => item.id === value).label} variant="light" color="primary" size="small" />

Read more about find Method

Complete code:

<FormControl fullWidth>
  <InputLabel id="demo-multiple-chip-label">Chuyên mục</InputLabel>
  <Select
    labelId="demo-multiple-chip-label"
    id="demo-multiple-chip"
    multiple
    value={personName}
    onChange={handleChange}
    input={<OutlinedInput id="select-multiple-chip" label="Chuyên mục" />}
    renderValue={(selected) => (
      <Box sx={{ display: "flex", flexWrap: "wrap", gap: 0.5 }}>
        {selected.map((value) => (
          <Chip
            key={value}
            label={DUMMY_DATA.find((item) => item.id === value).label}
            variant="light"
            color="primary"
            size="small"
          />
        ))}
      </Box>
    )}
    MenuProps={MenuProps}
  >
    {DUMMY_DATA.map((o) => (
      <MenuItem key={o.id} value={o.id}>
        {o.label}
      </MenuItem>
    ))}
  </Select>
</FormControl>;
like image 62
Alpha Avatar answered Sep 04 '25 22:09

Alpha


Write value={o.label} instead of value={o.id}

<MenuItem key={o.id} value={o.label}>
     {o.label}
 </MenuItem>

Hope it works fine.

like image 22
Arifur Rahaman Avatar answered Sep 04 '25 23:09

Arifur Rahaman