@siamf/otp-timer 4.0.1 → 5.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/README.md +122 -43
- 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/components/OtpTimers.d.ts +15 -0
- package/dist/cjs/components/OtpTimers.js +38 -0
- package/dist/cjs/components/OtpTimers.js.map +1 -0
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +5 -3
- 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/components/OtpTimers.d.ts +15 -0
- package/dist/esm/components/OtpTimers.js +36 -0
- package/dist/esm/components/OtpTimers.js.map +1 -0
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/components/Otptimer.d.ts +0 -23
- package/dist/cjs/components/Otptimer.js +0 -51
- package/dist/cjs/components/Otptimer.js.map +0 -1
- package/dist/esm/components/Otptimer.d.ts +0 -23
- package/dist/esm/components/Otptimer.js +0 -46
- package/dist/esm/components/Otptimer.js.map +0 -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
|
|
|
@@ -42,7 +45,15 @@ const MyComponent = () => {
|
|
|
42
45
|
|
|
43
46
|
return (
|
|
44
47
|
<div>
|
|
45
|
-
<Otptimer
|
|
48
|
+
<Otptimer
|
|
49
|
+
minutes={0}
|
|
50
|
+
seconds={30}
|
|
51
|
+
onResend={handleResend}
|
|
52
|
+
renderText={(props) => <p>You can resend code in {props.minutes} minutes and {props.seconds} seconds</p>}
|
|
53
|
+
renderButton={(props) => <button {...props}>
|
|
54
|
+
Send Code Again
|
|
55
|
+
</button>}
|
|
56
|
+
/>
|
|
46
57
|
</div>
|
|
47
58
|
);
|
|
48
59
|
};
|
|
@@ -50,77 +61,145 @@ const MyComponent = () => {
|
|
|
50
61
|
export default MyComponent;
|
|
51
62
|
```
|
|
52
63
|
|
|
53
|
-
|
|
64
|
+
### `OtpInput` Component
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import { useState } from "react";
|
|
68
|
+
import { OtpInput } from "@siamf/otp-timer";
|
|
69
|
+
|
|
70
|
+
export default function App() {
|
|
71
|
+
const [otp, setOtp] = useState<string>(");
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<OtpInput
|
|
75
|
+
value={otp}
|
|
76
|
+
onChange={setOtp}
|
|
77
|
+
numInputs={4}
|
|
78
|
+
renderSeparator={<span>-</span>}
|
|
79
|
+
renderInput={(props) => <input {...props} />}
|
|
80
|
+
/>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
# Customization
|
|
86
|
+
|
|
87
|
+
## `OtpTimer` component
|
|
88
|
+
|
|
89
|
+
### Options
|
|
54
90
|
|
|
55
91
|
|name|Description|Default Value|
|
|
56
92
|
|:---:| :-------------:|:-----------:|
|
|
57
93
|
|seconds|number of seconds for which timer must be set|30|
|
|
58
94
|
|minutes|number of minutes for which the timer must be set|0|
|
|
59
95
|
|onResend|function that would get triggered on clicking the resend button|n/a|
|
|
60
|
-
|
|
|
61
|
-
|
|
|
62
|
-
|showSpinner|If you want to show any button loading|false|
|
|
63
|
-
|fetching|If you want to show any button loading icon with fetching state|false|
|
|
64
|
-
|spinnerComponent|Your custom spinner component for render|null|
|
|
96
|
+
|renderText|Customizable text content(See Example Code)|Required|
|
|
97
|
+
|renderButton|Customizable button content(See Example Code)|Required|
|
|
65
98
|
|
|
66
|
-
|
|
67
|
-
## Styling
|
|
99
|
+
## `OtpInput` component
|
|
68
100
|
|
|
69
101
|
<table>
|
|
70
102
|
<tr>
|
|
71
|
-
<th>
|
|
72
|
-
<th>
|
|
73
|
-
<th>
|
|
103
|
+
<th>Name<br/></th>
|
|
104
|
+
<th>Type</th>
|
|
105
|
+
<th>Required</th>
|
|
106
|
+
<th>Default</th>
|
|
107
|
+
<th>Description</th>
|
|
108
|
+
</tr>
|
|
109
|
+
<tr>
|
|
110
|
+
<td>numInputs</td>
|
|
111
|
+
<td>number</td>
|
|
112
|
+
<td>true</td>
|
|
113
|
+
<td>4</td>
|
|
114
|
+
<td>Number of OTP inputs to be rendered.</td>
|
|
74
115
|
</tr>
|
|
75
116
|
<tr>
|
|
76
|
-
<td>
|
|
77
|
-
<td>
|
|
78
|
-
<td>
|
|
117
|
+
<td>renderInput</td>
|
|
118
|
+
<td>function</td>
|
|
119
|
+
<td>true</td>
|
|
120
|
+
<td>none</td>
|
|
121
|
+
<td>A function that returns the input that is supposed to be rendered for each of the input fields.
|
|
122
|
+
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.
|
|
123
|
+
</td>
|
|
79
124
|
</tr>
|
|
80
125
|
<tr>
|
|
81
|
-
<td>
|
|
82
|
-
<td>
|
|
83
|
-
<td>
|
|
126
|
+
<td>onChange</td>
|
|
127
|
+
<td>function</td>
|
|
128
|
+
<td>true</td>
|
|
129
|
+
<td>console.log</td>
|
|
130
|
+
<td>Returns OTP code typed in inputs.</td>
|
|
84
131
|
</tr>
|
|
85
132
|
<tr>
|
|
86
|
-
<td>
|
|
87
|
-
<td>
|
|
88
|
-
<td>
|
|
133
|
+
<td>onPaste</td>
|
|
134
|
+
<td>function</td>
|
|
135
|
+
<td>false</td>
|
|
136
|
+
<td>none</td>
|
|
137
|
+
<td>Provide a custom onPaste event handler scoped to the OTP inputs container. Executes when content is pasted into any OTP field.
|
|
138
|
+
</br></br>
|
|
139
|
+
Example:
|
|
140
|
+
<pre>
|
|
141
|
+
const handlePaste: React.ClipboardEventHandler<HTMLDivElement> = (event) => {
|
|
142
|
+
const data = event.clipboardData.getData('text');
|
|
143
|
+
console.log(data)
|
|
144
|
+
};</pre>
|
|
145
|
+
|
|
146
|
+
</td>
|
|
89
147
|
</tr>
|
|
90
148
|
<tr>
|
|
91
|
-
<td>
|
|
92
|
-
<td>
|
|
93
|
-
<td>
|
|
149
|
+
<td>value</td>
|
|
150
|
+
<td>string / number</td>
|
|
151
|
+
<td>true</td>
|
|
152
|
+
<td>''</td>
|
|
153
|
+
<td>The value of the OTP passed into the component.</td>
|
|
94
154
|
</tr>
|
|
155
|
+
<tr>
|
|
156
|
+
<td>placeholder</td>
|
|
157
|
+
<td>string</td>
|
|
158
|
+
<td>false</td>
|
|
159
|
+
<td>none</td>
|
|
160
|
+
<td>Specify an expected value of each input. The length of this string should be equal to <code>numInputs</code>.</td>
|
|
161
|
+
</tr>
|
|
95
162
|
<tr>
|
|
96
|
-
<td>
|
|
97
|
-
<td>
|
|
98
|
-
<td>
|
|
163
|
+
<td>renderSeparator</td>
|
|
164
|
+
<td>component / function<br/></td>
|
|
165
|
+
<td>false</td>
|
|
166
|
+
<td>none</td>
|
|
167
|
+
<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.
|
|
99
168
|
</tr>
|
|
100
169
|
<tr>
|
|
101
|
-
<td>
|
|
102
|
-
<td>
|
|
103
|
-
<td>
|
|
170
|
+
<td>containerStyle</td>
|
|
171
|
+
<td>style (object) / className (string)</td>
|
|
172
|
+
<td>false</td>
|
|
173
|
+
<td>none</td>
|
|
174
|
+
<td>Style applied or class passed to container of inputs.</td>
|
|
104
175
|
</tr>
|
|
105
176
|
<tr>
|
|
106
|
-
<td>
|
|
107
|
-
<td>
|
|
108
|
-
<td>
|
|
177
|
+
<td>inputStyle</td>
|
|
178
|
+
<td>style (object) / className (string)</td>
|
|
179
|
+
<td>false</td>
|
|
180
|
+
<td>none</td>
|
|
181
|
+
<td>Style applied or class passed to each input.</td>
|
|
109
182
|
</tr>
|
|
110
183
|
<tr>
|
|
111
|
-
<td>
|
|
112
|
-
<td
|
|
113
|
-
<td>
|
|
184
|
+
<td>inputType</td>
|
|
185
|
+
<td><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#input_types"><input> type<a></td>
|
|
186
|
+
<td>false</td>
|
|
187
|
+
<td>text</td>
|
|
188
|
+
<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>
|
|
114
189
|
</tr>
|
|
115
190
|
<tr>
|
|
116
|
-
<td>
|
|
117
|
-
<td>
|
|
118
|
-
<td>
|
|
191
|
+
<td>shouldAutoFocus</td>
|
|
192
|
+
<td>boolean</td>
|
|
193
|
+
<td>false</td>
|
|
194
|
+
<td>false</td>
|
|
195
|
+
<td>Auto focuses input on initial page load.</td>
|
|
119
196
|
</tr>
|
|
120
197
|
<tr>
|
|
121
|
-
<td>
|
|
122
|
-
<td>
|
|
123
|
-
<td>
|
|
198
|
+
<td>skipDefaultStyles</td>
|
|
199
|
+
<td>boolean</td>
|
|
200
|
+
<td>false</td>
|
|
201
|
+
<td>false</td>
|
|
202
|
+
<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>
|
|
124
203
|
</tr>
|
|
125
204
|
</table>
|
|
126
205
|
|
|
@@ -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,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AAC,CAAC;AAGF,kBAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
interface OtpTimerProps {
|
|
3
|
+
minutes?: number;
|
|
4
|
+
seconds?: number;
|
|
5
|
+
onResend: () => void;
|
|
6
|
+
renderText: (props: {
|
|
7
|
+
minutes: number;
|
|
8
|
+
seconds: number;
|
|
9
|
+
}) => React.ReactNode;
|
|
10
|
+
renderButton: (props: {
|
|
11
|
+
onClick: () => void;
|
|
12
|
+
}) => React.ReactElement<HTMLButtonElement>;
|
|
13
|
+
}
|
|
14
|
+
declare const OtpTimer: ({ minutes: initialMinutes, seconds: initialSeconds, onResend, renderText, renderButton, }: OtpTimerProps) => import("react/jsx-runtime").JSX.Element;
|
|
15
|
+
export default OtpTimer;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const OtpTimer = ({ minutes: initialMinutes = 0, seconds: initialSeconds = 30, onResend, renderText, renderButton, }) => {
|
|
6
|
+
//Error Throwing
|
|
7
|
+
if (initialSeconds > 59)
|
|
8
|
+
throw new Error("Seconds cannot be greater than 59");
|
|
9
|
+
if (initialMinutes > 59)
|
|
10
|
+
throw new Error("Minutes cannot be greater than 59");
|
|
11
|
+
//State
|
|
12
|
+
const [minutes, setMinutes] = (0, react_1.useState)(initialMinutes);
|
|
13
|
+
const [seconds, setSeconds] = (0, react_1.useState)(initialSeconds);
|
|
14
|
+
(0, react_1.useEffect)(() => {
|
|
15
|
+
if (minutes === 0 && seconds === 0)
|
|
16
|
+
return;
|
|
17
|
+
const interval = setInterval(() => {
|
|
18
|
+
if (seconds > 0) {
|
|
19
|
+
setSeconds((prev) => prev - 1);
|
|
20
|
+
}
|
|
21
|
+
else if (minutes > 0) {
|
|
22
|
+
setMinutes((prev) => prev - 1);
|
|
23
|
+
setSeconds(59);
|
|
24
|
+
}
|
|
25
|
+
}, 1000);
|
|
26
|
+
return () => clearInterval(interval);
|
|
27
|
+
}, [minutes, seconds]);
|
|
28
|
+
const handleResend = () => {
|
|
29
|
+
setMinutes(initialMinutes);
|
|
30
|
+
setSeconds(initialSeconds);
|
|
31
|
+
onResend();
|
|
32
|
+
};
|
|
33
|
+
return ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: minutes === 0 && seconds === 0
|
|
34
|
+
? renderButton({ onClick: handleResend })
|
|
35
|
+
: renderText({ minutes, seconds }) }));
|
|
36
|
+
};
|
|
37
|
+
exports.default = OtpTimer;
|
|
38
|
+
//# sourceMappingURL=OtpTimers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OtpTimers.js","sourceRoot":"","sources":["../../../src/components/OtpTimers.tsx"],"names":[],"mappings":";;;AAAA,iCAA6D;AAU7D,MAAM,QAAQ,GAAG,CAAC,EAAE,OAAO,EAAE,cAAc,GAAG,CAAC,EAAE,OAAO,EAAE,cAAc,GAAG,EAAE,EAAE,QAAQ,EAAE,UAAU,EAAE,YAAY,GAAkB,EAAE,EAAE;IACnI,gBAAgB;IAChB,IAAI,cAAc,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAC9E,IAAI,cAAc,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAE9E,OAAO;IACP,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,IAAA,gBAAQ,EAAC,cAAc,CAAC,CAAC;IACvD,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,IAAA,gBAAQ,EAAC,cAAc,CAAC,CAAC;IAEvD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACX,IAAI,OAAO,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC;YAAE,OAAO;QAE3C,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE;YAC9B,IAAI,OAAO,GAAG,CAAC,EAAE,CAAC;gBACd,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;YACnC,CAAC;iBAAM,IAAI,OAAO,GAAG,CAAC,EAAE,CAAC;gBACrB,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;gBAC/B,UAAU,CAAC,EAAE,CAAC,CAAC;YACnB,CAAC;QACL,CAAC,EAAE,IAAI,CAAC,CAAC;QAET,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IACzC,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAEvB,MAAM,YAAY,GAAG,GAAG,EAAE;QACtB,UAAU,CAAC,cAAc,CAAC,CAAC;QAC3B,UAAU,CAAC,cAAc,CAAC,CAAC;QAC3B,QAAQ,EAAE,CAAC;IACf,CAAC,CAAC;IAEF,OAAO,CACH,uBAAC,gBAAQ,cACJ,OAAO,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC;YAC3B,CAAC,CAAC,YAAY,CAAC,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;YACzC,CAAC,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,GAC/B,CACd,CAAC;AACN,CAAC,CAAC;AAEF,kBAAe,QAAQ,CAAC"}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { default as
|
|
1
|
+
export { default as OtpTimer } from "./components/OtpTimers";
|
|
2
|
+
export { default as OtpInput } from "./components/OtpInput";
|
package/dist/cjs/index.js
CHANGED
|
@@ -3,7 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
var
|
|
8
|
-
Object.defineProperty(exports, "
|
|
6
|
+
exports.OtpInput = exports.OtpTimer = void 0;
|
|
7
|
+
var OtpTimers_1 = require("./components/OtpTimers");
|
|
8
|
+
Object.defineProperty(exports, "OtpTimer", { enumerable: true, get: function () { return __importDefault(OtpTimers_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,oDAA6D;AAApD,sHAAA,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,MAAM,WAAW,GAAG,GAAG,E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ACA,CACT,CAAC;AACN,CAAC,CAAC;AAGF,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
interface OtpTimerProps {
|
|
3
|
+
minutes?: number;
|
|
4
|
+
seconds?: number;
|
|
5
|
+
onResend: () => void;
|
|
6
|
+
renderText: (props: {
|
|
7
|
+
minutes: number;
|
|
8
|
+
seconds: number;
|
|
9
|
+
}) => React.ReactNode;
|
|
10
|
+
renderButton: (props: {
|
|
11
|
+
onClick: () => void;
|
|
12
|
+
}) => React.ReactElement<HTMLButtonElement>;
|
|
13
|
+
}
|
|
14
|
+
declare const OtpTimer: ({ minutes: initialMinutes, seconds: initialSeconds, onResend, renderText, renderButton, }: OtpTimerProps) => import("react/jsx-runtime").JSX.Element;
|
|
15
|
+
export default OtpTimer;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState, Fragment } from "react";
|
|
3
|
+
const OtpTimer = ({ minutes: initialMinutes = 0, seconds: initialSeconds = 30, onResend, renderText, renderButton, }) => {
|
|
4
|
+
//Error Throwing
|
|
5
|
+
if (initialSeconds > 59)
|
|
6
|
+
throw new Error("Seconds cannot be greater than 59");
|
|
7
|
+
if (initialMinutes > 59)
|
|
8
|
+
throw new Error("Minutes cannot be greater than 59");
|
|
9
|
+
//State
|
|
10
|
+
const [minutes, setMinutes] = useState(initialMinutes);
|
|
11
|
+
const [seconds, setSeconds] = useState(initialSeconds);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (minutes === 0 && seconds === 0)
|
|
14
|
+
return;
|
|
15
|
+
const interval = setInterval(() => {
|
|
16
|
+
if (seconds > 0) {
|
|
17
|
+
setSeconds((prev) => prev - 1);
|
|
18
|
+
}
|
|
19
|
+
else if (minutes > 0) {
|
|
20
|
+
setMinutes((prev) => prev - 1);
|
|
21
|
+
setSeconds(59);
|
|
22
|
+
}
|
|
23
|
+
}, 1000);
|
|
24
|
+
return () => clearInterval(interval);
|
|
25
|
+
}, [minutes, seconds]);
|
|
26
|
+
const handleResend = () => {
|
|
27
|
+
setMinutes(initialMinutes);
|
|
28
|
+
setSeconds(initialSeconds);
|
|
29
|
+
onResend();
|
|
30
|
+
};
|
|
31
|
+
return (_jsx(Fragment, { children: minutes === 0 && seconds === 0
|
|
32
|
+
? renderButton({ onClick: handleResend })
|
|
33
|
+
: renderText({ minutes, seconds }) }));
|
|
34
|
+
};
|
|
35
|
+
export default OtpTimer;
|
|
36
|
+
//# sourceMappingURL=OtpTimers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OtpTimers.js","sourceRoot":"","sources":["../../../src/components/OtpTimers.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAU7D,MAAM,QAAQ,GAAG,CAAC,EAAE,OAAO,EAAE,cAAc,GAAG,CAAC,EAAE,OAAO,EAAE,cAAc,GAAG,EAAE,EAAE,QAAQ,EAAE,UAAU,EAAE,YAAY,GAAkB,EAAE,EAAE;IACnI,gBAAgB;IAChB,IAAI,cAAc,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAC9E,IAAI,cAAc,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAE9E,OAAO;IACP,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,cAAc,CAAC,CAAC;IACvD,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,cAAc,CAAC,CAAC;IAEvD,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,OAAO,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC;YAAE,OAAO;QAE3C,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE;YAC9B,IAAI,OAAO,GAAG,CAAC,EAAE,CAAC;gBACd,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;YACnC,CAAC;iBAAM,IAAI,OAAO,GAAG,CAAC,EAAE,CAAC;gBACrB,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;gBAC/B,UAAU,CAAC,EAAE,CAAC,CAAC;YACnB,CAAC;QACL,CAAC,EAAE,IAAI,CAAC,CAAC;QAET,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IACzC,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAEvB,MAAM,YAAY,GAAG,GAAG,EAAE;QACtB,UAAU,CAAC,cAAc,CAAC,CAAC;QAC3B,UAAU,CAAC,cAAc,CAAC,CAAC;QAC3B,QAAQ,EAAE,CAAC;IACf,CAAC,CAAC;IAEF,OAAO,CACH,KAAC,QAAQ,cACJ,OAAO,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC;YAC3B,CAAC,CAAC,YAAY,CAAC,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;YACzC,CAAC,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,GAC/B,CACd,CAAC;AACN,CAAC,CAAC;AAEF,eAAe,QAAQ,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { default as
|
|
1
|
+
export { default as OtpTimer } from "./components/OtpTimers";
|
|
2
|
+
export { default as OtpInput } from "./components/OtpInput";
|
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,wBAAwB,CAAC;AAC7D,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,uBAAuB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
interface Props {
|
|
3
|
-
seconds?: number;
|
|
4
|
-
minutes?: number;
|
|
5
|
-
text?: string;
|
|
6
|
-
buttonText?: string;
|
|
7
|
-
containerClass?: string;
|
|
8
|
-
buttonContainerClass?: string;
|
|
9
|
-
textClass?: string;
|
|
10
|
-
timerClass?: string;
|
|
11
|
-
buttonStyle?: React.CSSProperties;
|
|
12
|
-
buttonClass?: string;
|
|
13
|
-
textStyle?: React.CSSProperties;
|
|
14
|
-
timerStyle?: React.CSSProperties;
|
|
15
|
-
buttonContainerStyle?: React.CSSProperties;
|
|
16
|
-
textContainerStyle?: React.CSSProperties;
|
|
17
|
-
onResend: () => void;
|
|
18
|
-
fetching?: boolean;
|
|
19
|
-
showSpinner?: boolean;
|
|
20
|
-
spinnerComponent?: React.ReactNode;
|
|
21
|
-
}
|
|
22
|
-
declare const Otptimer: (props: Props) => import("react/jsx-runtime").JSX.Element;
|
|
23
|
-
export default Otptimer;
|
|
@@ -1,51 +0,0 @@
|
|
|
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 Otptimer = (props) => {
|
|
9
|
-
//From props
|
|
10
|
-
const { text = "Resend otp in", containerClass = "otp-timer-container", buttonText = "Resend", textClass = "otp-text", buttonContainerClass = "button-container", timerClass = "otp-timer", buttonStyle, buttonContainerStyle, timerStyle, textStyle, textContainerStyle, onResend, fetching = false, buttonClass, showSpinner = false, spinnerComponent } = props;
|
|
11
|
-
//State
|
|
12
|
-
const [minutes, setMinutes] = react_1.default.useState(props.minutes ? props.minutes : 0);
|
|
13
|
-
const [seconds, setSeconds] = react_1.default.useState(props.seconds ? props.seconds : 30);
|
|
14
|
-
//Handler
|
|
15
|
-
let myInterval;
|
|
16
|
-
const onTimeHandler = () => {
|
|
17
|
-
myInterval = setInterval(() => {
|
|
18
|
-
if (seconds > 0) {
|
|
19
|
-
setSeconds(seconds - 1);
|
|
20
|
-
}
|
|
21
|
-
if (seconds === 0) {
|
|
22
|
-
if (minutes === 0) {
|
|
23
|
-
clearInterval(myInterval);
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
setMinutes(minutes - 1);
|
|
27
|
-
setSeconds(59);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}, 1000);
|
|
31
|
-
};
|
|
32
|
-
//Effect
|
|
33
|
-
react_1.default.useEffect(() => {
|
|
34
|
-
onTimeHandler();
|
|
35
|
-
return () => clearInterval(myInterval);
|
|
36
|
-
});
|
|
37
|
-
//Handler
|
|
38
|
-
const resendHandler = () => {
|
|
39
|
-
setSeconds(props.seconds ? props.seconds : 30);
|
|
40
|
-
setMinutes(props.minutes ? props.minutes : 0);
|
|
41
|
-
onTimeHandler();
|
|
42
|
-
onResend();
|
|
43
|
-
};
|
|
44
|
-
//Styling
|
|
45
|
-
const ButtonStyles = Object.assign({ border: "none", background: "none", cursor: "pointer", padding: 0, margin: 0 }, buttonStyle);
|
|
46
|
-
const textContainerStyles = Object.assign({ fontSize: "15px" }, textContainerStyle);
|
|
47
|
-
return ((0, jsx_runtime_1.jsx)("div", { children: minutes === 0 && seconds === 0 ? ((0, jsx_runtime_1.jsx)("div", { className: buttonContainerClass, style: buttonContainerStyle, children: (0, jsx_runtime_1.jsxs)("button", { onClick: resendHandler, className: buttonClass, style: ButtonStyles, children: [(0, jsx_runtime_1.jsx)("span", { children: buttonText }), showSpinner && fetching &&
|
|
48
|
-
spinnerComponent] }) })) : ((0, jsx_runtime_1.jsxs)("div", { className: containerClass, style: textContainerStyles, children: [(0, jsx_runtime_1.jsx)("span", { className: textClass, style: textStyle, children: text }), " ", (0, jsx_runtime_1.jsxs)("span", { className: timerClass, style: timerStyle, children: [(0, jsx_runtime_1.jsx)("span", { children: minutes < 10 ? `0${minutes}` : minutes }), ":", (0, jsx_runtime_1.jsx)("span", { children: seconds < 10 ? `0${seconds}` : seconds })] })] })) }));
|
|
49
|
-
};
|
|
50
|
-
exports.default = Otptimer;
|
|
51
|
-
//# sourceMappingURL=Otptimer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Otptimer.js","sourceRoot":"","sources":["../../../src/components/Otptimer.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAwB1B,MAAM,QAAQ,GAAG,CAAC,KAAY,EAAE,EAAE;IAC9B,YAAY;IACZ,MAAM,EAAE,IAAI,GAAG,eAAe,EAAE,cAAc,GAAG,qBAAqB,EAAE,UAAU,GAAG,QAAQ,EAAE,SAAS,GAAG,UAAU,EAAE,oBAAoB,GAAG,kBAAkB,EAAE,UAAU,GAAG,WAAW,EAAE,WAAW,EAAE,oBAAoB,EAAE,UAAU,EAAE,SAAS,EAAE,kBAAkB,EAAE,QAAQ,EAAE,QAAQ,GAAG,KAAK,EAAE,WAAW,EAAE,WAAW,GAAG,KAAK,EAAE,gBAAgB,EAAE,GAAG,KAAK,CAAC;IACnW,OAAO;IACP,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,eAAK,CAAC,QAAQ,CAAS,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACvF,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,eAAK,CAAC,QAAQ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EAAE,CAAC,EACV,MAAM,EAAE,CAAC,IACN,WAAW,CACjB,CAAA;IACD,MAAM,mBAAmB,mBACrB,QAAQ,EAAE,MAAM,IACb,kBAAkB,CACxB,CAAA;IACD,OAAO,CACH,0CACK,OAAO,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAC9B,gCAAK,SAAS,EAAE,oBAAoB,EAAE,KAAK,EAAE,oBAAoB,YAC7D,oCAAQ,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,EAAE,YAAY,aACvE,2CAAO,UAAU,GAAQ,EACxB,WAAW,IAAI,QAAQ;wBACpB,gBAAgB,IAEf,GACP,CACT,CAAC,CAAC,CAAC,CACA,iCAAK,SAAS,EAAE,cAAc,EAAE,KAAK,EAAE,mBAAmB,aACtD,iCAAM,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,YAAG,IAAI,GAAQ,EAAC,GAAG,EAC/D,kCAAM,SAAS,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,aAC1C,2CAAO,OAAO,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,OAAO,EAAE,CAAC,CAAC,CAAC,OAAO,GAAQ,OACrD,2CAAO,OAAO,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,OAAO,EAAE,CAAC,CAAC,CAAC,OAAO,GAAQ,IAClD,IACL,CACT,GACC,CACT,CAAC;AACN,CAAC,CAAC;AACF,kBAAe,QAAQ,CAAC"}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
interface Props {
|
|
3
|
-
seconds?: number;
|
|
4
|
-
minutes?: number;
|
|
5
|
-
text?: string;
|
|
6
|
-
buttonText?: string;
|
|
7
|
-
containerClass?: string;
|
|
8
|
-
buttonContainerClass?: string;
|
|
9
|
-
textClass?: string;
|
|
10
|
-
timerClass?: string;
|
|
11
|
-
buttonStyle?: React.CSSProperties;
|
|
12
|
-
buttonClass?: string;
|
|
13
|
-
textStyle?: React.CSSProperties;
|
|
14
|
-
timerStyle?: React.CSSProperties;
|
|
15
|
-
buttonContainerStyle?: React.CSSProperties;
|
|
16
|
-
textContainerStyle?: React.CSSProperties;
|
|
17
|
-
onResend: () => void;
|
|
18
|
-
fetching?: boolean;
|
|
19
|
-
showSpinner?: boolean;
|
|
20
|
-
spinnerComponent?: React.ReactNode;
|
|
21
|
-
}
|
|
22
|
-
declare const Otptimer: (props: Props) => import("react/jsx-runtime").JSX.Element;
|
|
23
|
-
export default Otptimer;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import React from 'react';
|
|
3
|
-
const Otptimer = (props) => {
|
|
4
|
-
//From props
|
|
5
|
-
const { text = "Resend otp in", containerClass = "otp-timer-container", buttonText = "Resend", textClass = "otp-text", buttonContainerClass = "button-container", timerClass = "otp-timer", buttonStyle, buttonContainerStyle, timerStyle, textStyle, textContainerStyle, onResend, fetching = false, buttonClass, showSpinner = false, spinnerComponent } = props;
|
|
6
|
-
//State
|
|
7
|
-
const [minutes, setMinutes] = React.useState(props.minutes ? props.minutes : 0);
|
|
8
|
-
const [seconds, setSeconds] = React.useState(props.seconds ? props.seconds : 30);
|
|
9
|
-
//Handler
|
|
10
|
-
let myInterval;
|
|
11
|
-
const onTimeHandler = () => {
|
|
12
|
-
myInterval = setInterval(() => {
|
|
13
|
-
if (seconds > 0) {
|
|
14
|
-
setSeconds(seconds - 1);
|
|
15
|
-
}
|
|
16
|
-
if (seconds === 0) {
|
|
17
|
-
if (minutes === 0) {
|
|
18
|
-
clearInterval(myInterval);
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
setMinutes(minutes - 1);
|
|
22
|
-
setSeconds(59);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}, 1000);
|
|
26
|
-
};
|
|
27
|
-
//Effect
|
|
28
|
-
React.useEffect(() => {
|
|
29
|
-
onTimeHandler();
|
|
30
|
-
return () => clearInterval(myInterval);
|
|
31
|
-
});
|
|
32
|
-
//Handler
|
|
33
|
-
const resendHandler = () => {
|
|
34
|
-
setSeconds(props.seconds ? props.seconds : 30);
|
|
35
|
-
setMinutes(props.minutes ? props.minutes : 0);
|
|
36
|
-
onTimeHandler();
|
|
37
|
-
onResend();
|
|
38
|
-
};
|
|
39
|
-
//Styling
|
|
40
|
-
const ButtonStyles = Object.assign({ border: "none", background: "none", cursor: "pointer", padding: 0, margin: 0 }, buttonStyle);
|
|
41
|
-
const textContainerStyles = Object.assign({ fontSize: "15px" }, textContainerStyle);
|
|
42
|
-
return (_jsx("div", { children: minutes === 0 && seconds === 0 ? (_jsx("div", { className: buttonContainerClass, style: buttonContainerStyle, children: _jsxs("button", { onClick: resendHandler, className: buttonClass, style: ButtonStyles, children: [_jsx("span", { children: buttonText }), showSpinner && fetching &&
|
|
43
|
-
spinnerComponent] }) })) : (_jsxs("div", { className: containerClass, style: textContainerStyles, children: [_jsx("span", { className: textClass, style: textStyle, children: text }), " ", _jsxs("span", { className: timerClass, style: timerStyle, children: [_jsx("span", { children: minutes < 10 ? `0${minutes}` : minutes }), ":", _jsx("span", { children: seconds < 10 ? `0${seconds}` : seconds })] })] })) }));
|
|
44
|
-
};
|
|
45
|
-
export default Otptimer;
|
|
46
|
-
//# sourceMappingURL=Otptimer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Otptimer.js","sourceRoot":"","sources":["../../../src/components/Otptimer.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAwB1B,MAAM,QAAQ,GAAG,CAAC,KAAY,EAAE,EAAE;IAC9B,YAAY;IACZ,MAAM,EAAE,IAAI,GAAG,eAAe,EAAE,cAAc,GAAG,qBAAqB,EAAE,UAAU,GAAG,QAAQ,EAAE,SAAS,GAAG,UAAU,EAAE,oBAAoB,GAAG,kBAAkB,EAAE,UAAU,GAAG,WAAW,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