Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
How to stop showing the checkbox in the input of Material UI TextField with select attribute when we have MenuItems with Checkboxes

I have added checkboxes to the MenuItems in the TextField of type Select. When I select one of the MenuItems, the checkbox is also displaying in the input of the TextField.

Req: I need to stop displaying the check box in the input of TextField. I need to strictly use the TextField with Selcet attribute.

Can someone please help me with this? Thnak you.

enter image description here

sandbox link: https://codesandbox.io/s/basicselect-material-demo-forked-06gns?file=/demo.js

code:

import * as React from "react";
import MenuItem from "@mui/material/MenuItem";
import FormControl from "@mui/material/FormControl";
import ListItemText from "@mui/material/ListItemText";
import Checkbox from "@mui/material/Checkbox";
import TextField from "@mui/material/TextField";

const ITEM_HEIGHT = 48;
const ITEM_PADDING_TOP = 8;
const MenuProps = {
  PaperProps: {
    style: {
      maxHeight: ITEM_HEIGHT * 4.5 + ITEM_PADDING_TOP,
      width: 250
     }
  }
};

const names = ["Oliver Hansen", "Van Henry"];

export default function MultipleSelectCheckmarks() {
  const [personName, setPersonName] = React.useState([]);

   const handleChange = (event) => {
   const {
      target: { value }
    } = event;
    setPersonName(
     // On autofill we get a the stringified value.
      typeof value === "string" ? value.split(",") : value
    );
  };

  return (
    <div>
      <FormControl sx={{ m: 1, width: 300 }}>
        <TextField
          // multiple
          select
          value={personName}
          onChange={handleChange}
          renderValue={(selected) => selected.join(", ")}
          MenuProps={MenuProps}
        >
          {names.map((name) => (
            <MenuItem key={name} value={name}>
              <Checkbox checked={personName.indexOf(name) > -1} />
              <ListItemText primary={name} />
            </MenuItem>
          ))}
        </TextField>
      </FormControl>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I did give it a try and changed some code, onchange and renderValue should be props on select, on not textfield so it was not working


import * as React from "react";
import MenuItem from "@mui/material/MenuItem";
import FormControl from "@mui/material/FormControl";
import FormControlLabel from "@mui/material/FormControlLabel";
import Checkbox from "@mui/material/Checkbox";
import TextField from "@mui/material/TextField";

const ITEM_HEIGHT = 48;
const ITEM_PADDING_TOP = 8;

const names = ["Oliver Hansen", "Van Henry"];

export default function MultipleSelectCheckmarks() {
  const [personName, setPersonName] = React.useState([]);

  const handleChange = (event) => {
    if (!personName.includes(event.target.value))
      setPersonName(event.target.value); // selected options
  };

  return (
    <div>
      <FormControl sx={{ m: 1, width: 300 }}>
        <TextField
          select
          SelectProps={{
            multiple: true,
            value: personName,
            onChange: (e) => handleChange(e),
            renderValue: (selected) => selected.join(", "),
            style: {
              width: "250px",
              maxHeight: `${ITEM_HEIGHT} * 4.5 + ${ITEM_PADDING_TOP}`
            }
          }}
        >
          {names.map((name) => (
            <MenuItem key={name} value={name}>
              <FormControlLabel
                control={<Checkbox checked={personName.includes(name)} />}
                label={name}
              />
            </MenuItem>
          ))}
        </TextField>
      </FormControl>
    </div>
  );
}

Please check working demo here: https://codesandbox.io/s/multi-select-with-textfield-uf100

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda