Exciting release!Introducing "safe npm". Learn more
Log inDemoInstall


Package Overview
File Explorer

Advanced tools


Pin input built with React component and styled-components


Version published
Weekly downloads
decreased by-30.29%

Weekly downloads



1.2.0 (2022-04-22)


  • Add custom className props (#41) (5997832)



React Input Pin Code

Validate CircleCI Build Status codecov npm NPM storybook

An accessible and simple pin input component built with styled-components for ReactJS.



Here to see documentation and examples.

Source code at https://github.com/luffy84217/react-input-pin-code.


To get started, install and save in your package.json dependencies, run:

npm install react-input-pin-code styled-components


yarn add react-input-pin-code styled-components

Please note that styled-components is peer dependency.


You can use the following two ways to import module.

import { PinInput } from 'react-input-pin-code' // ES Module or var PinInput = require('react-input-pin-code').PinInput // CommonJS Module


Basic Example

import React from 'react'; import { PinInput } from 'react-input-pin-code'; export default () => { const [values, setValues] = React.useState(['', '', '']); return ( <PinInput values={values} onChange={(value, index, values) => setValues(values)} /> ); };

Filling one character at a time for each input. When a character is entered, the focus transfers to the next input automatically in the sequence until all inputs are filled in. Here is the explanation of behavior below:

  • You can fill in only one character per input.
  • To overwrite the value of an input, you donʼt need to select the text in the input. If you type a digit while the input is focused, the input will update.
  • Pressing Backspace when it is empty in the input will move focus to the previous input (if any) and clear the value of.

Pin input length

PinInput expects an array of strings for the values prop. The number of input fields rendered is equal to the length of the array.

Passing id, className and name

You can pass id or name or containerClassName or inputClassName prop.ClassName and name will be shared amongst the inputs, which means they will each have their name set to this value, it is comfortable for the use of pseudo element in global.css file. id will append an index to any string you provide and pass to the corresponding input. For example, if you pass foo as id and bar as name and there are four inputs, the resulting ids are foo-0, foo-1, foo-2, and foo-3 and each input has the name bar.

Size of input

You can control the size of the inputs with the size prop. It comes in four sizes.

VarietySizeFont Size
xs1.5rem (24px)0.75rem (12px)
sm2rem (32px)0.875rem (14px)
md2.5rem (40px)1rem (16px)
lg3rem (48px)1.125rem (18px)

Allowing alphanumeric input values

By default, the PinInput is passed number as type prop so it accepts only numeric values to show up. To add support for alphanumeric values, pass the type prop and set its value to either text or number.

Validate & Format

PinInput provides validate prop to specify native attribute pattern, which is a regular expression which the input's value must match in order for the value to pass constraint validation.

Format prop specify a pure function to transform raw input. For example, to set the values to upper case:

(char: string) => char.toUpperCase()

Valid & Invalid state

You can pass validate prop to validate input values, if value of the input doesn't match test, it will show error border instantly enter, while it will only show valid border after all inputs are filled in and pass validation.

You can disable this behavior by setting showState prop to false.

Adding mask

You can set mask prop to true to prevent from showing input values. It is equivalent to setting input native attribute type to password.

Disable focus management

By default, PinInput moves focus automatically to the next input once a field is filled. It also transfers focus to a previous input when Backspace is pressed with focus on an empty input.

To disable this behavior, set autoTab prop to false


PinInput offers a way let it focus from the start. Simply set autoFocus prop to true to focus the first input upon initial mounting of the component.

Once all inputs are filled in

PinInput provides onComplete event handler to access input values, unlike onChange, onComplete only triggers when all inputs are filled in.

NOTICE if you offer a list of allowed characters to validate prop, onComplete will trigger ony when all input values pass validation.

Changing the placeholder

By default, PinInput placeholder is (○), you can change that if you want.

AutoFill and Copy + Paste

Try copying & pasting 1234 into any of the inputs.

By default, you can only change one input at a time, but if one of the input field receives a longer string by pasting into it, PinInput attempts to validate the string and fill the other inputs.

Stateful (uncontrolled) Pin Input

There is also an uncontrolled version, StatefulPinInput, which manages its own state. The default input length for this component is four, but you can change that by passing number to length prop. Aslo you can pass initialValue as an initial value for StatefulPinInput.

import React from 'react'; import { StatefulPinInput } from 'react-input-pin-code'; export default () => { return ( <StatefulPinInput length={4} initialValue="1234" /> ); };


PinInput Props

aria-describedbystringSets aria-describedby attribute.
aria-labelstring"Please enter pin code"Sets aria-label attribute.
aria-labelledbystringSets aria-labelledby attribute.
autoCompletestring"off"Determines if browser should provide value suggestions.
autoFocusbooleanfalseIf true the input will be focused on the first mount.
autoTabbooleantrueIf true focus will move automatically to the next/previous input once filled or deleted
borderColorstringrgb(204,204,204)Let you customize border color when the input is not focused.
errorBorderColorstringrgb(220,53,69)Let you customize border color when the input is invalid.
focusBorderColorstringrgb(13,110,253)Let you customize border color when the input is focused.
validBorderColorstringrgb(25,135,84)Let you customize border color when the input is valid.
disabledbooleanRenders component in disabled state.
format(char: string) => stringPure function to transform raw input.
idstringId attribute value to be added to the input element and as a label's for attribute value.
inputModestringThis attribute allows a browser to display an appropriate virtual keyboard. (NOTE type prop will determine appropriate inputMode automatically. if you pass inputMode prop, it will override built-in inputMode.)
maskbooleanfalsePrevent from showing the pin code
namestringName attribute.
onBlur(event: React.FocusEvent) => voidCalled the onBlur event triggers.
onChange(value: string | string[], index: number, values: string[]) => voidCalled when input value is changed.
onComplete(values: string[]) => voidCalled when all inputs are filled in and validated
onFocus(event: React.FocusEvent) => voidCalled the onFocus event triggers.
onKeyDown(event: React.KeyboardEvent) => voidCalled the onKeyDown event triggers.
placeholderstring"o"Displayed when the pin code is not entered yet.
requiredbooleanRenders component in required state.
showStatebooleantrueDetermine whether valid/invalid border shows up or not.
size"xs" | "sm" | "md" | "lg""md"Renders component in provided size.
containerStyleobject{}Lets you customize container element.
inputStyleobject{}Lets you customize input element.
type"number" | "text""number"The type of values the pin-input should allow
validatestring | string[] | RegExpA regular expression which the input's value must match in order for the value to pass constraint validation.
values(*)string[]PinInput value attribute.

StatefulPinInput Props

lengthnumber4The number of input fields.
initialValuestring | string[]""The initial value of the pin input.





Last updated on 22 Apr 2022

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
support@socket.devSocket SOC 2 Logo


  • Package Issues
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc