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

multiple-otp-field

v1.1.9

Published

A dynamic, reusable OTP input component for React with SCSS support.

Downloads

487

Readme

Multiple OTP Field

A customizable and responsive OTP (One Time Password) input component for React. This package provides an easy-to-use OTP input field that supports automatic focus movement between inputs, customizable length, and submission handling.

Features

  • Customizable OTP length (default is 4)
  • Auto-focuses on the next input field when a digit is entered
  • Handles backspace to move focus back to the previous field
  • Supports on OTP submission callback when the full OTP is entered
  • Fully responsive and adaptable to various screen sizes

Installation

To install this package, run the following command in your project directory:

npm install multiple-otp-field

Or if you’re using Yarn:

yarn add multiple-otp-field

Usage

Basic Example

Here’s how to use the OTP input component in your React app.

  1. Import the OTP input component:
import OtpInput from "multiple-otp-field";
  1. Use the component in your app:
import React from "react";
import OtpInput from "multiple-otp-field";

const App: React.FC = () => {
  const handleOtpSubmit = (otp: string) => {
    console.log("Submitted OTP:", otp);
    // You can submit the OTP to your backend or perform any other logic here
  };

  return (
    <div>
      <h1>Enter OTP</h1>
      <OtpInput length={6} onOtpSubmit={handleOtpSubmit} />
    </div>
  );
};

export default App;

Props

The OtpInput component accepts the following props:

| Prop | Type | Default | Description | | -------------------- | ----------------------- | ---------------- | ---------------------------------------------------------------------------------------------------- | | length | number | 4 | The number of OTP digits (input fields). Default is 4. | | onOtpSubmit | (otp: string) => void | () => {} | A callback function that is triggered when the full OTP is entered. It receives the OTP as a string. | | inputClassName | string | "otpInput" | CSS class name for individual input fields. Default is "otpInput". | | containerClassName | string | "otpContainer" | CSS class name for the container div. Default is "otpContainer". |

Example with Custom OTP Length and Styles

import React from "react";
import OtpInput from "multiple-otp-field";
import "./App.scss"; // Include your custom SCSS file

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

  return (
    <div className="app-container">
      <h1>OTP Input with Custom Styles</h1>
      <OtpInput
        length={6}
        onOtpSubmit={handleOtpSubmit}
        inputClassName="custom-input"
        containerClassName="custom-container"
      />
    </div>
  );
};

export default App;

Styling and Responsiveness

The component is fully responsive, and you can easily customize its appearance using CSS. The input fields will automatically adjust their size based on the screen width.

To customize the appearance, you can style the component by targeting the .otpInput class or provide custom class names via the inputClassName and containerClassName props:

.otpInput {
  width: 3rem;
  height: 3rem;
  margin: 0.5rem;
  font-size: 1.5rem;
  text-align: center;
  border-radius: 0.25rem;
  border: 1px solid #ccc;
}

.otpInput:focus {
  outline: none;
  border-color: #4caf50;
}

.custom-container {
  display: flex;
  justify-content: center;
}

.custom-input {
  width: 4rem;
  height: 4rem;
  margin: 0.5rem;
  font-size: 2rem;
  text-align: center;
  border-radius: 0.5rem;
  border: 2px solid #007bff;
}

Contributing

We welcome contributions to this project! If you'd like to contribute, feel free to:

  1. Fork the repository
  2. Create a new branch
  3. Make your changes and improvements
  4. Create a pull request with a description of your changes

License

This project is licensed under the MIT License - see the LICENSE file for details.