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

162
Vistas
¿Cómo creo un componente reutilizable para esta versión de Autocompletar?

Estoy usando este Autocompletar un par de veces. Sin embargo, tengo estos estados de uso que deben definirse, por lo que estoy bastante confundido sobre cómo puedo hacer que esto sea un componente reutilizable.

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

0

Probablemente podría intentar extraer del componente lo que lo hace específico, de modo que siga siendo genérico pero aún tenga la capacidad de ver la personalización inyectada en él. Es posible que el código a continuación no sea del todo correcto, pero lo que importa es la idea general

 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 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