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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mohsensami/pin-code

v0.0.2

Published

A flexible and customizable React hook for handling multiple OTP (One-Time Password) inputs with advanced features.

Readme

React Input OTP

A flexible and customizable React hook for handling multiple OTP (One-Time Password) inputs with advanced features.

Features

  • 🔄 Multiple input fields support
  • ⚡ Automatic focus management
  • 🔢 Customizable input lengths
  • 💾 Default values support
  • ⌨️ Keyboard navigation (Backspace support)
  • 🎯 TypeScript support

Installation

npm install @mohsensami/pin-code
# or
yarn add @mohsensami/pin-code

Usage

Basic Example

import React from "react";
import { usePinCode } from "@mohsensami/pin-code";

const OTPForm = () => {
  const { getInputProps, otpValues } = usePinCode({
    inputs: [
      { name: "part1", length: 4 },
      { name: "part2", length: 4 },
      { name: "part3", length: 4 },
    ],
  });

  return (
    <div className="otp-container">
      <input {...getInputProps(0)} />
      <span>-</span>
      <input {...getInputProps(1)} />
      <span>-</span>
      <input {...getInputProps(2)} />
    </div>
  );
};

Advanced Example with Default Values

import React from "react";
import { usePinCode } from "@mohsensami/pin-code";

const AdvancedOTPForm = () => {
  const { getInputProps, otpValues, setOtpValue } = usePinCode({
    inputs: [
      { name: "firstPart", length: 8, defaultValue: "First" },
      { name: "secondPart", length: 10, defaultValue: "Second" },
      { name: "thirdPart", length: 10, defaultValue: "Third" },
    ],
  });

  return (
    <div className="advanced-otp">
      <div>
        <input
          type="text"
          className="form-control text-center"
          dir="ltr"
          {...getInputProps(0)}
        />
      </div>

      <div>
        <span className="separator">-</span>
      </div>

      <div>
        <input
          type="text"
          className="form-control text-center"
          dir="ltr"
          {...getInputProps(1)}
        />
      </div>

      <div>
        <span className="separator">+</span>
      </div>

      <div>
        <input
          type="text"
          className="form-control text-center"
          dir="ltr"
          {...getInputProps(2)}
        />
      </div>
    </div>
  );
};

API Reference

usePinCode Hook

const { getInputProps, otpValues, setOtpValue } = usePinCode({
  inputs: Array<{
    name: string;
    length: number;
    defaultValue?: string;
  }>;
});

Parameters

  • inputs: An array of input configurations
    • name: Unique identifier for the input
    • length: Maximum length of the input
    • defaultValue: (Optional) Initial value for the input

Returns

  • getInputProps: Function that returns input props for a specific index
  • otpValues: Object containing current values of all inputs
  • setOtpValue: Function to manually set value for a specific input

Input Props

The getInputProps function returns an object with the following properties:

  • name: Input name
  • value: Current input value
  • maxLength: Maximum allowed length
  • onChange: Change event handler
  • onKeyDown: Key down event handler
  • ref: Input reference

Styling

You can style the inputs and separators using CSS. Here's an example:

.otp-container {
  display: flex;
  align-items: center;
  gap: 10px;
}

.otp-container input {
  width: 50px;
  height: 50px;
  text-align: center;
  font-size: 20px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.otp-container .separator {
  font-size: 24px;
  color: #666;
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT