-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotp-input.tsx
150 lines (137 loc) · 3.97 KB
/
otp-input.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React, {
type FC,
type Dispatch,
useState,
useRef,
useEffect,
} from 'react';
import { Clipboard, type TextStyle } from 'react-native';
import { View, TextInput, type NativeSyntheticEvent } from 'react-native';
import type { TextInputKeyPressEventData } from 'react-native/Libraries/Components/TextInput/TextInput';
import tw from './tailwind';
import { neutral700 } from './colors';
interface OTPInputProps {
length: number;
value?: string;
onOtpChange: Dispatch<string>;
incorrect?: boolean;
textStyle?: TextStyle;
viewStyle?: string;
}
interface RefMapping {
[key: number]: TextInput | null;
}
export const OTPInput: FC<OTPInputProps> = ({
length,
value,
onOtpChange,
incorrect,
textStyle,
viewStyle,
}) => {
const refs = useRef<RefMapping>({});
const [code, setCode] = useState(Array.from({ length: length }, () => ''));
const [focusedIndex, setFocusedIndex] = useState(0);
useEffect(() => {
if (value) {
let pv = value.split('');
setCode(pv);
onOtpChange(value);
}
}, [value]);
const focusInput = (index: number) => {
if (refs.current[index] && refs.current[index]!) {
refs.current[index]!.focus();
}
};
const handlePaste = async () => {
const clipboard = await Clipboard.getString();
if (!clipboard) {
return;
}
const currentCode = clipboard.substring(0, length).split('');
const endIndex = currentCode.length - 1;
if (currentCode.length < length) {
do {
currentCode.push('');
} while (currentCode.length < length);
}
refs.current[endIndex < length - 1 ? endIndex + 1 : endIndex]?.focus();
setCode(currentCode);
onOtpChange(currentCode.join(''));
};
const handleType = (value: string, index: number) => {
const hasDeleted = !value && code[index] !== '';
const currentCode = code.map((curr, i) => {
if (i === index) {
return value;
}
return curr;
});
if (!hasDeleted && index < length - 1) {
refs.current[index + 1]?.focus();
}
setCode(currentCode);
onOtpChange(currentCode.join(''));
};
const handleChange = async (value: string, index: number) => {
onOtpChange(value);
if (value.length > 1) {
await handlePaste();
return;
}
};
const handleKeyPress = (
event: NativeSyntheticEvent<TextInputKeyPressEventData>,
index: number
) => {
if (event.nativeEvent.key === 'Backspace' && index !== 0 && !code[index]) {
refs.current[index - 1]?.focus();
}
};
return (
<View
style={tw`flex-row justify-between py-4 ${viewStyle ? viewStyle : ' '}`}
>
{Array.from({ length }, (_, i) => i).map((_, i) => {
const isFocused = i === focusedIndex;
return (
<TextInput
onFocus={() => setFocusedIndex(i)}
onKeyPress={(e) => handleKeyPress(e, i)}
ref={(el) => (refs.current[i] = el)}
key={_}
value={code[i]}
onChangeText={(value) =>
value.length > 1 ? handleChange(value, i) : handleType(value, i)
}
style={[
tw`flex-1 text-center font-body text-base h-12 m-1 rounded-md border ${
incorrect
? 'border-red-400'
: isFocused
? 'border-neutral-500'
: 'border-neutral'
} items-center justify-center self-center p-3`,
textStyle,
]}
onSelectionChange={(event) => {
if (event.nativeEvent.selection.end === 0) {
focusInput(i - 1);
}
}}
maxLength={i === 0 ? length : 1}
keyboardType="number-pad"
inputMode="numeric"
textContentType="oneTimeCode"
selectTextOnFocus
placeholder="•"
placeholderTextColor={neutral700}
textAlignVertical="center"
textAlign="center"
/>
);
})}
</View>
);
};