import { forwardRef, useImperativeHandle, useRef } from 'react'; import { Animated, Pressable, TextInput, type NativeSyntheticEvent, type TextInputFocusEventData, type TextInputProps } from 'react-native'; import { Icon } from '../../components/wui-icon'; import useAnimatedValue from '../../hooks/useAnimatedValue'; import { useTheme } from '../../hooks/useTheme'; import type { IconType, SizeType } from '../../utils/TypesUtil'; import styles, { outerBorderRadius } from './styles'; const AnimatedPressable = Animated.createAnimatedComponent(Pressable); interface InputRef { clear: () => void; focus: () => void; blur: () => void; } export type InputTextProps = TextInputProps & { inputStyle?: TextInputProps['style']; icon?: IconType; disabled?: boolean; size?: Exclude; }; export const InputText = forwardRef( ( { children, placeholder, inputStyle, icon, size = 'sm', disabled, returnKeyType, onBlur, onFocus, ...rest }: InputTextProps, ref ) => { const inputRef = useRef(null); const Theme = useTheme(); const { animatedValue, valueRef, setStartValue, setEndValue } = useAnimatedValue( Theme['gray-glass-002'], Theme['gray-glass-005'], 100 ); const outerRadius = outerBorderRadius[size]; useImperativeHandle(ref, () => ({ clear: () => { if (inputRef.current) { inputRef.current.clear(); } }, focus: () => { if (inputRef.current) { inputRef.current.focus(); } }, blur: () => { if (inputRef.current) { inputRef.current.blur(); } } })); const handleBlur = (e: NativeSyntheticEvent) => { setStartValue(); onBlur?.(e); }; const handleFocus = (e: NativeSyntheticEvent) => { setEndValue(); onFocus?.(e); }; const innerBorder = valueRef.current.interpolate({ inputRange: [0, 1], outputRange: [Theme['gray-glass-005'], Theme['accent-100']] }); const outerBorder = valueRef.current.interpolate({ inputRange: [0, 1], outputRange: ['transparent', Theme['accent-glass-015']] }); return ( <> inputRef.current?.focus()} testID={rest.testID} > {icon && } {children} ); } );