Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Input focus is not working with specific component react

I have component PhoneConformition which uses input :

 <Flexbox>
            {CODE_INPUTS.map((input, index) => (
              <Controller
                key={input.name}
                name={input.name}
                control={control}
                defaultValue=""
                // rules={{ required: true }}
                render={(props) => (
                  <InputCode
                    {...props}
                    ref={inputListRefs.current[index]}
                    className={styles.phoneConfirmation__formInput}
                    onValueSet={() => handleOnValueSet(index)}

                  />
                )} // props contains: onChange, onBlur and value
              />
            ))}
          </Flexbox>

Where InputCode is =



type Props = {
  className?: string;
  name: string;
  onChange: (...event: any[]) => void;
  onValueSet: (value: string) => void;
};

const InputCode = forwardRef<HTMLInputElement, Props>(
  ({ className, name, onChange, onValueSet }, ref) => {
    const classNames = [styles.inputCode, className].join(" ");

    const [valueState, setValueState] = useState<string>("");

    const handleChange = (event: React.SyntheticEvent<HTMLInputElement>) => {
      event.preventDefault();
    };

    const handleKeyPress = (event: React.KeyboardEvent<HTMLInputElement>) => {
      const value: number = parseInt(event.key, 10);

      if (!isNaN(value)) {
        const valueString: string = value.toString();

        setValueState(valueString);
        onChange(valueString);
        onValueSet(valueString);
      }
    };

    return (
      <div className={classNames}>
        <input
          ref={ref}
          name={name}
          className={styles.inputCode__input}
          type="text"
          onChange={handleChange}
          onKeyPress={handleKeyPress}
          value={valueState}
        />
      </div>
    );
  }
);

export default InputCode;

BUT if i want to use the function on value set function where it focus on another function according to index.Everything is wokring instead of .focus

  const handleOnValueSet = (index: number) => {
    const formvalues = getValues();

    onCodeChange(formvalues);

    if (index + 1 < CODE_INPUTS.length) {
      inputListRefs.current[index + 1].current?.focus();
    }
  }

.focus is not working with the onValueSet however with onChange or onKeyDown working correctly.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Found the solution. The issue was that the second onCodeChange call started before the first one finished.

I just added a setTimeOut of 10ms on the second call and it worked.

Thanks all

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!