Skip to content

necipallef/react-native-confirmation-code-field

 
 

Repository files navigation

react-native-confirmation-code-field

npm npm downloads Travis

A react-native confirmation code field compatible with iOS, Android and Web Platforms (based on this project Migration Guide)

Component features:

  • 🔮 Simple. Easy to use;
  • 🍎 Support "fast paste SMS-code" on iOS. And custom code paste for Android;
  • 🚮 Clearing part of the code by clicking on the cell;
  • blur() and focus() methods;
  • 🛠 Extendable and hackable;
  • 🤓 Readable changelog.

Links

Screenshots

Install

yarn add react-native-confirmation-code-field
# or
npm install react-native-confirmation-code-field

Usage

import React, { useCallback } from 'react';
import CodeInput from 'react-native-confirmation-code-field';

export const App = () => {
  const handlerOnFulfill = useCallback(code => console.log(code), []);

  return <CodeInput onFulfill={handlerOnFulfill} />;
};

How paste or clear code

Paste code can helpful for Android platform when you can read SMS.

import React, { Component, createRef } from 'react';
import CodeInput from 'react-native-confirmation-code-field';

class App extends Component {
  handlerOnFulfill = code => {
    if (isValidCode(code)) {
      console.log(code);
    } else {
      this.clearCode();
    }
  };

  field = createRef();

  clearCode() {
    const { current } = this.field;

    if (current) {
      current.clear();
    }
  }

  pasteCode() {
    const { current } = this.field;

    if (current) {
      current.handlerOnTextChange(value);
    }
  }

  render() {
    return <CodeInput ref={this.field} onFulfill={this.handlerOnFulfill} />;
  }
}

Analogs

How it works?

This component consists of:

  1. Container <View {...containerProps}/>;
  2. Render the "Cells" for the text code inside the container ("Cells" is <TextInput {...cellProps} />);
  3. And over this render invisible <TextInput {...inputProps}/>;
  4. "Cursor" inside cell is simulated component

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%