import React, { forwardRef, useCallback } from 'react'; import { StyleSheet } from 'react-native'; import type { StyleProp, ViewStyle, ColorValue, BlurEvent, FocusEvent, } from 'react-native'; import { StyledInputWrapper, StyledContainer, StyledBorder, StyledBottomContainer, StyledSuffixContainer, } from './StyledTextInput'; import { useTheme } from '../../theme'; import type { State } from './StyledTextInput'; import ErrorOrHelpText from './ErrorOrHelpText'; import SuffixComponent from './SuffixComponent'; import MaxLengthMessage from './MaxLengthMessage'; import FloatingLabel from './FloatingLabel'; import InputRow from './InputRow'; import type { InternalTextInputProps, NativeTextInputProps, TextInputHandles, TextInputProps, TextInputRef, } from './types'; export type { TextInputHandles, TextInputVariant, TextInputProps, } from './types'; export const getState = ({ disabled, error, editable, loading, isEmptyValue, }: { disabled?: boolean; error?: string; editable?: boolean; loading: boolean; isEmptyValue?: boolean; }): State => { if (disabled) { return 'disabled'; } if (error) { return 'error'; } if (!editable || loading) { return 'readonly'; } if (!isEmptyValue) { return 'filled'; } return 'default'; }; // Fix issue: Placeholder is not shown on iOS when multiline is true // https://github.com/callstack/react-native-paper/pull/3331 const EMPTY_PLACEHOLDER_VALUE = ' '; // Simplified style extraction functions const extractBackgroundColor = (style: StyleProp) => { const flattened = StyleSheet.flatten(style); return flattened?.backgroundColor; }; const extractBorderStyles = (style: StyleProp) => { const flattened = StyleSheet.flatten(style); if (!flattened) return {}; const borderKeys = Object.keys(flattened).filter((key) => key.startsWith('border') ); const borderStyles: Record = {}; borderKeys.forEach((key) => { borderStyles[key] = flattened[key as keyof typeof flattened]; }); return borderStyles; }; // Create container style without background color using a helper function const getContainerStyle = ( style: StyleProp, customBackgroundColor?: ColorValue ) => { const flattenedStyle = style ? { ...StyleSheet.flatten(style) } : {}; if (customBackgroundColor) { delete flattenedStyle.backgroundColor; } return flattenedStyle; }; /** * TextInput Layout Structure: * * ┌─────────────────────────────────────────────────────────────────────────┐ * │ StyledContainer (with StyledBorder overlay) │ * │ ┌─────────────────────────────────────────────────────────┐ │ * │ │ StyledInputWrapper │ │ * │ │ ┌─────────────────────────────────────────────────┐ │ │ * │ │ │ FloatingLabel (Optional) │ │ │ * │ │ │ "Label" or animated position │ │ ┌──────┐ │ * │ │ └─────────────────────────────────────────────────┘ │ │Suffix│ │ * │ │ │ │ Icon │ │ * │ │ ┌─────────────────────────────────────────────────┐ │ │ or │ │ * │ │ │ InputRow Component │ │ │Custom│ │ * │ │ │ ┌──────────┐ ┌─────────────────────────────┐ │ │ │(V- │ │ * │ │ │ │ Prefix │ │ Input Field │ │ │ │Center│ │ * │ │ │ │(Animated)│ │ (Animated TextInput) │ │ │ │ ed) │ │ * │ │ │ └──────────┘ └─────────────────────────────┘ │ │ └──────┘ │ * │ │ └─────────────────────────────────────────────────┘ │ │ * │ │ │ │ * │ │ ┌─────────────────────────────────────────────────┐ │ │ * │ │ │ StyledBottomContainer │ │ │ * │ │ │ ┌─────────────────┐ ┌─────────────────────┐ │ │ │ * │ │ │ │ Error/Help Text │ │ Character Count │ │ │ │ * │ │ │ │ (flex: 4) │ │ (flex: 1) │ │ │ │ * │ │ │ └─────────────────┘ └─────────────────────┘ │ │ │ * │ │ └─────────────────────────────────────────────────┘ │ │ * │ └─────────────────────────────────────────────────────────┘ │ * └─────────────────────────────────────────────────────────────────────────┘ * * Note: StyledBorder uses StyleSheet.absoluteFillObject to overlay the entire * StyledContainer, providing the border and background styling. */ export const InternalTextInput = forwardRef< TextInputHandles, InternalTextInputProps >( ( { label, prefix, suffix, style, textStyle, testID, accessibilityLabelledBy, error, required, editable = true, disabled = false, loading = false, maxLength, hideCharacterCount = false, helpText, value, defaultValue, renderInputValue, allowFontScaling = false, variant = 'text', accessibilityLabel, groupStyleEnabled = false, ...nativeProps }: InternalTextInputProps, ref?: React.Ref ) => { const { onFocus, onBlur, onChangeText, ...restOfNativeProps } = nativeProps; // Inline the simple getDisplayText function const displayText = (value !== undefined ? value : defaultValue) ?? ''; const isEmptyValue = displayText.length === 0; const [isFocused, setIsFocused] = React.useState(false); const state = getState({ disabled, error, editable, loading, isEmptyValue, }); const theme = useTheme(); const innerTextInput = React.useRef(null); React.useImperativeHandle( ref, () => ({ // we don't expose this method, it's for testing https://medium.com/developer-rants/how-to-test-useref-without-mocking-useref-699165f4994e getNativeTextInputRef: () => innerTextInput.current, focus: () => { innerTextInput.current?.focus(); }, clear: () => innerTextInput.current?.clear(), setNativeProps: (args: NativeTextInputProps) => innerTextInput.current?.setNativeProps?.(args), isFocused: () => innerTextInput.current?.isFocused() || false, blur: () => innerTextInput.current?.blur(), }), [innerTextInput] ); // Simplified style extraction const borderStyles = extractBorderStyles(textStyle); const customBackgroundColor = extractBackgroundColor(style); const backgroundColor = customBackgroundColor ?? theme.__hd__.textInput.colors.containerBackground; // Simplified callback functions (removed unnecessary memoization for simple cases) const handleFocus = useCallback( (event?: FocusEvent) => { setIsFocused(true); onFocus?.(event); }, [onFocus] ); const handleBlur = useCallback( (event?: BlurEvent) => { setIsFocused(false); onBlur?.(event); }, [onBlur] ); const handleChangeText = useCallback( (text: string) => { if (editable) { onChangeText?.(text); } }, [onChangeText, editable] ); const handleContainerPress = () => { innerTextInput.current?.focus(); }; // Create text input style without border properties const textInputStyle = textStyle ? { ...StyleSheet.flatten(textStyle) } : {}; Object.keys(borderStyles).forEach((key) => { delete textInputStyle[key as keyof typeof textInputStyle]; }); const nativeInputTestIDSuffix = testID ? `-${testID}` : ''; const nativeInputProps: NativeTextInputProps = { style: StyleSheet.flatten([ { backgroundColor, color: theme.__hd__.textInput.colors.text.default, }, textInputStyle, ]), testID: `text-input${nativeInputTestIDSuffix}`, accessibilityState: { disabled: state === 'disabled' || state === 'readonly', }, accessibilityLabelledBy, allowFontScaling, ...restOfNativeProps, onFocus: handleFocus, onBlur: handleBlur, onChangeText: handleChangeText, editable, maxLength, value, defaultValue, placeholder: isFocused || label === undefined ? nativeProps.placeholder : EMPTY_PLACEHOLDER_VALUE, accessibilityLabel, }; const containerStyle = getContainerStyle(style, customBackgroundColor); const isDisabledOrReadonly = state === 'disabled' || state === 'readonly'; return ( {/* StyledBorder: Absolute positioned overlay covering entire container - Uses StyleSheet.absoluteFillObject to cover full StyledContainer - Provides border styling and background color - pointerEvents="none" to allow interaction with underlying components */} {!!label && ( )} ); } ); const TextInput = React.forwardRef( (props, ref) => ); TextInput.displayName = 'TextInput'; export default TextInput;