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

151
Visualizações
How to set value and option in material UI AutoComplete and onChange assign the value to a object?

I have a brand object with the field cat_id, on page load categories load into autocomplete select. Now I want to set category id as a value with a category option in autocomplete and also set cat_id from the autocomplete option value onChange.

const handleChange = (e) => {
  const { name, value } = e.target;
  setBrand({
    ...brand,
    [name]: value,
  });
};

<Autocomplete
  getOptionSelected={(option, value) => option.category_name === value.id}
  getOptionLabel={(option) => option.category_name}
  name="cat_id"
  onInputChange={handleChange}
  options={categories}
  loading={loading}
  renderInput={(params) => (
    <TextField
      {...params}
      label="Category"
      fullWidth
      variant="outlined"
      required
      InputProps={{
        ...params.InputProps,
        endAdornment: (
          <Fragment>
            {" "}
            {loading ? <CircularProgress color="inherit" /> : null}{" "}
            {params.InputProps.endAdornment}
          </Fragment>
        ),
      }}
    />
  )}
/>;

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

After waiting a while for the help. I manage to solve this but I'm not sure is this a recommended way. I directly set the field value onChange and didn't use handleChange function.

<Autocomplete
  getOptionSelected={(option, value) => option.category_name === value.id}
  getOptionLabel={(option) => option.category_name}
  name="cat_id"
  options={categories}
  loading={loading}
  onChange={(e,option) => setBrand({...brand,cat_id: option.id,})}
  renderInput={(params) => (
    <TextField
      {...params}
      label="Category"
      fullWidth
      variant="outlined"
      required
      InputProps={{
        ...params.InputProps,
        endAdornment: (
          <Fragment>
            {" "}
            {loading ? <CircularProgress color="inherit" /> : null}{" "}
            {params.InputProps.endAdornment}
          </Fragment>
        ),
      }}
    />
  )}
/>;
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