summaryrefslogtreecommitdiff
path: root/http/public/scripts/otp-form-wc.js
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2026-06-04 15:15:02 +0100
committerJoe Carstairs <me@joeac.net>2026-06-04 15:16:34 +0100
commit813836fbba884388e6013e9fd377430ef33b6c8d (patch)
tree056149f5c51eabaa89960a7d8d2eb68fe9b158e8 /http/public/scripts/otp-form-wc.js
parentfac1199cd61e069b1a144d2548477a2c3b3b94b0 (diff)
http: adds scripts
Diffstat (limited to 'http/public/scripts/otp-form-wc.js')
-rw-r--r--http/public/scripts/otp-form-wc.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/http/public/scripts/otp-form-wc.js b/http/public/scripts/otp-form-wc.js
new file mode 100644
index 0000000..49e1b02
--- /dev/null
+++ b/http/public/scripts/otp-form-wc.js
@@ -0,0 +1,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);