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

160
Visualizações
How to use focus on a material UI Text Field using react?

Hi guys I have used material UI and react to create a form. I would like to use the focus property on a particular ref. Please find below the code for reference :-

Using createRef for initialising the reference.Following is the code for the constructor:-

      constructor(props) {
super(props);
    this.state = {
  gender: '',
  empType: '',
  firstName: '',
  lastName: '',
  email: '',
  officePincode: '',
  dob: '',
  mobile: '',
  monthlySalary: '',
  residentialPincode: '',
  panNumber: ''
};

this.firstNameRef = React.createRef();
this.lastNameRef = React.createRef();
this.dobRef = React.createRef();
this.emailRef = React.createRef();
this.monthlySalaryRef = React.createRef();
this.officePincodeRef = React.createRef();
this.residentialPincodeRef = React.createRef();
this.panNumberRef = React.createRef();

}

I am using this array in the render method:-

    const focusArray = [
  this.firstNameRef,
  this.lastNameRef,
  this.dobRef,
  this.emailRef,
  this.monthlySalaryRef,
  this.officePincodeRef,
  this.residentialPincodeRef,
  this.panNumberRef
];

// eslint-disable-next-line no-plusplus
for (let index = 0; index < focusArray.length; index++) {
  const currentRef = focusArray[index];
  if (currentRef.current) {
    if (currentRef.current.value === '') {
      currentRef.current.focus();
      break;
    }
  }
}

Please find below code for the text field:-

            <TextField
          id="firstName"
          name="firstName"
          ref={this.firstNameRef}
          value={firstName}
          disabled={
            firstName && disableNameField.has(params.get('utm_source'))
          }
          label="First Name"
          error={formSubmitted && !validateName(firstName)}
          onChange={this.handleChange()}
          variant="outlined"
          margin="normal"
          helperText={
            formSubmitted && !validateName(firstName)
              ? '*Please enter first name'
              : ''
          }
          className={classes.input_field}
          InputLabelProps={{
            classes: {
              root: classes.cssLabel,
              focused: classes.cssFocused
            }
          }}
          InputProps={{
            classes: {
              root: classes.cssOutlinedInput,
              focused: classes.cssFocused,
              notchedOutline: firstName
                ? classes.noOutline
                : classes.notchedOutline
            }
          }}
        />

I am using a class based react component. Any help or suggestion is appreciated.

about 4 years ago · Juan Pablo Isaza
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