admin管理员组

文章数量:1023009

I made OTP input in React You can see this image. One line is one input and I have 6 inputs. The inputs work that is not a problem. I need when the ponent opens, the first input must be autofocus. When I use <input autofocus/> the last input is autofocus, I need the first input.

My parent ponent

  const [code, setcode] = useState(new Array(6).fill(""));

OTP ponent

 <div className="digit-inputs">
        {props.code.map((data, index) => {
          return (
            <input
              disabled={props.second <= 0 ? true : false}
              type="text"
              className="otp-field"
              name="otp"
              maxLength={1}
              key={index}
              style={data ? { borderBottom: "3px solid #7dbf2a" } : { borderBottom: "3px solid grey" }}
              value={data}
              onChange={(e) => handleChange(e.target, index)}
              onFocus={(e) => e.target.select}
            />
          );
        })}
      </div>

My function

  const handleChange = (element, index) => {
    if (isNaN(element.value)) return false;

    props.setcode([...props.code.map((d, indx) => (indx === index ? element.value : d))]);

    //Focus next input

    if (element.nextSibling) {
      element.nextSibling.focus();
    }
  };

I made OTP input in React You can see this image. One line is one input and I have 6 inputs. The inputs work that is not a problem. I need when the ponent opens, the first input must be autofocus. When I use <input autofocus/> the last input is autofocus, I need the first input.

My parent ponent

  const [code, setcode] = useState(new Array(6).fill(""));

OTP ponent

 <div className="digit-inputs">
        {props.code.map((data, index) => {
          return (
            <input
              disabled={props.second <= 0 ? true : false}
              type="text"
              className="otp-field"
              name="otp"
              maxLength={1}
              key={index}
              style={data ? { borderBottom: "3px solid #7dbf2a" } : { borderBottom: "3px solid grey" }}
              value={data}
              onChange={(e) => handleChange(e.target, index)}
              onFocus={(e) => e.target.select}
            />
          );
        })}
      </div>

My function

  const handleChange = (element, index) => {
    if (isNaN(element.value)) return false;

    props.setcode([...props.code.map((d, indx) => (indx === index ? element.value : d))]);

    //Focus next input

    if (element.nextSibling) {
      element.nextSibling.focus();
    }
  };

Share Improve this question asked May 24, 2022 at 11:05 Rahil ƏliyevRahil Əliyev 3291 gold badge6 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

If you just wanted the focus when the ponent mounts, you can use the index of the array. just add autoFocus={index === 0}.

         <input
          type="text"
          className="otp-field"
          name="otp"
          maxLength={1}
          key={index}
          style={
            data
              ? { borderBottom: "3px solid #7dbf2a" }
              : { borderBottom: "3px solid grey" }
          }
          //value={data}
          onFocus={(e) => e.target.select}
          autoFocus={index === 0} // add this line
          //onChange
        />

I made OTP input in React You can see this image. One line is one input and I have 6 inputs. The inputs work that is not a problem. I need when the ponent opens, the first input must be autofocus. When I use <input autofocus/> the last input is autofocus, I need the first input.

My parent ponent

  const [code, setcode] = useState(new Array(6).fill(""));

OTP ponent

 <div className="digit-inputs">
        {props.code.map((data, index) => {
          return (
            <input
              disabled={props.second <= 0 ? true : false}
              type="text"
              className="otp-field"
              name="otp"
              maxLength={1}
              key={index}
              style={data ? { borderBottom: "3px solid #7dbf2a" } : { borderBottom: "3px solid grey" }}
              value={data}
              onChange={(e) => handleChange(e.target, index)}
              onFocus={(e) => e.target.select}
            />
          );
        })}
      </div>

My function

  const handleChange = (element, index) => {
    if (isNaN(element.value)) return false;

    props.setcode([...props.code.map((d, indx) => (indx === index ? element.value : d))]);

    //Focus next input

    if (element.nextSibling) {
      element.nextSibling.focus();
    }
  };

I made OTP input in React You can see this image. One line is one input and I have 6 inputs. The inputs work that is not a problem. I need when the ponent opens, the first input must be autofocus. When I use <input autofocus/> the last input is autofocus, I need the first input.

My parent ponent

  const [code, setcode] = useState(new Array(6).fill(""));

OTP ponent

 <div className="digit-inputs">
        {props.code.map((data, index) => {
          return (
            <input
              disabled={props.second <= 0 ? true : false}
              type="text"
              className="otp-field"
              name="otp"
              maxLength={1}
              key={index}
              style={data ? { borderBottom: "3px solid #7dbf2a" } : { borderBottom: "3px solid grey" }}
              value={data}
              onChange={(e) => handleChange(e.target, index)}
              onFocus={(e) => e.target.select}
            />
          );
        })}
      </div>

My function

  const handleChange = (element, index) => {
    if (isNaN(element.value)) return false;

    props.setcode([...props.code.map((d, indx) => (indx === index ? element.value : d))]);

    //Focus next input

    if (element.nextSibling) {
      element.nextSibling.focus();
    }
  };

Share Improve this question asked May 24, 2022 at 11:05 Rahil ƏliyevRahil Əliyev 3291 gold badge6 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

If you just wanted the focus when the ponent mounts, you can use the index of the array. just add autoFocus={index === 0}.

         <input
          type="text"
          className="otp-field"
          name="otp"
          maxLength={1}
          key={index}
          style={
            data
              ? { borderBottom: "3px solid #7dbf2a" }
              : { borderBottom: "3px solid grey" }
          }
          //value={data}
          onFocus={(e) => e.target.select}
          autoFocus={index === 0} // add this line
          //onChange
        />

本文标签: javascriptFirst input autofocus for OTP input fields in ReactStack Overflow