Socket
Socket
Sign inDemoInstall

@ryuuji3/react-mask-hook

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ryuuji3/react-mask-hook

A simple masking library as a react hook


Version published
Maintainers
1
Install size
24.4 kB
Created

Readme

Source

Run tests

react-mask-hook

An input masking lib designed to be testable and with simplicity in mind.

Installation:

npm install @ryuuji3/react-mask-hook

Sample Usage:

import useMask from '@ryuuji3/react-mask-hook'

function PostalCodeDemo() {
    const [ value, setValue ] = useState('')

    function handleChange(value) {
        setValue(value.toUpperCase()) // render as uppercase
    }

    const maskProps = useMask({
        value,
        onChange: handleChange,
        // array of regexp and formatting characters
        mask: [ /[a-z]/i, /[\d]/, /[a-z]/i, " ", /[\d]/, /[a-z]/i, /[\d]/ ], 
        placeholder: '_', // renders all regexp with _ (ie. "___ ___")
    })

    return (
        <label>
            Sample input with postal code mask:
            <input
                name="test"
                type="text"
                {...maskProps}
            />
        </label>
    );
}

function TelephoneDemo() {
    const [ value, setValue ] = useState('')
    const maskProps = useMask({
        value,
        onChange: setValue,
        mask: '(###)-###-####', // deprecated syntax but still supported
        placeholder: '_',
    })

    return (
        <label>
            Sample input with phone mask:
            <input
                name="test"
                type="tel"
                {...maskProps}
            />
        </label>
    );
}

function DateDemo() {
    const [ value, setValue ] = useState('')
    const maskProps = useMask({
        value,
        onChange: setValue,
        mask: '## - ## - ####',
        placeholder: 'DD - MM - YYYY', // will render mask exactly like this
    })

    return (
        <label>
            Sample input with date mask:
            <input
                name="test"
                type="text"
                {...maskProps}
            />
        </label>
    );
}

API

const { value, placeholder, onChange, onKeyUp, onKeyDown, onFocus } = useMask({ value, onChange, mask, placeholder })

Arguments:

argumentdescription
valueis the string value without formatting. this will hold only the raw values without formatting characters or spaces
onChangeis the callback used to update value. you must supply local state or deferred state.
maskis an array of regular expressions and formatting characters.
placeholderThe third argument is a single character to use as the displayed placeholder instead of the regular expression, or it can be an entire string that matches the mask but with different characters are the placeholder. Your displayed mask must have the same length as the mask.

You may opt-out of placeholder, but the other properties returned by useMask are required and should be bound to the input in order for the masking to function as expected.

Keywords

FAQs

Last updated on 04 Jan 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc