react-native-otp-fields 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-otp-fields",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "A customizable React Native OTP input component with smooth focus handling, digit-only validation, and easy integration for verification codes, PINs, or passcodes.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,108 @@
1
+ import React, { useRef, useState } from 'react';
2
+ import {
3
+ View,
4
+ TextInput,
5
+ StyleSheet,
6
+ ViewStyle,
7
+ TextStyle,
8
+ TextInputKeyPressEvent,
9
+ } from 'react-native';
10
+
11
+ interface OtpInputProps {
12
+ length?: number;
13
+ onOtpComplete?: (otp: string) => void;
14
+ inputStyle?: TextStyle;
15
+ containerStyle?: ViewStyle;
16
+ };
17
+
18
+ export default function OtpInput({
19
+ length = 6,
20
+ onOtpComplete,
21
+ inputStyle,
22
+ containerStyle,
23
+ }: OtpInputProps) {
24
+ const [otp, setOtp] = useState<string[]>(Array(length).fill(''));
25
+ const inputsRef = useRef<Array<TextInput | null>>([]);
26
+
27
+ const handleChangeText = (text: string, index: number) => {
28
+ const newOtp = [...otp];
29
+ const char = text.slice(-1);
30
+ const digitRegex = /^\d$/;
31
+
32
+ if (digitRegex.test(char)) {
33
+ newOtp[index] = char;
34
+ setOtp(newOtp);
35
+
36
+ if (index < length - 1) {
37
+ inputsRef.current[index + 1]?.focus();
38
+ }
39
+
40
+ if (newOtp.every(d => d !== '')) {
41
+ onOtpComplete?.(newOtp.join(''));
42
+ }
43
+ } else if (text === '') {
44
+ newOtp[index] = '';
45
+ setOtp(newOtp);
46
+ }
47
+ };
48
+
49
+ const handleKeyPress = ({ nativeEvent }: TextInputKeyPressEvent, index: number) => {
50
+ if (nativeEvent.key === 'Backspace') {
51
+ setOtp((prevOtp) => {
52
+ const newOtp = [...prevOtp];
53
+
54
+ if (newOtp[index] === '') {
55
+ if (index > 0) {
56
+ newOtp[index - 1] = '';
57
+ inputsRef.current[index - 1]?.focus();
58
+ }
59
+ } else {
60
+ newOtp[index] = '';
61
+ inputsRef.current[index]?.focus();
62
+ }
63
+
64
+ return newOtp;
65
+ });
66
+ }
67
+ };
68
+
69
+
70
+ return (
71
+ <View style={[styles.container, containerStyle]}>
72
+ {Array.from({ length }).map((_, i) => (
73
+ <TextInput
74
+ key={i}
75
+ // @ts-ignore
76
+ ref={ref => (inputsRef.current[i] = ref)}
77
+ value={otp[i]}
78
+ onChangeText={text => handleChangeText(text, i)}
79
+ onKeyPress={e => handleKeyPress(e, i)}
80
+ keyboardType="number-pad"
81
+ maxLength={1}
82
+ style={[styles.input, inputStyle]}
83
+ returnKeyType="done"
84
+ autoFocus={i === 0}
85
+ />
86
+ ))}
87
+ </View>
88
+ );
89
+ };
90
+
91
+ const styles = StyleSheet.create({
92
+ container: {
93
+ flexDirection: 'row',
94
+ gap: 12,
95
+ justifyContent: 'center',
96
+ padding: 20,
97
+ },
98
+ input: {
99
+ width: 48,
100
+ height: 58,
101
+ borderWidth: 1,
102
+ borderColor: '#ccc',
103
+ borderRadius: 8,
104
+ textAlign: 'center',
105
+ fontSize: 20,
106
+ backgroundColor: '#fff',
107
+ },
108
+ });
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default as OtpInput } from "./OtpInput"