Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-confirmation-code-field

retyui415.5kMIT7.6.1TypeScript support: included

A react-native component to input confirmation code for both Android and IOS

react, react-component, react-native, react-native-component, android, ios, code-input, confirmation-code, pin-code, confirmation-code-input, pin-code-input

readme

react-native-confirmation-code-field

react-native-confirmation-code-field on npm react-native-confirmation-code-field downloads react-native-confirmation-code-field install size CI status

A simple react-native confirmation code field compatible with iOS, Android.

Component features:

  • 🔮 Simple and tiny 3.8 KB. Easy to use;
  • 🚮 Clearing part of the code by clicking on the cell;
  • 🍎 Support "fast paste SMS-code" on iOS. And custom code paste for Android;
  • ⚡ TextInput ref support;
  • 🛠 Highly customizable. Can be used as masked TextInput;
  • 🤓 Readable changelog.

Screenshots

| | | | --- | --- | | react-native-confirmation-code-field animated example | react-native-confirmation-code-field mask example
react-native-confirmation-code-field unmask example
react-native-confirmation-code-field underline example
react-native-confirmation-code-field formatting example |

Install

yarn add react-native-confirmation-code-field

How it works

I use an invisible <TextInput/> component that will be stretched over <Cell/> components.

JSX structure looks like that:

// Root view (rectangle with a red border on 3d visualization below)
<View style={rootStyle}>
  // Each Cell element is result of a `renderCell` function (gray boxes)
  <Cell>1</Cell>
  <Cell>2</Cell>
  <Cell>3</Cell>
  <Cell>|</Cell>
  <Cell></Cell>
  <Cell></Cell>
  // Invisible Text Input with absolute position (gray rectangle with text '123')
  <TextInput value="123"/>
</View>

3d layout of component

It needs to solve next problems:

  • When user pastes code from SMS on iOS issue#25
  • Better UX when user types fast, or system sluggish, characters might lost when component switching focus between <TextInput/>.

Basic example

I took a minimal implementation approach. It mean that this component provides low-level functionality so you can create incredible UI without tears 😭. I recommend you start with creating your own wrapper where you apply all styles and basic configuration.

You can use a ready-made solution out of the box:

import React, {useState} from 'react';
import {SafeAreaView, Text, StyleSheet} from 'react-native';

import {
  CodeField,
  Cursor,
  useBlurOnFulfill,
  useClearByFocusCell,
} from 'react-native-confirmation-code-field';

const styles = StyleSheet.create({
  root: {flex: 1, padding: 20},
  title: {textAlign: 'center', fontSize: 30},
  codeFieldRoot: {marginTop: 20},
  cell: {
    width: 40,
    height: 40,
    lineHeight: 38,
    fontSize: 24,
    borderWidth: 2,
    borderColor: '#00000030',
    textAlign: 'center',
  },
  focusCell: {
    borderColor: '#000',
  },
});

const CELL_COUNT = 6;

const App = () => {
  const [value, setValue] = useState('');
  const ref = useBlurOnFulfill({value, cellCount: CELL_COUNT});
  const [props, getCellOnLayoutHandler] = useClearByFocusCell({
    value,
    setValue,
  });

  return (
    <SafeAreaView style={styles.root}>
      <Text style={styles.title}>Verification</Text>
      <CodeField
        ref={ref}
        {...props}
        // Use `caretHidden={false}` when users can't paste a text value, because context menu doesn't appear
        value={value}
        onChangeText={setValue}
        cellCount={CELL_COUNT}
        rootStyle={styles.codeFieldRoot}
        keyboardType="number-pad"
        textContentType="oneTimeCode"
        autoComplete={Platform.select({ android: 'sms-otp', default: 'one-time-code' })}
        testID="my-code-input"
        renderCell={({index, symbol, isFocused}) => (
          <Text
            key={index}
            style={[styles.cell, isFocused && styles.focusCell]}
            onLayout={getCellOnLayoutHandler(index)}>
            {symbol || (isFocused ? <Cursor/> : null)}
          </Text>
        )}
      />
    </SafeAreaView>
  );
};

export default App;

Compatibility

For `react-native@0.63.xand below useyarn add react-native-confirmation-code-field@6, otherwise use the latest versionyarn add react-native-confirmation-code-field`

changelog

Change Log

This project adheres to Semantic Versioning.

7.6.0

  • Fix a "clearInterval called with an invalid handle" error, see: #245, thanks @fcaldarelli

7.5.0

  • Fix typescript error Type Error: 'Cursor' cannot be used as a JSX component

7.4.0

  • Add autoComplete prop by default, for CodeField component #239

7.3.2

  • Fix disabled field by default on iOS after enabling the new architecture #230

7.3.0

  • Add a new property InputComponent?: ComponentType #193, thanks @muslumsezgin

7.2.0

  • Mark all peerDependencies as optional

7.1.0

  • Fix warning: unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering. on WEB #171, #175

7.0.0

  • Support react-native 0.64.x and above only!!!
  • Remove a crazy workaround for listening to user taps on the <TextInput/> component
  • Optimize a bundle size
  • Export CodeFieldProps and RenderCellOptions types

6.7.0

  • Fix warning: unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering. on WEB #171, #175

6.5.1

  • Revert previous fix (6.5.0) to solve an issue #153

6.5.0

  • Fix copy\paste menu appearing when caretHidden={true} and value={''} #140, thanks @sprotymo

6.4.0

  • Fix RTL layout rtl example usage

6.3.0

  • A module was adapted for old react-native versions (now you can use a 6 version for old that 0.62.x)

6.2.0

  • Publish a compatible bundle for react-native-web

6.1.1

  • Fixed typescript issue #127

6.0.0

  • Added support of react native 0.62.x

5.0.0

New release, requirements react>=16.4.0 & react-native:>=0.59.0

  • stateless component and new API (see docs)
  • rewrited on TypeScript
  • deprecated onFulfill use onChangeText instead of it:

`typescript jsx const CELL_COUNT = 6;

<CodeField cellCount={CELL_COUNT} onChangeText={(value) => { if (value.length === CELL_COUNT) { onFulfill(value); } }} />; `

4.2.0

  • Add more controls on mask symbol component, inspired by 91
  • Fix android cursor alignment bug #94. Thanks @necipallef

4.1.0

  • Fix problem with inherit method, rename onTextChange => onChangeText #81
  • Updated type of prop inputProps (Flow.js & TS)
  • Release diff on npmfs.com

4.0.0

  • Fix problem with focus on Inputs #78

3.7.1

  • Fix problem when cursor inherit <Cell/> styles #72

3.7.0

  • Simulate secureTextEntry #62
  • Reduce package size

3.6.0

3.5.0

  • Add new prop CellComponent?: ComponentType, it can be useful for create some animations Example

3.4.1

3.4.0

  • Added support for react-native-web #50

3.3.0

  • Added new prop normalizeCode: (code: string) => string via #47 #41

3.2.3

  • Fix onPress event handler #45

3.2.2

3.2.1

  • Fix issue about testID #38

3.2.0

3.1.3

  • Fix autoFucus prop #32

3.1.0

  • Fix iOS borderBottom style #28

3.0.0

  • Remove base implementation based on One code cell === One TextInput
  • Fix iOS fast paste SMS code
  • Update inputProps: Function prop, now is inputProps: ?Object
  • Remove canPasteCode, now it is works always by default
  • Remove inputStyle: Function use cellProps
  • Remove onChangeText use inputProps={{onChangeText: (text) => {}}}
  • Remove some variant values: border-b-t and border-l-r
  • Add new component focus() and blur() methods
  • Fix typescript definition #27

2.0.5

  • Fix typescript definition #27

2.0.4

  • Fix Flow.js types
  • Fix spread non-iterable instance #24

2.0.1

  • Update TypeScript definition changes

2.0.0

  • New example for RN 0.57 version
  • Merged changes from 1.2.1 version
  • Update dependencies

    RN@0.57 - Fixed extreme <TextInput> slowness (5017b86 by @gnprice)

  • Remove ignoreCaseWhenCompareCode and compareWithCode props.
  • Add paste support. By default it's disabled. That would enable set canPasteCode={true}
  • Rename property getInputProps => inputProps
  • Rename property getInputStyle => inputStyle
  • Remove iOS hack for onKeyPress (use latest RN version)
  • Remove INDEX typing
  • Fix desynchronization onFocus and setState #16

1.2.3

  • Fix desynchronization onFocus and setState #16

1.2.1

  • Add export for types #10

1.2.0

  • Add new prop maskSymbol?: string #6

1.1.0

  • Add keyboardType prop #4
  • Fix typescript module name #3

1.0.0

  • Rename ignoreCase => ignoreCaseWhenCompareCode property
  • Rename className => variant property

  • Add new methods (getInputStyle()and getInputProps()_ for help users to customize inputs

  • Add new property containerProps, testID
  • Add onChangeCode() callback and defaultCode props. #22#33
  • Add clear() method to TypeScript definition #21

  • Fix slowly focus change 10

  • Fix iOS input bag. #38

0.0.0

  • Fork dead repository