react-native-confirmation-code-field
Version:
A react-native component to input confirmation code for both Android and IOS
99 lines (71 loc) • 2.92 kB
Markdown
[](https://www.npmjs.com/package/react-native-confirmation-code-field)
[](https://travis-ci.org/retyui/react-native-confirmation-code-field)
A react-native confirmation code field for both IOS and Android (base on [this](https://github.com/ttdung11t2/react-native-confirmation-code-input) project [Migration Guide](docs/migration.md))
### Component features:
- 🔮 Simple. Easy to use;
- 🍎 IOS "fast paste SMS-code" support (\* and custom code paste for Android);
- 🚮 Clearing part of the code by clicking on the cell;
- ⚡ `blur()` and `focus()` methods support;
- 🛠 Extendable and hackable;
- 🤓 Readable [changelog](CHANGELOG.md).
- [API documentation](docs/API.md)
- [Example](examples/rn57)
- Live demo [IOS / Android](https://snack.expo.io/@retyui/demo-for-react-native-confirmation-code-field), [react-native-web](https://retyui.github.io/react-native-confirmation-code-field/)
<img width="250" height="312" src="https://raw.githubusercontent.com/retyui/react-native-confirmation-code-field/master/docs/img/redDemo.jpg"/><img width="250" height="312" src="https://raw.githubusercontent.com/retyui/react-native-confirmation-code-field/master/docs/img/darkDemo.jpg"/>
```sh
yarn add react-native-confirmation-code-field
npm install react-native-confirmation-code-field
```
```js
import React, { Component } from 'react';
import CodeInput from 'react-native-confirmation-code-field';
class App extends Component {
handlerOnFulfill = code => console.log(code);
render() {
return <CodeInput onFulfill={this.handlerOnFulfill} />;
}
}
```
Paste code can helpful for Android platform when you can read SMS.
```js
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} />;
}
}
```
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](src/components/Cursor.js)