From 92dc50b8bd01b093a7b6e7ffd2782c2c57876d1e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lucas=20Garc=C3=ADa?= <lucas@codeccoop.org>
Date: Thu, 25 Apr 2024 21:00:18 +0200
Subject: [PATCH] feat: wpcf7 iban field presentation script

---
 includes/fields/wpcf7/iban/class-field.php | 24 +++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/includes/fields/wpcf7/iban/class-field.php b/includes/fields/wpcf7/iban/class-field.php
index 828c4e5..6be9c5c 100644
--- a/includes/fields/wpcf7/iban/class-field.php
+++ b/includes/fields/wpcf7/iban/class-field.php
@@ -76,7 +76,7 @@ class Field extends BaseField
 			$validation_error,
 		);
 
-        return $html;
+        return $html . $this->add_script($tag);
     }
 
     public function add_rules($schema, $form)
@@ -103,4 +103,26 @@ class Field extends BaseField
             }
         }
     }
+
+	private function add_script($tag)
+	{
+		ob_start();
+        ?>
+		<script>
+		const input = document.currentScript.parentElement.querySelector('input[name="<?= $tag->name ?>"]');
+		input.addEventListener("input", ({ target }) => {
+			const value = String(target.value);
+			const chars = value.split("").filter((c) => c !== " ");
+			target.value = chars.reduce((repr, char, i) => {
+				if (i % 4 === 0) {
+					char = " " + char;
+				}
+				return repr + char;
+			});
+		});
+		</script>
+		<?php
+        return ob_get_clean();
+
+	}
 }
-- 
GitLab