1
1
@props ([
2
2
' digits' => 6 ,
3
- ' eventCallback' => null ,
4
3
' name' => ' code' ,
5
4
] )
6
5
7
6
<div x-data =" {
8
7
totalDigits: @js ($digits ),
9
- eventCallback: @js ($eventCallback ),
10
8
init() {
11
- setTimeout (() => {
9
+ $nextTick (() => {
12
10
const firstInput = this.$refs.input1;
13
11
if (firstInput) {
14
12
firstInput.focus();
15
13
}
16
- }, 100 );
14
+ });
17
15
},
18
16
get digitIndices() {
19
17
return Array.from({ length: this.totalDigits }, (_, i) => i + 1);
38
36
},
39
37
onComplete() {
40
38
this.updateHiddenField();
41
-
42
- if (this.eventCallback) {
43
- window.dispatchEvent(new CustomEvent(this.eventCallback, {
44
- detail: { code: this.getCode() }
45
- }));
46
- }
47
39
},
48
40
handleNumberKey(index, key) {
49
41
this.getInput(index).value = key;
52
44
this.getInput(index + 1).focus();
53
45
}
54
46
55
- setTimeout (() => {
47
+ $nextTick (() => {
56
48
this.updateHiddenField();
57
49
if (index === this.totalDigits && this.isComplete()) {
58
50
this.onComplete();
59
51
}
60
- }, 100 );
52
+ });
61
53
},
62
54
handleBackspace(index) {
63
55
const currentInput = this.getInput(index);
68
60
previousInput.value = '';
69
61
previousInput.focus();
70
62
}
71
- setTimeout(() => this.updateHiddenField(), 100 );
63
+ this.updateHiddenField();
72
64
},
73
65
handleKeyDown(index, event) {
74
66
const key = event.key;
97
89
this.getInput(nextIndex).focus();
98
90
99
91
if (numericOnly.length >= this.totalDigits) {
100
- setTimeout(() => {
101
- this.updateHiddenField();
102
- this.onComplete();
103
- }, 100);
92
+ this.updateHiddenField();
93
+ this.onComplete();
104
94
}
105
95
},
106
96
clearAll() {
111
101
this.$refs.input1.focus();
112
102
}
113
103
}"
114
- @focus -auth-2fa-auth-code.window =" $refs.input1 && $refs.input1.focus()"
115
- @clear -auth-2fa-auth-code.window =" clearAll()"
116
- class =" relative" >
104
+ @focus -auth-2fa-auth-code.window =" $refs.input1 && $refs.input1.focus()"
105
+ @clear -auth-2fa-auth-code.window =" clearAll()"
106
+ class =" relative" >
117
107
118
108
<div class =" flex items-center" >
119
109
@for ($x = 1 ; $x <= $digits ; $x ++ )
@@ -137,7 +127,7 @@ class="flex h-10 w-10 items-center justify-center border border-zinc-300 bg-acce
137
127
</div >
138
128
139
129
<input
140
- {{ $attributes -> except ([' eventCallback ' , ' digits' ]) } }
130
+ {{ $attributes -> except ([' digits' ]) } }
141
131
type =" hidden"
142
132
class =" hidden"
143
133
x-ref =" code"
0 commit comments