0.14.2 • Published 10 months ago

react-native-mask-text v0.14.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

react-native-mask-text

This is a library to mask Text and Input components in React Native and Expo (Android, iOS and Web).

Motivation

This package was created based on other libraries of React Native text mask, with the goal of meeting the need of having a package to be used with all React Native contexts (multi-platform concept) and also be maintained currently. You can read this blog post on Substack to see more information about the creation to the current moment of this package.

Install

yarn add react-native-mask-text

Custom Mask

Pattern used in masked components:

  • 9 - accept digit.
  • A - accept alpha.
  • S - accept alphanumeric.

Ex: AAA-9999

Usage MaskedTextInput (custom)

Component similar with <TextInput /> but with custom mask option.

import { StyleSheet } from "react-native";
import { MaskedTextInput } from "react-native-mask-text";

//...

<MaskedTextInput
  mask="AAA-9999"
  onChangeText={(text, rawText) => {
    console.log(text);
    console.log(rawText);
  }}
  style={styles.input}
/>;

//...

const styles = StyleSheet.create({
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
  },
});

Usage MaskedText (custom)

Component similar with <Text /> but with custom mask option.

import { MaskedText } from "react-native-mask-text";

//...

<MaskedText mask="99/99/9999">30081990</MaskedText>;

Date Mask

These options only are used if you use prop type="date" in your component:

OptionTypeMandatoryDefault ValueDescription
dateFormatstringNoyyyy/mm/ddDate Format

Usage MaskedTextInput (date)

Component similar with <TextInput /> but with date mask option.

import { StyleSheet } from "react-native";
import { MaskedTextInput } from "react-native-mask-text";

//...

<MaskedTextInput
  type="date"
  options={{
    dateFormat: 'YYYY/DD/MM',
  }}
  onChangeText={(text, rawText) => {
    console.log(text);
    console.log(rawText);
  }}
  style={styles.input}
  keyboardType="numeric"
/>

//...

const styles = StyleSheet.create({
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
  },
});

Time Mask

These options only are used if you use prop type="time" in your component:

OptionTypeMandatoryDefault ValueDescription
timeFormatstringNoHH:mm:ssTime Format

Usage MaskedTextInput (time)

Component similar with <TextInput /> but with time mask option.

import { StyleSheet } from "react-native";
import { MaskedTextInput } from "react-native-mask-text";

//...

<MaskedTextInput
  type="time"
  options={{
    timeFormat: 'HH:mm:ss', // or 'HH:mm'
  }}
  onChangeText={(text, rawText) => {
    setMaskedValue(text)
    setUnmaskedValue(rawText)
  }}
  style={styles.input}
  keyboardType="numeric"
/>

//...

const styles = StyleSheet.create({
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
  },
});

Currency Mask

These options only are used if you use prop type="currency" in your component:

OptionTypeMandatoryDefault ValueDescription
prefixstringNonullString to prepend
decimalSeparatorstringNonullSeparation for decimals
groupSeparatorstringNonullGrouping separator of the integer part
precisionnumberNo0Precision for fraction part (cents)
groupSizenumberNo3Primary grouping size of the integer part
secondaryGroupSizenumberNonullSecondary grouping size of the integer part
fractionGroupSeparatorstringNonullGrouping separator of the fraction part
fractionGroupSizenumberNonullGrouping size of the fraction part
suffixstringNonullString to append

Usage MaskedTextInput (currency)

Component similar with <TextInput /> but with currency mask option.

import { StyleSheet } from "react-native";
import { MaskedTextInput } from "react-native-mask-text";

//...

<MaskedTextInput
  type="currency"
  options={{
    prefix: '$',
    decimalSeparator: '.',
    groupSeparator: ',',
    precision: 2
  }}
  onChangeText={(text, rawText) => {
    console.log(text);
    console.log(rawText);
  }}
  style={styles.input}
  keyboardType="numeric"
/>

//...

const styles = StyleSheet.create({
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
  },
});

Usage MaskedText (currency)

Component similar with <Text /> but with currency mask option.

import { MaskedText } from "react-native-mask-text";

//...

<MaskedText
  type="currency"
  options={{
    prefix: '$',
    decimalSeparator: '.',
    groupSeparator: ',',
    precision: 2
  }}
>
  5999
</MaskedText>;

Usage mask function

Function used to mask text.

import { mask } from "react-native-mask-text";

const code = mask("ABC1234","AAA-9999") // return ABC-1234

Usage unMask function

Function used to remove text mask.

import { unMask } from "react-native-mask-text";

const code = unMask("ABC-1234") // return ABC1234

Example

You can see an example app with Expo CLI here.

You can see a SignUp example on Expo Snack working with iOS, Mobile, and Web here.

Contributing

See Contributing.md

License

The app's source code is made available under the MIT license. Some of the dependencies are licensed differently, with the BSD license, for example.

Contact

Akinn Rosa - Github - akinncar@hotmail.com

0.14.0

11 months ago

0.14.1

11 months ago

0.14.2

10 months ago

0.13.2

1 year ago

0.13.0

2 years ago

0.13.1

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.12.1

2 years ago

0.10.0

2 years ago

0.9.0

2 years ago

0.8.0

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago