import React, { forwardRef, useContext, useImperativeHandle, useRef, useState, } from 'react'; import { NativeSyntheticEvent, StyleSheet, TextInput, TextInputFocusEventData, TouchableOpacity, View, } from 'react-native'; import { useComponentId } from '../Application'; import { ApplicationContext, ComponentContext, MiniAppContext, } from '../Context'; import { Colors, Spacing, Styles } from '../Consts'; import { Icon } from '../Icon'; import { useScaleSize, Text } from '../Text'; import { ErrorView, getBorderColor, getSizeStyle, RenderTrailing, } from './common'; import { InputPhoneNumberProps } from './index'; import styles from './styles'; import SystemTextInput from './SystemTextInput'; import { Image } from '../Image'; import { Typography } from '../Text/types'; /** * Input default component */ const InputPhoneNumber = forwardRef( ( { value, onChangeText, size = 'small', placeholder = '0123456789', onBlur, onFocus, errorMessage, trailing, trailingColor, onPressTrailing, disabled = false, errorSpacing, loading = false, secureTextEntry, keyboardType, style, params, hintText, editable = true, showClearIcon = true, defaultValue, ...props }: InputPhoneNumberProps, ref, ) => { const { theme } = useContext(ApplicationContext); const context = useContext(MiniAppContext); const scaleHeight = useScaleSize(size === 'small' ? 48 : 56, 1.1); const [focused, setFocused] = useState(false); const haveValue = !!value || !!defaultValue; const inputRef = useRef(null); const componentName = 'InputPhoneNumber'; const showBaseLineDebug = context?.features?.showBaseLineDebug ?? false; const { componentId } = useComponentId( `${componentName}/${placeholder}`, props.accessibilityLabel, ); const onClearText = () => { inputRef?.current?.clear(); _onChangeText(''); }; const _onChangeText = (text: string) => { onChangeText?.(text); }; const _onFocus = (e: NativeSyntheticEvent) => { setFocused(true); onFocus?.(e); }; const _onBlur = (e: NativeSyntheticEvent) => { setFocused(false); onBlur?.(e); }; const _setText = (text: string) => { inputRef?.current?.setNativeProps({ text }); _onChangeText(text); }; useImperativeHandle(ref, () => { return { clear: onClearText, focus: () => inputRef.current?.focus(), blur: () => inputRef.current?.blur(), setText: _setText, }; }); let inputTextStyles = {}; let dividerHeight = 24; let size14 = useScaleSize(14); let size18 = useScaleSize(18); let typography: Typography; if (size === 'small') { inputTextStyles = { fontSize: size14, fontWeight: '600' }; dividerHeight = 24; typography = 'header_s_semibold'; } else { inputTextStyles = { fontSize: size18, fontWeight: '700' }; dividerHeight = 32; typography = 'header_m_bold'; } /** * Render input view */ const renderInputView = () => { const disabledColor = theme.colors.text.disable; const secure = secureTextEntry; let textColor = theme.colors.text.default; let placeholderColor = theme.colors.text.hint; let iconTintColor = trailingColor ?? theme.colors.text.default; const borderWidth = focused ? 1.5 : 1; if (disabled) { textColor = disabledColor; placeholderColor = disabledColor; iconTintColor = disabledColor; } return ( {'+84'} {showClearIcon && focused && haveValue && ( )} ); }; let inputState = 'active'; if (value && value?.length > 0) { inputState = 'filled'; } if (errorMessage && errorMessage?.length > 0) { inputState = 'error'; } return ( {renderInputView()} ); }, ); const innerStyle = StyleSheet.create({ iconFlag: { width: 24, height: 24, marginHorizontal: 2, }, }); export default InputPhoneNumber;