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

@svipwrap/react-confirmation-code-input

v0.2.5

Published

<!-- Improved compatibility of back to top link: See: https://github.com/othneildrew/Best-README-Template/pull/73 -->

Downloads

29

Readme

Contributors Forks Stargazers Issues MIT License

About The Project

This library provides a hook that lets you write a confirmation code input component while the library manages the refs and some interactions like focusing the next input element when an input has been entered. It can thus be considered headless.

The library also provides you with a component implemented on top of that hook. It lets you override the input and container elements' styling by passing respective class names of your choice as properties to the component.

Getting Started

Prerequisites

This is a react library primarily providing a headles react hook for you to implement your own confirmation code input whilst delegating the handling of the element refs and some of the behaviour to said hook. You therefore need a react version as a peer dependency that has support for hooks i.e. any version of react >= 16.8.

Installation

NPM

npm install react-confirmation-code-input

Yarn

yarn add react-confirmation-code-input

PNPM

pnpm add react-confirmation-code-input

Usage

Usage utilizing the hook

import * as React from "react";
import { useConfirmationCodeInput } from "react-confirmation-code-input";
import "./style.css";

export default function App() {
  const {
    refs,
    value: inputs,
    clear,
    setFocus,
    reset,
    inputProps,
  } = useConfirmationCodeInput({
    length: 5,
    allowedPattern: "numeric",
    initialValue: "01234",
    autoFocus: true,
  });

  // reset and setFocus take optional params so we can't pass them directly
  // to onClick but have to eliminate the optional parameter first
  const onReset = React.useCallback(() => reset(), []);
  const onSetFocus = React.useCallback(() => setFocus(), []);

  return (
    <main>
      <div className="input-container">
        {refs.map((ref, index) => (
          <input key={index} value={inputs[index]} ref={ref} {...inputProps} />
        ))}
      </div>
      <div className="button-box">
        <button onClick={onReset}>Reset</button>
        <button onClick={clear}>Clear</button>
        <button onClick={onSetFocus}>Focus</button>
      </div>
    </main>
  );
}

Open in StackBlitz

Using the provided Component

import * as React from "react";
import {
  ConfirmationCodeInput,
  ConfirmationCodeInputHandles as Handles,
} from "react-confirmation-code-input";
import "./style.css";

export default function App() {
  const handlesRef = React.useRef<Handles>(null);
  const { reset, clear, setFocus } = React.useMemo(
    () => ({
      reset: () => handlesRef.current.reset(),
      clear: () => handlesRef.current.clear(),
      setFocus: () => handlesRef.current.setFocus(),
    }),
    []
  );

  return (
    <main>
      <ConfirmationCodeInput
        ref={handlesRef}
        length={5}
        allowedPattern="numeric"
        autoFocus
        initialValue="01234"
      />
      <div className="button-box">
        <button onClick={reset}>Reset</button>
        <button onClick={clear}>Clear</button>
        <button onClick={setFocus}>Focus</button>
      </div>
    </main>
  );
}

Open in StackBlitz

API

hook version

The hook takes an object as parameter and returns an object as a result:

useConfirmationCodeInput(options: UseConfirmationCodeInputProps) => UseConfirmationCodeInputResult

UseConfirmationCodeInputProps:

allowedPattern?: AllowedPattern;
autoFocus?: boolean;
useValueHook?: UseValue;
initialValue?: string;
length: number;
onChange?: (value: string) => void;

| Parameter | Description | Type | Required | Default | | -------------- | ------------------------------------------------ | ----------------------- | -------- | --------- | | allowedPattern | Accepted pattern | AllowedPattern | no | "numeric" | | useValueHook | The hook to store and modify the input value | UseValue | no | | | autoFocus | wheter to put focus on first input upon mounting | boolean | no | true | | initialValue | Initial value as string | string | no | | | length | Number of individual inputs | number | yes | | | onChange | method called when the value changes | (value: string) => void | no | |

AllowedPattern:

Allowed Pattern is either one of the predefined patterns "numeric" | "alpha" | "alphanumeric", a RegExp or a function with the following signature:

(input: string, index: number, value: string[]) => boolean;

UseValue

Only provide this if you want to intercept/modify the input or the way it is stored. The signature is as follows:

(options: UseValueOptions) => [string[], Mutators];

UseValueOptions and Mutators are described below. You need to provide the same signature if you want to pass in a custom useValue parameter.

UseValueOptions:

An object of the shape { allowedPattern: AllowedPattern, length: number }:

| Property | Description | | -------------- | ---------------------------- | | allowedPattern | The pattern for valid inputs | | length | The length of a valid code |

Mutators:

An object of the shape:

{
    clear: () => void;
    reset: (value?: string) => void;
    update: (input: string, index: number, options: UpdateOptions) => void;
}

| Property | Description | Type | | -------- | --------------------------------------------------------- | ---------------------------------------------------------------- | | clear | A method to clear the value | () => void | | reset | A method to reset the value to either the given value | (value?: string) => void | | update | A method to update the value upon a given input character | (input: string, index: number, options: UpdateOptions) => void |

UseConfirmationCodeInputResult:

The result of calling useConfirmationCodeInput is an option adhering to the following interface:

    refs: Array<RefObject<HTMLInputElement>>;
    value: string[];
    reset: (value?: string) => void;
    clear: () => void;
    setFocus: (index?: number) => void;
    inputProps: {
        onFocus: FocusEventHandler<HTMLInputElement>;
        onKeyDown: KeyboardEventHandler<HTMLInputElement>;
        onPaste: ClipboardEventHandler<HTMLInputElement>;
        onInput: FormEventHandler<HTMLInputElement>;
    };

| Property | Description | Type | | ---------- | --------------------------------------------------------------------------------- | ------------------------------------ | | refs | An array of refs each of which needs to be passed to the respective input element | Array<RefObject<HTMLInputElement>> | | value | The current value as a string array with each value comprising a single character | string[] | | reset | see Mutators | (value?: string) => void | | clear | see Mutators | () => void | | setFocus | see Mutators | (index?: number) => void | | inputProps | The object with properties that must be passed to each individual input | InputProps |

InputProps

The inputProps returned by the useConfirmationCodeInputResult hook are supposed to be spread into each of the input elements. They comprise needed event handlers on which the hook relies in order to react to user events.

| Property | Description | Type | | --------- | ----------------------- | ----------------------------------------- | | onFocus | onFocus event handler | FocusEventHandler<HTMLInputElement> | | onKeyDown | onKeyDown event handler | KeyboardEventHandler<HTMLInputElement> | | onPaste | onPaste event handler | ClipboardEventHandler<HTMLInputElement> | | onInput | onInput event handler | FormEventHandler<HTMLInputElement> |

Component

The component takes the following parameters:

| Property | Description | Type | Required | Default | | -------------- | ------------------------------------------------------------- | ---------------------------------------------------- | -------- | --------- | | ref | reference to get access to the exposed Mutators | React.MutableRefObject | no | | | length | See corresponding hook param | number | yes | | | allowedPattern | See corresponding hook param | AllowedPattern | no | numeric | | initialValue | See corresponding hook param | string | no | | | onChange | See corresponding hook param | (value: string) => void | no | | | autoFocus | See corresponding hook param | boolean | no | | | containerCls | CSS class to be appended to the input container's class names | string | no | | | inputCls | CSS class to be appended to the input's class names | string | no | | | disabled | Whether the inputs are disabled | boolean | no | | | isPassword | Whether the inputs are of type password | boolean | no | | | inputMode | change the appearance of the keyboard | InputMode | no | numeric |

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Project Link: https://github.com/SSzzPP/svipwrap-react-confirmation-code-input