-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
254 lines (228 loc) · 7.25 KB
/
index.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
function ___$insertStyle(css) {
if (!css) {
return;
}
if (typeof window === 'undefined') {
return;
}
var style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.innerHTML = css;
document.head.appendChild(style);
return css;
}
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
___$insertStyle(".react-otp-ui-input-container {\n display: flex;\n align-items: center;\n max-width: 100%;\n}\n.react-otp-ui-input-container .react-otp-ui-input {\n width: 50px;\n height: 50px;\n margin-right: 10px;\n outline: none;\n border-radius: 3px;\n border: 1px solid rgba(0, 0, 0, 0.3);\n text-align: center;\n font-size: 24px;\n font-weight: bold;\n}\n.react-otp-ui-input-container .react-otp-ui-input:last-child input {\n margin-right: 0;\n}\n.react-otp-ui-input-container .input-separator {\n display: inline-block;\n margin-right: 10px;\n font-size: 24px;\n font-weight: bold;\n}");
const MOVE_LEFT = "moveleft";
const MOVE_RIGHT = "move_right";
const SET_VALUE = "SET_VALUE";
const UPDATE_VALUE = "UPDATE_VALUE";
const SET_ALL_VALUES = 'SET_ALL_VALUES';
const getRandomID = () => `_${Math.random().toString(36).substr(2, 9)}`;
const getInitialState = (numberOfInputs) => {
const otpInputsData = [];
for (let i = 1; i <= numberOfInputs; i += 1) {
otpInputsData.push({
id: getRandomID(),
value: "",
});
}
return otpInputsData;
};
const otpFormReducer = (state, action) => {
const { type, value, index, isBackspace, data } = action;
const newState = [...state];
if (type === SET_VALUE) {
if (isBackspace) {
newState[index].value = "";
} else if (newState[index]) {
newState[index].value = value;
}
} else if (type === SET_ALL_VALUES) {
return data
}
return newState;
};
const OtpInput = ({
className,
changeFocusPosition,
disabled,
data,
isLastInput,
inputIndex,
onChange,
onFocus,
secureInput,
separator,
showSeparator,
}) => {
const handleOnKeyDown = (e) => {
const { key, keyCode } = e;
if (key === "Backspace") {
e.preventDefault();
changeFocusPosition(MOVE_LEFT, true);
} else if (key === "ArrowLeft") {
e.preventDefault();
changeFocusPosition(MOVE_LEFT);
} else if (key === "ArrowRight") {
e.preventDefault();
changeFocusPosition(MOVE_RIGHT);
} else if (key === "Spacebar" || key === "Space") {
e.preventDefault();
} else if (keyCode >= 48 && keyCode <= 57) {
changeFocusPosition(UPDATE_VALUE, false, e.key);
} else if (key === "Delete") {
e.preventDefault();
}
};
return (
React__default['default'].createElement('div', {className: "react-otp-input-container"}, [
React__default['default'].createElement('input', {
type: !secureInput ? "text" : "password",
onChange: (e) => onChange(e, inputIndex),
id: data.id,
maxLength: 1,
value: data.value,
key: data.id,
disabled: disabled,
onKeyDown: handleOnKeyDown,
className: `${className} react-otp-ui-input`,
onFocus: () => onFocus(inputIndex)}
),
showSeparator && !isLastInput && (
React__default['default'].createElement('span', {className: "input-separator"}, [separator && separator[0]])
)
])
);
};
const OtpForm = ({
className,
disabled,
numberOfInputs,
onChange,
secureInput,
separator,
showSeparator,
}) => {
const [inputsArray, setInputsArray] = React.useReducer(
otpFormReducer,
getInitialState(numberOfInputs)
);
const [focusId, setFocusId] = React.useState("");
const [currentFocusIndex, setCurrentFocusIndex] = React.useState(0);
const checkIsNextOrPrevPositionValid = (curPos, prevOrNext) => {
// prevOrNext - true - Previous position
// prevOrNext - false - Nextposition
if (!prevOrNext) {
return curPos + 1 <= numberOfInputs;
} else {
return curPos - 1 >= 0;
}
};
const handleOnChange = (e, inputIndex) => {
const { value } = e.target;
const updatedValue = value.replace(/[^0-9.]/g, "");
const isNextPositionValid = checkIsNextOrPrevPositionValid(inputIndex);
if (isNextPositionValid) {
setInputsArray({
type: SET_VALUE,
value: updatedValue,
index: inputIndex,
});
}
if (inputIndex !== numberOfInputs - 1) {
setCurrentFocusIndex(inputIndex + 1);
setFocusId(inputsArray[inputIndex + 1].id);
}
};
const changeFocusPosition = (dir, isBackspace, value) => {
if (isBackspace && currentFocusIndex >= 0) {
setInputsArray({
type: SET_VALUE,
index: currentFocusIndex,
isBackspace,
});
if (currentFocusIndex !== 0) {
setCurrentFocusIndex(currentFocusIndex - 1);
setFocusId(inputsArray[currentFocusIndex - 1].id);
}
} else if (dir === MOVE_RIGHT && currentFocusIndex !== numberOfInputs - 1) {
setCurrentFocusIndex(currentFocusIndex + 1);
setFocusId(inputsArray[currentFocusIndex + 1].id);
} else if (dir === MOVE_LEFT && currentFocusIndex !== 0) {
setCurrentFocusIndex(currentFocusIndex - 1);
setFocusId(inputsArray[currentFocusIndex - 1].id);
} else if (dir === UPDATE_VALUE) {
setInputsArray({
type: SET_VALUE,
index: currentFocusIndex,
value,
});
if (currentFocusIndex !== numberOfInputs - 1) {
setCurrentFocusIndex(currentFocusIndex + 1);
setFocusId(inputsArray[currentFocusIndex + 1].id);
}
}
};
const handleOnFocus = (index) => {
setCurrentFocusIndex(index);
};
React.useEffect(() => {
const el = document.getElementById(focusId);
if (el) el.focus();
}, [focusId]);
React.useEffect(() => {
const returnState = {};
let otpValue = "";
for (let i = 0; i < inputsArray.length; i += 1) {
returnState[`digit${i + 1}`] = inputsArray[i].value;
otpValue += inputsArray[i].value;
}
returnState.otpValue = otpValue;
if (onChange instanceof Function) {
onChange(returnState);
}
}, [inputsArray]);
React.useEffect(() => {
const inputs = getInitialState(numberOfInputs);
setInputsArray({
type: SET_ALL_VALUES,
data: inputs
});
}, [numberOfInputs]);
return (
React__default['default'].createElement('div', {className: "react-otp-ui-input-container"}, [
inputsArray.map((input, index) => (
OtpInput({
secureInput: secureInput,
onChange: handleOnChange,
data: input,
inputIndex: index,
className: className,
disabled: disabled,
key: input.id,
showSeparator: showSeparator,
separator: separator,
isLastInput: index === numberOfInputs - 1,
changeFocusPosition: changeFocusPosition,
onFocus: handleOnFocus}
)
))
])
);
};
OtpForm.defaultProps = {
className: "",
disabled: false,
handleOnChange: null,
numberOfInputs: 4,
secureInput: true,
separator: "-",
showSeparator: false,
};
exports.OtpInput = OtpInput;
exports['default'] = OtpForm;
//# sourceMappingURL=index.js.map