1.3.1 • Published 10 months ago

react-otp-kit v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

react-otp-kit

A lightweight and versatile component designed to simplify the implementation of OTP (One-Time Password) input fields in React applications for desktop and mobile.

npm license issues

react-otp-kit demo

Live Demo

Installation

To install the latest stable version:

npm install --save react-otp-kit

Basic Usage

import { useState } from "react";
import { OtpKit } from "react-otp-kit";
import "react-otp-kit/dist/index.css";

function App() {
  const [otp, setOtp] = useState("");

  const handleChange = (newOtp: string) => {
    setOtp(newOtp);
  };

  return (
    <>
      <OtpKit
        value={otp}
        onChange={handleChange}
        type={"number"}
      />
    </>
  );
};

export default App;

API

Props

NameTypeRequiredDefaultDescriptionStatus
valuestringYes-The current value of the OTP input.Stable
onChange(value: string) => voidYes-Callback function triggered when the OTP value changes.Stable
numOfInputsnumberNo6Number of OTP input fields to display.Stable
placeholderstringNo-Placeholder text for the OTP input fields.Stable
autoSubmitbooleanNofalseAutomatically submit the OTP when all input fields are filled.Stable
autoFocusbooleanNotrueAutomatically focus on the first OTP input field on component mount.Stable
separatorObjectNo-Configuration for displaying separators between OTP input fields.Stable
separator.showbooleanNo-Show separators between input fields.Stable
separator.valuestringNo-The value to display as a separator.Stable
separator.intervalsnumberYes-The intervals at which separators should be displayed.Stable
separator.classNamestringNo-CSS class for styling the separator.Stable
submitOtpButtonObjectNoDefault Submit Button ConfigConfiguration for the submit OTP button.Stable
submitOtpButton.showbooleanNotrueShow the submit OTP button.Stable
submitOtpButton.textstringNoSubmitText to display on the submit OTP button.Stable
submitOtpButton.classNamestringNorok__submit_buttonCSS class for styling the submit OTP button.Stable
clearOtpButtonObjectNoDefault Clear Button ConfigConfiguration for the clear OTP button.Stable
clearOtpButton.showbooleanNofalseShow the clear OTP button.Stable
clearOtpButton.textstringNoClearText to display on the clear OTP button.Stable
clearOtpButton.classNamestringNorok__clear_buttonCSS class for styling the clear OTP button.Stable
type"number" \| "text" \| "password"Yes-The type of the OTP input fields (number, text, or password).Stable
inputStylesObjectNo-Custom styles for the OTP input fields.Stable
inputStyles.generalStylesstringNorok__input--defaultStylesGeneral CSS class for styling the OTP input fields.Stable
inputStyles.onFillstringNorok__defaultFillCSS class for styling the OTP input fields when they are filled.Stable

ResendCode Props

NameTypeRequiredDefaultDescriptionStatus
resendOtpButtonObjectYes-Configuration for the resend OTP button.Stable
resendOtpButton.localFunctions() => anyNo-Function to call for local OTP generation.Stable
resendOtpButton.apiURLstringNo-API URL for fetching a new OTP.Stable
resendOtpButton.initialCountdownnumberNo60Initial countdown time for the resend button in seconds.Stable
resendOtpButton.showbooleanNotrueShow the resend OTP button.Stable
resendOtpButton.textstringNoResend codeText to display on the resend OTP button.Stable
resendOtpButton.classNamestringNorok__resend_buttonCSS class for styling the resend OTP button.Stable
resendOtpButton.responseData(data: any) => voidNo-Callback function to handle the response data from the OTP generation function or API.Stable

ResendCode Button & Timer

import { useState } from "react";
import { OtpKitResendCode } from "react-otp-kit";

function App() {
   const [localOtp, setLocalOtp] = useState("");
  const [apiOtp, setApiOtp] = useState("");

  const handleLocalResponseData = (data: any) => {
    setLocalOtp(data);
  };

  const handleApiResponseData = (data: any) => {
    setApiOtp(data.otp);
  };

  // Example: locally generating random codes as OTP
  const generateRandomDigits = () => {
    let result = "";
    const characters = "0123456789";
    const length = 6;
    for (let i = 0; i < length; i++) {
      result += characters.charAt(
        Math.floor(Math.random() * characters.length)
      );
    }
    return result;
  };

  return (
    <>
      <OtpKitResendCode
        resendOtpButton={{
          localFunctions: generateRandomDigits,
          initialCountdown: 10,
          text: "Resend code (Local)",
          className: "resendbutton__styles",
          responseData: handleLocalResponseData, // Pass the function to handle response data
        }}
      />
      <p>Generated Local OTP: {localOtp}</p>
      <OtpKitResendCode
        resendOtpButton={{
          apiURL:
            "https://run.mocky.io/v3/5b8c2be7-ae6d-4ca4-9ece-fe5d96fdb120",
          initialCountdown: 10,
          text: "Resend code (API)",
          className: "resendbutton__styles",
          responseData: handleApiResponseData, // Pass the function to handle response data
        }}
      />
      <p>Generated API OTP: {apiOtp}</p>
    </>
  );
};

export default App

⚠️ Warning

To ensure proper functionality, avoid overriding the following props on the input component returned from the renderInput prop. Modifying these props can result in unexpected behavior:

  • ref
  • value
  • onChange
  • onFocus
  • onBlur
  • onKeyDown
  • onPaste
  • onInput
  • type
  • inputMode

Contributing Contributing Open Source

We welcome contributions from the community to improve the React OTP Kit. Whether you want to report bugs, suggest new features, or submit pull requests, your involvement is highly appreciated. Please refer to our contributing guidelines for more information on how to get started.

License License

This project is licensed under the MIT License. For more details, please refer to the LICENSE file.

Contributors Contributors

A big thank you to all the contributors who have helped make this project better. Your contributions are what make this project possible. If you’d like to see your name on this list, check out our contributing guidelines and get involved!

Contributors

Contributors

1.2.8

10 months ago

1.3.1

10 months ago

1.3.0

10 months ago

1.2.9

10 months ago

1.2.0

1 year ago

1.1.0

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

0.1.0

1 year ago