blob: 49e1b02664baa03abd30f186557f4fa62e0a7c09 (
plain)
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
|
class OtpForm extends HTMLElement {
/** @type MutationObserver? */
observer;
constructor() {
super();
}
connectedCallback() {
this.observer = new MutationObserver(() => {
this.clearInputs();
this.configureInputs();
});
this.observer.observe(this, { childList: true, subtree: true });
}
disconnectedCallback() {
this.observer?.disconnect();
}
clearInputs() {
console.log("clearing all inputs");
/** @type NodeListOf<HTMLInputElement> */
const inputs = this.querySelectorAll('input:not([type="submit"])');
for (const input of inputs) {
input.value = "";
}
}
configureInputs() {
this.observer?.disconnect();
/** @type NodeListOf<HTMLInputElement> */
const inputs = this.querySelectorAll('input:not([type="submit"])');
/** @type NodeListOf<HTMLFormElement> */
const form = this.querySelector("form");
for (const input of inputs) {
input.addEventListener("focus", () => {
input.select();
});
input.addEventListener("input", () => {
if (input.value.length > 0) {
input.value = input.value.slice(0, 1).toLocaleUpperCase();
/** @type HTMLInputElement */
const nextInput = input.nextElementSibling;
if (nextInput) {
nextInput.focus();
return;
}
/** @type HTMLInputElement */
const submitButton = form.querySelector('input[type="submit"]');
submitButton.focus();
let areAllInputsEntered = true;
inputs.forEach((input) => {
areAllInputsEntered = areAllInputsEntered && input.value.length > 0;
});
if (areAllInputsEntered) {
form.requestSubmit(submitButton);
}
}
});
}
}
}
customElements.define("otp-form", OtpForm);
|