From 42c8cbc1befc40f468d5a471e427ed3512fda6bf Mon Sep 17 00:00:00 2001 From: Tsaqif Date: Thu, 26 Sep 2024 12:42:16 +0700 Subject: [PATCH] Fix autogrow of baseTextInput for markdown text input Signed-off-by: Tsaqif --- .../TextInput/BaseTextInput/index.native.tsx | 25 ++++++++++++----- .../TextInput/BaseTextInput/index.tsx | 27 ++++++++++++++----- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/components/TextInput/BaseTextInput/index.native.tsx b/src/components/TextInput/BaseTextInput/index.native.tsx index a03e9dbb9aa..a43b13afbe0 100644 --- a/src/components/TextInput/BaseTextInput/index.native.tsx +++ b/src/components/TextInput/BaseTextInput/index.native.tsx @@ -1,6 +1,6 @@ import {Str} from 'expensify-common'; import type {ForwardedRef} from 'react'; -import React, {forwardRef, useCallback, useEffect, useRef, useState} from 'react'; +import React, {forwardRef, useCallback, useEffect, useRef, useState, useMemo} from 'react'; import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, TextInput, TextInputFocusEventData, ViewStyle} from 'react-native'; import {ActivityIndicator, Animated, StyleSheet, View} from 'react-native'; import Checkbox from '@components/Checkbox'; @@ -71,6 +71,7 @@ function BaseTextInput( ref: ForwardedRef, ) { const InputComponent = isMarkdownEnabled ? RNMarkdownTextInput : RNTextInput; + const isAutoGrowHeightMarkdown = isMarkdownEnabled && autoGrowHeight; const inputProps = {shouldSaveDraft: false, shouldUseDefaultValue: false, ...props}; const theme = useTheme(); @@ -261,6 +262,17 @@ function BaseTextInput( const inputPaddingLeft = !!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft); + const verticalPadding = useMemo (() => { + const flattenedInputStyle = StyleSheet.flatten(inputStyle); + const topBottomPadding = (flattenedInputStyle.paddingTop ?? 0) + (flattenedInputStyle.paddingBottom ?? 0); + if (topBottomPadding !== 0) { + return topBottomPadding; + } + + return flattenedInputStyle.padding ?? 0; + }, [inputStyle]); + + return ( <> @@ -273,7 +285,7 @@ function BaseTextInput( onLayout={onLayout} accessibilityLabel={label} style={[ - autoGrowHeight && styles.autoGrowHeightInputContainer(textInputHeight, variables.componentSizeLarge, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0), + autoGrowHeight && !isAutoGrowHeightMarkdown && styles.autoGrowHeightInputContainer(textInputHeight, variables.componentSizeLarge, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0), !isMultiline && styles.componentHeightLarge, touchableInputWrapperStyle, ]} @@ -300,7 +312,7 @@ function BaseTextInput( /> ) : null} - + {iconLeft && ( )} - {contentWidth && ( + {contentWidth && !isAutoGrowHeightMarkdown ( { @@ -444,7 +457,7 @@ function BaseTextInput( This text view is used to calculate width or height of the input value given textStyle in this component. This Text component is intentionally positioned out of the screen. */} - {(!!autoGrow || autoGrowHeight) && ( + {(!!autoGrow || autoGrowHeight) && !isAutoGrowHeightMarkdown && ( // Add +2 to width on Safari browsers so that text is not cut off due to the cursor or when changing the value // https://github.com/Expensify/App/issues/8158 // https://github.com/Expensify/App/issues/26628 diff --git a/src/components/TextInput/BaseTextInput/index.tsx b/src/components/TextInput/BaseTextInput/index.tsx index c5471fa11bc..343a1b4a7d0 100644 --- a/src/components/TextInput/BaseTextInput/index.tsx +++ b/src/components/TextInput/BaseTextInput/index.tsx @@ -76,6 +76,7 @@ function BaseTextInput( ref: ForwardedRef, ) { const InputComponent = isMarkdownEnabled ? RNMarkdownTextInput : RNTextInput; + const isAutoGrowHeightMarkdown = isMarkdownEnabled && autoGrowHeight; const theme = useTheme(); const styles = useThemeStyles(); @@ -95,6 +96,7 @@ function BaseTextInput( const [textInputHeight, setTextInputHeight] = useState(0); 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 input = useRef(null); @@ -282,6 +284,16 @@ function BaseTextInput( const inputPaddingLeft = !!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft); const inputPaddingRight = !!suffixCharacter && StyleUtils.getPaddingRight(StyleUtils.getCharacterPadding(suffixCharacter) + styles.pr1.paddingRight); + const verticalPadding = useMemo (() => { + const flattenedInputStyle = StyleSheet.flatten(inputStyle); + const topBottomPadding = (flattenedInputStyle.paddingTop ?? 0) + (flattenedInputStyle.paddingBottom ?? 0); + if (topBottomPadding !== 0) { + return topBottomPadding; + } + + return flattenedInputStyle.padding ?? 0; + }, [inputStyle]); + return ( <> ) : null} - + + {iconLeft && ( )} - {contentWidth && ( + {contentWidth && !isAutoGrowHeightMarkdown ( { @@ -489,7 +504,7 @@ function BaseTextInput( This text view is used to calculate width or height of the input value given textStyle in this component. This Text component is intentionally positioned out of the screen. */} - {(!!autoGrow || autoGrowHeight) && ( + {(!!autoGrow || autoGrowHeight) && !isAutoGrowHeightMarkdown && ( // Add +2 to width on Safari browsers so that text is not cut off due to the cursor or when changing the value // Reference: https://github.com/Expensify/App/issues/8158, https://github.com/Expensify/App/issues/26628 // For mobile Chrome, ensure proper display of the text selection handle (blue bubble down).