Created
September 25, 2018 21:05
-
-
Save mauriciord/ee2d6fc55179daf86f7ecc884e1a2c50 to your computer and use it in GitHub Desktop.
Masked Input React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import phone from 'shared/masks/phone'; | |
import cep from 'shared/masks/cep'; | |
const masks = { | |
phone, | |
cep, | |
}; | |
const getMask = (mask) => { | |
if (Object.keys(masks).includes(mask)) { | |
return masks[mask]; | |
} | |
return value => value; | |
}; | |
const onChangeTextWrapper = (props, mask) => (text) => { | |
props.form.setFieldValue(props.field.name, mask(text)); | |
}; | |
const MaskedInput = (props) => { | |
const Component = props.maskedComponent; | |
const onChangeText = onChangeTextWrapper(props, getMask(props.mask)); | |
return <Component {...props} onChangeText={onChangeText} />; | |
}; | |
export default MaskedInput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment