Skip to content

Instantly share code, notes, and snippets.

@mauriciord
Created September 25, 2018 21:05
Show Gist options
  • Save mauriciord/ee2d6fc55179daf86f7ecc884e1a2c50 to your computer and use it in GitHub Desktop.
Save mauriciord/ee2d6fc55179daf86f7ecc884e1a2c50 to your computer and use it in GitHub Desktop.
Masked Input React Native
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