npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cpr-mask

v2.2.6

Published

A masking component for react

Downloads

246

Readme

Build Status

Cpr-mask is a React component for making masked inputs with the ability to switch between masks. For example, if you have a phone number and want the value masked like this (801) 777-8776 But once the value's length is more than 10 you want it to be 8-01777877611 or something Cpr-mask can do that for you.

Cpr-mask can also be used to make an input that only allows characters that match a regular expression.

API

masks

[
	{
		pattern: "(111) 111-1111",
		condition: value => value.length <= 10
	},
	{
		pattern: "1-111111111111",
		condition: value => value.length > 10
	}
]

The main prop. It's an array of mask objects. Each mask object has a pattern and a condition. The pattern represents how the value will appear in the input. They are made of masked characters and placeholder characters. Placeholder characters are either 1, A, W, or *. - 1 Any number can go into this slot. - A Letters a-z. - W Numbers or Letters. - * Anything A mask pattern of (1)-A-*-WWW could accept the value of "5Z$1AB".

The condition property is what determines what mask is currently displayed. It's a function that takes the input's non-masked value as an argument, and expects true/false as the return. The condition functions are run in order of the array and the first to return true will be used.

Incompatible Masks

It's possible to give cpr-mask two or masks that would conflict with each other. An example would be.

[
	{
		pattern: "AA-AA",
		condition: value => value.length < 5
	},
	{
		pattern: "11-111",
		condition: value => value >= 5
	}
]

This wouldn't work because "abcd" is okay with the first mask but once you type in "abcde" and it would try to switch those would be invalid characters. Cpr-mask will throw a Incompatible masks error in cases like these.

Encoding and Decoding cpr-mask value

Cpr-mask gives you the ability to encode and decode values coming from and going to the input. Encode is a function that will be run with the initialValue prop before cpr-mask attempts to mask the value. Decode is a function that is just run onBlur for the value coming out of cpr-mask. Both function were written for Canopy convenience can the same effect can be acheived other ways.

Cpr-mask props

The majority of the props concern how the cpr-mask component is styled.

  • className: The outermost class of cpr-mask
  • inputClass: The css class given to the input field
  • validateMethod: A function run on the non-masked value to see if the invalidClass should be applied or not
  • invalidClass: The class given to the outermost div if the validateMethod returns false
  • nonValidMsg: The message that appears beneath the input when validateMethod returns false.
  • nonValidMsgClass: The class applied to the span containing the nonValidMsg
  • alignment: The string of either left or right. Determines how to align text inside the input field.
  • sideChars: An object determines if characters should be put directly to the left or right of the input.
{
	left: "$",
	right: "%"
}
  • filler: The character used for empty spots in the mask. Default is " ".
  • placeholder: The placeholder given to the input. If none is provided cpr-mask will use a mask with the filler prop characters.
  • disabled: disable the input

Note: The validateMethod dependant classes apply only if the input has been blurred once.

The simplified html is like this

<div className={`${props.className} ${props.invalidClass}`}>
	<div>
		<span>{props.sideChars.left}</span>
		<input className={props.inputClass} style={{textAlign: props.alignment}}/>
		<span>{props.sideChars.right}</span>
	</div>
	<span className={props.nonValidMsgClass}>{props.nonValidMsg}</span>
</div>

Cpr-mask interaction

The two available ways to get the non-masked value out of cpr-mask are onChange and onBlur. These functions can be provided by props and will be given the non-masked value on invocation.