react-native-otp-fields 1.0.0
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/LICENSE +21 -0
- package/README.md +7 -0
- package/dist/OtpInput.d.ts +9 -0
- package/dist/OtpInput.js +72 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +8 -0
- package/package.json +35 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 NectarByte
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
import { ViewStyle, TextStyle } from 'react-native';
|
2
|
+
interface OtpInputProps {
|
3
|
+
length?: number;
|
4
|
+
onOtpComplete?: (otp: string) => void;
|
5
|
+
inputStyle?: TextStyle;
|
6
|
+
containerStyle?: ViewStyle;
|
7
|
+
}
|
8
|
+
export default function OtpInput({ length, onOtpComplete, inputStyle, containerStyle, }: OtpInputProps): import("react/jsx-runtime").JSX.Element;
|
9
|
+
export {};
|
package/dist/OtpInput.js
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.default = OtpInput;
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
5
|
+
const react_1 = require("react");
|
6
|
+
const react_native_1 = require("react-native");
|
7
|
+
;
|
8
|
+
function OtpInput({ length = 6, onOtpComplete, inputStyle, containerStyle, }) {
|
9
|
+
const [otp, setOtp] = (0, react_1.useState)(Array(length).fill(''));
|
10
|
+
const inputsRef = (0, react_1.useRef)([]);
|
11
|
+
const handleChangeText = (text, index) => {
|
12
|
+
var _a;
|
13
|
+
const newOtp = [...otp];
|
14
|
+
const char = text.slice(-1);
|
15
|
+
const digitRegex = /^\d$/;
|
16
|
+
if (digitRegex.test(char)) {
|
17
|
+
newOtp[index] = char;
|
18
|
+
setOtp(newOtp);
|
19
|
+
if (index < length - 1) {
|
20
|
+
(_a = inputsRef.current[index + 1]) === null || _a === void 0 ? void 0 : _a.focus();
|
21
|
+
}
|
22
|
+
if (newOtp.every(d => d !== '')) {
|
23
|
+
onOtpComplete === null || onOtpComplete === void 0 ? void 0 : onOtpComplete(newOtp.join(''));
|
24
|
+
}
|
25
|
+
}
|
26
|
+
else if (text === '') {
|
27
|
+
newOtp[index] = '';
|
28
|
+
setOtp(newOtp);
|
29
|
+
}
|
30
|
+
};
|
31
|
+
const handleKeyPress = ({ nativeEvent }, index) => {
|
32
|
+
if (nativeEvent.key === 'Backspace') {
|
33
|
+
setOtp((prevOtp) => {
|
34
|
+
var _a, _b;
|
35
|
+
const newOtp = [...prevOtp];
|
36
|
+
if (newOtp[index] === '') {
|
37
|
+
if (index > 0) {
|
38
|
+
newOtp[index - 1] = '';
|
39
|
+
(_a = inputsRef.current[index - 1]) === null || _a === void 0 ? void 0 : _a.focus();
|
40
|
+
}
|
41
|
+
}
|
42
|
+
else {
|
43
|
+
newOtp[index] = '';
|
44
|
+
(_b = inputsRef.current[index]) === null || _b === void 0 ? void 0 : _b.focus();
|
45
|
+
}
|
46
|
+
return newOtp;
|
47
|
+
});
|
48
|
+
}
|
49
|
+
};
|
50
|
+
return ((0, jsx_runtime_1.jsx)(react_native_1.View, { style: [styles.container, containerStyle], children: Array.from({ length }).map((_, i) => ((0, jsx_runtime_1.jsx)(react_native_1.TextInput, {
|
51
|
+
// @ts-ignore
|
52
|
+
ref: ref => (inputsRef.current[i] = ref), value: otp[i], onChangeText: text => handleChangeText(text, i), onKeyPress: e => handleKeyPress(e, i), keyboardType: "number-pad", maxLength: 1, style: [styles.input, inputStyle], returnKeyType: "done", autoFocus: i === 0 }, i))) }));
|
53
|
+
}
|
54
|
+
;
|
55
|
+
const styles = react_native_1.StyleSheet.create({
|
56
|
+
container: {
|
57
|
+
flexDirection: 'row',
|
58
|
+
gap: 12,
|
59
|
+
justifyContent: 'center',
|
60
|
+
padding: 20,
|
61
|
+
},
|
62
|
+
input: {
|
63
|
+
width: 48,
|
64
|
+
height: 58,
|
65
|
+
borderWidth: 1,
|
66
|
+
borderColor: '#ccc',
|
67
|
+
borderRadius: 8,
|
68
|
+
textAlign: 'center',
|
69
|
+
fontSize: 20,
|
70
|
+
backgroundColor: '#fff',
|
71
|
+
},
|
72
|
+
});
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export { default as OtpInput } from "./OtpInput";
|
package/dist/index.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.OtpInput = void 0;
|
7
|
+
var OtpInput_1 = require("./OtpInput");
|
8
|
+
Object.defineProperty(exports, "OtpInput", { enumerable: true, get: function () { return __importDefault(OtpInput_1).default; } });
|
package/package.json
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
{
|
2
|
+
"name": "react-native-otp-fields",
|
3
|
+
"version": "1.0.0",
|
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
|
+
"main": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
7
|
+
"scripts": {
|
8
|
+
"build": "tsc",
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
10
|
+
},
|
11
|
+
"author": "NectarByte",
|
12
|
+
"license": "MIT",
|
13
|
+
"repository": {
|
14
|
+
"type": "git",
|
15
|
+
"url": ""
|
16
|
+
},
|
17
|
+
"keywords": [
|
18
|
+
"react-native",
|
19
|
+
"otp",
|
20
|
+
"otp-input",
|
21
|
+
"verification-code",
|
22
|
+
"pin-input",
|
23
|
+
"react-native-component",
|
24
|
+
"digit-input"
|
25
|
+
],
|
26
|
+
"peerDependencies": {
|
27
|
+
"react": ">=18",
|
28
|
+
"react-native": ">=0.71"
|
29
|
+
},
|
30
|
+
"devDependencies": {
|
31
|
+
"@types/react": "^18.0.0",
|
32
|
+
"@types/react-native": "^0.73.0",
|
33
|
+
"typescript": "^5.9.2"
|
34
|
+
}
|
35
|
+
}
|