Last active
September 8, 2019 11:55
-
-
Save mdmoin7/e29c9cd6944d276b753483903c800f89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { StyleSheet } from 'react-native'; | |
import { Content, Item, Input } from 'native-base'; | |
import { Grid, Col } from 'react-native-easy-grid'; | |
class OtpInputs extends React.Component { | |
state={otp:[]}; | |
otpTextInput = []; | |
componentDidMount() { | |
this.otpTextInput[0]._root.focus(); | |
} | |
renderInputs() { | |
const inputs = Array(6).fill(0); | |
const txt = inputs.map( | |
(i, j) => <Col key={j} style={styles.txtMargin}><Item regular> | |
<Input | |
style={[styles.inputRadius, { borderRadius: 10 }]} | |
keyboardType="numeric" | |
onChangeText={v => this.focusNext(j, v)} | |
onKeyPress={e => this.focusPrevious(e.nativeEvent.key, j)} | |
ref={ref => this.otpTextInput[j] = ref} | |
/> | |
</Item></Col> | |
); | |
return txt; | |
} | |
focusPrevious(key, index) { | |
if (key === 'Backspace' && index !== 0) | |
this.otpTextInput[index - 1]._root.focus(); | |
} | |
focusNext(index, value) { | |
if (index < this.otpTextInput.length - 1 && value) { | |
this.otpTextInput[index + 1]._root.focus(); | |
} | |
if (index === this.otpTextInput.length - 1) { | |
this.otpTextInput[index]._root.blur(); | |
} | |
const otp = this.state.otp; | |
otp[index] = value; | |
this.setState({ otp }); | |
this.props.getOtp(otp.join('')); | |
} | |
render() { | |
return ( | |
<Content padder> | |
<Grid style={styles.gridPad}> | |
{this.renderInputs()} | |
</Grid> | |
</Content> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
gridPad: { padding: 30 }, | |
txtMargin: { margin: 3 }, | |
inputRadius: { textAlign: 'center' } | |
}); | |
export default OtpInputs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment