import React, {useState, useRef} from 'react'; import {View, TextInput, Text, Animated} from 'react-native'; import styles from '../styles/global'; import {FloatingLabelProps} from '../types'; const FloatingLabelTextInput = ({ label = '', error = '', containerStyle, labelColor = '#111', labelStyle = {}, inputStyle = {}, icon, rightIcon, floatUpRange = 25, ...props }: FloatingLabelProps) => { const [val, setValue] = useState(props.value ? props.value : ''); const moveText = useRef(new Animated.Value(props.value ? 1 : 0)).current; const onChangeText = (text: string) => { setValue(text); props.onChangeText ? props.onChangeText(text) : () => {}; }; const onFocusHandler = () => { moveTextTop(); props?.onFocus ? props?.onFocus() : () => {}; }; const onBlurHandler = () => { if (!val) { moveTextBottom(); } props?.onBlur ? props?.onBlur() : () => {}; }; const moveTextTop = () => { Animated.timing(moveText, { toValue: 1, duration: 200, useNativeDriver: true, }).start(); }; const moveTextBottom = () => { Animated.timing(moveText, { toValue: 0, duration: 200, useNativeDriver: true, }).start(); }; const yVal = moveText.interpolate({ inputRange: [0, 1], outputRange: [0, -floatUpRange], }); const animStyle = { transform: [ { translateY: yVal, }, ], }; return ( {label} {icon ? ( {icon} ) : ( )} onChangeText(text)} /> {rightIcon ? ( {rightIcon} ) : ( )} {error} ); }; export default FloatingLabelTextInput;