Skip to content

Commit

Permalink
migrate TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
sumo-slonik committed Nov 28, 2024
1 parent 4385dcf commit 3744f48
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
20 changes: 7 additions & 13 deletions src/components/TextInput/BaseTextInput/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 Down Expand Up @@ -99,8 +100,9 @@ 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);

Expand All @@ -122,16 +124,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.value = withSpring(scale, {overshootClamping: false});

Check failure on line 127 in src/components/TextInput/BaseTextInput/index.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
labelTranslateY.value = withSpring(translateY, {overshootClamping: false});

Check failure on line 128 in src/components/TextInput/BaseTextInput/index.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'value' is deprecated. Use the new `.get()` and `.set(value)` methods instead
},
[labelScale, labelTranslateY],
);
Expand Down
6 changes: 4 additions & 2 deletions src/components/TextInput/TextInputLabel/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from 'react';
import {Animated} from 'react-native';
import Animated, {useAnimatedStyle} from 'react-native-reanimated';
import useThemeStyles from '@hooks/useThemeStyles';
import type TextInputLabelProps from './types';

function TextInputLabel({label, labelScale, labelTranslateY}: TextInputLabelProps) {
const styles = useThemeStyles();

const animatedStyle = useAnimatedStyle(() => styles.textInputLabelTransformation(labelTranslateY, labelScale));

return (
<Animated.Text
allowFontScaling={false}
style={[styles.textInputLabel, styles.textInputLabelTransformation(labelTranslateY, labelScale)]}
style={[styles.textInputLabel, animatedStyle]}
>
{label}
</Animated.Text>
Expand Down
6 changes: 4 additions & 2 deletions src/components/TextInput/TextInputLabel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useEffect, useRef} from 'react';
// eslint-disable-next-line no-restricted-imports
import type {Text} from 'react-native';
import {Animated} from 'react-native';
import Animated, {useAnimatedStyle} from 'react-native-reanimated';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import textRef from '@src/types/utils/textRef';
Expand All @@ -19,12 +19,14 @@ function TextInputLabel({for: inputId = '', label, labelTranslateY, labelScale}:
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);

const animatedStyle = useAnimatedStyle(() => styles.textInputLabelTransformation(labelTranslateY, labelScale));

return (
<Animated.Text
// eslint-disable-next-line react-compiler/react-compiler
ref={textRef(labelRef)}
role={CONST.ROLE.PRESENTATION}
style={[styles.textInputLabel, styles.textInputLabelTransformation(labelTranslateY, labelScale), styles.pointerEventsNone]}
style={[styles.textInputLabel, animatedStyle, styles.pointerEventsNone]}
>
{label}
</Animated.Text>
Expand Down
6 changes: 3 additions & 3 deletions src/components/TextInput/TextInputLabel/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {Animated} from 'react-native';
import type {SharedValue} from 'react-native-reanimated';

type TextInputLabelProps = {
/** Label */
label: string;

/** Label vertical translate */
labelTranslateY: Animated.Value;
labelTranslateY: SharedValue<number>;

/** Label scale */
labelScale: Animated.Value;
labelScale: SharedValue<number>;

/** For attribute for label */
for?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1219,9 +1219,9 @@ const styles = (theme: ThemeColors) =>
backgroundColor: theme.componentBG,
},

textInputLabelTransformation: (translateY: AnimatableNumericValue, scale: AnimatableNumericValue) =>
textInputLabelTransformation: (translateY: SharedValue<number>, scale: SharedValue<number>) =>
({
transform: [{translateY}, {scale}],
transform: [{translateY: translateY.value}, {scale: scale.value}],
} satisfies TextStyle),

baseTextInput: {
Expand Down

0 comments on commit 3744f48

Please sign in to comment.