Skip to content

Commit ac5fa58

Browse files
committed
Refactor input-otp-component it to use the $nextTick
1 parent f33fa41 commit ac5fa58

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

resources/views/components/input-otp.blade.php

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
@props([
22
'digits' => 6,
3-
'eventCallback' => null,
43
'name' => 'code',
54
])
65

76
<div x-data="{
87
totalDigits: @js($digits),
9-
eventCallback: @js($eventCallback),
108
init() {
11-
setTimeout(() => {
9+
$nextTick(() => {
1210
const firstInput = this.$refs.input1;
1311
if (firstInput) {
1412
firstInput.focus();
1513
}
16-
}, 100);
14+
});
1715
},
1816
get digitIndices() {
1917
return Array.from({ length: this.totalDigits }, (_, i) => i + 1);
@@ -38,12 +36,6 @@
3836
},
3937
onComplete() {
4038
this.updateHiddenField();
41-
42-
if (this.eventCallback) {
43-
window.dispatchEvent(new CustomEvent(this.eventCallback, {
44-
detail: { code: this.getCode() }
45-
}));
46-
}
4739
},
4840
handleNumberKey(index, key) {
4941
this.getInput(index).value = key;
@@ -52,12 +44,12 @@
5244
this.getInput(index + 1).focus();
5345
}
5446
55-
setTimeout(() => {
47+
$nextTick(() => {
5648
this.updateHiddenField();
5749
if (index === this.totalDigits && this.isComplete()) {
5850
this.onComplete();
5951
}
60-
}, 100);
52+
});
6153
},
6254
handleBackspace(index) {
6355
const currentInput = this.getInput(index);
@@ -68,7 +60,7 @@
6860
previousInput.value = '';
6961
previousInput.focus();
7062
}
71-
setTimeout(() => this.updateHiddenField(), 100);
63+
this.updateHiddenField();
7264
},
7365
handleKeyDown(index, event) {
7466
const key = event.key;
@@ -97,10 +89,8 @@
9789
this.getInput(nextIndex).focus();
9890
9991
if (numericOnly.length >= this.totalDigits) {
100-
setTimeout(() => {
101-
this.updateHiddenField();
102-
this.onComplete();
103-
}, 100);
92+
this.updateHiddenField();
93+
this.onComplete();
10494
}
10595
},
10696
clearAll() {
@@ -111,9 +101,9 @@
111101
this.$refs.input1.focus();
112102
}
113103
}"
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">
117107

118108
<div class="flex items-center">
119109
@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
137127
</div>
138128

139129
<input
140-
{{ $attributes->except(['eventCallback', 'digits']) }}
130+
{{ $attributes->except(['digits']) }}
141131
type="hidden"
142132
class="hidden"
143133
x-ref="code"

0 commit comments

Comments
 (0)