-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLAY-1731] Text Input Masking for React: Currency, Zip Code, Postal …
…Code, SSN (#3986) **What does this PR do?** - Create a `mask` prop for React Text Input kit which masks the value as users type. - "currency" ($1,234.56), "zipCode" (12345), "postalCode" (12345-6789), and "ssn" (123-45-6789) - Create a `handleChange` function that handles masking (if `mask` prop is filled in) and then the `onChange` prop after - Add Jest tests **Screenshots:** Screenshots to visualize your addition/change ![Screenshot 2024-12-06 at 4 37 40 PM](https://github.com/user-attachments/assets/803813fe-5e46-41d5-8a34-cc8c9c707e20) **How to test?** Steps to confirm the desired behavior: 1. Go to /kits/text_input/react#mask 2. Test the text input masks 3. Also, test the other docs to make sure "unmasked" inputs work as usual. #### 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. - [x] **TESTS** I have added test coverage to my code.
- Loading branch information
Showing
6 changed files
with
328 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
playbook/app/pb_kits/playbook/pb_text_input/docs/_text_input_mask.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React, { useState } from 'react' | ||
|
||
import Caption from '../../pb_caption/_caption' | ||
import TextInput from '../../pb_text_input/_text_input' | ||
import Title from '../../pb_title/_title' | ||
|
||
const TextInputMask = (props) => { | ||
const [ssn, setSSN] = useState('') | ||
const handleOnChangeSSN = ({ target }) => { | ||
setSSN(target.value) | ||
} | ||
const ref = React.createRef() | ||
|
||
const [formFields, setFormFields] = useState({ | ||
currency: '', | ||
zipCode: '', | ||
postalCode: '', | ||
ssn: '', | ||
}) | ||
|
||
const handleOnChangeFormField = ({ target }) => { | ||
const { name, value } = target | ||
setFormFields({ ...formFields, [name]: value }) | ||
} | ||
|
||
return ( | ||
<div> | ||
<TextInput | ||
label="Currency" | ||
mask="currency" | ||
name="currency" | ||
onChange={handleOnChangeFormField} | ||
value={formFields.currency} | ||
{...props} | ||
/> | ||
<TextInput | ||
label="Zip Code" | ||
mask="zipCode" | ||
name="zipCode" | ||
onChange={handleOnChangeFormField} | ||
value={formFields.zipCode} | ||
{...props} | ||
/> | ||
<TextInput | ||
label="Postal Code" | ||
mask="postalCode" | ||
name="postalCode" | ||
onChange={handleOnChangeFormField} | ||
value={formFields.postalCode} | ||
{...props} | ||
/> | ||
<TextInput | ||
label="SSN" | ||
mask="ssn" | ||
name="ssn" | ||
onChange={handleOnChangeFormField} | ||
value={formFields.ssn} | ||
{...props} | ||
/> | ||
|
||
<br /> | ||
<br /> | ||
|
||
<Title>{'Event Handler Props'}</Title> | ||
|
||
<br /> | ||
<Caption>{'onChange'}</Caption> | ||
|
||
<br /> | ||
|
||
<TextInput | ||
label="SSN" | ||
mask="ssn" | ||
onChange={handleOnChangeSSN} | ||
placeholder="Enter SSN" | ||
ref={ref} | ||
value={ssn} | ||
{...props} | ||
/> | ||
|
||
{ssn !== '' && ( | ||
<React.Fragment>{`SSN is: ${ssn}`}</React.Fragment> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default TextInputMask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
type InputMask = { | ||
format: (value: string) => string | ||
pattern: string | ||
placeholder: string | ||
} | ||
|
||
type InputMaskDictionary = { | ||
[key in 'currency' | 'zipCode' | 'postalCode' | 'ssn']: InputMask | ||
} | ||
|
||
const formatCurrency = (value: string): string => { | ||
const numericValue = value.replace(/[^0-9]/g, '').slice(0, 15) | ||
|
||
if (!numericValue) return '' | ||
|
||
const dollars = parseFloat((parseInt(numericValue) / 100).toFixed(2)) | ||
if (dollars === 0) return '' | ||
|
||
return new Intl.NumberFormat('en-US', { | ||
style: 'currency', | ||
currency: 'USD', | ||
maximumFractionDigits: 2, | ||
}).format(dollars) | ||
} | ||
|
||
const formatBasicPostal = (value: string): string => { | ||
return value.replace(/\D/g, '').slice(0, 5) | ||
} | ||
|
||
const formatExtendedPostal = (value: string): string => { | ||
const cleaned = value.replace(/\D/g, '').slice(0, 9) | ||
return cleaned.replace(/(\d{5})(?=\d)/, '$1-') | ||
} | ||
|
||
const formatSSN = (value: string): string => { | ||
const cleaned = value.replace(/\D/g, '').slice(0, 9) | ||
return cleaned | ||
.replace(/(\d{5})(?=\d)/, '$1-') | ||
.replace(/(\d{3})(?=\d)/, '$1-') | ||
} | ||
|
||
export const INPUTMASKS: InputMaskDictionary = { | ||
currency: { | ||
format: formatCurrency, | ||
// eslint-disable-next-line no-useless-escape | ||
pattern: '^\\$\\d{1,3}(?:,\\d{3})*(?:\\.\\d{2})?$', | ||
placeholder: '$0.00', | ||
}, | ||
zipCode: { | ||
format: formatBasicPostal, | ||
pattern: '\\d{5}', | ||
placeholder: '12345', | ||
}, | ||
postalCode: { | ||
format: formatExtendedPostal, | ||
pattern: '\\d{5}-\\d{4}', | ||
placeholder: '12345-6789', | ||
}, | ||
ssn: { | ||
format: formatSSN, | ||
pattern: '\\d{3}-\\d{2}-\\d{4}', | ||
placeholder: '123-45-6789', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters