npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-one-time-password

v1.0.4

Published

A react OTP component for websites and webapps

Downloads

302

Readme

OTP Input Component

A customizable OTP (One-Time Password) input component for React.

  • Keyboard support
  • Custom separators support
  • TypeScript compatible
  • Resend and Timer Logic support
  • Tailwind classes support

Installation and Usage

To install:

npm i react-one-time-password

Take a look at the basic usage:

import React, { useState } from "react";
import { OTPInput } from "react-one-time-password";

const App: React.FC = () => {
  const handleOtpChange = (otp: string) => {
    console.log("OTP:", otp);
  };

  return (
    <div>
      <h1>Enter OTP</h1>
      <OTPInput
        numberOfInputs={6}
        onChange={handleOtpChange}
        // Custom styles for larger inputs
        inputStyle={{ width: "2em", height: "3em", fontSize: "1.5em" }}
      />
    </div>
  );
};

export default App;

Asides from the direct style props, you can also pass the classNames prop in case you need custom class names for styling or Tailwind classes.

The classNames prop supports the following keys:

  • container: For the main container of the OTP input fields.

  • input: For individual OTP input fields.

  • resend-button-container: For the container of the resend button.

  • resend-button: For the resend button itself.

  • input-separators: For the separators between OTP input

<OTPInput
  numberOfInputs={4}
  onChange={(otp) => console.log(otp)}
  // Tailwind classes
  classNames={{
    container: "flex items-center",
    input: "w-12 h-12 text-center border rounded-md",
    "resend-button-container": "mt-4",
    "resend-button": "px-4 py-2 bg-blue-500 text-white rounded",
    "input-separators": "mx-2",
  }}
/>

If you wish to have a Resend OTP and Timer logic, you can enable the showResendButton prop and optionally pass any custom markup via the renderResendContainer and renderResendButton as shown below:

import React, { useState } from "react";
import { OTPInput } from "react-one-time-password";

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

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

  const handleResend = () => {
    console.log("Resend OTP triggered");
    // Add your resend logic here, e.g., making an API call to resend the OTP
  };

  const customRenderResendContainer = (children: React.ReactNode) => (
    <div style={{ marginTop: "1rem", textAlign: "center" }}>{children}</div>
  );

  const customRenderResendButton = (
    onClick: () => void,
    disabled: boolean,
    timer: number
  ) => (
    <button
      onClick={onClick}
      disabled={disabled}
      style={{
        padding: "0.5rem 1rem",
        backgroundColor: disabled ? "#ccc" : "#007bff",
        color: "#fff",
        border: "none",
        borderRadius: "4px",
        cursor: disabled ? "not-allowed" : "pointer",
      }}
    >
      {disabled ? `Resend OTP in ${timer} seconds` : "Resend OTP"}
    </button>
  );

  return (
    <div>
      <h2>OTP Verification</h2>
      <OTPInput
        numberOfInputs={6}
        onChange={handleOtpChange}
        inputWidth="1em"
        inputHeight="3em"
        borderColor="#007bff"
        borderRadius="4px"
        resendTimeout={60}
        onResend={handleResend}
        showResendButton
        renderResendContainer={customRenderResendContainer}
        renderResendButton={customRenderResendButton}
      />
      <p>Entered OTP: {otp}</p>
    </div>
  );
}

export default App;

For more options, feel free to check out the props below:

| Prop | Type | Default | Description | | ------------------------ | ------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | ------------------------------------------------------------ | | numberOfInputs | number | N/A | Number of OTP input fields. | | onChange | (otp: string) => void | N/A | Callback function to handle OTP change. | | inputWidth | string | "1em" | Width of each OTP input field. | | inputHeight | "auto" \| "fit-content" \| string | "3em" | Height of each OTP input field. | | disableAutoFocus | boolean | false | Disable auto-focus on the first input field. | | borderColor | string | N/A | Border color of the input fields when focused. | | borderRadius | string | N/A | Border radius of the input fields. | | showSeparators | boolean | true | Show separators between input fields. | | renderCustomSeparators | () => React.ReactNode \| React.ReactNode | () => <span style={{ margin: "0 0.5rem" }}>-</span> | Custom separator component or function to render separators. | | inputStyle | CSSProperties | N/A | Custom styles for the input fields. | | containerStyle | CSSProperties | N/A | Custom styles for the container of the input fields. | | inputType | "password" \| "text" \| "tel" | "tel" | Type of the input fields. | | inputMode | "none" \| "numeric" \| "tel" | "numeric" | Input mode of the input fields. | | resendTimeout | number | 60 | Timeout in seconds before the resend button is enabled. | | onResend | () => void | N/A | Callback function to handle resend action. | | resendContainerStyle | CSSProperties | N/A | Custom styles for the resend container. | | resendButtonStyle | CSSProperties | N/A | Custom styles for the resend button. | | renderResendContainer | (children: React.ReactNode) => React.ReactNode | N/A | Custom function to render the resend container. | | renderResendButton | (onClick: () => void, disabled: boolean, timer: number) => React.ReactNode | N/A | Custom function to render the resend button. | | showResendButton | boolean | false | Show the resend button. | | shouldDisableInput | boolean | false | Disable input fields when OTP is complete. | | classNames | { container?: string; input?: string; "resend-button-container"?: string; "resend-button"?: string; "input-seperators"} | N/A | Optional CSS class names for customizing component styles. |

Special Thanks to these Contributors