@@ -2,7 +2,7 @@ import React, { forwardRef, useRef, useState } from "react";
2
2
import styles from './otpInputs.module.css'
3
3
4
4
//Our parent component
5
- const OTPInputGroup = ( { autoSubmit = false , setTest } ) => {
5
+ const OTPInputGroup = ( { autoSubmit = false } ) => {
6
6
//state to store all input boxes
7
7
const [ inputValues , setInputValues ] = useState ( {
8
8
input1 : '' ,
@@ -55,7 +55,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
55
55
handleSubmit = { handleSubmit }
56
56
autoSubmit = { autoSubmit }
57
57
nextRef = { input2 }
58
- setTest = { setTest }
59
58
/>
60
59
< OTPInput
61
60
id = "input2"
@@ -67,7 +66,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
67
66
handleSubmit = { handleSubmit }
68
67
autoSubmit = { autoSubmit }
69
68
nextRef = { input3 }
70
- setTest = { setTest }
71
69
/>
72
70
< OTPInput
73
71
id = "input3"
@@ -79,7 +77,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
79
77
handleSubmit = { handleSubmit }
80
78
autoSubmit = { autoSubmit }
81
79
nextRef = { input4 }
82
- setTest = { setTest }
83
80
/>
84
81
{ /* Seperator */ }
85
82
< span className = { styles . seperator } > –</ span >
@@ -94,7 +91,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
94
91
handleSubmit = { handleSubmit }
95
92
autoSubmit = { autoSubmit }
96
93
nextRef = { input5 }
97
- setTest = { setTest }
98
94
/>
99
95
< OTPInput
100
96
id = "input5"
@@ -106,7 +102,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
106
102
handleSubmit = { handleSubmit }
107
103
autoSubmit = { autoSubmit }
108
104
nextRef = { input6 }
109
- setTest = { setTest }
110
105
/>
111
106
< OTPInput
112
107
id = "input6"
@@ -118,7 +113,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
118
113
handleSubmit = { handleSubmit }
119
114
autoSubmit = { autoSubmit }
120
115
nextRef = { null }
121
- setTest = { setTest }
122
116
/>
123
117
</ div >
124
118
< div className = "btnGroup" onClick = { handleSubmit } >
@@ -131,24 +125,17 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
131
125
//Our child component
132
126
const OTPInput = forwardRef ( ( props , ref ) => {
133
127
134
- const { setTest , id, className, previousRef, nextRef, value, onValueChange, handleSubmit, autoSubmit } = props
128
+ const { id, className, previousRef, nextRef, value, onValueChange, handleSubmit, autoSubmit } = props
135
129
136
130
//This callback function only runs when a key is released
137
131
const handleKeyUp = ( e ) => {
138
-
139
- setTest ( e )
140
-
141
132
// Uncomment to debug the component
142
133
// console.log({
143
134
// current: ref,
144
135
// next: nextRef,
145
136
// previous: previousRef
146
137
// })
147
138
148
- function isAlphaNumeric ( str ) {
149
- return str && str . match ( / [ a - z A - Z 0 - 9 ] / ) ;
150
- }
151
-
152
139
// Check if key is backspace or arrowleft
153
140
if ( e . key === 'Backspace' || e . key === 'ArrowLeft' ) {
154
141
// Find the previous element
@@ -159,11 +146,13 @@ const OTPInput = forwardRef((props, ref) => {
159
146
return prev . select ( ) ;
160
147
}
161
148
} else if (
162
- // Check for alphanumeric character input on desktop or virtual keyboard
163
- ( ( e . inputType === "text" || e . inputType === "textInput" ) && ( isAlphaNumeric ( e . key ) || ( e . data && isAlphaNumeric ( e . data ) ) ) ) ||
164
- // Check for mobile keyboard input (including keyCode 229)
165
- ( e . inputType === "physicalKeyboard" && ( ( e . key && e . key . match ( / [ a - z A - Z 0 - 9 ] / ) ) || e . keyCode === 229 ) ) ||
166
- // Check if ArrowRight was pressed
149
+ // Check for alphanumeric character input
150
+ ( e ?. key && e ?. key ?. match ( / [ a - z A - Z 0 - 9 ] / ) && e ?. key ?. length === 1 ) ||
151
+ // Check for some mobile keyboards that send keyCode 229
152
+ ( e ?. keyCode === 229 && e ?. target ?. value ?. match ( / [ a - z A - Z 0 - 9 ] / ) && e ?. target ?. value ?. length === 1 ) ||
153
+ // check for e.data
154
+ ( e ?. data && e ?. data ?. match ( / [ a - z A - Z 0 - 9 ] / ) ) ||
155
+ //Check if ArrowRight was pressed
167
156
e . key === 'ArrowRight'
168
157
) {
169
158
// Find the next element
0 commit comments