Skip to content

memoize TextField inner components style to avoid redundant renders #2170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/incubator/TextField/FloatingPlaceholder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ const FloatingPlaceholder = ({
const animation = useRef(new Animated.Value(Number(context.isFocused))).current;
const hidePlaceholder = !context.isValid && validationMessagePosition === ValidationMessagePosition.TOP;

useEffect(() => {
const toValue = floatOnFocus ? context.isFocused || context.hasValue : context.hasValue;
Animated.timing(animation, {
toValue: Number(toValue),
duration: 200,
useNativeDriver: true
}).start();
}, [floatOnFocus, context.isFocused, context.hasValue]);

const animatedStyle = useMemo(() => {
return {
transform: [
Expand All @@ -46,14 +55,8 @@ const FloatingPlaceholder = ({
};
}, [placeholderOffset, extraOffset]);

useEffect(() => {
const toValue = floatOnFocus ? context.isFocused || context.hasValue : context.hasValue;
Animated.timing(animation, {
toValue: Number(toValue),
duration: 200,
useNativeDriver: true
}).start();
}, [floatOnFocus, context.isFocused, context.hasValue]);
const style = useMemo(() => [styles.placeholder, floatingPlaceholderStyle, animatedStyle],
[floatingPlaceholderStyle, animatedStyle]);

const onPlaceholderLayout = useCallback((event: LayoutChangeEvent) => {
const {width, height} = event.nativeEvent.layout;
Expand All @@ -70,7 +73,7 @@ const FloatingPlaceholder = ({
<Text
animated
color={getColorByState(floatingPlaceholderColor, context)}
style={[styles.placeholder, floatingPlaceholderStyle, animatedStyle]}
style={style}
onLayout={onPlaceholderLayout}
testID={testID}
>
Expand Down
8 changes: 6 additions & 2 deletions src/incubator/TextField/Label.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useContext} from 'react';
import React, {useContext, useMemo} from 'react';
import {StyleSheet} from 'react-native';
import {Colors} from '../../style';
import Text from '../../components/text';
Expand All @@ -20,12 +20,16 @@ const Label = ({

const forceHidingLabel = !context.isValid && validationMessagePosition === ValidationMessagePosition.TOP;

const style = useMemo(() => {
return [styles.label, labelStyle, floatingPlaceholder && styles.dummyPlaceholder];
}, [labelStyle, floatingPlaceholder]);

if ((label || floatingPlaceholder) && !forceHidingLabel) {
return (
<Text
testID={testID}
color={getColorByState(labelColor, context)}
style={[styles.label, labelStyle, floatingPlaceholder && styles.dummyPlaceholder]}
style={style}
{...labelProps}
>
{label}
Expand Down
6 changes: 4 additions & 2 deletions src/incubator/TextField/ValidationMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useContext} from 'react';
import React, {useContext, useMemo} from 'react';
import {StyleSheet} from 'react-native';
import Text from '../../components/text';
import FieldContext from './FieldContext';
Expand All @@ -15,6 +15,8 @@ const ValidationMessage = ({
}: ValidationMessageProps) => {
const context = useContext(FieldContext);

const style = useMemo(() => [styles.validationMessage, validationMessageStyle], [validationMessageStyle]);

if (!enableErrors || (!retainSpace && context.isValid)) {
return null;
}
Expand All @@ -23,7 +25,7 @@ const ValidationMessage = ({
const showValidationMessage = !context.isValid || (!validate && !!validationMessage);

return (
<Text testID={testID} $textDangerLight style={[styles.validationMessage, validationMessageStyle]}>
<Text testID={testID} $textDangerLight style={style}>
{showValidationMessage ? relevantValidationMessage : ''}
</Text>
);
Expand Down
3 changes: 2 additions & 1 deletion src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const TextField = (props: InternalTextFieldProps) => {
const {margins, paddings, typography, color} = modifiers;
const typographyStyle = useMemo(() => omit(typography, 'lineHeight'), [typography]);
const colorStyle = useMemo(() => color && {color}, [color]);
const _floatingPlaceholderStyle = useMemo(() => [typographyStyle, floatingPlaceholderStyle], [typographyStyle, floatingPlaceholderStyle]);

const fieldStyle = [fieldStyleProp, dynamicFieldStyle?.(context, {preset: props.preset})];
const hidePlaceholder = shouldHidePlaceholder(props, fieldState.isFocused);
Expand Down Expand Up @@ -118,7 +119,7 @@ const TextField = (props: InternalTextFieldProps) => {
{floatingPlaceholder && (
<FloatingPlaceholder
placeholder={placeholder}
floatingPlaceholderStyle={[typographyStyle, floatingPlaceholderStyle]}
floatingPlaceholderStyle={_floatingPlaceholderStyle}
floatingPlaceholderColor={floatingPlaceholderColor}
floatOnFocus={floatOnFocus}
validationMessagePosition={validationMessagePosition}
Expand Down