import { useRef } from 'react'; import { StyleProp, Text, View, ViewStyle } from 'react-native'; import { tw } from '../../core/tailwind'; import PhoneInputRN from 'react-native-phone-number-input'; import { FieldValues, UseFormReturn } from 'react-hook-form'; const PhoneInput = PhoneInputRN as any; export type PhoneNumberFieldProps = React.ComponentProps & { label?: string; errorText?: string | undefined; containerStyle?: StyleProp; labelStyle?: StyleProp; required?: boolean; form?: UseFormReturn; name?: string; }; export const PhoneNumberField = (props: PhoneNumberFieldProps) => { const { label, errorText, editable, containerStyle, labelStyle, required, name, form, ...restOfProps } = props; const phoneInputRef = useRef(null); return ( {label ? ( {label} * ) : null} { if (form) { form.setValue(name, t); if (phoneInputRef.current && !phoneInputRef.current.isValidNumber(t)) { form.setError(name, { type: 'custom', message: 'Invalid phone number' }); } else { form.clearErrors(name); } } }} {...restOfProps} /> {errorText ? {errorText} : null} ); };