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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-mask-io

v1.0.15

Published

Masked input component for React

Readme

react-mask-io

A lightweight and controllable library for applying input masks in React, with support for:

  • controlled and uncontrolled inputs
  • masks by string, RegExp[] or function
  • initial value (defaultValue)
  • safe synchronization with forms (RHF, Formik, etc.)
  • no ref usage
  • compatible with any UI library

Installation

npm install react-mask-io

or

yarn add react-mask-io

or

pnpm add react-mask-io

Concept

react-mask-io does not render its own input.

It receives a render function (children) and returns only:

  • value
  • onChange
  • onBlur
  • disabled

This ensures complete compatibility with any input component.

Mask Types

String mask

  • 9 → number (0–9)
  • a → letter (A–Z, converted to uppercase)
  • * → alphanumeric (A-Z, 0-9)
  • any other character → literal

Example:

"999.999.999-99"
"a-9999999-a"

Array of RegExp

const mask = [
  /\d/, /\d/, /\d/, ".", /\d/, /\d/, /\d/
];

Function

const mask = (value: string) => value.replace(/\D/g, "").slice(0, 11);

Basic usage (HTML input)

import InputMask from "react-mask-io";

<InputMask mask="999.999.999-99">
  {({ value, onChange, onBlur }) => (
    <input
      value={value}
      onChange={onChange}
      onBlur={onBlur}
    />
  )}
</InputMask>

Uncontrolled input (initial value)

<InputMask
  mask="999.999.999-99"
  defaultValue="12345678900"
>
  {({ value, onChange }) => (
    <input value={value} onChange={onChange} />
  )}
</InputMask>

Controlled input

const [cpf, setCpf] = useState("");

<InputMask
  mask="999.999.999-99"
  value={cpf}
  onChange={(e) => setCpf(e.target.value)}
>
  {({ value, onChange }) => (
    <input value={value} onChange={onChange} />
  )}
</InputMask>

alwaysShowMask

Shows the mask format even with an empty value.

<InputMask
  mask="999.999.999-99"
  alwaysShowMask
>
  {({ value, onChange }) => (
    <input value={value} onChange={onChange} />
  )}
</InputMask>

beforeMaskedValueChange

Intercepts the state before being applied.

Useful for:

  • normalization
  • custom rules
  • blocking characters
<InputMask
  mask="a-9999999-a"
  beforeMaskedValueChange={({ currentState, nextState }) => {
    if (!nextState.value.startsWith("X")) {
      return currentState;
    }

    return nextState;
  }}
>
  {({ value, onChange }) => (
    <input value={value} onChange={onChange} />
  )}
</InputMask>

Integration with UI libraries

Material UI

<InputMask mask="999.999.999-99">
  {({ value, onChange, onBlur }) => (
    <TextField
      value={value}
      onChange={onChange}
      onBlur={onBlur}
      label="CPF"
    />
  )}
</InputMask>

Chakra UI

<InputMask mask="999-99-9999">
  {({ value, onChange }) => (
    <Input value={value} onChange={onChange} />
  )}
</InputMask>

Ant Design

<InputMask mask="99.999.999">
  {({ value, onChange }) => (
    <Input value={value} onChange={onChange} />
  )}
</InputMask>

React Hook Form

<Controller
  name="cpf"
  control={control}
  render={({ field }) => (
    <InputMask mask="999.999.999-99" {...field}>
      {({ value, onChange }) => (
        <input value={value} onChange={onChange} />
      )}
    </InputMask>
  )}
/>

Important behavior

  • Invalid characters do not enter
  • Excess characters are ignored
  • onChange only fires when the actual value changes
  • Does not generate form loops
  • Does not use ref
  • Does not control focus or cursor

Props

| Prop | Type | Description | |------|------|-------------| | mask | string | RegExp[] | function | Defines the mask | | value | string | Controlled mode | | defaultValue | string | Initial value | | alwaysShowMask | boolean | Display empty format | | disabled | boolean | Disable input | | beforeMaskedValueChange | function | Intercepts state | | children | function | Render function |

What is not the library's responsibility

  • Semantic validation (valid CPF, valid NIE, etc.)
  • Error messages
  • Styling
  • Form management

These responsibilities belong to the form or UI framework.

Philosophy

  • Mask normalizes input.
  • Validation validates data.

react-mask-io does one thing — and does it well.