1- import React , { Component } from 'react' ;
2- import { StyleSheet , Text , View , Button , Alert , TextInput } from 'react-native' ;
1+ import React , { useRef , useState } from 'react' ;
2+ import {
3+ StyleSheet ,
4+ Text ,
5+ View ,
6+ Button ,
7+ Alert ,
8+ TextInput ,
9+ SafeAreaView ,
10+ ScrollView ,
11+ } from 'react-native' ;
312
413import OTPTextView from 'react-native-otp-textinput' ;
514
615const styles = StyleSheet . create ( {
7- container : {
16+ safeAreaView : {
817 flex : 1 ,
18+ } ,
19+ container : {
920 justifyContent : 'center' ,
1021 alignItems : 'center' ,
1122 backgroundColor : '#F5FCFF' ,
1223 padding : 5 ,
24+ paddingVertical : 20 ,
1325 } ,
1426 welcome : {
1527 fontSize : 20 ,
1628 textAlign : 'center' ,
1729 margin : 10 ,
1830 } ,
1931 instructions : {
20- fontSize : 22 ,
32+ fontSize : 18 ,
2133 fontWeight : '500' ,
2234 textAlign : 'center' ,
2335 color : '#333333' ,
24- marginBottom : 20 ,
36+ marginBottom : 10 ,
2537 } ,
2638 textInputContainer : {
2739 marginBottom : 20 ,
@@ -35,6 +47,7 @@ const styles = StyleSheet.create({
3547 justifyContent : 'space-around' ,
3648 marginBottom : 20 ,
3749 width : '60%' ,
50+ gap : 20 ,
3851 } ,
3952 textInput : {
4053 height : 40 ,
@@ -47,94 +60,80 @@ const styles = StyleSheet.create({
4760 marginBottom : 10 ,
4861 textAlign : 'center' ,
4962 } ,
50- buttonStyle : {
51- marginHorizontal : 20 ,
52- } ,
5363} ) ;
5464
55- export default class App extends Component {
56- state = {
57- otpInput : '' ,
58- inputText : '' ,
59- } ;
65+ const App : React . FC = ( ) => {
66+ const [ otpInput , setOtpInput ] = useState < string > ( "" ) ;
6067
61- alertText = ( ) => {
62- const { otpInput = '' } = this . state ;
63- if ( otpInput ) {
64- Alert . alert ( otpInput ) ;
65- }
66- } ;
68+ const input = useRef < OTPTextView > ( null ) ;
6769
68- clear = ( ) => {
69- this . input1 . clear ( ) ;
70- } ;
70+ const clear = ( ) => input . current ?. clear ( ) ;
7171
72- updateOtpText = ( ) => {
73- // will automatically trigger handleOnTextChange callback passed
74- this . input1 . setValue ( this . state . inputText ) ;
75- } ;
72+ const updateOtpText = ( ) => input . current ?. setValue ( otpInput ) ;
7673
77- render ( ) {
78- return (
79- < View style = { styles . container } >
80- < Text style = { styles . instructions } > react-native-otp-textinput</ Text >
74+ const showTextAlert = ( ) => otpInput && Alert . alert ( otpInput ) ;
75+
76+ return (
77+ < SafeAreaView style = { styles . safeAreaView } >
78+ < ScrollView contentContainerStyle = { styles . container } >
8179 < OTPTextView
82- ref = { e => ( this . input1 = e ) }
80+ ref = { input }
8381 containerStyle = { styles . textInputContainer }
84- handleTextChange = { text => this . setState ( { otpInput : text } ) }
82+ handleTextChange = { setOtpInput }
8583 inputCount = { 4 }
8684 keyboardType = "numeric"
8785 />
8886 < TextInput
8987 maxLength = { 4 }
90- onChangeText = { e => this . setState ( { inputText : e } ) }
88+ onChangeText = { setOtpInput }
9189 style = { styles . textInput }
9290 />
9391 < View style = { styles . buttonWrapper } >
94- < Button title = "Clear" onPress = { this . clear } />
92+ < Button title = "Clear" onPress = { clear } />
9593 < Button
96- style = { styles . buttonStyle }
9794 title = "Update"
98- onPress = { this . updateOtpText }
95+ onPress = { updateOtpText }
9996 />
10097 < Button
101- style = { styles . buttonStyle }
10298 title = "Submit"
103- onPress = { this . alertText }
99+ onPress = { showTextAlert }
104100 />
105101 </ View >
106102 < Text style = { styles . instructions } > Customizations</ Text >
107103 < OTPTextView
108- handleTextChange = { e => { } }
104+ handleTextChange = { ( ) => { } }
109105 containerStyle = { styles . textInputContainer }
110106 textInputStyle = { styles . roundedTextInput }
111107 inputCount = { 5 }
112108 inputCellLength = { 2 }
113109 />
114110 < OTPTextView
115- handleTextChange = { e => { } }
111+ handleTextChange = { ( ) => { } }
116112 containerStyle = { styles . textInputContainer }
117113 textInputStyle = { styles . roundedTextInput }
118114 defaultValue = "1234"
119115 />
120116 < OTPTextView
121- handleTextChange = { e => { } }
117+ handleTextChange = { ( ) => { } }
122118 containerStyle = { styles . textInputContainer }
123- textInputStyle = { [ styles . roundedTextInput , { borderRadius : 100 } ] }
119+ // textInputStyle={[styles.roundedTextInput, { borderRadius: 100 }]}
124120 tintColor = "#000"
125121 />
122+ < TextInput />
126123 < OTPTextView
127- handleTextChange = { e => { } }
124+ handleTextChange = { ( ) => { } }
128125 containerStyle = { styles . textInputContainer }
129126 tintColor = { [ '#FF0000' , '#FFFF00' , '#00FF00' , '#0000FF' ] }
130127 />
131128 < OTPTextView
132- handleTextChange = { e => { } }
129+ handleTextChange = { ( ) => { } }
133130 containerStyle = { styles . textInputContainer }
134131 tintColor = "#000"
135132 offTintColor = { [ '#FF0000' , '#FFFF00' , '#00FF00' , '#0000FF' ] }
136133 />
137- </ View >
138- ) ;
139- }
134+ </ ScrollView >
135+ </ SafeAreaView >
136+ )
140137}
138+
139+ export default App ;
0 commit comments