Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda