vlad-zhukov / react-text-mask-hoc

A higher-order text-mask component decorator for React and React Native.
https://npm.im/react-text-mask-hoc
MIT License
19 stars 7 forks source link
input-mask inputmask react react-native text-formatting text-mask

react-text-mask-hoc · npm

A higher-order text-mask component for React and React Native.

text-mask is great. It's a feature-rich solution for creating input masks for various libraries and frameworks. However the React implementation has some long-standing bugs and feature requests that bury the potential of the library.

Features:

Table of Contents

Install

yarn add react-text-mask-hoc
  # or
npm install --save react-text-mask-hoc

Usage

import {TextMask, InputAdapter} from 'react-text-mask-hoc';

export default () => (
    <TextMask
        // You can provide your own adapter component or use one of included in the library.
        Component={InputAdapter}
        mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
        guide={false}
        value="5554953947"
    />
);

To use in React Native import react-text-mask-hoc/ReactNative instead:

import {TextMask, TextInputAdapter} from 'react-text-mask-hoc/ReactNative';

Examples

API

TextMask

A component that grants text-mask functionality to the passed component.

It's a controlled component by default, but it also maintains its own state, however it can also be switched to uncontrolled.

Props

Instance methods


Adapters

Adapters are React components that implement a special interface for the withTextMask.

List of adapters included in this library:

Specification

An adapter must be a React component that takes value, caretPosition and onChange props, and exposes a caretPosition getter that always returns a positive integer number.


TextMaskTransformer

A class that calculates a value and a caret position. Based on the createTextMaskInputElement() from text-mask-core.

Exported for testing purposes only.