0.0.7 • Published 9 months ago

react-code-input-beautify v0.0.7

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

CodeInput Component

CodeInput is a flexible React component that provides a customizable input for entering multi-character codes (e.g., OTP, verification codes). You can control the number of input fields, allowed characters, and easily manage input through callbacks for change and submission.

Installation

To install the package, use npm or yarn:

npm install react-code-input-beautify
# or
yarn add react-code-input-beautify

Usage

import React, { useState } from "react";
import { CodeInput } from "react-code-input-beautify";
import "./index.css";

const App = () => {
  const [code, setCode] = useState("");

  const handleChange = (value) => {
    setCode(value);
  };

  const handleSubmitEndCode = (value) => {
    console.log("Submitted Code:", value);
  };

  return (
    <CodeInput
      fields={6}
      value={code}
      onChange={handleChange}
      onSubmitEndCode={handleSubmitEndCode}
      condition="alphanumeric"
      classNameInput="example-classname-for-inputs"
      classNameInputsWrapper="example-classname-for-inputs-wrapper"
      typeLetterCase="upperCase"
      placeholder="______"
    />
  );
};

export default App;

Props

PropTypeDefaultDescription
fieldsnumberRequiredNumber of input fields (length of the code).
valuestringRequiredThe current value of the input.
onChange(value: string) => voidRequiredCallback for when the input value changes.
onSubmitEndCode(value: string) => voidOptionalCallback triggered when the last character is entered, provided that all fields are also entered
classNameInputstring""Custom class name for each input field.
classNameInputsWrapperstring""Custom class name for the input wrapper div.
condition"numbers", "letters", "alphanumeric", "any", RegExp"any"Condition that defines what type of characters are allowed.
typeLetterCase"upperCase", "lowerCase", "any""any"Specifies whether the input should be in uppercase or lowercase.
placeholderstring""Placeholder to show in the inputs.
InputsPropsReact.InputHTMLAttributes<HTMLInputElement>{}Additional props to pass to each input element.
WrapperPropsReact.HTMLAttributes<HTMLDivElement>{}Additional props to pass to the wrapper element.