|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import {StyleSheet, Text, View, Button, Alert, TextInput} from 'react-native'; |
| 3 | + |
| 4 | +import OTPTextView from 'react-native-otp-textinput'; |
| 5 | + |
| 6 | +const styles = StyleSheet.create({ |
| 7 | + container: { |
| 8 | + flex: 1, |
| 9 | + justifyContent: 'center', |
| 10 | + alignItems: 'center', |
| 11 | + backgroundColor: '#F5FCFF', |
| 12 | + padding: 5, |
| 13 | + }, |
| 14 | + welcome: { |
| 15 | + fontSize: 20, |
| 16 | + textAlign: 'center', |
| 17 | + margin: 10, |
| 18 | + }, |
| 19 | + instructions: { |
| 20 | + fontSize: 22, |
| 21 | + fontWeight: '500', |
| 22 | + textAlign: 'center', |
| 23 | + color: '#333333', |
| 24 | + marginBottom: 20, |
| 25 | + }, |
| 26 | + textInputContainer: { |
| 27 | + marginBottom: 20, |
| 28 | + }, |
| 29 | + roundedTextInput: { |
| 30 | + borderRadius: 10, |
| 31 | + borderWidth: 4, |
| 32 | + }, |
| 33 | + buttonWrapper: { |
| 34 | + flexDirection: 'row', |
| 35 | + justifyContent: 'space-around', |
| 36 | + marginBottom: 20, |
| 37 | + width: '60%', |
| 38 | + }, |
| 39 | + textInput: { |
| 40 | + height: 40, |
| 41 | + width: '80%', |
| 42 | + borderColor: '#000', |
| 43 | + borderWidth: 1, |
| 44 | + padding: 10, |
| 45 | + fontSize: 16, |
| 46 | + letterSpacing: 5, |
| 47 | + marginBottom: 10, |
| 48 | + textAlign: 'center', |
| 49 | + }, |
| 50 | + buttonStyle: { |
| 51 | + marginHorizontal: 20, |
| 52 | + }, |
| 53 | +}); |
| 54 | + |
| 55 | +export default class App extends Component { |
| 56 | + state = { |
| 57 | + otpInput: '', |
| 58 | + inputText: '', |
| 59 | + }; |
| 60 | + |
| 61 | + alertText = () => { |
| 62 | + const {otpInput = ''} = this.state; |
| 63 | + if (otpInput) { |
| 64 | + Alert.alert(otpInput); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + clear = () => { |
| 69 | + this.input1.clear(); |
| 70 | + }; |
| 71 | + |
| 72 | + updateOtpText = () => { |
| 73 | + // will automatically trigger handleOnTextChange callback passed |
| 74 | + this.input1.setValue(this.state.inputText); |
| 75 | + }; |
| 76 | + |
| 77 | + render() { |
| 78 | + return ( |
| 79 | + <View style={styles.container}> |
| 80 | + <Text style={styles.instructions}>react-native-otp-textinput</Text> |
| 81 | + <OTPTextView |
| 82 | + ref={e => (this.input1 = e)} |
| 83 | + containerStyle={styles.textInputContainer} |
| 84 | + handleTextChange={text => this.setState({otpInput: text})} |
| 85 | + inputCount={4} |
| 86 | + keyboardType="numeric" |
| 87 | + /> |
| 88 | + <TextInput |
| 89 | + maxLength={4} |
| 90 | + onChangeText={e => this.setState({inputText: e})} |
| 91 | + style={styles.textInput} |
| 92 | + /> |
| 93 | + <View style={styles.buttonWrapper}> |
| 94 | + <Button title="Clear" onPress={this.clear} /> |
| 95 | + <Button |
| 96 | + style={styles.buttonStyle} |
| 97 | + title="Update" |
| 98 | + onPress={this.updateOtpText} |
| 99 | + /> |
| 100 | + <Button |
| 101 | + style={styles.buttonStyle} |
| 102 | + title="Submit" |
| 103 | + onPress={this.alertText} |
| 104 | + /> |
| 105 | + </View> |
| 106 | + <Text style={styles.instructions}>Customizations</Text> |
| 107 | + <OTPTextView |
| 108 | + handleTextChange={e => {}} |
| 109 | + containerStyle={styles.textInputContainer} |
| 110 | + textInputStyle={styles.roundedTextInput} |
| 111 | + inputCount={5} |
| 112 | + inputCellLength={2} |
| 113 | + /> |
| 114 | + <OTPTextView |
| 115 | + handleTextChange={e => {}} |
| 116 | + containerStyle={styles.textInputContainer} |
| 117 | + textInputStyle={styles.roundedTextInput} |
| 118 | + defaultValue="1234" |
| 119 | + /> |
| 120 | + <OTPTextView |
| 121 | + handleTextChange={e => {}} |
| 122 | + containerStyle={styles.textInputContainer} |
| 123 | + textInputStyle={[styles.roundedTextInput, {borderRadius: 100}]} |
| 124 | + tintColor="#000" |
| 125 | + /> |
| 126 | + <OTPTextView |
| 127 | + handleTextChange={e => {}} |
| 128 | + containerStyle={styles.textInputContainer} |
| 129 | + tintColor={['#FF0000', '#FFFF00', '#00FF00', '#0000FF']} |
| 130 | + /> |
| 131 | + <OTPTextView |
| 132 | + handleTextChange={e => {}} |
| 133 | + containerStyle={styles.textInputContainer} |
| 134 | + tintColor="#000" |
| 135 | + offTintColor={['#FF0000', '#FFFF00', '#00FF00', '#0000FF']} |
| 136 | + /> |
| 137 | + </View> |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments