ar-design 0.3.68 → 0.3.70
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.
|
@@ -4,18 +4,28 @@ import Input from "..";
|
|
|
4
4
|
const Otp = ({ character, onChange, ...attributes }) => {
|
|
5
5
|
// refs
|
|
6
6
|
const _inputs = useRef([]);
|
|
7
|
+
const _isPasteCombo = useRef(false);
|
|
7
8
|
const _value = useRef({});
|
|
8
9
|
// methods
|
|
9
|
-
const
|
|
10
|
+
const handleInput = useCallback((index) => (event) => {
|
|
10
11
|
if (attributes.disabled)
|
|
11
12
|
return;
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
if (_isPasteCombo.current)
|
|
14
|
+
return;
|
|
15
|
+
let { value } = event.currentTarget;
|
|
14
16
|
_value.current = { ..._value.current, [index]: value };
|
|
17
|
+
if (value.length >= 1) {
|
|
18
|
+
if (!_inputs.current[index + 1]) {
|
|
19
|
+
_inputs.current[character - 1]?.focus();
|
|
20
|
+
_inputs.current[character - 1]?.select();
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
_inputs.current[index + 1]?.focus();
|
|
24
|
+
}
|
|
15
25
|
onChange?.({
|
|
16
26
|
...event,
|
|
17
27
|
target: {
|
|
18
|
-
...event.
|
|
28
|
+
...event.currentTarget,
|
|
19
29
|
name: attributes.name ?? "",
|
|
20
30
|
value: Object.keys(_value.current)
|
|
21
31
|
.sort((a, b) => Number(a) - Number(b))
|
|
@@ -25,24 +35,51 @@ const Otp = ({ character, onChange, ...attributes }) => {
|
|
|
25
35
|
});
|
|
26
36
|
}, [onChange, attributes.name, attributes.disabled]);
|
|
27
37
|
const handleKeyUp = useCallback((index) => (event) => {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
const input = event.currentTarget;
|
|
39
|
+
const { value } = input;
|
|
40
|
+
const beforeInput = _inputs.current[index];
|
|
41
|
+
// referans
|
|
42
|
+
const beforeInputValue = beforeInput.value;
|
|
43
|
+
if (beforeInputValue.length > 1) {
|
|
44
|
+
let i = 0;
|
|
45
|
+
const chars = beforeInputValue.split("");
|
|
46
|
+
const interval = setInterval(() => {
|
|
47
|
+
const input = _inputs.current[i];
|
|
48
|
+
if (input) {
|
|
49
|
+
input.value = chars[i];
|
|
50
|
+
input.focus();
|
|
51
|
+
}
|
|
52
|
+
i++;
|
|
53
|
+
if (i >= chars.length)
|
|
54
|
+
clearInterval(interval);
|
|
55
|
+
}, 50);
|
|
56
|
+
return;
|
|
40
57
|
}
|
|
58
|
+
const lastChar = value.slice(-1);
|
|
59
|
+
event.currentTarget.value = lastChar;
|
|
41
60
|
if (event.key === "Backspace" && value.length === 0) {
|
|
42
61
|
_inputs.current[index - 1]?.focus();
|
|
43
|
-
_inputs.current[index - 1]?.select();
|
|
44
62
|
}
|
|
45
63
|
}, []);
|
|
64
|
+
const handleKeyDown = useCallback((index) => (event) => {
|
|
65
|
+
_isPasteCombo.current = (event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "v";
|
|
66
|
+
if (_isPasteCombo.current)
|
|
67
|
+
return;
|
|
68
|
+
if (index === 0 && (event.key === "ArrowDown" || event.key === "ArrowLeft"))
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
if (index + 1 >= character && (event.key === "ArrowUp" || event.key === "ArrowRight"))
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
if (!/[0-9]/.test(event.key) && event.key.length === 1)
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
if (event.key === "ArrowDown" || event.key === "ArrowLeft") {
|
|
75
|
+
_inputs.current[index - 1]?.focus();
|
|
76
|
+
setTimeout(() => _inputs.current[index - 1]?.select(), 0);
|
|
77
|
+
}
|
|
78
|
+
else if (event.key === "ArrowUp" || event.key === "ArrowRight") {
|
|
79
|
+
_inputs.current[index + 1]?.focus();
|
|
80
|
+
setTimeout(() => _inputs.current[index + 1]?.select(), 0);
|
|
81
|
+
}
|
|
82
|
+
}, [character]);
|
|
46
83
|
const handleClick = useCallback((event) => {
|
|
47
84
|
const input = event.currentTarget;
|
|
48
85
|
if (document.activeElement === input)
|
|
@@ -51,7 +88,7 @@ const Otp = ({ character, onChange, ...attributes }) => {
|
|
|
51
88
|
return (React.createElement("div", { className: "ar-input-otp-wrapper" }, Array.from({ length: character }, (_, index) => (React.createElement("span", { key: index },
|
|
52
89
|
React.createElement(Input, { ref: (el) => {
|
|
53
90
|
_inputs.current[index] = el;
|
|
54
|
-
}, ...attributes, value: _value.current[index] ?? "",
|
|
91
|
+
}, ...attributes, value: _value.current[index] ?? "", onInput: handleInput(index), onKeyUp: handleKeyUp(index), onKeyDown: handleKeyDown(index), onFocus: (event) => event.target.select(), onClick: handleClick, size: 1, placeholder: undefined, autoFocus: index === 0, autoComplete: "off" }))))));
|
|
55
92
|
};
|
|
56
93
|
Otp.displayName = "Input.Otp";
|
|
57
94
|
export default Otp;
|