Coder Social home page Coder Social logo

euisoft / block-code Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 1.0 1.36 MB

A fully customizable, one-time password input component for the web built with React

Home Page: https://euisoft.github.io/block-code

License: MIT License

Shell 0.43% JavaScript 3.09% TypeScript 92.00% SCSS 4.48%
otp code input verification passcode 2fa react reactjs

block-code's Introduction

block-code

A fully customizable, one-time password input component for the web built with React

Build states

Highlights

  • Easy to use
  • Fully customizable
  • React hook first
  • Separate logic and ui

Installation and usage

yarn add block-code

Here is a quick example to get you started:

import { FC } from 'react'
import { BlockCode, IBlockCodeProps } from 'block-code'
import 'block-code/style.css'

export const YourComponent: FC<IBlockCodeProps> = props => (
  <BlockCode className="block-code-default" {...props} />
)

BlockCode provides 3 themes built-in: block-code-default, block-code-circular, block-code-underline

import { FC } from 'react'
import { BlockCode, IBlockCodeProps } from 'block-code'
import 'block-code/style.css'

export const YourComponent: FC<IBlockCodeProps> = ({
  children,
  ...restProps
}) => (
  <section>
    <BlockCode className="block-code-circular" {...restProps} />
    {children}
  </section>
)

or custom your style:

import { FC } from 'react'
import { BlockCode, IBlockCodeProps } from 'block-code'
import 'custom.css'

export const YourComponent: FC<IBlockCodeProps> = ({
  children,
  ...restProps
}) => (
  <section>
    <BlockCode className="custom" {...restProps} />
    {children}
  </section>
)

How to catch your data?

import { FC } from 'react'
import { BlockCode, IBlockCodeProps } from 'block-code'
import 'block-code/style.css'

export const YourComponent: FC<IBlockCodeProps> = props => {
  /**
   * @param data - { value, raw, onReset }
   *
   * value: The value after joining: `123456`
   *
   * raw: The raw value: ['1', '2', '3', '4', '5', '6']
   *
   * onReset: A function to reset the state, set value to empty, and set focus index to 0.
   */
  const onComplete = data => {
    // TODO:
    // Use your data here
  }

  return (
    <BlockCode
      className="block-code-default"
      onComplete={onComplete}
      {...props}
    />
  )
}

How to allow only number, or validate your data before adding into the state?

import { FC } from 'react'
import { BlockCode, IBlockCodeProps, isNumber } from 'block-code'
import 'block-code/style.css'

export const YourComponent: FC<IBlockCodeProps> = props => {
  const onValidateBeforeChange = async data => {
    /**
     * If `isNumeric` returns true, then the value will be added into the state
     *
     * You can use any condition here, `onValidateBeforeChange` asks for `Promise<boolean>`
     */
    return isNumeric(data.value)
  }

  return (
    <BlockCode
      className="block-code-default"
      onValidateBeforeChange={onValidateBeforeChange}
      {...props}
    />
  )
}

How to disable inputs?

BlockCode allows you to pass down input props

import { FC } from 'react'
import { BlockCode, IBlockCodeProps, isNumber } from 'block-code'
import 'block-code/style.css'

export const YourComponent: FC<IBlockCodeProps> = props => {
  return (
    <BlockCode
      className="block-code-default"
      inputProps={{
        disabled: true
      }}
      {...props}
    />
  )
}

How to custom my component?

BlockCode separates logic into hook useBlockCode. You can use it to custom your component

import { VFC } from 'react'
import { IBlockCodeProps, useBlockCode } from 'block-code'

export const YourComponent: VFC<IBlockCodeProps> = props => {
  const { className, ...restProps } = props
  const { ids, onCreateInputProps } = useBlockCode(restProps)

  return (
    <div className={className}>
      {ids.map((id, index) => (
        <input key={id} {...onCreateInputProps(index)} />
      ))}
    </div>
  )
}

The useBlockCode hook accept props same as BlockCode component which is IBlockCodeProps. It returns:

values,
setValues,
setActiveIndex,
refs,
onSetValueAtIndex,
onSetActiveIndex,
onResetValues,
onReset,
onPaste,
onNextFocusAfterChangingValue,
onKeyDown,
onInput,
onEnoughData,
onCreateRef,
onCreateInputProps,
onCheckValue,
onAfterAddingValue,
onAddValue,
initState,
initActiveIndex,
ids,
activeIndex

API

  • maxInputs - Number of inputs to be rendered.
    • Default: 6
  • inputProps - Input props will be passed into <input />
    • Default:
      {
        "maxLength": 1,
        "className": "input"
      }
  • autoFocus - Auto focus on the first input
    • Default: true
  • onValuesChange - A function will be called when the value got changed
    • Default: none
  • onInputChange - A function will be called when onInput and onKeyDown got triggered
    • Default: none
  • onValidateBeforeChange - A function will be called to validate the value before adding into state. If you don't provide validate function, all values will be allowed
    • Default: none
  • onComplete - A function will be called only when the data is enough. It will pass the data to you. The data is enough when fits the condition below:
    • maxInputs * inputProps.maxLength. For example maxnInputs is 6 and inputProps.maxLength is 1. The onComplete will be triggered if the value has length is 6
    • Default: none

License

This project is licensed under the terms of the MIT license.

block-code's People

Contributors

andyngdz avatar semantic-release-bot avatar

Stargazers

Aadhitya A avatar Johannes Schobel avatar Nick Welsh avatar Harsh Pathak avatar Ryan Soderberg avatar John Grant avatar Rodney Leviton avatar  avatar Subhan Bakh avatar Worakarn  (Peter) Ounhattapradit avatar Manassarn "Noom" Manoonchai avatar Emin Gasimov avatar

Watchers

 avatar

Forkers

the-hated-one

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.