import React, { ReactElement } from 'react'; import { TextInput, Text, View, StyleSheet, TextInputProps, TouchableOpacity, ViewStyle, } from 'react-native'; import { useFormContext } from '../context/FormContext'; import { CountryCode } from '../types'; interface SmartFormFieldProps extends Omit { name: string; placeholder?: string; style?: any; errorStyle?: any; label?: string; labelStyle?: any; leftIcon?: ReactElement | (() => ReactElement); rightIcon?: ReactElement | (() => ReactElement); onLeftIconPress?: () => void; onRightIconPress?: () => void; leftIconStyle?: ViewStyle; rightIconStyle?: ViewStyle; inputContainerStyle?: ViewStyle; countryCode?: CountryCode; } export const SmartFormField: React.FC = ({ name, placeholder, style, errorStyle, label, labelStyle, leftIcon, rightIcon, onLeftIconPress, onRightIconPress, leftIconStyle, rightIconStyle, inputContainerStyle, countryCode, ...textInputProps }) => { const form = useFormContext(); const fieldProps = form.getFieldProps(name); const renderIcon = (icon: ReactElement | (() => ReactElement) | undefined) => { if (!icon) return null; return typeof icon === 'function' ? icon() : icon; }; return ( {label && {label}} {leftIcon && (onLeftIconPress ? ( {renderIcon(leftIcon)} ) : ( {renderIcon(leftIcon)} ))} {rightIcon && (onRightIconPress ? ( {renderIcon(rightIcon)} ) : ( {renderIcon(rightIcon)} ))} {fieldProps.error && {fieldProps.error}} ); }; const styles = StyleSheet.create({ container: { marginBottom: 16, }, label: { fontSize: 14, fontWeight: '600', marginBottom: 4, color: '#333', }, inputContainer: { flexDirection: 'row', alignItems: 'center', borderWidth: 1, borderColor: '#ddd', borderRadius: 8, backgroundColor: '#fff', }, inputContainerError: { borderColor: '#e74c3c', }, input: { flex: 1, padding: 12, fontSize: 16, color: '#000', }, inputWithLeftIcon: { paddingLeft: 4, }, inputWithRightIcon: { paddingRight: 4, }, iconContainer: { justifyContent: 'center', alignItems: 'center', padding: 8, }, leftIconContainer: { paddingLeft: 12, }, rightIconContainer: { paddingRight: 12, }, error: { color: '#e74c3c', fontSize: 12, marginTop: 4, }, });