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

mfk-mask-input

v1.0.6

Published

A vanilla React masked input component using IMask

Downloads

714

Readme

mfk-mask-input

A lightweight and flexible vanilla React masked input component built with IMask.

🚀 Live Demo →

🔗 AntD Input Variant

Features

  • 🎭 Flexible Masking - Supports various mask patterns (phone, credit card, date, custom)
  • 🪶 Lightweight - Vanilla React input, no UI framework dependencies
  • 📝 TypeScript Support - Full TypeScript support with type definitions
  • Minimal Dependencies - Only React and IMask as peer dependencies
  • 🔧 Customizable - Extensive IMask options support
  • 🎯 Developer Friendly - Access both masked and unmasked values in onChange

Installation

npm install mfk-mask-input
# or
yarn add mfk-mask-input
# or
pnpm add mfk-mask-input
# or
bun add mfk-mask-input

Peer Dependencies

This package requires the following peer dependencies:

{
  "react": "^18.0.0 || ^19.0.0",
  "react-dom": "^18.0.0 || ^19.0.0",
  "imask": "^6.0.0 || ^7.0.0"
}

Quick Start

import { InputMask } from "mfk-mask-input";

function MyComponent() {
  return (
    <InputMask
      mask="(000) 000-0000"
      placeholder="Enter phone number"
      onChange={(e) => {
        console.log("Masked:", e.maskedValue); // "(555) 123-4567"
        console.log("Unmasked:", e.unmaskedValue); // "5551234567"
      }}
    />
  );
}

Usage Examples

Phone Number

<InputMask mask="(000) 000-0000" placeholder="(555) 123-4567" />

Credit Card

<InputMask mask="0000 0000 0000 0000" placeholder="1234 5678 9012 3456" />

Date

<InputMask mask="00/00/0000" placeholder="DD/MM/YYYY" />

Custom Patterns

<InputMask
  mask="AAA-000"
  definitions={{
    A: /[A-Z]/,
    "0": /[0-9]/,
  }}
  placeholder="ABC-123"
/>

Multiple Masks (Dynamic)

<InputMask
  mask={[
    { mask: "(000) 000-0000" },
    { mask: "+0 (000) 000-0000" },
    { mask: "+00 (000) 000-0000" },
  ]}
  placeholder="Phone number"
/>

Controlled Component

import { useState } from "react";
import { InputMask } from "mfk-mask-input";

function ControlledExample() {
  const [value, setValue] = useState("");

  return (
    <InputMask
      mask="0000-0000-0000-0000"
      value={value}
      onChange={(e) => setValue(e.maskedValue)}
    />
  );
}

With Advanced IMask Options

<InputMask
  mask={Number}
  maskOptions={{
    scale: 2,
    thousandsSeparator: ",",
    radix: ".",
    mapToRadix: ["."],
    min: 0,
    max: 999999,
  }}
  placeholder="0.00"
/>

With Custom Styling

<InputMask
  mask="(000) 000-0000"
  className="my-input-class"
  style={{ padding: "8px", border: "1px solid #ccc" }}
/>

API

Props

All standard HTML input props are supported, plus the following:

| Prop | Type | Required | Description | | -------------- | -------------------------------- | -------- | -------------------------------------------------------------------------------------------------------- | | mask | MaskType | Yes | Mask pattern or configuration. Can be a string, RegExp, Date, Number, function, or array of mask objects | | maskOptions | InputMaskOptions | No | Additional IMask configuration options | | definitions | object | No | Custom character definitions (e.g., { 'A': /[A-Z]/ }) | | onChange | (event: OnChangeEvent) => void | No | Change handler with extended event object | | value | string | No | Controlled component value | | defaultValue | string | No | Default value for uncontrolled usage |

Types

OnChangeEvent

The extended onChange event includes:

interface OnChangeEvent {
  target: HTMLInputElement;
  maskedValue: string; // Formatted value with mask
  unmaskedValue: string; // Raw value without mask
  // ... standard event properties
}

MaskType

type MaskType =
  | string
  | RegExp
  | typeof Number
  | typeof Date
  | ((value: string) => string)
  | Array<{ mask: string | RegExp /* other options */ }>;

Advanced Usage

Custom Definitions

Define custom placeholder characters:

<InputMask
  mask="00/00/0000"
  definitions={{
    "0": /[0-9]/, // Digit
    A: /[A-Z]/, // Uppercase letter
    a: /[a-z]/, // Lowercase letter
    "*": /[A-Za-z0-9]/, // Alphanumeric
  }}
/>

With Form Libraries

React Hook Form

import { useForm, Controller } from "react-hook-form";
import { InputMask } from "mfk-mask-input";

function FormExample() {
  const { control } = useForm();

  return (
    <Controller
      name="phone"
      control={control}
      render={({ field }) => (
        <InputMask
          mask="(000) 000-0000"
          {...field}
          onChange={(e) => field.onChange(e.maskedValue)}
        />
      )}
    />
  );
}

Formik

import { Formik, Field } from "formik";
import { InputMask } from "mfk-mask-input";

function FormikExample() {
  return (
    <Formik initialValues={{ phone: "" }}>
      {({ setFieldValue }) => (
        <Field name="phone">
          {({ field }) => (
            <InputMask
              mask="(000) 000-0000"
              value={field.value}
              onChange={(e) => setFieldValue("phone", e.maskedValue)}
            />
          )}
        </Field>
      )}
    </Formik>
  );
}

IMask Documentation

For complete IMask options and patterns, see the official IMask documentation.

TypeScript

This package includes TypeScript definitions. Import types as needed:

import type {
  MaskedInputProps,
  OnChangeEvent,
  InputMaskOptions,
} from "mfk-mask-input";

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.