import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState, } from 'react'; import { NativeSyntheticEvent, TextInput, TextInputFocusEventData, TouchableOpacity, View, } from 'react-native'; import { ApplicationContext, MiniAppContext } from '../Context'; import { Icon } from '../Icon'; import { Text, useScaleSize } from '../Text'; import { Spacing } from '../Consts'; import { ErrorView, FloatingView, getBorderColor, MAX_LENGTH } from './common'; import { InputTextAreaProps } from './index'; import styles from './styles'; import SystemTextInput from './SystemTextInput'; import { useComponentId } from '../Application'; const InputTextArea = forwardRef( ( { errorMessage, onChangeText, floatingIconColor, floatingIcon, floatingValue, onFocus, onBlur, disabled = false, value, defaultValue, height, placeholder, maxLength = MAX_LENGTH, required, errorSpacing, style, hintText, ...props }: InputTextAreaProps, ref, ) => { const componentName = 'InputTextArea'; const { componentId } = useComponentId( `${componentName}/${placeholder}`, props.accessibilityLabel, ); const { theme } = useContext(ApplicationContext); const context = useContext(MiniAppContext); const DEFAULT_HEIGHT = useScaleSize(104); const showBaseLineDebug = context?.features?.showBaseLineDebug ?? false; const scaledFontSize = useScaleSize(14); const [focused, setFocused] = useState(false); const [inputValue, setInputValue] = useState(defaultValue ?? ''); const inputRef = useRef(null); useEffect(() => { setInputValue(value ?? ''); }, [value]); const onClearText = () => { inputRef?.current?.clear(); inputRef.current?.setNativeProps({ text: '' }); _onChangeText(''); }; const _onChangeText = (text: string) => { onChangeText?.(text); setInputValue(text); }; const _onFocus = (e: NativeSyntheticEvent) => { setFocused(true); onFocus?.(e); }; const _onBlur = (e: NativeSyntheticEvent) => { setFocused(false); onBlur?.(e); }; useImperativeHandle(ref, () => { return { clear: onClearText, focus: () => inputRef.current?.focus(), blur: () => inputRef.current?.blur(), setText: (text: string) => _onChangeText(text), }; }); const renderCountingView = () => { return ( {`${inputValue.length}/${maxLength}`} ); }; const renderInputView = () => { const disabledColor = theme.colors.text.disable; let textColor = theme.colors.text.default; let placeholderColor = theme.colors.text.hint; if (disabled) { textColor = disabledColor; placeholderColor = disabledColor; } return ( {focused && inputValue.length > 0 && ( )} {renderCountingView()} ); }; return ( {renderInputView()} ); }, ); export default InputTextArea;