Skip to content

Commit 79b24d7

Browse files
Merge pull request #9 from KaboCash/v1.1
added support for mobile keyboard
2 parents ee53a3f + 05d78f2 commit 79b24d7

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

src/App.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useState } from "react";
21
import OTPInputGroup from "./otpInputs";
32

43
function App() {
@@ -40,20 +39,13 @@ function App() {
4039
}
4140
}
4241

43-
const [test, setTest] = useState('')
44-
4542
return (
4643
<div className="app" style={styles.app}>
4744
<div style={styles.formWrapper}>
48-
<p>
49-
{
50-
test
51-
}
52-
</p>
5345
<form method="post">
5446
<h2 style={styles.title}>Enter verification code</h2>
5547
<div style={styles.notification}>Check your inbox for a verification code and enter it below</div>
56-
<OTPInputGroup setTest={setTest} autoSubmit={true} />
48+
<OTPInputGroup autoSubmit={true} />
5749
</form>
5850
</div>
5951
</div>

src/otpInputs.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { forwardRef, useRef, useState } from "react";
22
import styles from './otpInputs.module.css'
33

44
//Our parent component
5-
const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
5+
const OTPInputGroup = ({ autoSubmit = false }) => {
66
//state to store all input boxes
77
const [inputValues, setInputValues] = useState({
88
input1: '',
@@ -55,7 +55,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
5555
handleSubmit={handleSubmit}
5656
autoSubmit={autoSubmit}
5757
nextRef={input2}
58-
setTest={setTest}
5958
/>
6059
<OTPInput
6160
id="input2"
@@ -67,7 +66,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
6766
handleSubmit={handleSubmit}
6867
autoSubmit={autoSubmit}
6968
nextRef={input3}
70-
setTest={setTest}
7169
/>
7270
<OTPInput
7371
id="input3"
@@ -79,7 +77,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
7977
handleSubmit={handleSubmit}
8078
autoSubmit={autoSubmit}
8179
nextRef={input4}
82-
setTest={setTest}
8380
/>
8481
{/* Seperator */}
8582
<span className={styles.seperator}>&ndash;</span>
@@ -94,7 +91,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
9491
handleSubmit={handleSubmit}
9592
autoSubmit={autoSubmit}
9693
nextRef={input5}
97-
setTest={setTest}
9894
/>
9995
<OTPInput
10096
id="input5"
@@ -106,7 +102,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
106102
handleSubmit={handleSubmit}
107103
autoSubmit={autoSubmit}
108104
nextRef={input6}
109-
setTest={setTest}
110105
/>
111106
<OTPInput
112107
id="input6"
@@ -118,7 +113,6 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
118113
handleSubmit={handleSubmit}
119114
autoSubmit={autoSubmit}
120115
nextRef={null}
121-
setTest={setTest}
122116
/>
123117
</div>
124118
<div className="btnGroup" onClick={handleSubmit}>
@@ -131,24 +125,17 @@ const OTPInputGroup = ({ autoSubmit = false, setTest }) => {
131125
//Our child component
132126
const OTPInput = forwardRef((props, ref) => {
133127

134-
const { setTest, id, className, previousRef, nextRef, value, onValueChange, handleSubmit, autoSubmit } = props
128+
const { id, className, previousRef, nextRef, value, onValueChange, handleSubmit, autoSubmit } = props
135129

136130
//This callback function only runs when a key is released
137131
const handleKeyUp = (e) => {
138-
139-
setTest(e)
140-
141132
// Uncomment to debug the component
142133
// console.log({
143134
// current: ref,
144135
// next: nextRef,
145136
// previous: previousRef
146137
// })
147138

148-
function isAlphaNumeric(str) {
149-
return str && str.match(/[a-zA-Z0-9]/);
150-
}
151-
152139
// Check if key is backspace or arrowleft
153140
if (e.key === 'Backspace' || e.key === 'ArrowLeft') {
154141
// Find the previous element
@@ -159,11 +146,13 @@ const OTPInput = forwardRef((props, ref) => {
159146
return prev.select();
160147
}
161148
} 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-zA-Z0-9]/)) || e.keyCode === 229)) ||
166-
// Check if ArrowRight was pressed
149+
// Check for alphanumeric character input
150+
(e?.key && e?.key?.match(/[a-zA-Z0-9]/) && e?.key?.length === 1) ||
151+
// Check for some mobile keyboards that send keyCode 229
152+
(e?.keyCode === 229 && e?.target?.value?.match(/[a-zA-Z0-9]/) && e?.target?.value?.length === 1) ||
153+
// check for e.data
154+
(e?.data && e?.data?.match(/[a-zA-Z0-9]/)) ||
155+
//Check if ArrowRight was pressed
167156
e.key === 'ArrowRight'
168157
) {
169158
// Find the next element

0 commit comments

Comments
 (0)