This repository was archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathindex.js
More file actions
218 lines (189 loc) · 5.77 KB
/
index.js
File metadata and controls
218 lines (189 loc) · 5.77 KB
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
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = require('react');
var React__default = _interopDefault(React);
var allowedCharactersValues = ['alpha', 'numeric', 'alphanumeric'];
var propsMap = {
alpha: {
type: 'text',
inputMode: 'text',
pattern: '[a-zA-Z]{1}'
},
alphanumeric: {
type: 'text',
inputMode: 'text',
pattern: '[a-zA-Z0-9]{1}'
},
numeric: {
type: 'tel',
inputMode: 'numeric',
pattern: '[0-9]{1}',
min: '0',
max: '9'
}
};
var valueValidation = {
alpha: function alpha(value) {
return /^[a-zA-Z]*$/.test(value);
},
alphanumeric: function alphanumeric(value) {
return /^[a-zA-Z0-9]*$/.test(value);
},
numeric: function numeric(value) {
return /^[0-9]*$/.test(value);
}
};
var AuthCode = React.forwardRef(function (_ref, ref) {
var _ref$allowedCharacter = _ref.allowedCharacters,
allowedCharacters = _ref$allowedCharacter === void 0 ? 'alphanumeric' : _ref$allowedCharacter,
ariaLabel = _ref.ariaLabel,
_ref$autoFocus = _ref.autoFocus,
autoFocus = _ref$autoFocus === void 0 ? true : _ref$autoFocus,
containerClassName = _ref.containerClassName,
disabled = _ref.disabled,
inputClassName = _ref.inputClassName,
_ref$isPassword = _ref.isPassword,
isPassword = _ref$isPassword === void 0 ? false : _ref$isPassword,
_ref$length = _ref.length,
length = _ref$length === void 0 ? 6 : _ref$length,
placeholder = _ref.placeholder,
onChange = _ref.onChange,
value = _ref.value;
var values = value ? value.split('') : [];
if (isNaN(length) || length < 1) {
throw new Error('Length should be a number and greater than 0');
}
if (!allowedCharactersValues.some(function (value) {
return value === allowedCharacters;
})) {
throw new Error('Invalid value for allowedCharacters. Use alpha, numeric, or alphanumeric');
}
if (value && value.length > length) {
throw new Error('Value length should not be greater than length');
}
if (value && !valueValidation[allowedCharacters](value)) {
throw new Error("Value should only contain " + allowedCharacters + " characters");
}
var inputsRef = React.useRef([]);
var inputProps = propsMap[allowedCharacters];
React.useImperativeHandle(ref, function () {
return {
focus: function focus() {
if (inputsRef.current) {
inputsRef.current[0].focus();
}
},
clear: function clear() {
if (inputsRef.current) {
for (var i = 0; i < inputsRef.current.length; i++) {
inputsRef.current[i].value = '';
}
inputsRef.current[0].focus();
}
sendResult();
}
};
});
React.useEffect(function () {
if (autoFocus) {
inputsRef.current[0].focus();
}
}, []);
var sendResult = function sendResult() {
var res = inputsRef.current.map(function (input) {
return input.value;
}).join('');
onChange && onChange(res);
};
var handleOnChange = function handleOnChange(e) {
var _e$target = e.target,
value = _e$target.value,
nextElementSibling = _e$target.nextElementSibling;
if (value.length > 1) {
e.target.value = value.charAt(0);
if (nextElementSibling !== null) {
nextElementSibling.focus();
}
} else {
if (value.match(inputProps.pattern)) {
if (nextElementSibling !== null) {
nextElementSibling.focus();
}
} else {
e.target.value = '';
}
}
sendResult();
};
var handleOnKeyDown = function handleOnKeyDown(e) {
var key = e.key;
var target = e.target;
if (key === 'Backspace') {
if (target.value === '') {
if (target.previousElementSibling !== null) {
var t = target.previousElementSibling;
t.value = '';
t.focus();
e.preventDefault();
}
} else {
target.value = '';
}
sendResult();
}
if (key === 'Tab' && target.value === '') {
e.preventDefault();
}
};
var handleOnFocus = function handleOnFocus(e) {
e.target.select();
};
var handleOnPaste = function handleOnPaste(e) {
var pastedValue = e.clipboardData.getData('Text');
var currentInput = 0;
for (var i = 0; i < pastedValue.length; i++) {
var pastedCharacter = pastedValue.charAt(i);
var currentValue = inputsRef.current[currentInput].value;
if (pastedCharacter.match(inputProps.pattern)) {
if (!currentValue) {
inputsRef.current[currentInput].value = pastedCharacter;
if (inputsRef.current[currentInput].nextElementSibling !== null) {
inputsRef.current[currentInput].nextElementSibling.focus();
currentInput++;
}
}
}
}
sendResult();
e.preventDefault();
};
var inputs = [];
var _loop = function _loop(i) {
inputs.push(React__default.createElement("input", Object.assign({
key: i,
onChange: handleOnChange,
onKeyDown: handleOnKeyDown,
onFocus: handleOnFocus,
onPaste: handleOnPaste
}, inputProps, {
type: isPassword ? 'password' : inputProps.type,
ref: function ref(el) {
inputsRef.current[i] = el;
},
maxLength: 1,
className: inputClassName,
autoComplete: i === 0 ? 'one-time-code' : 'off',
"aria-label": ariaLabel ? ariaLabel + ". Character " + (i + 1) + "." : "Character " + (i + 1) + ".",
disabled: disabled,
placeholder: placeholder,
defaultValue: values[i] || ''
})));
};
for (var i = 0; i < length; i++) {
_loop(i);
}
return React__default.createElement("div", {
className: containerClassName
}, inputs);
});
module.exports = AuthCode;
//# sourceMappingURL=index.js.map