reactrondev / react-native-confirmation-code-field

A react-native confirmation code field compatible with IOS, Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

Links

Component features:

  • ๐Ÿ”ฎ Simple. 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;
  • ๐Ÿค“ Readable changelog.

Screenshots

react-native-confirmation-code-field animated example react-native-confirmation-code-field mask example react-native-confirmation-code-field underline example react-native-confirmation-code-field basic example

Install

# for react-native@0.62.x and above
yarn add react-native-confirmation-code-field

# for react-native@0.61.x and below
yarn add react-native-confirmation-code-field@5

How it work

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;

About

A react-native confirmation code field compatible with IOS, Android

License:MIT License


Languages

Language:TypeScript 86.7%Language:JavaScript 13.3%