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

112
Visualizações
Input from a textfield autofills the other textfields based on data

I have this example data:

export const data = [
  {
    state: "state",
    firstName: "Alex",
    lastName: "Dhan",
    country: "US"
  },
  {
    state: "state2",
    firstName: "Pin",
    lastName: "Io",
    country: "Japan"
  },
  {
    state: "state3",
    firstName: "Third",
    lastName: "last name3",
    country: "South Korea"
  },
  {
    state: "state4",
    firstName: "Alex",
    lastName: "Kin",
    country: "South Korea"
  },
  {
    state: "state5",
    firstName: "Alex",
    lastName: "Kin",
    country: "India"
  }
];

How can I match the first and last name that was entered by the user, and then have it autofill the other textfields as well? There might also be instances where first names are the same or the first and last name are the same but the state and country is different. How can I do this?

Recreated codesandbox: https://codesandbox.io/s/textfield-with-search-f2fjx1?file=/data.js:0-513

 <form onSubmit={handleSubmit}>
      <TextField
        variant="outlined"
        label="First Name"
        fullWidth
        value={firstName}
        onChange={(e) => setFirstName(e.target.value)}
        required
      />
      <TextField
        variant="outlined"
        label="Last Name"
        fullWidth
        value={lastName}
        onChange={(e) => setLastName(e.target.value)}
        required
      />
      <TextField
        variant="outlined"
        label="State"
        fullWidth
        value={state}
        onChange={(e) => setState(e.target.value)}
        required
      />
      <br /> <br />
      <button type="submit">Submit</button>
      <br />
    </form>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

use useEffect. there do all the necessary checks. or all the same checks in submit

useEffect(() => {
  const res = Object.values(data).filter(item => {
    /* check for matches here */
    return item.firstName === firstName && item.lastName === lastName
  });
  if(res.length === 1 && !state) {
    /* fill in the required fields here */
    setState(res[0].state)
  }
  /** here you specify the necessary dependencies (which will be in the filter) */
}, [firstName, lastName]);
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