Browse Source

Fixing bug when phone number has only 10 digits

master
GabrielTrettel 3 years ago
parent
commit
23ee4e2c8d
  1. 46
      src/app/components/forms/PhoneNumberFormField.js

46
src/app/components/forms/PhoneNumberFormField.js

@ -1,25 +1,33 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import { useFormikContext } from "formik"; import { useFormikContext } from "formik";
import { unMask, mask } from "react-native-mask-text";
import TextInput from "../TextInput"; import TextInput from "../TextInput";
import ErrorMessage from "./ErrorMessage"; import ErrorMessage from "./ErrorMessage";
import { View, Text, StyleSheet } from "react-native"; import { View, Text, StyleSheet } from "react-native";
import { TouchableOpacity } from "react-native-gesture-handler"; import { TouchableOpacity } from "react-native-gesture-handler";
import colors from "../../config/colors"; import colors from "../../config/colors";
import defaultStyles from "../../config/styles"; import defaultStyles from "../../config/styles";
import { MaskedTextInput } from "react-native-mask-text";
// import { MaskedTextInput } from "react-native-mask-text";
function PhoneNumberFormField({ function PhoneNumberFormField({
name, name,
flex = 0, flex = 0,
maxLength = 0,
maxLength = 12,
placeholder = "", placeholder = "",
paddingLeft = 16, paddingLeft = 16,
paddingRight = 16, paddingRight = 16,
}) { }) {
const { values, setFieldValue, errors, touched } = useFormikContext();
const {
values,
setFieldTouched,
setFieldValue,
errors,
touched,
} = useFormikContext();
var current_mask =
values[name].length >= 11 ? "(99) 99999-9999" : "(99) 9999-9999";
// console.log(values);
return ( return (
<View flex={flex}> <View flex={flex}>
<View <View
@ -28,14 +36,15 @@ function PhoneNumberFormField({
paddingRight: paddingRight, paddingRight: paddingRight,
}} }}
> >
<MaskedTextInput
mask="(99) 99999-9999"
placeholder={placeholder}
placeholderTextColor={colors.medium}
onChangeText={(_, rawText) => {
<TextInput
height={48}
onBlur={() => setFieldTouched(name)}
onChangeText={(val) => {
const rawText = unMask(val, current_mask);
setFieldValue(name, rawText.substr(0, maxLength)); setFieldValue(name, rawText.substr(0, maxLength));
}} }}
style={styles.container}
value={mask(values[name], current_mask)}
placeholder={placeholder}
keyboardType="numeric" keyboardType="numeric"
/> />
</View> </View>
@ -47,19 +56,4 @@ function PhoneNumberFormField({
); );
} }
const styles = StyleSheet.create({
container: {
...defaultStyles.shadow,
...defaultStyles.text,
flexDirection: "row",
alignItems: "center",
backgroundColor: colors.white,
borderRadius: 6,
borderColor: colors.grayBG,
borderWidth: 1,
padding: 5,
paddingLeft: 12,
height: 60,
},
});
export default PhoneNumberFormField; export default PhoneNumberFormField;
Loading…
Cancel
Save