import React, { useCallback, useState } from 'react'; import { View } from 'react-native'; import type { BlurEvent, FocusEvent } from 'react-native'; import type { IconName } from '@hero-design/rn'; import { StyledInputRow } from './StyledTextInput'; import PrefixComponent from './PrefixComponent'; import InputComponent from './InputComponent'; import type { State } from './StyledTextInput'; import type { NativeTextInputProps, TextInputRef, TextInputVariant, } from './types'; interface InputRowProps { /** Current state of the input (focused, error, disabled, etc.) */ state: State; /** Optional prefix icon or component */ prefix?: IconName | React.ReactElement; /** Input variant - 'text' or 'textarea' */ variant: TextInputVariant; /** Native TextInput props passed to the input component */ nativeInputProps: NativeTextInputProps; /** Optional custom render function for input value */ renderInputValue?: ( inputProps: NativeTextInputProps, ref?: React.ForwardedRef ) => React.ReactNode; /** Whether the input value is empty */ isEmptyValue: boolean; /** Whether the input should be visible when not focused and empty. Defaults to true. */ shouldShowWhenUnfocused?: boolean; /** Test ID for testing purposes */ testID?: string; } /** * InputRow Component * * Layout Structure: * ┌─────────────────────────────────────────────────────────┐ * │ StyledInputRow │ * │ ┌──────────┐ ┌─────────────────────────────────────┐ │ * │ │ Prefix │ │ Input Field │ │ * │ │ (Icon or │ │ (TextInput or Custom) │ │ * │ │ Custom) │ │ flex: 1 │ │ * │ └──────────┘ └─────────────────────────────────────┘ │ * └─────────────────────────────────────────────────────────┘ * * Rendering Behavior: * - Components are always rendered but hidden with opacity in idle state * - Uses accessibility props to hide components from screen readers when not visible * * Renders the main input row containing: * 1. Conditionally visible prefix component (icon or custom element) * 2. Conditionally visible input component (TextInput or custom rendered input) */ const InputRow = React.forwardRef( ( { state, prefix, variant, nativeInputProps, renderInputValue, isEmptyValue, testID, shouldShowWhenUnfocused = false, }, ref ) => { // We need to track focus state locally because the native TextInput's onFocus/onBlur // events are the source of truth for when the input is actually focused, and we need // this state to control visibility aand accessibility behavior const [isFocused, setIsFocused] = useState(false); const shouldShow = shouldShowWhenUnfocused || isFocused || !isEmptyValue; // Simplified callback functions (removed unnecessary memoization for simple cases) const handleFocus = useCallback( (event?: FocusEvent) => { setIsFocused(true); nativeInputProps.onFocus?.(event); }, [nativeInputProps] ); const handleBlur = useCallback( (event?: BlurEvent) => { setIsFocused(false); nativeInputProps.onBlur?.(event); }, [nativeInputProps] ); return ( {/* Prefix animation */} {/* Input animation */} ); } ); export default InputRow;