README
react-masked-text
This is a simple masked text (normal text and input text) component for React.
Thanks to vanilla-masker =). Thanks to benhurott
Supported Versions
React: 16.0.0 or higher
Install
npm install react-masked-text --save
Usage (TextInputMask)
import React, {Component} from 'react';
// import the component
import {TextInputMask} from 'react-masked-text';
export default class MyComponent extends Component {
constructor(props) {
super(props);
}
isValid() {
// isValid method returns if the inputed value is valid.
// Ex: if you input 40/02/1990 30:20:20, it will return false
// because in this case, the day and the hour is invalid.
let valid = this.refs['myDateText'].isValid();
// get converted value. Using kind=datetime, it returns the Date object.
// If it's using kind=money, it returns a Number object.
let rawValue = this.refs['myDateText'].getRawValue();
}
render() {
// the kind is required but options is required only for some specific kinds.
return (
<TextInputMask
ref={'myDateText'}
kind={'datetime'}
options={{
format: 'DD-MM-YYYY HH:mm:ss'
}} />
);
}
}
Props
value
If you set this prop, this component becomes a controlled component.
defaultValue
Use this props if you're using this component as an uncontrolled component and you want to set its default value (initial value). You may notice that it doesn't make sense to set value and defaultValue at the same time.
kind
credit-card: use the mask 9999 9999 9999 9999. It accepts options (see later in this doc).
cpf: use the mask 999.999.999-99
and numeric
keyboard.
cnpj: use the mask 99.999.999/9999-99
and numeric
keyboard.
zip-code: use the mask 99999-999
and numeric
keyboard.
only-numbers: accept only numbers on field with numeric
keyboard.
money: use the mask R$ 0,00
on the field with numeric
keyboard. It accepts options (see later in this doc).
cel-phone: use the mask (99) 9999-9999
or (99) 99999-9999
(changing automaticaly by length). It accepts options (see later in this doc).
datetime: use datetime mask with a similiar moment format (default DD/MM/YYYY HH:mm:ss). It accepts options (see later in this doc).
custom: use your custom mask (see the options props later in this doc).
onChangeText
Invoked after new value applied to mask.
/**
* @param {String} text the text AFTER mask is applied.
*/
onChangeText(text) {
// ...
}
<TextInputMask
kind={'only-numbers'}
onChangeText={this.onChangeText.bind(this)} />
checkText
Allow you to check and prevent value to be inputed.
/**
* @param {String} previous the previous text in the masked field.
* @param {String} next the next text that will be setted to field.
* @return {Boolean} return true if must accept the value.
*/
checkText(previous, next) {
return next === 'your valid value or other boolean condition';
}
<TextInputMask
kind={'only-numbers'}
checkText={this.checkText.bind(this)} />
Options
Some kinds accept options, use it like this: <TextInputMask kind={'money'} options={{ unit: 'US