Skip to content

Commit

Permalink
update BaseTextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
perunt committed Dec 17, 2024
1 parent e8ceb29 commit 7e14df1
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/components/TextInput/BaseTextInput/implementation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -99,10 +100,12 @@ function BaseTextInput(
const [height, setHeight] = useState<number>(variables.componentSizeLarge);
const [width, setWidth] = useState<number | null>(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<number>(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE);
const labelTranslateY = useSharedValue<number>(initialActiveLabel ? styleConst.ACTIVE_LABEL_TRANSLATE_Y : styleConst.INACTIVE_LABEL_TRANSLATE_Y);

const input = useRef<HTMLInputElement | null>(null);
const isLabelActive = useRef(initialActiveLabel);
const didScrollToEndRef = useRef(false);

// AutoFocus which only works on mount:
useEffect(() => {
Expand All @@ -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],
);
Expand Down Expand Up @@ -427,7 +422,19 @@ function BaseTextInput(
</Text>
</View>
)}
{isFocused && !isReadOnly && shouldShowClearButton && !!value && <TextInputClearButton onPressButton={() => setValue('')} />}
{isFocused && !isReadOnly && shouldShowClearButton && !!value && (
<View
onLayout={() => {
if (didScrollToEndRef.current || !input.current) {
return;
}
InputUtils.scrollToRight(input.current);
didScrollToEndRef.current = true;
}}
>
<TextInputClearButton onPressButton={() => setValue('')} />
</View>
)}
{!!inputProps.isLoading && (
<ActivityIndicator
size="small"
Expand Down

0 comments on commit 7e14df1

Please sign in to comment.