|
| 1 | +export default class PbTextInput { |
| 2 | + static start() { |
| 3 | + const inputElements = document.querySelectorAll('[data-pb-input-mask="true"]'); |
| 4 | + |
| 5 | + inputElements.forEach((inputElement) => { |
| 6 | + inputElement.addEventListener("input", (event) => { |
| 7 | + const maskType = inputElement.getAttribute("mask"); |
| 8 | + const cursorPosition = inputElement.selectionStart; |
| 9 | + |
| 10 | + let rawValue = event.target.value; |
| 11 | + let formattedValue = rawValue; |
| 12 | + |
| 13 | + // Apply formatting based on the mask type |
| 14 | + switch (maskType) { |
| 15 | + case "currency": |
| 16 | + formattedValue = formatCurrency(rawValue); |
| 17 | + break; |
| 18 | + case "ssn": |
| 19 | + formattedValue = formatSSN(rawValue); |
| 20 | + break; |
| 21 | + case "postal_code": |
| 22 | + formattedValue = formatPostalCode(rawValue); |
| 23 | + break; |
| 24 | + case "zip_code": |
| 25 | + formattedValue = formatZipCode(rawValue); |
| 26 | + break; |
| 27 | + } |
| 28 | + |
| 29 | + // Update the sanitized input field in the same wrapper |
| 30 | + const sanitizedInput = inputElement |
| 31 | + .closest(".text_input_wrapper") |
| 32 | + ?.querySelector('[data="sanitized-pb-input"]'); |
| 33 | + |
| 34 | + if (sanitizedInput) { |
| 35 | + switch (maskType) { |
| 36 | + case "ssn": |
| 37 | + sanitizedInput.value = sanitizeSSN(formattedValue); |
| 38 | + break; |
| 39 | + case "currency": |
| 40 | + sanitizedInput.value = sanitizeCurrency(formattedValue); |
| 41 | + break; |
| 42 | + default: |
| 43 | + sanitizedInput.value = formattedValue; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + inputElement.value = formattedValue; |
| 48 | + setCursorPosition(inputElement, cursorPosition, rawValue, formattedValue); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function formatCurrency(value) { |
| 56 | + const numericValue = value.replace(/[^0-9]/g, "").slice(0, 15); |
| 57 | + |
| 58 | + if (!numericValue) return ""; |
| 59 | + |
| 60 | + const dollars = parseFloat((parseInt(numericValue) / 100).toFixed(2)); |
| 61 | + if (dollars === 0) return ""; |
| 62 | + |
| 63 | + return new Intl.NumberFormat("en-US", { |
| 64 | + style: "currency", |
| 65 | + currency: "USD", |
| 66 | + maximumFractionDigits: 2, |
| 67 | + }).format(dollars); |
| 68 | +} |
| 69 | + |
| 70 | +function formatSSN(value) { |
| 71 | + const cleaned = value.replace(/\D/g, "").slice(0, 9); |
| 72 | + return cleaned |
| 73 | + .replace(/(\d{5})(?=\d)/, "$1-") |
| 74 | + .replace(/(\d{3})(?=\d)/, "$1-"); |
| 75 | +} |
| 76 | + |
| 77 | +function formatZipCode(value) { |
| 78 | + return value.replace(/\D/g, "").slice(0, 5); |
| 79 | +} |
| 80 | + |
| 81 | +function formatPostalCode(value) { |
| 82 | + const cleaned = value.replace(/\D/g, "").slice(0, 9); |
| 83 | + return cleaned.replace(/(\d{5})(?=\d)/, "$1-"); |
| 84 | +} |
| 85 | + |
| 86 | +function sanitizeSSN(input) { |
| 87 | + return input.replace(/\D/g, ""); |
| 88 | +} |
| 89 | + |
| 90 | +function sanitizeCurrency(input) { |
| 91 | + return input.replace(/[$,]/g, ""); |
| 92 | +} |
| 93 | + |
| 94 | +// function to set cursor position |
| 95 | +function setCursorPosition(inputElement, cursorPosition, rawValue, formattedValue) { |
| 96 | + const difference = formattedValue.length - rawValue.length; |
| 97 | + |
| 98 | + const newPosition = Math.max(0, cursorPosition + difference); |
| 99 | + |
| 100 | + requestAnimationFrame(() => { |
| 101 | + inputElement.setSelectionRange(newPosition, newPosition); |
| 102 | + }); |
| 103 | +} |
0 commit comments