@siamf/otp-timer 4.0.1 → 4.5.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/README.md +138 -3
- package/dist/cjs/components/OtpInput.d.ts +25 -0
- package/dist/cjs/components/OtpInput.js +167 -0
- package/dist/cjs/components/OtpInput.js.map +1 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/components/OtpInput.d.ts +25 -0
- package/dist/esm/components/OtpInput.js +162 -0
- package/dist/esm/components/OtpInput.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
# @siamf/otp-timer
|
|
11
11
|
|
|
12
|
-
A simple and customizable react otp timer component with typescript support. very simple and more customizable design and smallest bundle size
|
|
12
|
+
A simple and customizable react otp timer & input component with typescript support. very simple and more customizable design and smallest bundle size. You can use this component into any react and nextjs projects.
|
|
13
13
|
|
|
14
14
|
<a href="https://www.buymeacoffee.com/siamahnaf" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>
|
|
15
15
|
|
|
@@ -17,6 +17,7 @@ A simple and customizable react otp timer component with typescript support. ver
|
|
|
17
17
|
- Customizable
|
|
18
18
|
- TypeScript Support
|
|
19
19
|
- Smallest Bundle Size(About 13kb)
|
|
20
|
+
- Otp Timer & Otp Input Component
|
|
20
21
|
|
|
21
22
|
|
|
22
23
|
## Installation
|
|
@@ -31,6 +32,8 @@ $ npm i @siamf/otp-timer
|
|
|
31
32
|
|
|
32
33
|
## Usage?
|
|
33
34
|
|
|
35
|
+
### `OtpTimer` Component
|
|
36
|
+
|
|
34
37
|
```javascript
|
|
35
38
|
import { Otptimer } from "@siamf/otp-timer";
|
|
36
39
|
|
|
@@ -50,7 +53,32 @@ const MyComponent = () => {
|
|
|
50
53
|
export default MyComponent;
|
|
51
54
|
```
|
|
52
55
|
|
|
53
|
-
|
|
56
|
+
### `OtpInput` Component
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
import { useState } from "react";
|
|
60
|
+
import { OtpInput } from "@siamf/otp-timer";
|
|
61
|
+
|
|
62
|
+
export default function App() {
|
|
63
|
+
const [otp, setOtp] = useState<string>(");
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<OtpInput
|
|
67
|
+
value={otp}
|
|
68
|
+
onChange={setOtp}
|
|
69
|
+
numInputs={4}
|
|
70
|
+
renderSeparator={<span>-</span>}
|
|
71
|
+
renderInput={(props) => <input {...props} />}
|
|
72
|
+
/>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
# Customization
|
|
78
|
+
|
|
79
|
+
## `OtpTimer` component
|
|
80
|
+
|
|
81
|
+
### Options
|
|
54
82
|
|
|
55
83
|
|name|Description|Default Value|
|
|
56
84
|
|:---:| :-------------:|:-----------:|
|
|
@@ -64,7 +92,7 @@ export default MyComponent;
|
|
|
64
92
|
|spinnerComponent|Your custom spinner component for render|null|
|
|
65
93
|
|
|
66
94
|
|
|
67
|
-
|
|
95
|
+
### Styling
|
|
68
96
|
|
|
69
97
|
<table>
|
|
70
98
|
<tr>
|
|
@@ -124,6 +152,113 @@ export default MyComponent;
|
|
|
124
152
|
</tr>
|
|
125
153
|
</table>
|
|
126
154
|
|
|
155
|
+
## `OtpInput` component
|
|
156
|
+
|
|
157
|
+
<table>
|
|
158
|
+
<tr>
|
|
159
|
+
<th>Name<br/></th>
|
|
160
|
+
<th>Type</th>
|
|
161
|
+
<th>Required</th>
|
|
162
|
+
<th>Default</th>
|
|
163
|
+
<th>Description</th>
|
|
164
|
+
</tr>
|
|
165
|
+
<tr>
|
|
166
|
+
<td>numInputs</td>
|
|
167
|
+
<td>number</td>
|
|
168
|
+
<td>true</td>
|
|
169
|
+
<td>4</td>
|
|
170
|
+
<td>Number of OTP inputs to be rendered.</td>
|
|
171
|
+
</tr>
|
|
172
|
+
<tr>
|
|
173
|
+
<td>renderInput</td>
|
|
174
|
+
<td>function</td>
|
|
175
|
+
<td>true</td>
|
|
176
|
+
<td>none</td>
|
|
177
|
+
<td>A function that returns the input that is supposed to be rendered for each of the input fields.
|
|
178
|
+
The function will get two arguments: <code>inputProps</code> and <code>index</code>. <code>inputProps</code> is an object that contains all the props <b>that should be passed to the input being rendered</b> (Overriding these props is not recommended because it might lead to some unexpected behaviour). <code>index</code> is the index of the input being rendered.
|
|
179
|
+
</td>
|
|
180
|
+
</tr>
|
|
181
|
+
<tr>
|
|
182
|
+
<td>onChange</td>
|
|
183
|
+
<td>function</td>
|
|
184
|
+
<td>true</td>
|
|
185
|
+
<td>console.log</td>
|
|
186
|
+
<td>Returns OTP code typed in inputs.</td>
|
|
187
|
+
</tr>
|
|
188
|
+
<tr>
|
|
189
|
+
<td>onPaste</td>
|
|
190
|
+
<td>function</td>
|
|
191
|
+
<td>false</td>
|
|
192
|
+
<td>none</td>
|
|
193
|
+
<td>Provide a custom onPaste event handler scoped to the OTP inputs container. Executes when content is pasted into any OTP field.
|
|
194
|
+
</br></br>
|
|
195
|
+
Example:
|
|
196
|
+
<pre>
|
|
197
|
+
const handlePaste: React.ClipboardEventHandler<HTMLDivElement> = (event) => {
|
|
198
|
+
const data = event.clipboardData.getData('text');
|
|
199
|
+
console.log(data)
|
|
200
|
+
};</pre>
|
|
201
|
+
|
|
202
|
+
</td>
|
|
203
|
+
</tr>
|
|
204
|
+
<tr>
|
|
205
|
+
<td>value</td>
|
|
206
|
+
<td>string / number</td>
|
|
207
|
+
<td>true</td>
|
|
208
|
+
<td>''</td>
|
|
209
|
+
<td>The value of the OTP passed into the component.</td>
|
|
210
|
+
</tr>
|
|
211
|
+
<tr>
|
|
212
|
+
<td>placeholder</td>
|
|
213
|
+
<td>string</td>
|
|
214
|
+
<td>false</td>
|
|
215
|
+
<td>none</td>
|
|
216
|
+
<td>Specify an expected value of each input. The length of this string should be equal to <code>numInputs</code>.</td>
|
|
217
|
+
</tr>
|
|
218
|
+
<tr>
|
|
219
|
+
<td>renderSeparator</td>
|
|
220
|
+
<td>component / function<br/></td>
|
|
221
|
+
<td>false</td>
|
|
222
|
+
<td>none</td>
|
|
223
|
+
<td>Provide a custom separator between inputs by passing a component. For instance, <code><span>-</span></code> would add <code>-</code> between each input.</td> You can also pass a function that returns a component, where the function will get the index of the separator being rendered as an argument.
|
|
224
|
+
</tr>
|
|
225
|
+
<tr>
|
|
226
|
+
<td>containerStyle</td>
|
|
227
|
+
<td>style (object) / className (string)</td>
|
|
228
|
+
<td>false</td>
|
|
229
|
+
<td>none</td>
|
|
230
|
+
<td>Style applied or class passed to container of inputs.</td>
|
|
231
|
+
</tr>
|
|
232
|
+
<tr>
|
|
233
|
+
<td>inputStyle</td>
|
|
234
|
+
<td>style (object) / className (string)</td>
|
|
235
|
+
<td>false</td>
|
|
236
|
+
<td>none</td>
|
|
237
|
+
<td>Style applied or class passed to each input.</td>
|
|
238
|
+
</tr>
|
|
239
|
+
<tr>
|
|
240
|
+
<td>inputType</td>
|
|
241
|
+
<td><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#input_types"><input> type<a></td>
|
|
242
|
+
<td>false</td>
|
|
243
|
+
<td>text</td>
|
|
244
|
+
<td>The type of the input that will be passed to the input element being rendered. In v2 <code>isInputNum</code> used to set the input type as <code>tel</code> and prevented non numerical entries, so as to avoid the spin buttons added to the inputs with input type <code>number</code>. That behaviour is still supported if you pass <code>tel</code> to the inputType prop.</td>
|
|
245
|
+
</tr>
|
|
246
|
+
<tr>
|
|
247
|
+
<td>shouldAutoFocus</td>
|
|
248
|
+
<td>boolean</td>
|
|
249
|
+
<td>false</td>
|
|
250
|
+
<td>false</td>
|
|
251
|
+
<td>Auto focuses input on initial page load.</td>
|
|
252
|
+
</tr>
|
|
253
|
+
<tr>
|
|
254
|
+
<td>skipDefaultStyles</td>
|
|
255
|
+
<td>boolean</td>
|
|
256
|
+
<td>false</td>
|
|
257
|
+
<td>false</td>
|
|
258
|
+
<td>The component comes with default styles for legacy reasons. Pass <code>true</code> to skip those styles. This prop will be removed in the next major release.</td>
|
|
259
|
+
</tr>
|
|
260
|
+
</table>
|
|
261
|
+
|
|
127
262
|
## Issues
|
|
128
263
|
|
|
129
264
|
You are welcome to create an issue.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
type AllowedInputTypes = "password" | "text" | "number" | "tel";
|
|
3
|
+
type InputProps = Required<Pick<React.InputHTMLAttributes<HTMLInputElement>, "value" | "onChange" | "onFocus" | "onBlur" | "onKeyDown" | "onPaste" | "aria-label" | "autoComplete" | "style" | "inputMode" | "onInput"> & {
|
|
4
|
+
ref: React.RefCallback<HTMLInputElement>;
|
|
5
|
+
placeholder: string | undefined;
|
|
6
|
+
className: string | undefined;
|
|
7
|
+
type: AllowedInputTypes;
|
|
8
|
+
}>;
|
|
9
|
+
interface OTPInputProps {
|
|
10
|
+
value?: string;
|
|
11
|
+
numInputs?: number;
|
|
12
|
+
onChange: (otp: string) => void;
|
|
13
|
+
onPaste?: (event: React.ClipboardEvent<HTMLDivElement>) => void;
|
|
14
|
+
renderInput: (inputProps: InputProps, index: number) => React.ReactNode;
|
|
15
|
+
shouldAutoFocus?: boolean;
|
|
16
|
+
placeholder?: string;
|
|
17
|
+
renderSeparator?: ((index: number) => React.ReactNode) | React.ReactNode;
|
|
18
|
+
containerStyle?: React.CSSProperties | string;
|
|
19
|
+
inputStyle?: React.CSSProperties | string;
|
|
20
|
+
inputType?: AllowedInputTypes;
|
|
21
|
+
skipDefaultStyles?: boolean;
|
|
22
|
+
}
|
|
23
|
+
declare const OtpInput: ({ value, numInputs, onChange, onPaste, renderInput, shouldAutoFocus, inputType, renderSeparator, placeholder, containerStyle, inputStyle, skipDefaultStyles, }: OTPInputProps) => import("react/jsx-runtime").JSX.Element;
|
|
24
|
+
export type { OTPInputProps, InputProps, AllowedInputTypes };
|
|
25
|
+
export default OtpInput;
|
|
@@ -0,0 +1,167 @@
|
|
|
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
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const isStyleObject = (obj) => typeof obj === "object" && obj !== null;
|
|
9
|
+
const OtpInput = ({ value = "", numInputs = 4, onChange, onPaste, renderInput, shouldAutoFocus = false, inputType = "text", renderSeparator, placeholder, containerStyle, inputStyle, skipDefaultStyles = false, }) => {
|
|
10
|
+
const [activeInput, setActiveInput] = react_1.default.useState(0);
|
|
11
|
+
const inputRefs = react_1.default.useRef([]);
|
|
12
|
+
const getOTPValue = () => (value ? value.toString().split("") : []);
|
|
13
|
+
const isInputNum = inputType === "number" || inputType === "tel";
|
|
14
|
+
react_1.default.useEffect(() => {
|
|
15
|
+
inputRefs.current = inputRefs.current.slice(0, numInputs);
|
|
16
|
+
}, [numInputs]);
|
|
17
|
+
react_1.default.useEffect(() => {
|
|
18
|
+
var _a;
|
|
19
|
+
if (shouldAutoFocus) {
|
|
20
|
+
(_a = inputRefs.current[0]) === null || _a === void 0 ? void 0 : _a.focus();
|
|
21
|
+
}
|
|
22
|
+
}, [shouldAutoFocus]);
|
|
23
|
+
const getPlaceholderValue = () => {
|
|
24
|
+
if (typeof placeholder === "string") {
|
|
25
|
+
if (placeholder.length === numInputs) {
|
|
26
|
+
return placeholder;
|
|
27
|
+
}
|
|
28
|
+
if (placeholder.length > 0) {
|
|
29
|
+
console.error("Length of the placeholder should be equal to the number of inputs.");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
};
|
|
34
|
+
const isInputValueValid = (value) => {
|
|
35
|
+
const isTypeValid = isInputNum ? !isNaN(Number(value)) : typeof value === "string";
|
|
36
|
+
return isTypeValid && value.trim().length === 1;
|
|
37
|
+
};
|
|
38
|
+
const handleChange = (event) => {
|
|
39
|
+
const { value } = event.target;
|
|
40
|
+
if (isInputValueValid(value)) {
|
|
41
|
+
changeCodeAtFocus(value);
|
|
42
|
+
focusInput(activeInput + 1);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const handleInputChange = (event) => {
|
|
46
|
+
const { nativeEvent } = event;
|
|
47
|
+
const value = event.target.value;
|
|
48
|
+
if (!isInputValueValid(value)) {
|
|
49
|
+
if (value.length === numInputs) {
|
|
50
|
+
const hasInvalidInput = value.split("").some((cellInput) => !isInputValueValid(cellInput));
|
|
51
|
+
if (!hasInvalidInput) {
|
|
52
|
+
handleOTPChange(value.split(""));
|
|
53
|
+
focusInput(numInputs - 1);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// @ts-expect-error - This was added previously to handle and edge case
|
|
57
|
+
// for dealing with keyCode "229 Unidentified" on Android. Check if this is
|
|
58
|
+
// still needed.
|
|
59
|
+
if (nativeEvent.data === null && nativeEvent.inputType === "deleteContentBackward") {
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
changeCodeAtFocus("");
|
|
62
|
+
focusInput(activeInput - 1);
|
|
63
|
+
}
|
|
64
|
+
event.target.value = "";
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const handleFocus = (event) => (index) => {
|
|
68
|
+
setActiveInput(index);
|
|
69
|
+
event.target.select();
|
|
70
|
+
};
|
|
71
|
+
const handleBlur = () => {
|
|
72
|
+
setActiveInput(activeInput - 1);
|
|
73
|
+
};
|
|
74
|
+
const handleKeyDown = (event) => {
|
|
75
|
+
const otp = getOTPValue();
|
|
76
|
+
if ([event.code, event.key].includes("Backspace")) {
|
|
77
|
+
event.preventDefault();
|
|
78
|
+
changeCodeAtFocus("");
|
|
79
|
+
focusInput(activeInput - 1);
|
|
80
|
+
}
|
|
81
|
+
else if (event.code === "Delete") {
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
changeCodeAtFocus("");
|
|
84
|
+
}
|
|
85
|
+
else if (event.code === "ArrowLeft") {
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
focusInput(activeInput - 1);
|
|
88
|
+
}
|
|
89
|
+
else if (event.code === "ArrowRight") {
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
focusInput(activeInput + 1);
|
|
92
|
+
}
|
|
93
|
+
else if (event.key === otp[activeInput]) {
|
|
94
|
+
event.preventDefault();
|
|
95
|
+
focusInput(activeInput + 1);
|
|
96
|
+
}
|
|
97
|
+
else if (event.code === "Spacebar" ||
|
|
98
|
+
event.code === "Space" ||
|
|
99
|
+
event.code === "ArrowUp" ||
|
|
100
|
+
event.code === "ArrowDown") {
|
|
101
|
+
event.preventDefault();
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const focusInput = (index) => {
|
|
105
|
+
var _a, _b;
|
|
106
|
+
const activeInput = Math.max(Math.min(numInputs - 1, index), 0);
|
|
107
|
+
if (inputRefs.current[activeInput]) {
|
|
108
|
+
(_a = inputRefs.current[activeInput]) === null || _a === void 0 ? void 0 : _a.focus();
|
|
109
|
+
(_b = inputRefs.current[activeInput]) === null || _b === void 0 ? void 0 : _b.select();
|
|
110
|
+
setActiveInput(activeInput);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
const changeCodeAtFocus = (value) => {
|
|
114
|
+
const otp = getOTPValue();
|
|
115
|
+
otp[activeInput] = value[0];
|
|
116
|
+
handleOTPChange(otp);
|
|
117
|
+
};
|
|
118
|
+
const handleOTPChange = (otp) => {
|
|
119
|
+
const otpValue = otp.join("");
|
|
120
|
+
onChange(otpValue);
|
|
121
|
+
};
|
|
122
|
+
const handlePaste = (event) => {
|
|
123
|
+
var _a;
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
const otp = getOTPValue();
|
|
126
|
+
let nextActiveInput = activeInput;
|
|
127
|
+
const pastedData = event.clipboardData
|
|
128
|
+
.getData("text/plain")
|
|
129
|
+
.slice(0, numInputs - activeInput)
|
|
130
|
+
.split("");
|
|
131
|
+
if (isInputNum && pastedData.some((value) => isNaN(Number(value)))) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
for (let pos = 0; pos < numInputs; ++pos) {
|
|
135
|
+
if (pos >= activeInput && pastedData.length > 0) {
|
|
136
|
+
otp[pos] = (_a = pastedData.shift()) !== null && _a !== void 0 ? _a : "";
|
|
137
|
+
nextActiveInput++;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
focusInput(nextActiveInput);
|
|
141
|
+
handleOTPChange(otp);
|
|
142
|
+
};
|
|
143
|
+
return ((0, jsx_runtime_1.jsx)("div", { style: Object.assign({ display: "flex", alignItems: "center" }, isStyleObject(containerStyle) && containerStyle), className: typeof containerStyle === "string" ? containerStyle : undefined, onPaste: onPaste, children: Array.from({ length: numInputs }, (_, index) => index).map((index) => {
|
|
144
|
+
var _a, _b, _c;
|
|
145
|
+
return ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [renderInput({
|
|
146
|
+
value: (_a = getOTPValue()[index]) !== null && _a !== void 0 ? _a : "",
|
|
147
|
+
placeholder: (_c = (_b = getPlaceholderValue()) === null || _b === void 0 ? void 0 : _b[index]) !== null && _c !== void 0 ? _c : undefined,
|
|
148
|
+
ref: (element) => {
|
|
149
|
+
inputRefs.current[index] = element;
|
|
150
|
+
},
|
|
151
|
+
onChange: handleChange,
|
|
152
|
+
onFocus: (event) => handleFocus(event)(index),
|
|
153
|
+
onBlur: handleBlur,
|
|
154
|
+
onKeyDown: handleKeyDown,
|
|
155
|
+
onPaste: handlePaste,
|
|
156
|
+
autoComplete: "off",
|
|
157
|
+
"aria-label": `Please enter OTP character ${index + 1}`,
|
|
158
|
+
style: Object.assign(!skipDefaultStyles ? { width: "1em", textAlign: "center" } : {}, isStyleObject(inputStyle) ? inputStyle : {}),
|
|
159
|
+
className: typeof inputStyle === "string" ? inputStyle : undefined,
|
|
160
|
+
type: inputType,
|
|
161
|
+
inputMode: isInputNum ? "numeric" : "text",
|
|
162
|
+
onInput: handleInputChange,
|
|
163
|
+
}, index), index < numInputs - 1 && (typeof renderSeparator === "function" ? renderSeparator(index) : renderSeparator)] }, index));
|
|
164
|
+
}) }));
|
|
165
|
+
};
|
|
166
|
+
exports.default = OtpInput;
|
|
167
|
+
//# sourceMappingURL=OtpInput.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OtpInput.js","sourceRoot":"","sources":["../../../src/components/OtpInput.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAyC1B,MAAM,aAAa,GAAG,CAAC,GAAY,EAAE,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,CAAC;AAEhF,MAAM,QAAQ,GAAG,CAAC,EACd,KAAK,GAAG,EAAE,EACV,SAAS,GAAG,CAAC,EACb,QAAQ,EACR,OAAO,EACP,WAAW,EACX,eAAe,GAAG,KAAK,EACvB,SAAS,GAAG,MAAM,EAClB,eAAe,EACf,WAAW,EACX,cAAc,EACd,UAAU,EACV,iBAAiB,GAAG,KAAK,GACb,EAAE,EAAE;IAChB,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,eAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACxD,MAAM,SAAS,GAAG,eAAK,CAAC,MAAM,CAAiC,EAAE,CAAC,CAAC;IAEnE,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAEpE,MAAM,UAAU,GAAG,SAAS,KAAK,QAAQ,IAAI,SAA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&& mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Otptimer = void 0;
|
|
6
|
+
exports.OtpInput = exports.Otptimer = void 0;
|
|
7
7
|
var Otptimer_1 = require("./components/Otptimer");
|
|
8
8
|
Object.defineProperty(exports, "Otptimer", { enumerable: true, get: function () { return __importDefault(Otptimer_1).default; } });
|
|
9
|
+
var OtpInput_1 = require("./components/OtpInput");
|
|
10
|
+
Object.defineProperty(exports, "OtpInput", { enumerable: true, get: function () { return __importDefault(OtpInput_1).default; } });
|
|
9
11
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA4D;AAAnD,qHAAA,OAAO,OAAY"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA4D;AAAnD,qHAAA,OAAO,OAAY;AAC5B,kDAA4D;AAAnD,qHAAA,OAAO,OAAY"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
type AllowedInputTypes = "password" | "text" | "number" | "tel";
|
|
3
|
+
type InputProps = Required<Pick<React.InputHTMLAttributes<HTMLInputElement>, "value" | "onChange" | "onFocus" | "onBlur" | "onKeyDown" | "onPaste" | "aria-label" | "autoComplete" | "style" | "inputMode" | "onInput"> & {
|
|
4
|
+
ref: React.RefCallback<HTMLInputElement>;
|
|
5
|
+
placeholder: string | undefined;
|
|
6
|
+
className: string | undefined;
|
|
7
|
+
type: AllowedInputTypes;
|
|
8
|
+
}>;
|
|
9
|
+
interface OTPInputProps {
|
|
10
|
+
value?: string;
|
|
11
|
+
numInputs?: number;
|
|
12
|
+
onChange: (otp: string) => void;
|
|
13
|
+
onPaste?: (event: React.ClipboardEvent<HTMLDivElement>) => void;
|
|
14
|
+
renderInput: (inputProps: InputProps, index: number) => React.ReactNode;
|
|
15
|
+
shouldAutoFocus?: boolean;
|
|
16
|
+
placeholder?: string;
|
|
17
|
+
renderSeparator?: ((index: number) => React.ReactNode) | React.ReactNode;
|
|
18
|
+
containerStyle?: React.CSSProperties | string;
|
|
19
|
+
inputStyle?: React.CSSProperties | string;
|
|
20
|
+
inputType?: AllowedInputTypes;
|
|
21
|
+
skipDefaultStyles?: boolean;
|
|
22
|
+
}
|
|
23
|
+
declare const OtpInput: ({ value, numInputs, onChange, onPaste, renderInput, shouldAutoFocus, inputType, renderSeparator, placeholder, containerStyle, inputStyle, skipDefaultStyles, }: OTPInputProps) => import("react/jsx-runtime").JSX.Element;
|
|
24
|
+
export type { OTPInputProps, InputProps, AllowedInputTypes };
|
|
25
|
+
export default OtpInput;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React from "react";
|
|
3
|
+
const isStyleObject = (obj) => typeof obj === "object" && obj !== null;
|
|
4
|
+
const OtpInput = ({ value = "", numInputs = 4, onChange, onPaste, renderInput, shouldAutoFocus = false, inputType = "text", renderSeparator, placeholder, containerStyle, inputStyle, skipDefaultStyles = false, }) => {
|
|
5
|
+
const [activeInput, setActiveInput] = React.useState(0);
|
|
6
|
+
const inputRefs = React.useRef([]);
|
|
7
|
+
const getOTPValue = () => (value ? value.toString().split("") : []);
|
|
8
|
+
const isInputNum = inputType === "number" || inputType === "tel";
|
|
9
|
+
React.useEffect(() => {
|
|
10
|
+
inputRefs.current = inputRefs.current.slice(0, numInputs);
|
|
11
|
+
}, [numInputs]);
|
|
12
|
+
React.useEffect(() => {
|
|
13
|
+
var _a;
|
|
14
|
+
if (shouldAutoFocus) {
|
|
15
|
+
(_a = inputRefs.current[0]) === null || _a === void 0 ? void 0 : _a.focus();
|
|
16
|
+
}
|
|
17
|
+
}, [shouldAutoFocus]);
|
|
18
|
+
const getPlaceholderValue = () => {
|
|
19
|
+
if (typeof placeholder === "string") {
|
|
20
|
+
if (placeholder.length === numInputs) {
|
|
21
|
+
return placeholder;
|
|
22
|
+
}
|
|
23
|
+
if (placeholder.length > 0) {
|
|
24
|
+
console.error("Length of the placeholder should be equal to the number of inputs.");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
};
|
|
29
|
+
const isInputValueValid = (value) => {
|
|
30
|
+
const isTypeValid = isInputNum ? !isNaN(Number(value)) : typeof value === "string";
|
|
31
|
+
return isTypeValid && value.trim().length === 1;
|
|
32
|
+
};
|
|
33
|
+
const handleChange = (event) => {
|
|
34
|
+
const { value } = event.target;
|
|
35
|
+
if (isInputValueValid(value)) {
|
|
36
|
+
changeCodeAtFocus(value);
|
|
37
|
+
focusInput(activeInput + 1);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const handleInputChange = (event) => {
|
|
41
|
+
const { nativeEvent } = event;
|
|
42
|
+
const value = event.target.value;
|
|
43
|
+
if (!isInputValueValid(value)) {
|
|
44
|
+
if (value.length === numInputs) {
|
|
45
|
+
const hasInvalidInput = value.split("").some((cellInput) => !isInputValueValid(cellInput));
|
|
46
|
+
if (!hasInvalidInput) {
|
|
47
|
+
handleOTPChange(value.split(""));
|
|
48
|
+
focusInput(numInputs - 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// @ts-expect-error - This was added previously to handle and edge case
|
|
52
|
+
// for dealing with keyCode "229 Unidentified" on Android. Check if this is
|
|
53
|
+
// still needed.
|
|
54
|
+
if (nativeEvent.data === null && nativeEvent.inputType === "deleteContentBackward") {
|
|
55
|
+
event.preventDefault();
|
|
56
|
+
changeCodeAtFocus("");
|
|
57
|
+
focusInput(activeInput - 1);
|
|
58
|
+
}
|
|
59
|
+
event.target.value = "";
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const handleFocus = (event) => (index) => {
|
|
63
|
+
setActiveInput(index);
|
|
64
|
+
event.target.select();
|
|
65
|
+
};
|
|
66
|
+
const handleBlur = () => {
|
|
67
|
+
setActiveInput(activeInput - 1);
|
|
68
|
+
};
|
|
69
|
+
const handleKeyDown = (event) => {
|
|
70
|
+
const otp = getOTPValue();
|
|
71
|
+
if ([event.code, event.key].includes("Backspace")) {
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
changeCodeAtFocus("");
|
|
74
|
+
focusInput(activeInput - 1);
|
|
75
|
+
}
|
|
76
|
+
else if (event.code === "Delete") {
|
|
77
|
+
event.preventDefault();
|
|
78
|
+
changeCodeAtFocus("");
|
|
79
|
+
}
|
|
80
|
+
else if (event.code === "ArrowLeft") {
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
focusInput(activeInput - 1);
|
|
83
|
+
}
|
|
84
|
+
else if (event.code === "ArrowRight") {
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
focusInput(activeInput + 1);
|
|
87
|
+
}
|
|
88
|
+
else if (event.key === otp[activeInput]) {
|
|
89
|
+
event.preventDefault();
|
|
90
|
+
focusInput(activeInput + 1);
|
|
91
|
+
}
|
|
92
|
+
else if (event.code === "Spacebar" ||
|
|
93
|
+
event.code === "Space" ||
|
|
94
|
+
event.code === "ArrowUp" ||
|
|
95
|
+
event.code === "ArrowDown") {
|
|
96
|
+
event.preventDefault();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const focusInput = (index) => {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
const activeInput = Math.max(Math.min(numInputs - 1, index), 0);
|
|
102
|
+
if (inputRefs.current[activeInput]) {
|
|
103
|
+
(_a = inputRefs.current[activeInput]) === null || _a === void 0 ? void 0 : _a.focus();
|
|
104
|
+
(_b = inputRefs.current[activeInput]) === null || _b === void 0 ? void 0 : _b.select();
|
|
105
|
+
setActiveInput(activeInput);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const changeCodeAtFocus = (value) => {
|
|
109
|
+
const otp = getOTPValue();
|
|
110
|
+
otp[activeInput] = value[0];
|
|
111
|
+
handleOTPChange(otp);
|
|
112
|
+
};
|
|
113
|
+
const handleOTPChange = (otp) => {
|
|
114
|
+
const otpValue = otp.join("");
|
|
115
|
+
onChange(otpValue);
|
|
116
|
+
};
|
|
117
|
+
const handlePaste = (event) => {
|
|
118
|
+
var _a;
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
const otp = getOTPValue();
|
|
121
|
+
let nextActiveInput = activeInput;
|
|
122
|
+
const pastedData = event.clipboardData
|
|
123
|
+
.getData("text/plain")
|
|
124
|
+
.slice(0, numInputs - activeInput)
|
|
125
|
+
.split("");
|
|
126
|
+
if (isInputNum && pastedData.some((value) => isNaN(Number(value)))) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
for (let pos = 0; pos < numInputs; ++pos) {
|
|
130
|
+
if (pos >= activeInput && pastedData.length > 0) {
|
|
131
|
+
otp[pos] = (_a = pastedData.shift()) !== null && _a !== void 0 ? _a : "";
|
|
132
|
+
nextActiveInput++;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
focusInput(nextActiveInput);
|
|
136
|
+
handleOTPChange(otp);
|
|
137
|
+
};
|
|
138
|
+
return (_jsx("div", { style: Object.assign({ display: "flex", alignItems: "center" }, isStyleObject(containerStyle) && containerStyle), className: typeof containerStyle === "string" ? containerStyle : undefined, onPaste: onPaste, children: Array.from({ length: numInputs }, (_, index) => index).map((index) => {
|
|
139
|
+
var _a, _b, _c;
|
|
140
|
+
return (_jsxs(React.Fragment, { children: [renderInput({
|
|
141
|
+
value: (_a = getOTPValue()[index]) !== null && _a !== void 0 ? _a : "",
|
|
142
|
+
placeholder: (_c = (_b = getPlaceholderValue()) === null || _b === void 0 ? void 0 : _b[index]) !== null && _c !== void 0 ? _c : undefined,
|
|
143
|
+
ref: (element) => {
|
|
144
|
+
inputRefs.current[index] = element;
|
|
145
|
+
},
|
|
146
|
+
onChange: handleChange,
|
|
147
|
+
onFocus: (event) => handleFocus(event)(index),
|
|
148
|
+
onBlur: handleBlur,
|
|
149
|
+
onKeyDown: handleKeyDown,
|
|
150
|
+
onPaste: handlePaste,
|
|
151
|
+
autoComplete: "off",
|
|
152
|
+
"aria-label": `Please enter OTP character ${index + 1}`,
|
|
153
|
+
style: Object.assign(!skipDefaultStyles ? { width: "1em", textAlign: "center" } : {}, isStyleObject(inputStyle) ? inputStyle : {}),
|
|
154
|
+
className: typeof inputStyle === "string" ? inputStyle : undefined,
|
|
155
|
+
type: inputType,
|
|
156
|
+
inputMode: isInputNum ? "numeric" : "text",
|
|
157
|
+
onInput: handleInputChange,
|
|
158
|
+
}, index), index < numInputs - 1 && (typeof renderSeparator === "function" ? renderSeparator(index) : renderSeparator)] }, index));
|
|
159
|
+
}) }));
|
|
160
|
+
};
|
|
161
|
+
export default OtpInput;
|
|
162
|
+
//# sourceMappingURL=OtpInput.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OtpInput.js","sourceRoot":"","sources":["../../../src/components/OtpInput.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAyC1B,MAAM,aAAa,GAAG,CAAC,GAAY,EAAE,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,CAAC;AAEhF,MAAM,QAAQ,GAAG,CAAC,EACd,KAAK,GAAG,EAAE,EACV,SAAS,GAAG,CAAC,EACb,QAAQ,EACR,OAAO,EACP,WAAW,EACX,eAAe,GAAG,KAAK,EACvB,SAAS,GAAG,MAAM,EAClB,eAAe,EACf,WAAW,EACX,cAAc,EACd,UAAU,EACV,iBAAiB,GAAG,KAAK,GACb,EAAE,EAAE;IAChB,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACxD,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAiC,EAAE,CAAC,CAAC;IAEnE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,CACpB,CAAA;SAAA,CAAC,GACA,CACT,CAAC;AACN,CAAC,CAAC;AAGF,eAAe,QAAQ,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,uBAAuB,CAAC"}
|
package/package.json
CHANGED