Git Product home page Git Product logo

react-native-confirmation-code-field's Introduction

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.

Links

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 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

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


# If you need React Native 0.63.x and below
yarn add react-native-confirmation-code-field@6

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"
        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;

react-native-confirmation-code-field's People

Contributors

retyui avatar ttdung11t2 avatar paitoanderson avatar bamlhs avatar arvinio avatar denisfrezzato avatar codebutler avatar francescopersico avatar joelwee avatar murtraja avatar muslumsezgin avatar reedyrm avatar salomaoluiz avatar leozzitowned avatar necipallef avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.