1589366460
This is a simple masked text (normal text and input text) component for React-Native.
Hey guys!
Unfortunatelly I’m not developing js apps anymore. This repo will not receive updates anymore.
React-native: 0.32.0 or higher
npm install react-native-masked-text --save
For all the masks you will use in this way:
import { TextInputMask } from 'react-native-masked-text'
//...
<TextInputMask
type={'type-of-the-mask'}
options={
{
// the options for your mask if needed
}
}
// dont forget to set the "value" and "onChangeText" props
value={this.state.text}
onChangeText={text => {
this.setState({
text: text
})
}}
/>
Mask:
(99) 9999-9999
or (99) 99999-9999
(will detect automatically)+999 999 999 999
If you need a different formatting, use the Custom
mask =).
Sample code (source):
<TextInputMask
type={'cel-phone'}
options={{
maskType: 'BRL',
withDDD: true,
dddMask: '(99) '
}}
value={this.state.international}
onChangeText={text => {
this.setState({
international: text
})
}}
/>
name | type | required | default | description |
---|---|---|---|---|
maskType | string | no | maskType |
the type of the mask to use. Available: BRL or INTERNATIONAL |
withDDD | boolean | no | true |
if the mask type is BRL , include the DDD |
dddMask | string | no | (99) |
if the mask type is BRL , the DDD mask |
You can get the unmasked
value using the getRawValue
method:
<TextInputMask
type={'cel-phone'}
options={{
maskType: 'BRL',
withDDD: true,
dddMask: '(99) '
}}
value={this.state.international}
onChangeText={text => {
this.setState({
international: text
})
}}
// add the ref to a local var
ref={(ref) => this.phoneField = ref}
/>
//...
const unmasked = this.phoneField.getRawValue()
// in the mask: (51) 98765-4321
// unmasked: 51987654321
Mask: 999.999.999-99
Sample code (source):
<TextInputMask
type={'cpf'}
value={this.state.cpf}
onChangeText={text => {
this.setState({
cpf: text
})
}}
/>
You can check if the cpf is valid by calling the isValid()
method:
<TextInputMask
type={'cpf'}
value={this.state.cpf}
onChangeText={text => {
this.setState({
cpf: text
})
}}
// add the ref to a local var
ref={(ref) => this.cpfField = ref}
/>
// get the validation
const cpfIsValid = this.cpfField.isValid()
console.log(cpfIsValid) // boolean
You can get the unmasked
cpf calling the getRawValue
method:
const unmasked = this.cpfField.getRawValue()
// in the mask: 123.456.789-01
// unmasked: 12345678901
Mask: 99.999.999/9999-99
Sample code (source):
<TextInputMask
type={'cnpj'}
value={this.state.cnpj}
onChangeText={text => {
this.setState({
cnpj: text
})
}}
/>
You can check if the cnpj is valid by calling the isValid()
method:
<TextInputMask
type={'cnpj'}
value={this.state.cnpj}
onChangeText={text => {
this.setState({
cnpj: text
})
}}
// add the ref to a local var
ref={(ref) => this.cnpjField = ref}
/>
// get the validation
const cnpjIsValid = this.cnpjField.isValid()
console.log(cnpjIsValid) // boolean
You can get the unmasked
cpf calling the getRawValue
method:
const unmasked = this.cnpjField.getRawValue()
// in the mask: 99.999.999/9999-99
// unmasked: 99999999999999
Mask:
9999 9999 9999 9999
or 9999 **** **** 9999
(obfuscated)9999 999999 99999
or 9999 ****** 99999
(obfuscated)9999 999999 9999
or 9999 ****** 9999
(obfuscated)Sample code (source)
<TextInputMask
type={'credit-card'}
options={{
obfuscated: false,
issuer: 'amex'
}}
value={this.state.creditCard}
onChangeText={text => {
this.setState({
creditCard: text
})
}}
/>
name | type | required | default | description |
---|---|---|---|---|
obfuscated | boolean | no | false |
if the mask should be obfuscated or not |
issuer | string | no | visa-or-mastercard |
the type of the card mask. The options are: visa-or-mastercard , amex or diners |
You can get the array containing the groups of the value value using the getRawValue
method:
<TextInputMask
type={'credit-card'}
options={{
obfuscated: false,
issuer: 'amex'
}}
value={this.state.creditCard}
onChangeText={text => {
this.setState({
creditCard: text
})
}}
// add the ref to a local var
ref={(ref) => this.creditCardField = ref}
/>
//...
const unmasked = this.creditCardField.getRawValue()
// in the mask: 9874 6541 3210 9874
// unmasked: [9874, 6541, 3210, 9874]
Mask: defined by pattern
9
- accept digit.A
- accept alpha.S
- accept alphanumeric.*
- accept all, EXCEPT white space.Ex: AAA-9999
Sample code (source):
//
// SIMPLE
//
<TextInputMask
type={'custom'}
options={{
/**
* mask: (String | required | default '')
* the mask pattern
* 9 - accept digit.
* A - accept alpha.
* S - accept alphanumeric.
* * - accept all, EXCEPT white space.
*/
mask: '999 AAA SSS ***'
}}
value={this.state.text}
onChangeText={text => {
this.setState({
text: text
})
}}
style={textInputStype}
/>
//
// ADVANCED
//
<TextInputMask
type={'custom'}
options={{
// required
/**
* mask: (String | required | default '')
* the mask pattern
* 9 - accept digit.
* A - accept alpha.
* S - accept alphanumeric.
* * - accept all, EXCEPT white space.
*/
mask: '999 AAA SSS ***',
// optional
/**
* validator: (Function | optional | defaults returns true)
* use this funcion to inform if the inputed value is a valid value (for invalid phone numbers, for example). The isValid method use this validator.
*/
validator: function(value, settings) {
return true
},
/**
* getRawValue: (Function | optional | defaults return current masked value)
* use this function to parse and return values to use what you want.
* for example, if you want to create a phone number mask (999) 999-99-99 but want to get only
* the numbers for value, use this method for this parse step.
*/
getRawValue: function(value, settings) {
return 'my converted value';
},
/**
* translation: (Object | optional | defaults 9, A, S, *)
* the dictionary that translate mask and value.
* you can change defaults by simple override the key (9, A, S, *) or create some new.
*/
translation: {
// this is a custom translation. The others (9, A, S, *) still works.
// this translation will be merged and turns into 9, A, S, *, #.
'#': function(val) {
if (val === ' ') {
return val;
}
// if returns null, undefined or '' (empty string), the value will be ignored.
return null;
},
// in this case, we will override build-in * translation (allow all characters)
// and set this to allow only blank spaces and some special characters.
'*': function(val) {
return [' ', '#', ',', '.', '!'].indexOf(val) >= 0 ? val : null;
}
}
}}
value={this.state.text}
onChangeText={text => {
this.setState({
text: text
})
}}
style={textInputStype}
/>
name | type | required | default | description |
---|---|---|---|---|
mask | string | YES | The mask pattern | |
validator | function | no | function that returns true |
the function that’s validate the value in the mask |
getRawValue | function | no | return current value | function to parsed value (like unmasked or converted) |
translation | object (map{string,function}) | no | 9 - digit , A - alpha , S - alphanumeric , * - all, except space |
The translator to use in the pattern |
Mask:
DD/MM/YYYY HH:mm:ss
DD/MM/YYYY
MM/DD/YYYY
YYYY/MM/DD
HH:mm:ss
HH:mm
HH
You can use -
instead of /
if you want.
Sample code (source):
<TextInputMask
type={'datetime'}
options={{
format: 'YYYY/MM/DD'
}}
value={this.state.dt}
onChangeText={text => {
this.setState({
dt: text
})
}}
/>
name | type | required | default | description |
---|---|---|---|---|
format | string | YES | The date format to be validated |
You can check if the date is valid by calling the isValid()
method:
<TextInputMask
type={'datetime'}
options={{
format: 'YYYY/MM/DD'
}}
value={this.state.dt}
onChangeText={text => {
this.setState({
dt: text
})
}}
// add the ref to a local var
ref={(ref) => this.datetimeField = ref}
/>
// get the validation
const isValid = this.datetimeField.isValid()
console.log(isValid) // boolean
You can get the moment object from the date if it’s valid calling the getRawValue
method:
const momentDate = this.datetimeField.getRawValue()
Mask: R$999,99
(fully customizable)
Sample code (source):
// SIMPLE
<TextInputMask
type={'money'}
value={this.state.simple}
onChangeText={text => {
this.setState({
simple: text
})
}}
/>
// ADVANCED
<TextInputMask
type={'money'}
options={{
precision: 2,
separator: ',',
delimiter: '.',
unit: 'R$',
suffixUnit: ''
}}
value={this.state.advanced}
onChangeText={text => {
this.setState({
advanced: text
})
}}
/>
name | type | required | default | description |
---|---|---|---|---|
precision | number | no | 2 |
The number of cents to show |
separator | string | no | , |
The cents separator |
delimiter | string | no | . |
The thousand separator |
unit | string | no | R$ |
The prefix text |
suffixUnit | string | no | '' |
The sufix text |
You can get the number
value of the mask calling the getRawValue
method:
<TextInputMask
type={'money'}
value={this.state.simple}
onChangeText={text => {
this.setState({
simple: text
})
}}
// add the ref to a local var
ref={(ref) => this.moneyField = ref}
/>
const numberValue = this.moneyField.getRawValue()
console.log(numberValue) // Number
// CAUTION: the javascript do not support giant numbers.
// so, if you have a big number in this mask, you could have problems with the value...
Mask: accept only numbers
Sample code (source):
<TextInputMask
type={'only-numbers'}
value={this.state.value}
onChangeText={text => {
this.setState({
value: text
})
}}
/>
Mask: 99999-999
Sample code (source):
<TextInputMask
type={'zip-code'}
value={this.state.value}
onChangeText={text => {
this.setState({
value: text
})
}}
/>
You can get the unmasked
value using the getRawValue
method:
<TextInputMask
type={'zip-code'}
value={this.state.value}
onChangeText={text => {
this.setState({
value: text
})
}}
// add the ref to a local var
ref={(ref) => this.zipCodeField = ref}
/>
//...
const unmasked = this.zipCodeField.getRawValue()
// in the mask: 98765-321
// unmasked: 98765321
rawText
in onChangeText
[1.12.0+]If you need the raw value in every text change, you can use the includeRawValueInChangeText
.
It will provide the masked and the raw text in every text change.
<TextInputMask
type={'cpf'}
value={this.state.value}
includeRawValueInChangeText={true}
onChangeText={(maskedText, rawText) => {
// maskedText: 123.456.789-01
// rawText: 12345678901
}}
/>
TextInput
instanceIf you want to get the TextInput
raw component, use the getElement()
method:
<TextInputMask
type={'zip-code'}
value={this.state.value}
onChangeText={text => {
this.setState({
value: text
})
}}
// add the ref to a local var
ref={(ref) => this.zipCodeField = ref}
/>
//...
const textInput = this.zipCodeField.getElement()
If you want, you can block a value to be added to the text using the checkText
prop:
<TextInputMask
//...
/**
* @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';
}
}
/>
You can use this prop if you want custom text input instead native TextInput component:
const Textfield = MKTextField.textfield()
.withPlaceholder('Text...')
.withStyle(styles.textfield)
.build();
<TextInputMask
// ...
// the custom text input component
customTextInput={Textfield}
// the props to be passed to the custom text input
customTextInputProps={{
style:{ width: '80%' },
label:'Birthday'
}}
/>
You can use all the normal TextInput props from React-Native, with this in mind:
If you want, you can check the code samples in this repo:
react-native-masked-text-samples
import React, { Component } from 'react'
// import the component
import { TextMask } from 'react-native-masked-text'
export default class MyComponent extends Component {
constructor(props) {
super(props)
this.state = {
text: '4567123409871234'
}
}
render() {
// the type is required but options is required only for some specific types.
// the sample below will output 4567 **** **** 1234
return (
<TextMask
value={this.state.text}
type={'credit-card'}
options={{
obfuscated: true
}}
/>
)
}
}
The same of TextInputMask, but for React-Native Text component instead TextInput.
Warning: if the value not match the mask, it will not appear.
getElement()
: return the instance of Text component.
If you want, we expose the MaskService
. You can use it:
Methods
type
(String, required): the type of the mask (cpf
, datetime
, etc…)value
(String, required): the value to be maskedsettings
(Object, optional): if the mask type accepts options, pass it in the settings parametertype
(String, required): the type of the mask (cpf
, datetime
, etc…)maskedValue
(String, required): the masked value to be converted in raw valuesettings
(Object, optional): if the mask type accepts options, pass it in the settings parametertype
(String, required): the type of the mask (cpf
, datetime
, etc…)value
(String, required): the value to be maskedsettings
(Object, optional): if the mask type accepts options, pass it in the settings parameterEx:
import { MaskService } from 'react-native-masked-text'
var money = MaskService.toMask('money', '123', {
unit: 'US$',
separator: '.',
delimiter: ','
})
// money -> US$ 1.23
es2015
error throw by babel, try run react-native start --reset-cache
Author: benhurott
GitHub: https://github.com/benhurott/react-native-masked-text
#react-native #programming
1598839687
If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?
In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.
Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.
React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.
Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.
Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.
The popularity of React Native comes from its advantages. Some of its advantages are as follows:
Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.
React Native is very close to native. Consider the following aspects as described on the React Native website:
Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.
#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native
1593420654
Have you ever thought of having your own app that runs smoothly over multiple platforms?
React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. Hire Dedicated React Native Developer from top React Native development company, HourlyDeveloper.io to design a spectacular React Native application for your business.
Consult with experts:- https://bit.ly/2A8L4vz
#hire dedicated react native developer #react native development company #react native development services #react native development #react native developer #react native
1616494982
Being one of the emerging frameworks for app development the need to develop react native apps has increased over the years.
Looking for a react native developer?
Worry not! WebClues infotech offers services to Hire React Native Developers for your app development needs. We at WebClues Infotech offer a wide range of Web & Mobile App Development services based o your business or Startup requirement for Android and iOS apps.
WebClues Infotech also has a flexible method of cost calculation for hiring react native developers such as Hourly, Weekly, or Project Basis.
Want to get your app idea into reality with a react native framework?
Get in touch with us.
Hire React Native Developer Now: https://www.webcluesinfotech.com/hire-react-native-app-developer/
For inquiry: https://www.webcluesinfotech.com/contact-us/
Email: sales@webcluesinfotech.com
#hire react native developers #hire dedicated react native developers #hire react native developer #hiring a react native developer #hire freelance react native developers #hire react native developers in 1 hour
1626928787
Want to develop app using React Native? Here are the tips that will help to reduce the cost of react native app development for you.
Cost is a major factor in helping entrepreneurs take decisions about investing in developing an app and the decision to hire react native app developers in USA can prove to be fruitful in the long run. Using react native for app development ensures a wide range of benefits to your business. Understanding your business and working on the aspects to strengthen business processes through a cost-efficient mobile app will be the key to success.
#best react native development companies from the us #top react native app development companies in usa #cost of hiring a react native developer in usa #top-notch react native developer in usa #best react native developers usa #react native
1622532701
React Native is an open-source framework that gives you wings to create truly native applications that do not compromise on your users’ experiences. TechAhead is a renowned React Native app development company having years of experience and a proven track record of delivering high-quality React Native app development services in record time. Get in touch with the TechAhead consultants to get started with your next mobile app project.
For more details read our full blog here https://dripivplus.com/reasons-to-consider-react-native-for-your-next-mobile-application-development/
#react native app development company #react native app developer #react native app development service #react native app development #react native application development company