ar-design 0.3.68 → 0.3.69
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.
|
@@ -6,16 +6,19 @@ const Otp = ({ character, onChange, ...attributes }) => {
|
|
|
6
6
|
const _inputs = useRef([]);
|
|
7
7
|
const _value = useRef({});
|
|
8
8
|
// methods
|
|
9
|
-
const
|
|
9
|
+
const handleInput = useCallback((index) => (event) => {
|
|
10
10
|
if (attributes.disabled)
|
|
11
11
|
return;
|
|
12
|
-
let { value } = event.
|
|
13
|
-
value = value.replace(/\D/g, "");
|
|
12
|
+
let { value } = event.currentTarget;
|
|
14
13
|
_value.current = { ..._value.current, [index]: value };
|
|
14
|
+
if (value.length >= 1) {
|
|
15
|
+
_inputs.current[index + 1]?.focus();
|
|
16
|
+
_inputs.current[index + 1]?.select();
|
|
17
|
+
}
|
|
15
18
|
onChange?.({
|
|
16
19
|
...event,
|
|
17
20
|
target: {
|
|
18
|
-
...event.
|
|
21
|
+
...event.currentTarget,
|
|
19
22
|
name: attributes.name ?? "",
|
|
20
23
|
value: Object.keys(_value.current)
|
|
21
24
|
.sort((a, b) => Number(a) - Number(b))
|
|
@@ -25,24 +28,31 @@ const Otp = ({ character, onChange, ...attributes }) => {
|
|
|
25
28
|
});
|
|
26
29
|
}, [onChange, attributes.name, attributes.disabled]);
|
|
27
30
|
const handleKeyUp = useCallback((index) => (event) => {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
if (event.key === "ArrowLeft") {
|
|
34
|
-
_inputs.current[index - 1]?.focus();
|
|
35
|
-
_inputs.current[index - 1]?.select();
|
|
36
|
-
}
|
|
37
|
-
else if (event.key === "ArrowRight") {
|
|
38
|
-
_inputs.current[index + 1]?.focus();
|
|
39
|
-
_inputs.current[index + 1]?.select();
|
|
40
|
-
}
|
|
31
|
+
const input = event.currentTarget;
|
|
32
|
+
const { value } = input;
|
|
33
|
+
const lastChar = value.slice(-1);
|
|
34
|
+
event.currentTarget.value = lastChar;
|
|
41
35
|
if (event.key === "Backspace" && value.length === 0) {
|
|
42
36
|
_inputs.current[index - 1]?.focus();
|
|
43
37
|
_inputs.current[index - 1]?.select();
|
|
44
38
|
}
|
|
45
39
|
}, []);
|
|
40
|
+
const handleKeyDown = (index) => (event) => {
|
|
41
|
+
if (index === 0 && (event.key === "ArrowDown" || event.key === "ArrowLeft"))
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
if (index + 1 >= character && (event.key === "ArrowUp" || event.key === "ArrowRight"))
|
|
44
|
+
event.preventDefault();
|
|
45
|
+
if (!/[0-9]/.test(event.key) && event.key.length === 1)
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
if (event.key === "ArrowDown" || event.key === "ArrowLeft") {
|
|
48
|
+
_inputs.current[index - 1]?.focus();
|
|
49
|
+
setTimeout(() => _inputs.current[index - 1]?.select(), 0);
|
|
50
|
+
}
|
|
51
|
+
else if (event.key === "ArrowUp" || event.key === "ArrowRight") {
|
|
52
|
+
_inputs.current[index + 1]?.focus();
|
|
53
|
+
setTimeout(() => _inputs.current[index + 1]?.select(), 0);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
46
56
|
const handleClick = useCallback((event) => {
|
|
47
57
|
const input = event.currentTarget;
|
|
48
58
|
if (document.activeElement === input)
|
|
@@ -51,7 +61,7 @@ const Otp = ({ character, onChange, ...attributes }) => {
|
|
|
51
61
|
return (React.createElement("div", { className: "ar-input-otp-wrapper" }, Array.from({ length: character }, (_, index) => (React.createElement("span", { key: index },
|
|
52
62
|
React.createElement(Input, { ref: (el) => {
|
|
53
63
|
_inputs.current[index] = el;
|
|
54
|
-
}, ...attributes, value: _value.current[index] ?? "",
|
|
64
|
+
}, ...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
65
|
};
|
|
56
66
|
Otp.displayName = "Input.Otp";
|
|
57
67
|
export default Otp;
|