• Stars
    star
    358
  • Rank 118,855 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📟 React component for entering PIN codes.

📟 React PIN Field tests codecov npm

React component for entering PIN codes.

gif

Live demo at https://soywod.github.io/react-pin-field/.

Installation

yarn add react-pin-field
# or
npm install react-pin-field

Usage

import PinField from "react-pin-field"

Props

type PinFieldProps = {
  ref?: React.Ref<HTMLInputElement[]>
  className?: string
  length?: number
  validate?: string | string[] | RegExp | ((key: string) => boolean)
  format?: (char: string) => string
  onResolveKey?: (key: string, ref?: HTMLInputElement) => any
  onRejectKey?: (key: string, ref?: HTMLInputElement) => any
  onChange?: (code: string) => void
  onComplete?: (code: string) => void
  style?: React.CSSProperties
} & React.InputHTMLAttributes<HTMLInputElement>

const defaultProps = {
  ref: {current: []},
  className: "",
  length: 5,
  validate: /^[a-zA-Z0-9]$/,
  format: key => key,
  onResolveKey: () => {},
  onRejectKey: () => {},
  onChange: () => {},
  onComplete: () => {},
  style: {},
}

Reference

Every input can be controlled thanks to the React reference:

<PinField ref={ref} />

// reset all inputs
ref.current.forEach(input => (input.value = ""))

// focus the third input
ref.current[2].focus()

Style

The pin field can be styled either with style or className. This last one allows you to use pseudo-classes like :nth-of-type, :focus, :hover, :valid, :invalid

Length

Length of the code (number of characters).

Validate

Characters can be validated with a validator. A validator can take the form of:

  • a String of allowed characters: abcABC123
  • an Array of allowed characters: ["a", "b", "c", "1", "2", "3"]
  • a RegExp: /^[a-zA-Z0-9]$/
  • a predicate: (char: string) => boolean

Format

Characters can be formatted with a formatter (char: string) => string.

Events

  • onResolveKey: when a key passes the validator
  • onRejectKey: when a key is rejected by the validator
  • onChange: when the code changes
  • onComplete: when the code has been fully filled

Examples

See the live demo.

Development

git clone https://github.com/soywod/react-pin-field.git
cd react-pin-field
yarn install

To start the development server:

yarn start

To build the lib:

yarn build

To build the demo:

yarn build:demo

Tests

Unit tests

Unit tests are handled by Jest (.test files) and Enzyme (.spec files).

yarn test:unit

End-to-end tests

End-to-end tests are handled by Cypress (.e2e files).

yarn start
yarn test:e2e

Sponsoring

github paypal ko-fi buy-me-a-coffee liberapay

More Repositories

1

himalaya

CLI to manage emails
Rust
2,976
star
2

kronos.vim

A simple task and time manager. Project moved here:
Vim Script
200
star
3

unfog

⏱ Minimalist CLI task & time manager, written in Haskell.
Haskell
172
star
4

iris.vim

📫 Simple mail client for Vim.
Vim Script
156
star
5

keepeye.vim

A Vim plugin to keep your eyes safe.
Vim Script
97
star
6

unfog.vim

⏱ Vim plugin for Unfog CLI task & time manager.
Vim Script
69
star
7

phonetics.vim

A vim plugin for checking and listening to English phonetics.
Vim Script
17
star
8

kronos

A synchronized cross-platform task and time manager.
17
star
9

react-captain

⚓ A collection of strongly typed React hooks and contexts.
TypeScript
16
star
10

comodoro

CLI to manage your time using the Pomodoro technique.
Rust
15
star
11

pin-field

📟 Native web component for entering PIN codes.
TypeScript
8
star
12

dvorak-alt-intl

The dvorak alternative international no dead keys layout exported from linux to mac and windows
8
star
13

c-map

A simple key-val array written in C
C
7
star
14

bufmark.vim

A buffer bookmark manager.
Vim Script
5
star
15

autosess.vim

An automatic vim sessions and view manager.
Vim Script
5
star
16

himalaya.vim

📫 Vim plugin for Himalaya CLI email client.
Vim Script
4
star
17

typescript.vim

Ts(x) files syntax and indent support for Vim.
Vim Script
4
star
18

dotfiles

🏠 Personal configuration managed by NixOS
Nix
4
star
19

catalyx

⚗️ Collection of native Web Components.
TypeScript
3
star
20

factae

💰 A simple billing tool for micro-enterprises
JavaScript
3
star
21

attestation-deplacement-derogatoire

Application mobile de génération d'attestation de déplacement dérogatoire.
TypeScript
3
star
22

kronos.server

A realtime server for Kronos protocol.
TypeScript
3
star
23

kvothe

The composer assistant
JavaScript
2
star
24

c-server

A simple Unix Web server written in C
C
2
star
25

soywod

Personal website.
TypeScript
2
star
26

cardamom

📇 Simple CLI to synchronize and query contacts from CardDAV servers
Rust
2
star
27

croplet

Rust
1
star
28

file64

A base64 file converter in Golang.
Go
1
star
29

mml

CLI to deal with Emacs MIME message Meta Language (MML).
Rust
1
star
30

advent-of-code-2021

Rust
1
star
31

react-use-bireducer

React hook for managing effectful reducers.
TypeScript
1
star
32

himalaya-repl

REPL to manage emails
Nix
1
star