GithubHelp home page GithubHelp logo

vlad-zhukov / react-text-mask-hoc Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 7.0 2.6 MB

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

Home Page: https://npm.im/react-text-mask-hoc

License: MIT License

JavaScript 100.00%
text-mask input-mask inputmask text-formatting react react-native

react-text-mask-hoc's Introduction

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()

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.


react-text-mask-hoc's People

Contributors

dependabot[bot] avatar vlad-zhukov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-text-mask-hoc's Issues

Add demo on codesandbox.io?

Is it possible to link to a demo from the Readme?
Will help to understand the value proposition and usage.
Thanks

How can I change the size of TextMask component

<TextMask mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/, ' ', 'e', 'x', 't', ' ', /\d/, /\d/, /\d/, /\d/, /\d/]} Component={InputAdapter} className="form-control" type="text" id="phone_s_PhoneNumber" name="phone_s_PhoneNumber" value={this.props.agencyInfo.phone_s_PhoneNumber} placeholder="" onChange={this.props.handleChange} required />
size="sm" or bsSize="sm" doesn't work for me
Any good idea?
Thanks.

Repeat parts in regex mask

Hi! Is it possible to make such a combination of characters?
(([\d\D]{5})([-]{1})){9} for the next check x4294-24H20-40j49-02d42-49s94-49292-49920-99222-24393
Not working for me :(
Thanks!

OnChange usage

Could you give an example of usage of onChange prop? I'm trying using but without success. Thanks

How can I make the placeholder sign two characters after decimal point

Thank you for the library. How can I make the placeholder that comes after decimal point 2 digits? For example when user tries to write 12.35 after writing 12 and writing '.' the component automatically shows '' placeholder with one digit like this 12. . When user tries to write 12.35 he ends up writing 12.53 because it accepts one and returns back to start after the decimal point. When there are 2 decimal digits so it has to be '__' or nothing at all. Without that component unusable.
I tried to change how placeholderChar works in the source without success.

    <TextMask
                Component={TextInputAdapter}
                autoFocus={false}
                value={serializeDecimal(values.discount_amount, 'currencyinput')}
                mask={currencyMask}
                blur={e => handleDiscountChange(e.nativeEvent.text, 'amount')}
              />


   currencyMask = createNumberMask({
      prefix: '$',
      decimalSymbol: maskValues.decimalSymbol,
      thousandsSeparatorSymbol: maskValues.thousandsSeparatorSymbol,
      decimalLimit: 2,
      requireDecimal: maskValues.requireDecimal,
      suffix: maskValues.currencySuffix,
      includeThousandsSeparator: true,
      allowLeadingZeroes: false,
      allowDecimal: true,
      allowNegative: true
    });

Cursor Issue on IOS Devices

React Native Environment Info:
System:
OS: macOS 10.14.1
CPU: x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
Memory: 1.07 GB / 8.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.10.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
IDEs:
Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmPackages:
react: 16.5.0 => 16.5.0
react-native: 0.57.1 => 0.57.1
npmGlobalPackages:
react-native-cli: 2.0.1
react-native-git-upgrade: 0.2.7

Description:

I am currently using Formik with react-text-mask-hoc to do a phone number masked input. It works perfectly fine on Android however on IOS the cursor goes the wrong character after passing the masked characters.
As shown in the GIF after entering 868 the cursor jumps back to the second position.
ezgif com-video-to-gif

This is my set-up

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

 <TextMask
          Component={TextInputAdapter}
          guide={false}
          mask={[/[1-9]/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
          maxLength={12}
          keyboardType={keyboardType}
          value={value}
          onChange={this.handleChange}
          onBlur={this.handleTouch}
          error={error}
          showMask
          style={styles.FieldInput}
        />

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.