Skip to content

Commit 8d9a8da

Browse files
authored
[PLAY-1773] Rails input masking: credit_card and cvv (#4267)
**What does this PR do?** - Continues work in [PLAY-1772](https://runway.powerhrg.com/backlog_items/PLAY-1772) and PLAY-1866. ([PLAY-1773](https://runway.powerhrg.com/backlog_items/PLAY-1772)). - Adds credit_card and cvv mask - Make mask prop an enum **Screenshots:** ![Screenshot 2025-02-12 at 9 08 17 AM](https://github.com/user-attachments/assets/3e7bcc2b-ef37-4d6c-92a6-40dfc44e4194) **How to test?** 1. Go to /kits/text_input/rails#mask 2. Type in the Credit Card and CVV text inputs 3. They should be masked as you type #### Checklist: - [x] **LABELS** Add a label: `enhancement`, `bug`, `improvement`, `new kit`, `deprecated`, or `breaking`. See [Changelog & Labels](https://github.com/powerhome/playbook/wiki/Changelog-&-Labels) for details. - [x] **DEPLOY** I have added the `milano` label to show I'm ready for a review.
1 parent bc8a528 commit 8d9a8da

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
placeholder: "123-45-6789"
2828
}) %>
2929

30+
<%= pb_rails("text_input", props: {
31+
label: "Credit Card",
32+
mask: "credit_card",
33+
margin_bottom: "md",
34+
placeholder: "1234 5678 9012 3456"
35+
}) %>
36+
37+
<%= pb_rails("text_input", props: {
38+
label: "CVV",
39+
mask: "cvv",
40+
margin_bottom: "md",
41+
placeholder: "123"
42+
}) %>
43+
3044
<%= pb_rails("title" , props: {
3145
text: "Hidden Input Under The Hood",
3246
padding_bottom: "sm"

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export default class PbTextInput extends PbEnhancedElement {
2727
ssn: 'ssn',
2828
postal_code: 'postalCode',
2929
zip_code: 'zipCode',
30+
credit_card: 'creditCard',
31+
cvv: 'cvv',
3032
}[maskType];
3133

3234
if (maskKey && INPUTMASKS[maskKey]) {
@@ -45,6 +47,9 @@ export default class PbTextInput extends PbEnhancedElement {
4547
case "currency":
4648
sanitizedInput.value = sanitizeCurrency(formattedValue);
4749
break;
50+
case "credit_card":
51+
sanitizedInput.value = sanitizeCreditCard(formattedValue);
52+
break;
4853
default:
4954
sanitizedInput.value = formattedValue;
5055
}
@@ -63,6 +68,10 @@ function sanitizeCurrency(input) {
6368
return input.replace(/[$,]/g, "");
6469
}
6570

71+
function sanitizeCreditCard(input) {
72+
return input.replace(/\D/g, "");
73+
}
74+
6675
function setCursorPosition(inputElement, cursorPosition, rawValue, formattedValue) {
6776
const difference = formattedValue.length - rawValue.length;
6877
const newPosition = Math.max(0, cursorPosition + difference);

playbook/app/pb_kits/playbook/pb_text_input/text_input.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
module Playbook
55
module PbTextInput
66
class TextInput < Playbook::KitBase
7-
VALID_MASKS = %w[currency zipCode postalCode ssn].freeze
7+
VALID_MASKS = %w[currency zip_code postal_code ssn credit_card cvv].freeze
88

99
MASK_PATTERNS = {
1010
"currency" => '^\$\d{1,3}(?:,\d{3})*(?:\.\d{2})?$',
1111
"zip_code" => '\d{5}',
1212
"postal_code" => '\d{5}-\d{4}',
1313
"ssn" => '\d{3}-\d{2}-\d{4}',
14+
"credit_card" => '\d{4} \d{4} \d{4} \d{4}',
15+
"cvv" => '\d{3,4}',
1416
}.freeze
1517

1618
prop :autocomplete, type: Playbook::Props::Boolean,
@@ -34,7 +36,8 @@ class TextInput < Playbook::KitBase
3436
prop :add_on, type: Playbook::Props::NestedProps,
3537
nested_kit: Playbook::PbTextInput::AddOn
3638

37-
prop :mask, type: Playbook::Props::String,
39+
prop :mask, type: Playbook::Props::Enum,
40+
values: ["currency", "zip_code", "postal_code", "ssn", "credit_card", "cvv", nil],
3841
default: nil
3942

4043
def classname

0 commit comments

Comments
 (0)