Socket
Socket
Sign inDemoInstall

react-imask

Package Overview
Dependencies
3
Maintainers
1
Versions
93
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-imask


Version published
Maintainers
1
Created

Package description

What is react-imask?

The react-imask package is a versatile input masking library for React applications. It allows developers to create input fields with predefined formats, ensuring that user input adheres to specific patterns. This is particularly useful for forms that require inputs like phone numbers, dates, credit card numbers, and more.

What are react-imask's main functionalities?

Basic Input Masking

This feature allows you to create a basic input mask for phone numbers. The mask ensures that the input follows the pattern (000) 000-0000.

import React from 'react';
import IMask from 'react-imask';

function PhoneInput() {
  return (
    <IMask.Input
      mask="(000) 000-0000"
      placeholder="(123) 456-7890"
    />
  );
}

export default PhoneInput;

Dynamic Masking

This feature demonstrates dynamic masking, where the input mask changes based on the input value. For example, it changes the mask for American Express card numbers.

import React, { useState } from 'react';
import IMask from 'react-imask';

function DynamicMaskInput() {
  const [mask, setMask] = useState('0000 0000 0000 0000');

  return (
    <IMask.Input
      mask={mask}
      placeholder="Enter your card number"
      onAccept={(value, mask) => {
        if (value.startsWith('34') || value.startsWith('37')) {
          setMask('0000 000000 00000'); // American Express
        } else {
          setMask('0000 0000 0000 0000'); // Other cards
        }
      }}
    />
  );
}

export default DynamicMaskInput;

Custom Masking with Regular Expressions

This feature allows you to use regular expressions for custom input masking. In this example, the input only accepts lowercase letters.

import React from 'react';
import IMask from 'react-imask';

function RegexMaskInput() {
  return (
    <IMask.Input
      mask={/^[a-z]*$/}
      placeholder="Enter lowercase letters only"
    />
  );
}

export default RegexMaskInput;

Other packages similar to react-imask

Readme

Source

React IMask Plugin

react-imask

npm version License: MIT

Install

npm install react-imask

Mask Input Example

import { IMaskInput } from 'react-imask';

<IMaskInput
  mask={Number}
  radix="."
  value="123"
  unmask={true} // true|false|'typed'
  inputRef={el => this.input = el}  // access to nested input
  // DO NOT USE onChange TO HANDLE CHANGES!
  // USE onAccept INSTEAD
  onAccept={
    // depending on prop above first argument is
    // `value` if `unmask=false`,
    // `unmaskedValue` if `unmask=true`,
    // `typedValue` if `unmask='typed'`
    (value, mask) => console.log(value)
  }
  // ...and more mask props in a guide

  // input props also available
  placeholder='Enter number here'
/>

Extend Existing Components

import { IMaskMixin } from 'react-imask';

// extend style component
const StyledInput = styled.input`
  color: green;
`;

const MaskedStyledInput = IMaskMixin(({inputRef, ...props}) => (
  <StyledInput
    {...props}
    innerRef={inputRef}  // bind internal input (if you use styled-components V4, use "ref" instead "innerRef")
  />
));

<MaskedStyledInput
  mask={Number}
  radix="."
  onAccept={(value, mask) => console.log(value)}
  // ...and more mask props in a guide

  // ...other styled props
/>

More options see in a guide.

Using hook

import { useState } from 'react';
import { useIMask } from 'react-imask';

function IMaskWithHook () {
  const [ opts, setOpts ] = useState({ mask: Number });
  const {
    ref,
    maskRef,
    value,
    setValue,
    unmaskedValue,
    setUnmaskedValue,
    typedValue,
    setTypedValue,
  } = useIMask(opts);
  
  return (
    <input ref={ref} />
  );
}

Many Thanks to

@Yordis Prieto

@Alexander Kiselev

Support Development

Paypal

Keywords

FAQs

Last updated on 31 Jan 2022

Did you know?

Socket

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc