Skip to content

Commit 177abe8

Browse files
committed
Use typescript file for mask logic
1 parent d8ee5aa commit 177abe8

File tree

2 files changed

+13
-43
lines changed

2 files changed

+13
-43
lines changed

playbook/app/pb_kits/playbook/pb_text_input/docs/_text_input_mask.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
margin_bottom: "md",
3939
name: "currency_name",
4040
id: "example-currency",
41-
placeholder: "$0.00",
41+
value: "$99.99",
4242
}) %>
4343

4444
<style>

playbook/app/pb_kits/playbook/pb_text_input/index.js

+12-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import PbEnhancedElement from "../pb_enhanced_element";
1+
import PbEnhancedElement from "../pb_enhanced_element"
2+
import { INPUTMASKS } from "./inputMask"
23

34
export default class PbTextInput extends PbEnhancedElement {
45
static get selector() {
@@ -8,6 +9,7 @@ export default class PbTextInput extends PbEnhancedElement {
89
connect() {
910
this.handleInput = this.handleInput.bind(this);
1011
this.element.addEventListener("input", this.handleInput);
12+
this.handleInput();
1113
}
1214

1315
disconnect() {
@@ -20,19 +22,15 @@ export default class PbTextInput extends PbEnhancedElement {
2022
const rawValue = this.element.value;
2123
let formattedValue = rawValue;
2224

23-
switch (maskType) {
24-
case "currency":
25-
formattedValue = formatCurrency(rawValue);
26-
break;
27-
case "ssn":
28-
formattedValue = formatSSN(rawValue);
29-
break;
30-
case "postal_code":
31-
formattedValue = formatPostalCode(rawValue);
32-
break;
33-
case "zip_code":
34-
formattedValue = formatZipCode(rawValue);
35-
break;
25+
const maskKey = {
26+
currency: 'currency',
27+
ssn: 'ssn',
28+
postal_code: 'postalCode',
29+
zip_code: 'zipCode',
30+
}[maskType];
31+
32+
if (maskKey && INPUTMASKS[maskKey]) {
33+
formattedValue = INPUTMASKS[maskKey].format(rawValue);
3634
}
3735

3836
const sanitizedInput = this.element
@@ -57,34 +55,6 @@ export default class PbTextInput extends PbEnhancedElement {
5755
}
5856
}
5957

60-
function formatCurrency(value) {
61-
const numericValue = value.replace(/[^0-9]/g, "").slice(0, 15);
62-
if (!numericValue) return "";
63-
const dollars = parseFloat((parseInt(numericValue) / 100).toFixed(2));
64-
if (dollars === 0) return "";
65-
return new Intl.NumberFormat("en-US", {
66-
style: "currency",
67-
currency: "USD",
68-
maximumFractionDigits: 2,
69-
}).format(dollars);
70-
}
71-
72-
function formatSSN(value) {
73-
const cleaned = value.replace(/\D/g, "").slice(0, 9);
74-
return cleaned
75-
.replace(/(\d{5})(?=\d)/, "$1-")
76-
.replace(/(\d{3})(?=\d)/, "$1-");
77-
}
78-
79-
function formatZipCode(value) {
80-
return value.replace(/\D/g, "").slice(0, 5);
81-
}
82-
83-
function formatPostalCode(value) {
84-
const cleaned = value.replace(/\D/g, "").slice(0, 9);
85-
return cleaned.replace(/(\d{5})(?=\d)/, "$1-");
86-
}
87-
8858
function sanitizeSSN(input) {
8959
return input.replace(/\D/g, "");
9060
}

0 commit comments

Comments
 (0)