import * as React from 'react'; import { Animated, Easing, TextInput, View, ViewProps } from 'react-native'; import { useRestyle, createVariant, createBox } from '@shopify/restyle'; import { colors, fonts, Theme } from './../../theme/theme'; import { Box } from '../Box'; import { Text, AnimatedText } from '../Text'; import { Normalize } from '../../utils/normalize'; type TextInputProps = React.ComponentPropsWithRef & { label?: string; height?: number; icon?: React.ReactNode; error?: boolean; textHelper?: string; disabled?: boolean; width?: string | number; fontSize?: number; outlined?: boolean; backgroundColor?: keyof Theme['colors']; borderColor?: keyof Theme['colors']; borderActiveColor?: keyof Theme['colors']; paddingLeft?: number; rightComponent?: React.ReactNode; maxLength?: number; }; const variant = createVariant({ themeKey: 'fonts', defaults: { flex: 1, paddingLeft: 'spacing-s', fontFamily: fonts.caustenRoundRegular, fontSize: Normalize(16), paddingBottom: 'spacing-xxxs', }, }); const ViewComponent = createBox< Theme, ViewProps & { children?: React.ReactNode } >(View); const restyleFunctions = [variant]; export const TextField = React.forwardRef( ( { width, label, height = Normalize(44), outlined = true, backgroundColor = 'white', borderColor = 'gray60', borderActiveColor = 'gray100', paddingLeft, fontSize, rightComponent, maxLength, ...rest }, ref ) => { const props: any = useRestyle(restyleFunctions as any, rest); const [focus, setFocus] = React.useState(false); const focusAnim = React.useRef(new Animated.Value(0)).current; React.useEffect(() => { Animated.timing(focusAnim, { toValue: focus || !!rest?.value ? 1 : 0, duration: 200, easing: Easing.bezier(0.4, 0, 0.2, 1), useNativeDriver: false, }).start(); }, [focusAnim, focus, rest?.value]); const alignItems = outlined ? 'center' : 'flex-end'; return ( {rest.icon && {rest.icon}} {label && ( {label} )} setFocus(true)} onBlur={() => setFocus(false)} placeholder={!focus && label ? '' : rest.placeholder} maxLength={maxLength || props.maxLength} /> {rightComponent} {rest.textHelper && ( {rest.textHelper} )} ); } );