Skip to content

Get Code text on every update of Field #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ A react-native confirmation code input for both IOS and Android
## Installation

```sh
npm install react-native-confirmation-code-input --save
npm install git+https://github.com/PujanShah22/react-native-confirmation-code-input.git
```

## Usage
Expand All @@ -43,6 +43,7 @@ render() {
size={30}
inputPosition='left'
onFulfill={(code) => this._onFulfill(code)}
onCodeChange={(code) => { this.state.code = code }}
/>

<CodeInput
Expand All @@ -58,6 +59,7 @@ render() {
onFulfill={(isValid) => this._onFinishCheckingCode1(isValid)}
containerStyle={{ marginTop: 30 }}
codeInputStyle={{ borderWidth: 1.5 }}
onCodeChange={(code) => { this.state.code = code }}
/>

<CodeInput
Expand All @@ -69,6 +71,7 @@ render() {
autoFocus={false}
codeInputStyle={{ fontWeight: '800' }}
onFulfill={(isValid, code) => this._onFinishCheckingCode2(isValid, code)}
onCodeChange={(code) => { this.state.code = code }}
/>
)
}
Expand All @@ -93,6 +96,7 @@ Prop | Type | Default | Description
`codeInputStyle` | style object | | custom style for code input
`containerStyle` | style object | | custom style for code input container
`onFulfill` | function | | callback function called when fulfilling code. If `compareWithCode` is null -> return `(code)` in callback, else return `(isValid, code)`. **Required**
`onCodeChange` | function | | callback function called when updating code.

## functions
clear input:
Expand All @@ -104,16 +108,9 @@ this.refs.refName.clear();
ref="refName"
/>
```
## Example
See [EXAMPLE](example)
```sh
git clone https://github.com/ttdung11t2/react-native-confirmation-code-input.git
cd react-native-confirmation-code-input/example
npm install
react-native run-ios / react-native run-android
```

## License

react-native-confirmation-code-input is released under the MIT license. See [LICENSE](LICENSE) for details.

Any question or support will welcome.
Any question or support will welcome.
16 changes: 13 additions & 3 deletions components/ConfirmationCodeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class ConfirmationCodeInput extends Component {
codeInputStyle: TextInput.propTypes.style,
containerStyle: viewPropTypes.style,
onFulfill: PropTypes.func,
onCodeChange: PropTypes.func
};

static defaultProps = {
Expand All @@ -35,7 +36,8 @@ export default class ConfirmationCodeInput extends Component {
inactiveColor: 'rgba(255, 255, 255, 0.2)',
space: 8,
compareWithCode: '',
ignoreCase: false
ignoreCase: false,
onCodeChange: (code) => null
};

constructor(props) {
Expand Down Expand Up @@ -69,6 +71,7 @@ export default class ConfirmationCodeInput extends Component {
}

_setFocus(index) {
this.props.onCodeChange(new Array(this.props.codeLength).fill('').join(''))
this.codeInputRefs[index].focus();
}

Expand Down Expand Up @@ -196,13 +199,20 @@ export default class ConfirmationCodeInput extends Component {
_onKeyPress(e) {
if (e.nativeEvent.key === 'Backspace') {
const { currentIndex } = this.state;
let newCodeArr = _.clone(this.state.codeArr);
const nextIndex = currentIndex > 0 ? currentIndex - 1 : 0;
for (const i in newCodeArr) {
if (i >= nextIndex) {
newCodeArr[i] = '';
}
}
this.props.onCodeChange(newCodeArr.join(''))
this._setFocus(nextIndex);
}
}

_onInputCode(character, index) {
const { codeLength, onFulfill, compareWithCode, ignoreCase } = this.props;
const { codeLength, onFulfill, compareWithCode, ignoreCase, onCodeChange } = this.props;
let newCodeArr = _.clone(this.state.codeArr);
newCodeArr[index] = character;

Expand All @@ -226,7 +236,7 @@ export default class ConfirmationCodeInput extends Component {
codeArr: newCodeArr,
currentIndex: prevState.currentIndex + 1
};
});
}, () => { onCodeChange(newCodeArr.join('')) });
}

render() {
Expand Down
20 changes: 15 additions & 5 deletions example/src/components/ConfirmationCodeInput.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {Component} from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { View, TextInput, StyleSheet, Dimensions } from 'react-native';
import _ from 'lodash';
Expand All @@ -19,6 +19,7 @@ export default class ConfirmationCodeInput extends Component {
codeInputStyle: TextInput.propTypes.style,
containerStyle: View.propTypes.style,
onFulfill: PropTypes.func,
onCodeChange: PropTypes.func
};

static defaultProps = {
Expand All @@ -32,7 +33,8 @@ export default class ConfirmationCodeInput extends Component {
inactiveColor: 'rgba(255, 255, 255, 0.2)',
space: 8,
compareWithCode: '',
ignoreCase: false
ignoreCase: false,
onCodeChange: (code) => null
};

constructor(props) {
Expand Down Expand Up @@ -66,6 +68,7 @@ export default class ConfirmationCodeInput extends Component {
}

_setFocus(index) {
this.props.onCodeChange(new Array(this.props.codeLength).fill('').join(''))
this.codeInputRefs[index].focus();
}

Expand Down Expand Up @@ -193,13 +196,20 @@ export default class ConfirmationCodeInput extends Component {
_onKeyPress(e) {
if (e.nativeEvent.key === 'Backspace') {
const { currentIndex } = this.state;
const nextIndex = currentIndex > 0 ? currentIndex - 1 : 0;
let newCodeArr = _.clone(this.state.codeArr);
const nextIndex = currentIndex > 0 ? currentIndex - 1 : 0;
for (const i in newCodeArr) {
if (i >= nextIndex) {
newCodeArr[i] = '';
}
}
this.props.onCodeChange(newCodeArr.join(''))
this._setFocus(nextIndex);
}
}

_onInputCode(character, index) {
const { codeLength, onFulfill, compareWithCode, ignoreCase } = this.props;
const { codeLength, onFulfill, compareWithCode, ignoreCase, onCodeChange } = this.props;
let newCodeArr = _.clone(this.state.codeArr);
newCodeArr[index] = character;

Expand All @@ -223,7 +233,7 @@ export default class ConfirmationCodeInput extends Component {
codeArr: newCodeArr,
currentIndex: prevState.currentIndex + 1
};
});
}, () => { onCodeChange(newCodeArr.join('')) });
}

render() {
Expand Down
5 changes: 3 additions & 2 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Text,
View,
ScrollView,
TextInput,
Alert
} from 'react-native';

Expand Down Expand Up @@ -103,6 +102,7 @@ class example extends Component {
size={30}
inputPosition='left'
onFulfill={(code) => this._onFulfill(code)}
onCodeChange={(code) => { this.state.code = code }}
/>
</View>

Expand All @@ -121,9 +121,9 @@ class example extends Component {
onFulfill={(isValid) => this._onFinishCheckingCode1(isValid)}
containerStyle={{ marginTop: 30 }}
codeInputStyle={{ borderWidth: 1.5 }}
onCodeChange={(code) => { this.state.code = code }}
/>
</View>

<View style={styles.inputWrapper3}>
<Text style={styles.inputLabel3}>CIRCLE CONFIRMATION CODE</Text>
<CodeInput
Expand All @@ -135,6 +135,7 @@ class example extends Component {
autoFocus={false}
codeInputStyle={{ fontWeight: '800' }}
onFulfill={(isValid, code) => this._onFinishCheckingCode2(isValid, code)}
onCodeChange={(code) => { this.state.code = code }}
/>
</View>
</ScrollView>
Expand Down