import React, { useImperativeHandle, useRef } from 'react'; import type { TextInput as RNTextInput, TextInputProps as RNTextInputProps, } from 'react-native'; import type { State } from '@hero-design/rn/src/components/TextInput/StyledTextInput'; import { StyledTextInput } from './StyledTextInput'; import { useTheme } from '../../theme'; import type { NativeTextInputProps, TextInputRef, TextInputVariant, } from './types'; interface DefaultTextInputProps { variant: TextInputVariant; /** Native TextInput props */ nativeInputProps: NativeTextInputProps; state: State; } const DefaultTextInput = React.forwardRef( ({ variant, nativeInputProps, state }, ref) => { const theme = useTheme(); const inputRef = useRef(null); useImperativeHandle( ref, () => ({ focus: () => inputRef.current?.focus(), blur: () => inputRef.current?.blur(), clear: () => inputRef.current?.clear(), isFocused: () => inputRef.current?.isFocused() ?? false, setNativeProps: (props: RNTextInputProps) => { inputRef.current?.setNativeProps(props); }, }), [] ); // Default styled input with theme and variant support return ( ); } ); interface InputComponentProps { /** Input type ('text' or 'textarea') */ variant: TextInputVariant; /** All props passed to the underlying TextInput */ nativeInputProps: NativeTextInputProps; /** Optional custom input renderer function */ renderInputValue?: ( inputProps: NativeTextInputProps, ref?: React.ForwardedRef ) => React.ReactNode; /** Current state of the input (default, filled, disabled, readonly, error) */ state: State; } /** * InputComponent * * Renders the actual text input element. Supports custom input rendering or uses the default StyledTextInput. * * Key Features: * - Flexible rendering: supports custom input renderers via renderInputValue prop * - Variant support: handles both 'text' and 'textarea' input types * - Multiline handling: automatically enables multiline for textarea variant * - Theme integration: applies theme colors for placeholder text * - Ref forwarding: properly forwards refs to the underlying TextInput * * Rendering Logic: * 1. If renderInputValue provided: Use custom renderer * 2. Otherwise: Use default StyledTextInput with theme and variant support * * Multiline Behavior: * - 'textarea' variant: Always multiline * - 'text' variant: Single line unless explicitly set via nativeInputProps * * @param props - The component props (see InputComponentProps interface for details) */ const InputComponent = React.forwardRef( ({ variant, nativeInputProps, renderInputValue, state }, ref) => { if (renderInputValue) { return <>{renderInputValue({ ...nativeInputProps }, ref)}; } return ( ); } ); InputComponent.displayName = 'InputComponent'; export default React.memo(InputComponent);