diff --git a/README.md b/README.md index 799fe11..3f1fdf3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -43,6 +43,7 @@ render() { size={30} inputPosition='left' onFulfill={(code) => this._onFulfill(code)} + onCodeChange={(code) => { this.state.code = code }} /> this._onFinishCheckingCode1(isValid)} containerStyle={{ marginTop: 30 }} codeInputStyle={{ borderWidth: 1.5 }} + onCodeChange={(code) => { this.state.code = code }} /> this._onFinishCheckingCode2(isValid, code)} + onCodeChange={(code) => { this.state.code = code }} /> ) } @@ -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: @@ -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. \ No newline at end of file +Any question or support will welcome. diff --git a/components/ConfirmationCodeInput.js b/components/ConfirmationCodeInput.js index 239c85a..1bf1ec4 100644 --- a/components/ConfirmationCodeInput.js +++ b/components/ConfirmationCodeInput.js @@ -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 = { @@ -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) { @@ -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(); } @@ -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; @@ -226,7 +236,7 @@ export default class ConfirmationCodeInput extends Component { codeArr: newCodeArr, currentIndex: prevState.currentIndex + 1 }; - }); + }, () => { onCodeChange(newCodeArr.join('')) }); } render() { diff --git a/example/src/components/ConfirmationCodeInput.js b/example/src/components/ConfirmationCodeInput.js index c9876a2..772e9a8 100644 --- a/example/src/components/ConfirmationCodeInput.js +++ b/example/src/components/ConfirmationCodeInput.js @@ -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'; @@ -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 = { @@ -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) { @@ -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(); } @@ -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; @@ -223,7 +233,7 @@ export default class ConfirmationCodeInput extends Component { codeArr: newCodeArr, currentIndex: prevState.currentIndex + 1 }; - }); + }, () => { onCodeChange(newCodeArr.join('')) }); } render() { diff --git a/example/src/index.js b/example/src/index.js index af5f74b..6144275 100644 --- a/example/src/index.js +++ b/example/src/index.js @@ -8,7 +8,6 @@ import { Text, View, ScrollView, - TextInput, Alert } from 'react-native'; @@ -103,6 +102,7 @@ class example extends Component { size={30} inputPosition='left' onFulfill={(code) => this._onFulfill(code)} + onCodeChange={(code) => { this.state.code = code }} /> @@ -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 }} /> - CIRCLE CONFIRMATION CODE this._onFinishCheckingCode2(isValid, code)} + onCodeChange={(code) => { this.state.code = code }} />