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

171
Visualizações
How do I make a reusable component for this version of Autocomplete?

I am using this Autocomplete a few times. However I have this useStates that needs to be defined so I'm quite confused on how I can make this as a reusable component?

 <Autocomplete
        required
        value={firstName}
        onChange={(event, newValue) => {
          if (typeof newValue === "string") {
            setFirstName(newValue);
          } else if (newValue && newValue.inputValue) {
            // Create a new value from the user input
            setFirstName(newValue.inputValue);
          } else {
            setFirstName(newValue);
          }
        }}
        selectOnFocus
        clearOnBlur
        handleHomeEndKeys
        id="free-solo-with-text-demo"
        options={data}
        getOptionLabel={(option) => {
          // Value selected with enter, right from the input
          if (typeof option === "string") {
            return option;
          }
          // Add "xxx" option created dynamically
          if (option.inputValue) {
            return option.inputValue;
          }
          // Regular option
          return option.firstName;
        }}
        renderOption={(props, option) => <li {...props}>{option.firstName}</li>}
        sx={{ width: 300 }}
        freeSolo
        renderInput={(params) => <TextField {...params} label="First Name" />}
      />
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could probably try to extract from the cuomponent what is making it specific, so ti remains generic but still has the ability to see customisation injected into it ? The code below might not all be right but it's the general idea that matters

const [ firstName, setFirstName] = useState('')

const  handleChange = (event, newValue) => {
if (typeof newValue === "string") {
      setFirstName(newValue);
    } else if (newValue && newValue.inputValue) {
     // Create a new value from the user input
      setFirstName(newValue.inputValue);
    } else {
      setFirstName(newValue);     }
        }
}

const handleOptionLabel = (option) => {
// Value selected with enter, right from the input
          if (typeof option === "string") {
            return option;
          }
          // Add "xxx" option created dynamically
          if (option.inputValue) {
            return option.inputValue;
          }
          // Regular option
          return option.firstName;
        }
}

const handleRenderOption = (props, option) => {
<li {...props}>{option.firstName}</li>
}

const handleRenderInput = (params) =>{
<TextField {...params} label="First Name" />
}

 <Autocomplete
        required
        value={firstName}
        onChange={handleCHange}
        selectOnFocus
        clearOnBlur
        handleHomeEndKeys
        id="free-solo-with-text-demo"
        options={data}
        getOptionLabel={handleOptionLabel}
        renderOption={handleRenderOption}
        sx={{ width: 300 }}
        freeSolo
        renderInput={handleRenderInput}
      />
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