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

266
Visualizações
React: Disable all TextField wrapper components if statement is true

I have a form that has a few textfields in it. If you have a certain role then you should be able to click in the fields and type but if you do not have the admin role then they should all be disabled. How would I disable all the textfields given this situation? Here is my code. I am fairly new to react and any help would be appreciated.

I could do something like this but is there an easier way with less repetition as this form is most definitely to get bigger and more complex and I should be able to disable the entire thing if not given the correct role.

export default function BasicTextFields() {
  const classes = useStyles();
  const hasRole = authService.verifyRole(role.admin)

  return (
   {hasRole ? (
    <form className={classes.root} noValidate autoComplete="off">
      <TextField id="standard-basic" label="Title" />
      <TextField id="filled-basic" label="Description"  />
      <TextField id="outlined-basic" label="Data" variant="outlined" />
    </form>
  );
  ) : (
      <form className={classes.root} noValidate autoComplete="off">
      <TextField id="standard-basic" label="Title" disabled/>
      <TextField id="filled-basic" label="Description"  disabled/>
      <TextField id="outlined-basic" label="Data" variant="outlined" disabled/>
    </form>
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Rather than duplicating your elements and passing disabled as a single keyword (which is a shorthand for disabled={true}, you can explicitly pass a boolean value to it:

export default function BasicTextFields() {
  const classes = useStyles();
  const hasRole = authService.verifyRole(role.admin)

  return (
    <form className={classes.root} noValidate autoComplete="off">
      <TextField id="standard-basic" label="Title" disabled={!hasRole}/>
      <TextField id="filled-basic" label="Description" disabled={!hasRole}/>
      <TextField id="outlined-basic" label="Data" variant="outlined" disabled={!hasRole}/>
    </form>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Little simpler...

<form className={classes.root} noValidate autoComplete="off">
  <TextField id="standard-basic" label="Title" disabled={!hasRole}/>
  <TextField id="filled-basic" label="Description" disabled={!hasRole} />
  <TextField id="outlined-basic" label="Data" variant="outlined" disabled={!hasRole}/>
</form>
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