import { forwardRef, ReactElement, useImperativeHandle, useRef, useState } from 'react'; import { NativeSyntheticEvent, NativeTouchEvent, StyleProp, Text, TextInput, View, ViewProps, ViewStyle, } from 'react-native'; import TextInputMask from 'react-native-text-input-mask'; import { Icon } from '../../components'; import { tw } from '../../core/tailwind'; export type MaskedTextFieldProps = React.ComponentProps & { ref?: React.RefObject; label?: string; mask?: string; errorText?: string | undefined; containerStyle?: StyleProp; textFieldStyle?: StyleProp; labelStyle?: StyleProp; leftIcon?: string | ReactElement; rightIcon?: string | ReactElement; bordered?: boolean; required?: boolean; focus?: boolean; }; export const MaskedTextField: React.FC = forwardRef( (props, ref) => { const inputRef = useRef(null); const { label, errorText, value, onBlur, onFocus, onChangeText, mask, containerStyle, textFieldStyle, labelStyle, leftIcon, rightIcon, onPressIn, bordered = true, required, ...restOfProps } = props; const [isFocused, setIsFocused] = useState(false); useImperativeHandle(ref, () => ({ focus: () => inputRef.current?.focus(), })); const handlePressIn = (e: NativeSyntheticEvent) => { onPressIn?.(e); }; const renderIcon = (icon: string | React.ReactElement) => { if (typeof icon === 'string' || icon instanceof String) { return ; } return icon; }; return ( {label ? ( {label} * ) : null} {leftIcon ? {renderIcon(leftIcon)} : null} { setIsFocused(false); onBlur?.(event); }} onFocus={(event) => { setIsFocused(true); onFocus?.(event); }} onPressIn={handlePressIn} onChangeText={(_formatted, extracted) => { return onChangeText?.(extracted ? extracted : ''); }} {...restOfProps} /> {rightIcon ? {renderIcon(rightIcon)} : null} {errorText ? {errorText} : null} ); } ) as React.FC;