react-otp-input icon indicating copy to clipboard operation
react-otp-input copied to clipboard

Backspace delete specific input field and stay on it

Open emran-jatri opened this issue 4 years ago • 3 comments

Backspace delete specific input field and stay on it. I see many issues and pull requests in this git, but after solving this issue git and npm are not updated? I need solved things right now, can someone please help me with how to do that? when I click on backspace it should work like the delete button on the keyboard. thanks you

emran-jatri avatar Sep 11 '21 06:09 emran-jatri

@emran-jatri Did you find a solution to this?

Joojo7 avatar May 20 '22 05:05 Joojo7

Found out a way to fix this

import React, { useEffect, useRef, useState } from "react";
import OtpInput from "react-otp-input";

const App = () => {
  const [otp, setOtp] = useState();
  const [prevOtp, setPrevOtp] = useState()
  const otpRef = useRef(null);

  const handleChange = (e) => {
    setPrevOtp(otp)
    setOtp(e)
  };

  useEffect(()=>{
    if(otp?.length < prevOtp?.length)
      otpRef.current.focusInput(otp.length)
  },[otp])

  return (
    <>
      <OtpInput
        ref={otpRef}
        value={otp}
        onChange={handleChange}
        numInputs={6}
        separator={<span>-</span>}
      />
    </>
  );
};

blactrojan avatar Jun 20 '22 12:06 blactrojan

The feature is available in: https://www.npmjs.com/package/react18-input-otp

ritikbanger avatar Aug 09 '22 17:08 ritikbanger