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

@elbstack/react-text-mask-hoc

v0.10.6

Published

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

Downloads

42

Readme

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:

  • Supports all features from text-mask, see its documentation for more information.
  • Custom components: you can mask any components through a simple adapter interface!
  • Platform agnostic: works in all browsers, React Native and Node.js (useful for server-side rendering)!

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

  • all text-mask settings
  • Component (React.Component): A component that follows the adapter specification.
  • [value] (String|Number): A value that will be masked. Will be used as an initial value on mounting, and later can be used to control the component. If isControlled prop is set to false, the value will be ignored on rerenders. Defaults to null.
  • [isControlled] (Boolean): A way to set the component behaviour to be controlled by a value prop or to ignore it (to be uncontrolled). Can also be used to switch it in runtime. Defaults to true.
  • [onChange] (Function): A function that is called on input changes. Takes 2 arguments: the native event (varies from a platform) and the next state (has value and caretPosition properties).
  • [componentRef] (Function): A function that is called with a reference to the Component.

Instance methods

  • the value getter
  • focus()
  • blur()

withTextMask(AdaptedComponent)

A helper HOC that passes AdaptedComponent down to the TextMask.

Arguments

  1. AdaptedComponent (React.Component): A component that follows the adapter specification.

Returns

An extended TextMask.

Usage

import {withTextMask} from 'react-text-mask-hoc';

class MyAdapterComponent extends React.Component {
    // ...
}

const MyTextMaskComponent = withTextMask(MyAdapterComponent)

export default () =>
    <MyTextMaskComponent
        mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
        guide={false}
        value="5554953947"
    />;

Adapters

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

List of adapters included in this library:

  • for React
    • InputAdapter
    • SpanAdapter
  • for React Native
    • TextInputAdapter
    • TextAdapter

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.