From 7e14df104bab8f2b9fa68ddce5873338ce3e9064 Mon Sep 17 00:00:00 2001 From: Taras Perun Date: Tue, 17 Dec 2024 16:45:37 +0100 Subject: [PATCH] update BaseTextInput --- .../BaseTextInput/implementation/index.tsx | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/components/TextInput/BaseTextInput/implementation/index.tsx b/src/components/TextInput/BaseTextInput/implementation/index.tsx index 69874be7703e..e8562b4246b1 100644 --- a/src/components/TextInput/BaseTextInput/implementation/index.tsx +++ b/src/components/TextInput/BaseTextInput/implementation/index.tsx @@ -2,7 +2,8 @@ import {Str} from 'expensify-common'; import type {ForwardedRef} from 'react'; import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react'; import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, TextInputFocusEventData, ViewStyle} from 'react-native'; -import {ActivityIndicator, Animated, StyleSheet, View} from 'react-native'; +import {ActivityIndicator, StyleSheet, View} from 'react-native'; +import {useSharedValue, withSpring} from 'react-native-reanimated'; import Checkbox from '@components/Checkbox'; import FormHelpMessage from '@components/FormHelpMessage'; import Icon from '@components/Icon'; @@ -24,8 +25,8 @@ import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; import * as Browser from '@libs/Browser'; +import * as InputUtils from '@libs/InputUtils'; import isInputAutoFilled from '@libs/isInputAutoFilled'; -import useNativeDriver from '@libs/useNativeDriver'; import variables from '@styles/variables'; import CONST from '@src/CONST'; @@ -99,10 +100,12 @@ function BaseTextInput( const [height, setHeight] = useState(variables.componentSizeLarge); const [width, setWidth] = useState(null); - const labelScale = useRef(new Animated.Value(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE)).current; - const labelTranslateY = useRef(new Animated.Value(initialActiveLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y)).current; + const labelScale = useSharedValue(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE); + const labelTranslateY = useSharedValue(initialActiveLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y); + const input = useRef(null); const isLabelActive = useRef(initialActiveLabel); + const didScrollToEndRef = useRef(false); // AutoFocus which only works on mount: useEffect(() => { @@ -122,16 +125,8 @@ function BaseTextInput( const animateLabel = useCallback( (translateY: number, scale: number) => { - Animated.parallel([ - Animated.spring(labelTranslateY, { - toValue: translateY, - useNativeDriver, - }), - Animated.spring(labelScale, { - toValue: scale, - useNativeDriver, - }), - ]).start(); + labelScale.set(withSpring(scale, {overshootClamping: false})); + labelTranslateY.set(withSpring(translateY, {overshootClamping: false})); }, [labelScale, labelTranslateY], ); @@ -427,7 +422,19 @@ function BaseTextInput( )} - {isFocused && !isReadOnly && shouldShowClearButton && !!value && setValue('')} />} + {isFocused && !isReadOnly && shouldShowClearButton && !!value && ( + { + if (didScrollToEndRef.current || !input.current) { + return; + } + InputUtils.scrollToRight(input.current); + didScrollToEndRef.current = true; + }} + > + setValue('')} /> + + )} {!!inputProps.isLoading && (