import { forwardRef } from 'react' import { NativeSyntheticEvent, Platform, TextInput, TextInputFocusEventData, TextStyle, } from 'react-native' import { Box } from './Box' import { BoxWithShadow } from './BoxWithShadow' import { Icon } from './Icon' import { Touchable } from './Touchables/Touchable' import type { InputProps } from './types' import { fontTextSize, fontWeights } from '../config' import { generateStyleSheet, generateStyledSystem } from '../utils' import { getLayoutProps } from '@/design-system/utils/getLayoutProps' import { useSecurePassword, useRef, useTheme, useState, useCallback, useImperativeHandle, useMemo, } from '@/hooks' import { convertEmToNumber, getColorValue } from '@/utils' const StyledInput = forwardRef((props, ref) => { const { colors, fonts, lineHeights } = useTheme() const fontSize = fontTextSize[props.fontSize || 'sm'] const fontWeight = fontWeights[props.fontWeight || 'Regular'] const fontFamily = fonts[props.fontFamily || 'Regular'] const lineHeightStyle = useMemo( () => ({ lineHeight: props.lineHeight ? convertEmToNumber(lineHeights[props.lineHeight], fontSize) : undefined, }), [lineHeights, props.lineHeight, fontSize] ) const textColorStyle = useMemo( () => ({ color: props.color ? getColorValue({ color: props.color, colors }) : colors.text.primary, }), [colors, props.color] ) const textAlignmentStyle = useMemo( () => ({ textAlign: props.textAlign, }), [props.textAlign] ) const textTransformStyle = useMemo( () => ({ textTransform: props.capitalize || props.textTransform === 'capitalize' ? 'capitalize' : props.lowercase || props.textTransform === 'lowercase' ? 'lowercase' : props.uppercase || props.textTransform === 'uppercase' ? 'uppercase' : 'none', }), [props.capitalize, props.lowercase, props.uppercase, props.textTransform] ) const textDecorationStyle = useMemo( () => ({ textDecorationLine: props.underline || props.textDecoration === 'underline' ? 'underline' : props.strikeThrough || props.textDecoration === 'line-through' ? 'line-through' : undefined, }), [props.underline, props.strikeThrough, props.textDecoration] ) const fontWeightStyle = useMemo( () => ({ fontWeight: (props.bold && fontWeights.Bold) || fontWeight, }), [fontWeight, props.bold] ) const fontFamilyStyle = useMemo( () => ({ fontFamily, }), [fontFamily] ) const fontSizeStyle = useMemo( () => ({ fontSize, }), [fontSize] ) const inputTextStyle = useMemo( () => generateStyleSheet([ props.italic && { fontStyle: 'italic' }, fontFamilyStyle, fontSizeStyle, fontWeightStyle, textDecorationStyle, textTransformStyle, textAlignmentStyle, textColorStyle, lineHeightStyle, ]), [ fontFamilyStyle, fontSizeStyle, fontWeightStyle, lineHeightStyle, props.italic, textAlignmentStyle, textColorStyle, textDecorationStyle, textTransformStyle, ] ) const style = generateStyledSystem(props, colors) return }) export const Input = forwardRef( ( { isDisabled, isInvalid, secureTextIconName, secureTextIconColor, onFocus, onBlur, lineHeight, secureTextIconSize = 24, ...props }, ref ) => { const { colors } = useTheme() const [isFocused, setIsFocused] = useState(false) const _inputRef = useRef(null) const { securePassword, toggleSecurePassword, iconName } = useSecurePassword(props.type) const { layoutProps, restProps: inputProps } = useMemo(() => getLayoutProps(props), [props]) const handleFocus = useCallback( (e?: NativeSyntheticEvent) => { if (isDisabled) return _inputRef.current?.focus() setIsFocused(true) if (onFocus && e) onFocus(e) }, [isDisabled, onFocus] ) const handleBlur = useCallback( (e?: NativeSyntheticEvent) => { _inputRef.current?.blur() setIsFocused(false) if (onBlur && e) onBlur(e) }, [setIsFocused, onBlur] ) useImperativeHandle( ref, () => ({ focus: () => handleFocus(), blur: () => handleBlur(), ..._inputRef.current, }) as unknown as TextInput, [handleBlur, handleFocus] ) return ( {props.type === 'password' ? ( ) : ( props.rightElement )} ) } )