import type { ComponentProps, ReactElement, ReactNode, Ref } from 'react'; import { forwardRef, useImperativeHandle, useRef, useState } from 'react'; import type { NativeSyntheticEvent, NativeTouchEvent, StyleProp, TextInputProps, ViewStyle } from 'react-native'; import { Text, TextInput, View } from 'react-native'; import { tw } from '../../core/tailwind'; import { Icon } from './Icon'; export type TextFieldProps = ComponentProps & { ref?: Ref; label?: string; errorText?: string | undefined; containerStyle?: StyleProp; textFieldStyle?: StyleProp; labelStyle?: StyleProp; leftIcon?: string | ReactElement; rightIcon?: string | ReactElement; beforeText?: ReactNode; bordered?: boolean; bgStyle?: StyleProp; required?: boolean; focus?: boolean; iconStyle?: any; }; export const TextField = forwardRef((props, ref) => { const inputRef = useRef(null); const { label, errorText, value, onBlur, onFocus, onChangeText, containerStyle, textFieldStyle, labelStyle, leftIcon, rightIcon, onPressIn, bordered = true, required, iconStyle, bgStyle, ...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={onChangeText} {...restOfProps} /> {rightIcon ? {renderIcon(rightIcon)} : null} {errorText ? {errorText} : null} ); }) as React.FC;