UNPKG

@navinc/base-react-components

Version:
83 lines 4.55 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx } from "react/jsx-runtime"; import styled from 'styled-components'; import { useEffect, useState, useMemo } from 'react'; import Input from '../input.js'; import { useField } from 'formik'; import VMasker from 'vanilla-masker'; import { InteractiveIcon } from '../icon.js'; const StyledInput = styled(Input).withConfig({ displayName: "brc-sc-StyledInput", componentId: "brc-sc-10av59v" }) ` button { position: absolute; right: 16px; top: 50%; transform: translateY(-50%); width: 24px; } `; const nonDigitsAndMaskRegExp = /[^\dX]/g; const removeFormatting = (input) => String(input).replace(nonDigitsAndMaskRegExp, ''); const nonDigitsRegExp = /\D/g; const removeNonDigits = (input) => String(input).replace(nonDigitsRegExp, ''); export const formatSSN = (input) => VMasker.toPattern(input, 'SSS-SS-9999'); export const maskSSN = (input) => { const raw = removeFormatting(input); return formatSSN(raw.slice(0, 5).replace(/\d/g, 'X') + raw.slice(5)); }; const createSsnValidator = ({ isRequired = false, requiredErrorMessage = 'Required', lengthErrorMessage = 'We need your full 9-digit social security number', } = {}) => (ssnInput) => { if (isRequired && !ssnInput) { return [requiredErrorMessage]; } if (ssnInput && removeNonDigits(ssnInput).length < 9) { return [lengthErrorMessage]; } }; export const SsnInput = (_a) => { var { name, onBlur, onChange, invalidOnTouched = true, isRequired, requiredErrorMessage, lengthErrorMessage } = _a, props = __rest(_a, ["name", "onBlur", "onChange", "invalidOnTouched", "isRequired", "requiredErrorMessage", "lengthErrorMessage"]); const memoizedValidateSSN = useMemo(() => createSsnValidator({ isRequired, requiredErrorMessage, lengthErrorMessage }), [isRequired, requiredErrorMessage, lengthErrorMessage]); const [field, meta, { setValue }] = useField({ name, validate: memoizedValidateSSN }); const [isVisible, setIsVisible] = useState(false); useEffect(() => { // Ensure initial value stored in parent form is a number const rawSSN = removeNonDigits(field.value); if (rawSSN !== field.value) { setValue(rawSSN); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return (_jsx(StyledInput, Object.assign({ type: "text", name: name, value: isVisible ? formatSSN(field.value) : maskSSN(field.value), onChange: (e) => { const { value } = e.target; const rawFieldValue = removeNonDigits(field.value); const rawValue = removeFormatting(value); const newSSN = rawValue.includes('X') ? removeNonDigits((rawFieldValue + rawValue.slice(rawFieldValue.length)).slice(0, rawValue.length)) : rawValue; const newMaxLengthSSN = newSSN.slice(0, 9); setValue(newMaxLengthSSN); onChange && onChange(e); }, onBlur: (e) => { field.onBlur(e); onBlur && onBlur(e); }, errors: (!invalidOnTouched || meta.touched) && meta.error, isInvalid: (!invalidOnTouched || meta.touched) && !!meta.error, hasSpaceForErrors: true }, props, { children: _jsx(InteractiveIcon, { name: isVisible ? 'feedback/invisible' : 'feedback/visible', /* Using onMouseDown instead of onClick. When using on click, the following occurs: * 1. on mouse down, the onBlur of the input fires and calls field.onBlur - this triggers a state update * 2. the component rerenders - this means this Icon component rerenders. The element is swapped out with * a new identical element * 3. on mouse up, the browser does not send the onClick event because the element is different. On click * requires a mouse-down followed by a mouse-up on the same element * * Using React.memo(VisibilityIconComponent) also works */ onMouseDown: () => setIsVisible(!isVisible) }) }))); }; //# sourceMappingURL=ssn-input.js.map