Socket
Socket
Sign inDemoInstall

react-native-confirmation-code-field

Package Overview
Dependencies
514
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-native-confirmation-code-field

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


Version published
Weekly downloads
73K
increased by12.44%
Maintainers
1
Install size
27.0 kB
Created
Weekly downloads
 

Changelog

Source

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

Readme

Source

react-native-confirmation-code-field

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

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

Component features:

  • 🔮 Simple and tiny 1.55 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 examplereact-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

# Support React Native 0.64.x and above
yarn add react-native-confirmation-code-field@next

How it works

I use an invisible <TextInput/> component that will be stretched over <Cell/> components. 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}
        value={value}
        onChangeText={setValue}
        cellCount={CELL_COUNT}
        rootStyle={styles.codeFieldRoot}
        keyboardType="number-pad"
        textContentType="oneTimeCode"
        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;

Keywords

FAQs

Last updated on 27 Dec 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • 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