@@ -42,8 +42,29 @@ class OTPTextView extends Component {
42
42
} ;
43
43
44
44
this . inputs = [ ] ;
45
+
46
+ this . checkTintColorCount ( ) ;
45
47
}
46
48
49
+ checkTintColorCount = ( ) => {
50
+ const { tintColor, offTintColor, inputCount } = this . props ;
51
+
52
+ if ( typeof tintColor !== "string" && tintColor . length !== inputCount ) {
53
+ throw new Error (
54
+ "If tint color is an array it's length should be equal to input count"
55
+ ) ;
56
+ }
57
+
58
+ if (
59
+ typeof offTintColor !== "string" &&
60
+ offTintColor . length !== inputCount
61
+ ) {
62
+ throw new Error (
63
+ "If off tint color is an array it's length should be equal to input count"
64
+ ) ;
65
+ }
66
+ } ;
67
+
47
68
basicValidation = ( text ) => {
48
69
const validText = / ^ [ 0 - 9 a - z A - Z ] + $ / ;
49
70
return text . match ( validText ) ;
@@ -92,26 +113,27 @@ class OTPTextView extends Component {
92
113
const val = this . state . otpText [ i ] || "" ;
93
114
const { handleTextChange, inputCellLength } = this . props ;
94
115
const { otpText } = this . state ;
116
+
95
117
if ( e . nativeEvent . key === "Backspace" && i !== 0 ) {
96
- if ( val . length === 0 ) {
97
- if ( otpText [ i - 1 ] . length === inputCellLength ) {
98
- this . setState (
99
- ( prevState ) => {
100
- let { otpText } = prevState ;
101
- otpText [ i - 1 ] = otpText [ i - 1 ]
102
- . split ( "" )
103
- . splice ( 0 , otpText [ i - 1 ] . length - 1 )
104
- . join ( "" ) ;
105
- return {
106
- otpText ,
107
- } ;
108
- } ,
109
- ( ) => {
110
- handleTextChange ( this . state . otpText . join ( "" ) ) ;
111
- this . inputs [ i - 1 ] . focus ( ) ;
112
- }
113
- ) ;
114
- }
118
+ if ( ! val . length && otpText [ i - 1 ] . length === inputCellLength ) {
119
+ this . setState (
120
+ ( prevState ) => {
121
+ let { otpText } = prevState ;
122
+
123
+ otpText [ i - 1 ] = otpText [ i - 1 ]
124
+ . split ( "" )
125
+ . splice ( 0 , otpText [ i - 1 ] . length - 1 )
126
+ . join ( "" ) ;
127
+
128
+ return {
129
+ otpText ,
130
+ } ;
131
+ } ,
132
+ ( ) => {
133
+ handleTextChange ( this . state . otpText . join ( "" ) ) ;
134
+ this . inputs [ i - 1 ] . focus ( ) ;
135
+ }
136
+ ) ;
115
137
}
116
138
}
117
139
} ;
@@ -169,11 +191,16 @@ class OTPTextView extends Component {
169
191
const inputStyle = [
170
192
styles . textInput ,
171
193
textInputStyle ,
172
- { borderColor : offTintColor } ,
194
+ {
195
+ borderColor :
196
+ typeof offTintColor === "string" ? offTintColor : offTintColor [ i ] ,
197
+ } ,
173
198
] ;
174
199
175
200
if ( focusedInput === i ) {
176
- inputStyle . push ( { borderColor : tintColor } ) ;
201
+ inputStyle . push ( {
202
+ borderColor : typeof tintColor === "string" ? tintColor : tintColor [ i ] ,
203
+ } ) ;
177
204
}
178
205
179
206
TextInputs . push (
@@ -208,8 +235,14 @@ OTPTextView.propTypes = {
208
235
containerStyle : PropTypes . any ,
209
236
textInputStyle : PropTypes . any ,
210
237
inputCellLength : PropTypes . number ,
211
- tintColor : PropTypes . string ,
212
- offTintColor : PropTypes . string ,
238
+ tintColor : PropTypes . oneOfType ( [
239
+ PropTypes . string ,
240
+ PropTypes . arrayOf ( PropTypes . string ) ,
241
+ ] ) ,
242
+ offTintColor : PropTypes . oneOfType ( [
243
+ PropTypes . string ,
244
+ PropTypes . arrayOf ( PropTypes . string ) ,
245
+ ] ) ,
213
246
handleTextChange : PropTypes . func ,
214
247
inputType : PropTypes . string ,
215
248
keyboardType : PropTypes . string ,
0 commit comments