Sha256: 0d83830f35e59b56d4fa51d82276cc0aee76175073eaab834f7d67cb025b5c5b

Contents?: true

Size: 1.69 KB

Versions: 24

Compression:

Stored size: 1.69 KB

Contents

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',
    },
} 

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
playbook_ui-14.11.0.pre.rc.3 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.alpha.PBNTR662stickyrightcolumnreact5160 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.11.0.pre.rc.2 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.11.0.pre.rc.1 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.11.0.pre.rc.0 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.9.0.pre.alpha.PBNTR767advancedtablemultiheadercolumns5136 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.9.0.pre.alpha.PBNTR775formmatingmaskdefaultvalue5121 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.9.0.pre.alpha.PBNTR775formmatingmaskdefaultvalue5120 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.24 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.23 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.22 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.21 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.9.0.pre.alpha.play1703errorstatealignment5060 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.20 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.19 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.18 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.9.0.pre.alpha.PLAY1660reactdropzone5020 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.17 app/pb_kits/playbook/pb_text_input/inputMask.ts
playbook_ui-14.10.0.pre.rc.16 app/pb_kits/playbook/pb_text_input/inputMask.ts