From cb50bcff1d0192874a02abcdc78c22e6c8e897f5 Mon Sep 17 00:00:00 2001 From: "mkhutornyi (via MelvinBot)" Date: Thu, 24 Sep 2026 11:50:55 +0000 Subject: [PATCH 1/4] Grow the autoGrowHeight input from its own content size on native The container height came from the hidden measurement element in TextInputMeasurement. On Android that element and the real input are laid out by different native text engines, so they disagree about where the value wraps and the box stays a line shorter than the visible input. Use the input's own onContentSizeChange as the height on native, cap the input with maxHeight instead of pinning it so Android keeps laying it out, and give the measurement element the same textBreakStrategy the input uses. Co-authored-by: mkhutornyi --- .../implementation/index.native.tsx | 34 +++++++++++++++---- .../TextInput/TextInputMeasurement/index.tsx | 6 ++++ src/styles/utils/index.ts | 12 ++++++- .../unit/AutoGrowHeightInputStyleUtilsTest.ts | 23 +++++++++++++ 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/components/TextInput/BaseTextInput/implementation/index.native.tsx b/src/components/TextInput/BaseTextInput/implementation/index.native.tsx index 880b65d742b5..4f85c14c427a 100644 --- a/src/components/TextInput/BaseTextInput/implementation/index.native.tsx +++ b/src/components/TextInput/BaseTextInput/implementation/index.native.tsx @@ -32,7 +32,7 @@ import variables from '@styles/variables'; import CONST from '@src/CONST'; import type {ComponentRef} from 'react'; -import type {BlurEvent, FocusEvent, GestureResponderEvent, LayoutChangeEvent, StyleProp, TextInput, ViewStyle} from 'react-native'; +import type {BlurEvent, FocusEvent, GestureResponderEvent, LayoutChangeEvent, StyleProp, TextInput, TextInputContentSizeChangeEvent, ViewStyle} from 'react-native'; import {Str} from 'expensify-common'; import React, {useCallback, useEffect, useRef, useState} from 'react'; @@ -121,6 +121,7 @@ function BaseTextInput({ const [passwordHidden, setPasswordHidden] = useState(inputProps.secureTextEntry); const [textInputWidth, setTextInputWidth] = useState(0); const [textInputHeight, setTextInputHeight] = useState(0); + const [inputContentHeight, setInputContentHeight] = useState(0); const [height, setHeight] = useState(variables.componentSizeLarge); const [width, setWidth] = useState(null); const [prefixCharacterPadding, setPrefixCharacterPadding] = useState(CONST.CHARACTER_WIDTH); @@ -195,6 +196,12 @@ function BaseTextInput({ } }; + const onContentSizeChange = (event: TextInputContentSizeChangeEvent) => { + inputProps.onContentSizeChange?.(event); + + setInputContentHeight(event.nativeEvent.contentSize.height); + }; + const onLayout = useCallback( (event: LayoutChangeEvent) => { if (!autoGrowHeight && multiline) { @@ -272,7 +279,15 @@ function BaseTextInput({ setPasswordHidden((prevPasswordHidden) => !prevPasswordHidden); }, []); - const shouldAddPaddingBottom = isMultiline || (autoGrowHeight && !isAutoGrowHeightMarkdown && textInputHeight > variables.componentSizeLarge); + const shouldUseAutoGrowHeight = autoGrowHeight && !isAutoGrowHeightMarkdown; + + // The hidden element in TextInputMeasurement and the real input are laid out by two different native text engines, + // so they don't always agree on where the value wraps, which leaves the container a line short of the visible input. + // The input's own content size is the height it actually needs, so prefer it once it has reported one. + const autoGrowContentHeight = + shouldUseAutoGrowHeight && inputContentHeight > 0 ? inputContentHeight + variables.inputPaddingTop + variables.inputPaddingBottom : textInputHeight; + + const shouldAddPaddingBottom = isMultiline || (shouldUseAutoGrowHeight && autoGrowContentHeight > variables.componentSizeLarge); const isReadOnly = inputProps.readOnly ?? inputProps.disabled; // Disabling this line for safeness as nullish coalescing works only if the value is undefined or null, and errorText can be an empty string @@ -323,10 +338,9 @@ function BaseTextInput({ // or if multiline is not supplied we calculate the text input height, using onLayout. onLayout={onLayout} style={[ - autoGrowHeight && - !isAutoGrowHeightMarkdown && + shouldUseAutoGrowHeight && styles.autoGrowHeightInputContainer( - textInputHeight + (shouldAddPaddingBottom ? styles.textInputContainer.padding : 0), + autoGrowContentHeight + (shouldAddPaddingBottom ? styles.textInputContainer.padding : 0), variables.componentSizeLarge, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0, ), @@ -422,9 +436,14 @@ function BaseTextInput({ shouldApplyHeight && {height, lineHeight: undefined}, // Stop scrollbar flashing when breaking lines with autoGrowHeight enabled. - ...(autoGrowHeight && !isAutoGrowHeightMarkdown + ...(shouldUseAutoGrowHeight ? [ - StyleUtils.getAutoGrowHeightInputStyle(textInputHeight, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0, autoGrowVerticalInset), + StyleUtils.getAutoGrowHeightInputStyle( + autoGrowContentHeight, + typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0, + autoGrowVerticalInset, + true, + ), styles.verticalAlignTop, ] : []), @@ -435,6 +454,7 @@ function BaseTextInput({ ]} multiline={isMultiline} maxLength={maxLength} + onContentSizeChange={shouldUseAutoGrowHeight ? onContentSizeChange : inputProps.onContentSizeChange} onFocus={onFocus} onBlur={onBlur} onChangeText={setValue} diff --git a/src/components/TextInput/TextInputMeasurement/index.tsx b/src/components/TextInput/TextInputMeasurement/index.tsx index 4c8b74af69be..da6cdf2c8a30 100644 --- a/src/components/TextInput/TextInputMeasurement/index.tsx +++ b/src/components/TextInput/TextInputMeasurement/index.tsx @@ -52,6 +52,9 @@ function TextInputMeasurement({ autoGrowHeight && styles.autoGrowHeightHiddenInput(width ?? 0, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : undefined), {width: contentWidth}, ]} + // Android breaks lines in a Text with a different strategy than in a TextInput, so the measurement + // has to opt into the same one the input uses to wrap the value at the same place. + textBreakStrategy="simple" accessible={false} accessibilityElementsHidden importantForAccessibility="no" @@ -82,6 +85,9 @@ function TextInputMeasurement({ styles.hiddenElementOutsideOfWindow, styles.visibilityHidden, ]} + // Android breaks lines in a Text with a different strategy than in a TextInput, so the measurement + // has to opt into the same one the input uses to wrap the value at the same place. + textBreakStrategy="simple" accessible={false} accessibilityElementsHidden importantForAccessibility="no" diff --git a/src/styles/utils/index.ts b/src/styles/utils/index.ts index d6a406ef8dbc..0e970b9a27be 100644 --- a/src/styles/utils/index.ts +++ b/src/styles/utils/index.ts @@ -1592,7 +1592,7 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({ /** * Returns auto grow height text input style */ - getAutoGrowHeightInputStyle: (textInputHeight: number, maxHeight: number, verticalInset: number): ViewStyle => { + getAutoGrowHeightInputStyle: (textInputHeight: number, maxHeight: number, verticalInset: number, shouldSizeToContent = false): ViewStyle => { // textInputHeight comes from the hidden measurement, which also includes the input's own vertical padding, // so flip as soon as it no longer fits the fixed height below. Otherwise content can be clipped at some font scales. if (textInputHeight > maxHeight - verticalInset) { @@ -1602,6 +1602,16 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({ }; } + // On native the input has to stay free to size itself to its content, because Android only reports + // `onContentSizeChange` when the underlying EditText is laid out again, which never happens while its height is pinned. + if (shouldSizeToContent) { + return { + ...styles.pr0, + ...styles.overflowHidden, + maxHeight: maxHeight - verticalInset, + }; + } + return { ...styles.pr0, ...styles.overflowHidden, diff --git a/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts b/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts index 764f79d70040..dab33b229209 100644 --- a/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts +++ b/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts @@ -75,4 +75,27 @@ describe('getAutoGrowHeightInputStyle', () => { expect(style.height).toBeUndefined(); expect(style.overflow).toBe(mockStyles.overflowAuto.overflow); }); + + it('caps a content sized input instead of pinning it', () => { + // Given a growing input that has to size itself to its content, because Android only reports + // onContentSizeChange while the underlying EditText is free to be laid out again + const verticalInset = getAutoGrowHeightInputVerticalInset(getContainerStyle(true), true); + + // When the style is built for it + const style = getAutoGrowHeightInputStyle(maxHeight - verticalInset, maxHeight, verticalInset, true); + + // Then the input is capped rather than pinned, so its height still tracks the number of lines + expect(style.height).toBeUndefined(); + expect(style.maxHeight).toBe(maxHeight - verticalInset); + }); + + it('still scrolls a content sized input once it no longer fits', () => { + // Given a content sized input whose content has grown past the cap + // When the style is built for it + const style = getAutoGrowHeightInputStyle(maxHeight - 29 + 1, maxHeight, 29, true); + + // Then it flips to scrolling exactly like the pinned input does + expect(style.maxHeight).toBeUndefined(); + expect(style.overflow).toBe(mockStyles.overflowAuto.overflow); + }); }); From 8f261b9cd1801ce0f59b985ccf155fb7637b7c07 Mon Sep 17 00:00:00 2001 From: "mkhutornyi (via MelvinBot)" Date: Thu, 24 Sep 2026 12:00:50 +0000 Subject: [PATCH 2/4] Fix: run oxfmt on the autoGrowHeight height expression The autoGrowContentHeight assignment fits within the 190 character print width, so oxfmt collapses it onto a single line. Commit that formatting so the Oxfmt check no longer reports a diff. Co-authored-by: mkhutornyi --- .../TextInput/BaseTextInput/implementation/index.native.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/TextInput/BaseTextInput/implementation/index.native.tsx b/src/components/TextInput/BaseTextInput/implementation/index.native.tsx index 4f85c14c427a..88f71db2a6f7 100644 --- a/src/components/TextInput/BaseTextInput/implementation/index.native.tsx +++ b/src/components/TextInput/BaseTextInput/implementation/index.native.tsx @@ -284,8 +284,7 @@ function BaseTextInput({ // The hidden element in TextInputMeasurement and the real input are laid out by two different native text engines, // so they don't always agree on where the value wraps, which leaves the container a line short of the visible input. // The input's own content size is the height it actually needs, so prefer it once it has reported one. - const autoGrowContentHeight = - shouldUseAutoGrowHeight && inputContentHeight > 0 ? inputContentHeight + variables.inputPaddingTop + variables.inputPaddingBottom : textInputHeight; + const autoGrowContentHeight = shouldUseAutoGrowHeight && inputContentHeight > 0 ? inputContentHeight + variables.inputPaddingTop + variables.inputPaddingBottom : textInputHeight; const shouldAddPaddingBottom = isMultiline || (shouldUseAutoGrowHeight && autoGrowContentHeight > variables.componentSizeLarge); const isReadOnly = inputProps.readOnly ?? inputProps.disabled; From a219a9a568f6eaa937316938e3c9c6d8f9cd8e70 Mon Sep 17 00:00:00 2001 From: "mkhutornyi (via MelvinBot)" Date: Wed, 30 Sep 2026 04:25:57 +0000 Subject: [PATCH 3/4] Read autoGrowHeight padding from inputStyle, memoize onContentSizeChange, and simplify comments Co-authored-by: mkhutornyi --- .../implementation/index.native.tsx | 19 ++++++++++------- .../TextInput/TextInputMeasurement/index.tsx | 6 ++---- src/styles/utils/index.ts | 16 ++++++++++++-- .../unit/AutoGrowHeightInputStyleUtilsTest.ts | 21 +++++++++++++------ 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/components/TextInput/BaseTextInput/implementation/index.native.tsx b/src/components/TextInput/BaseTextInput/implementation/index.native.tsx index 88f71db2a6f7..f87ffa4a309b 100644 --- a/src/components/TextInput/BaseTextInput/implementation/index.native.tsx +++ b/src/components/TextInput/BaseTextInput/implementation/index.native.tsx @@ -196,11 +196,15 @@ function BaseTextInput({ } }; - const onContentSizeChange = (event: TextInputContentSizeChangeEvent) => { - inputProps.onContentSizeChange?.(event); + const onContentSizeChangeProp = inputProps.onContentSizeChange; + const onContentSizeChange = useCallback( + (event: TextInputContentSizeChangeEvent) => { + onContentSizeChangeProp?.(event); - setInputContentHeight(event.nativeEvent.contentSize.height); - }; + setInputContentHeight(event.nativeEvent.contentSize.height); + }, + [onContentSizeChangeProp], + ); const onLayout = useCallback( (event: LayoutChangeEvent) => { @@ -281,10 +285,9 @@ function BaseTextInput({ const shouldUseAutoGrowHeight = autoGrowHeight && !isAutoGrowHeightMarkdown; - // The hidden element in TextInputMeasurement and the real input are laid out by two different native text engines, - // so they don't always agree on where the value wraps, which leaves the container a line short of the visible input. - // The input's own content size is the height it actually needs, so prefer it once it has reported one. - const autoGrowContentHeight = shouldUseAutoGrowHeight && inputContentHeight > 0 ? inputContentHeight + variables.inputPaddingTop + variables.inputPaddingBottom : textInputHeight; + // Prefer the input's own content height, because the hidden measurement can wrap text differently. + // The multiline input has no vertical padding, so add back the padding the measurement includes. + const autoGrowContentHeight = shouldUseAutoGrowHeight && inputContentHeight > 0 ? inputContentHeight + StyleUtils.getVerticalPaddingFromStyle(inputStyle) : textInputHeight; const shouldAddPaddingBottom = isMultiline || (shouldUseAutoGrowHeight && autoGrowContentHeight > variables.componentSizeLarge); const isReadOnly = inputProps.readOnly ?? inputProps.disabled; diff --git a/src/components/TextInput/TextInputMeasurement/index.tsx b/src/components/TextInput/TextInputMeasurement/index.tsx index da6cdf2c8a30..2731c2a18f3a 100644 --- a/src/components/TextInput/TextInputMeasurement/index.tsx +++ b/src/components/TextInput/TextInputMeasurement/index.tsx @@ -52,8 +52,7 @@ function TextInputMeasurement({ autoGrowHeight && styles.autoGrowHeightHiddenInput(width ?? 0, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : undefined), {width: contentWidth}, ]} - // Android breaks lines in a Text with a different strategy than in a TextInput, so the measurement - // has to opt into the same one the input uses to wrap the value at the same place. + // Match the input's line breaking on Android. textBreakStrategy="simple" accessible={false} accessibilityElementsHidden @@ -85,8 +84,7 @@ function TextInputMeasurement({ styles.hiddenElementOutsideOfWindow, styles.visibilityHidden, ]} - // Android breaks lines in a Text with a different strategy than in a TextInput, so the measurement - // has to opt into the same one the input uses to wrap the value at the same place. + // Match the input's line breaking on Android. textBreakStrategy="simple" accessible={false} accessibilityElementsHidden diff --git a/src/styles/utils/index.ts b/src/styles/utils/index.ts index 0e970b9a27be..103d67582821 100644 --- a/src/styles/utils/index.ts +++ b/src/styles/utils/index.ts @@ -868,6 +868,18 @@ function getVerticalPaddingDiffFromStyle(textInputContainerStyles: ViewStyle): n return paddingTop - paddingBottom; } +/** + * Get the sum of the top and bottom padding from provided styles + */ +function getVerticalPaddingFromStyle(style: StyleProp): number { + const flatStyle = StyleSheet.flatten(style); + const getNumericPadding = (paddingValue: string | number | Animated.AnimatedNode | null | undefined): number => (typeof paddingValue === 'number' ? paddingValue : 0); + + const paddingTop = getNumericPadding(flatStyle?.paddingTop ?? flatStyle?.paddingVertical ?? flatStyle?.padding); + const paddingBottom = getNumericPadding(flatStyle?.paddingBottom ?? flatStyle?.paddingVertical ?? flatStyle?.padding); + return paddingTop + paddingBottom; +} + /** * Get the vertical space the auto grow height input's ancestors take up inside the input container (borders, padding and the multiline label padding) */ @@ -1480,6 +1492,7 @@ const staticStyleUtils = { getPaddingRight, getPaddingBottom, getVerticalPaddingDiffFromStyle, + getVerticalPaddingFromStyle, getAutoGrowHeightInputVerticalInset, hasSafeAreas, getHeight, @@ -1602,8 +1615,7 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({ }; } - // On native the input has to stay free to size itself to its content, because Android only reports - // `onContentSizeChange` when the underlying EditText is laid out again, which never happens while its height is pinned. + // On native, cap the input instead of pinning it so it sizes to its content. if (shouldSizeToContent) { return { ...styles.pr0, diff --git a/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts b/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts index dab33b229209..aad3a44fb0fe 100644 --- a/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts +++ b/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts @@ -8,7 +8,7 @@ import type {ViewStyle} from 'react-native'; import {StyleSheet} from 'react-native'; const mockStyles = createThemeStyles(defaultTheme); -const {getAutoGrowHeightInputStyle, getAutoGrowHeightInputVerticalInset} = createStyleUtils(defaultTheme, mockStyles); +const {getAutoGrowHeightInputStyle, getAutoGrowHeightInputVerticalInset, getVerticalPaddingFromStyle} = createStyleUtils(defaultTheme, mockStyles); const maxHeight = variables.textInputAutoGrowMaxHeight; @@ -40,6 +40,16 @@ describe('getAutoGrowHeightInputVerticalInset', () => { }); }); +describe('getVerticalPaddingFromStyle', () => { + it('uses the padding of the merged input style', () => { + // Given the base input style, and one where a caller overrides the vertical padding + // When the vertical padding is read from each + // Then it follows the override, so the auto grow height matches the hidden measurement + expect(getVerticalPaddingFromStyle(mockStyles.baseTextInput)).toBe(variables.inputPaddingTop + variables.inputPaddingBottom); + expect(getVerticalPaddingFromStyle([mockStyles.baseTextInput, {paddingTop: 10, paddingBottom: 2}])).toBe(12); + }); +}); + describe('getAutoGrowHeightInputStyle', () => { it('sizes the growing input so it matches the height it gets after the overflow flip', () => { const verticalInset = getAutoGrowHeightInputVerticalInset(getContainerStyle(true), true); @@ -77,24 +87,23 @@ describe('getAutoGrowHeightInputStyle', () => { }); it('caps a content sized input instead of pinning it', () => { - // Given a growing input that has to size itself to its content, because Android only reports - // onContentSizeChange while the underlying EditText is free to be laid out again + // Given a native input that sizes itself to its content const verticalInset = getAutoGrowHeightInputVerticalInset(getContainerStyle(true), true); // When the style is built for it const style = getAutoGrowHeightInputStyle(maxHeight - verticalInset, maxHeight, verticalInset, true); - // Then the input is capped rather than pinned, so its height still tracks the number of lines + // Then the input is capped, not pinned, so its height follows the number of lines expect(style.height).toBeUndefined(); expect(style.maxHeight).toBe(maxHeight - verticalInset); }); it('still scrolls a content sized input once it no longer fits', () => { - // Given a content sized input whose content has grown past the cap + // Given a content sized input that has grown past the cap // When the style is built for it const style = getAutoGrowHeightInputStyle(maxHeight - 29 + 1, maxHeight, 29, true); - // Then it flips to scrolling exactly like the pinned input does + // Then it switches to scrolling, like the pinned input expect(style.maxHeight).toBeUndefined(); expect(style.overflow).toBe(mockStyles.overflowAuto.overflow); }); From cd63f6783670854eddb13c572e513d9b360ddaf9 Mon Sep 17 00:00:00 2001 From: "mkhutornyi (via MelvinBot)" Date: Wed, 30 Sep 2026 05:02:17 +0000 Subject: [PATCH 4/4] Keep only the textBreakStrategy change on the measurement text Revert the native onContentSizeChange sizing and the shouldSizeToContent style branch, so the PR only aligns the hidden measurement text's line breaker with the input's. Co-authored-by: mkhutornyi --- .../implementation/index.native.tsx | 36 ++++--------------- src/styles/utils/index.ts | 24 +------------ .../unit/AutoGrowHeightInputStyleUtilsTest.ts | 34 +----------------- 3 files changed, 9 insertions(+), 85 deletions(-) diff --git a/src/components/TextInput/BaseTextInput/implementation/index.native.tsx b/src/components/TextInput/BaseTextInput/implementation/index.native.tsx index f87ffa4a309b..880b65d742b5 100644 --- a/src/components/TextInput/BaseTextInput/implementation/index.native.tsx +++ b/src/components/TextInput/BaseTextInput/implementation/index.native.tsx @@ -32,7 +32,7 @@ import variables from '@styles/variables'; import CONST from '@src/CONST'; import type {ComponentRef} from 'react'; -import type {BlurEvent, FocusEvent, GestureResponderEvent, LayoutChangeEvent, StyleProp, TextInput, TextInputContentSizeChangeEvent, ViewStyle} from 'react-native'; +import type {BlurEvent, FocusEvent, GestureResponderEvent, LayoutChangeEvent, StyleProp, TextInput, ViewStyle} from 'react-native'; import {Str} from 'expensify-common'; import React, {useCallback, useEffect, useRef, useState} from 'react'; @@ -121,7 +121,6 @@ function BaseTextInput({ const [passwordHidden, setPasswordHidden] = useState(inputProps.secureTextEntry); const [textInputWidth, setTextInputWidth] = useState(0); const [textInputHeight, setTextInputHeight] = useState(0); - const [inputContentHeight, setInputContentHeight] = useState(0); const [height, setHeight] = useState(variables.componentSizeLarge); const [width, setWidth] = useState(null); const [prefixCharacterPadding, setPrefixCharacterPadding] = useState(CONST.CHARACTER_WIDTH); @@ -196,16 +195,6 @@ function BaseTextInput({ } }; - const onContentSizeChangeProp = inputProps.onContentSizeChange; - const onContentSizeChange = useCallback( - (event: TextInputContentSizeChangeEvent) => { - onContentSizeChangeProp?.(event); - - setInputContentHeight(event.nativeEvent.contentSize.height); - }, - [onContentSizeChangeProp], - ); - const onLayout = useCallback( (event: LayoutChangeEvent) => { if (!autoGrowHeight && multiline) { @@ -283,13 +272,7 @@ function BaseTextInput({ setPasswordHidden((prevPasswordHidden) => !prevPasswordHidden); }, []); - const shouldUseAutoGrowHeight = autoGrowHeight && !isAutoGrowHeightMarkdown; - - // Prefer the input's own content height, because the hidden measurement can wrap text differently. - // The multiline input has no vertical padding, so add back the padding the measurement includes. - const autoGrowContentHeight = shouldUseAutoGrowHeight && inputContentHeight > 0 ? inputContentHeight + StyleUtils.getVerticalPaddingFromStyle(inputStyle) : textInputHeight; - - const shouldAddPaddingBottom = isMultiline || (shouldUseAutoGrowHeight && autoGrowContentHeight > variables.componentSizeLarge); + const shouldAddPaddingBottom = isMultiline || (autoGrowHeight && !isAutoGrowHeightMarkdown && textInputHeight > variables.componentSizeLarge); const isReadOnly = inputProps.readOnly ?? inputProps.disabled; // Disabling this line for safeness as nullish coalescing works only if the value is undefined or null, and errorText can be an empty string @@ -340,9 +323,10 @@ function BaseTextInput({ // or if multiline is not supplied we calculate the text input height, using onLayout. onLayout={onLayout} style={[ - shouldUseAutoGrowHeight && + autoGrowHeight && + !isAutoGrowHeightMarkdown && styles.autoGrowHeightInputContainer( - autoGrowContentHeight + (shouldAddPaddingBottom ? styles.textInputContainer.padding : 0), + textInputHeight + (shouldAddPaddingBottom ? styles.textInputContainer.padding : 0), variables.componentSizeLarge, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0, ), @@ -438,14 +422,9 @@ function BaseTextInput({ shouldApplyHeight && {height, lineHeight: undefined}, // Stop scrollbar flashing when breaking lines with autoGrowHeight enabled. - ...(shouldUseAutoGrowHeight + ...(autoGrowHeight && !isAutoGrowHeightMarkdown ? [ - StyleUtils.getAutoGrowHeightInputStyle( - autoGrowContentHeight, - typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0, - autoGrowVerticalInset, - true, - ), + StyleUtils.getAutoGrowHeightInputStyle(textInputHeight, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : 0, autoGrowVerticalInset), styles.verticalAlignTop, ] : []), @@ -456,7 +435,6 @@ function BaseTextInput({ ]} multiline={isMultiline} maxLength={maxLength} - onContentSizeChange={shouldUseAutoGrowHeight ? onContentSizeChange : inputProps.onContentSizeChange} onFocus={onFocus} onBlur={onBlur} onChangeText={setValue} diff --git a/src/styles/utils/index.ts b/src/styles/utils/index.ts index 103d67582821..d6a406ef8dbc 100644 --- a/src/styles/utils/index.ts +++ b/src/styles/utils/index.ts @@ -868,18 +868,6 @@ function getVerticalPaddingDiffFromStyle(textInputContainerStyles: ViewStyle): n return paddingTop - paddingBottom; } -/** - * Get the sum of the top and bottom padding from provided styles - */ -function getVerticalPaddingFromStyle(style: StyleProp): number { - const flatStyle = StyleSheet.flatten(style); - const getNumericPadding = (paddingValue: string | number | Animated.AnimatedNode | null | undefined): number => (typeof paddingValue === 'number' ? paddingValue : 0); - - const paddingTop = getNumericPadding(flatStyle?.paddingTop ?? flatStyle?.paddingVertical ?? flatStyle?.padding); - const paddingBottom = getNumericPadding(flatStyle?.paddingBottom ?? flatStyle?.paddingVertical ?? flatStyle?.padding); - return paddingTop + paddingBottom; -} - /** * Get the vertical space the auto grow height input's ancestors take up inside the input container (borders, padding and the multiline label padding) */ @@ -1492,7 +1480,6 @@ const staticStyleUtils = { getPaddingRight, getPaddingBottom, getVerticalPaddingDiffFromStyle, - getVerticalPaddingFromStyle, getAutoGrowHeightInputVerticalInset, hasSafeAreas, getHeight, @@ -1605,7 +1592,7 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({ /** * Returns auto grow height text input style */ - getAutoGrowHeightInputStyle: (textInputHeight: number, maxHeight: number, verticalInset: number, shouldSizeToContent = false): ViewStyle => { + getAutoGrowHeightInputStyle: (textInputHeight: number, maxHeight: number, verticalInset: number): ViewStyle => { // textInputHeight comes from the hidden measurement, which also includes the input's own vertical padding, // so flip as soon as it no longer fits the fixed height below. Otherwise content can be clipped at some font scales. if (textInputHeight > maxHeight - verticalInset) { @@ -1615,15 +1602,6 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({ }; } - // On native, cap the input instead of pinning it so it sizes to its content. - if (shouldSizeToContent) { - return { - ...styles.pr0, - ...styles.overflowHidden, - maxHeight: maxHeight - verticalInset, - }; - } - return { ...styles.pr0, ...styles.overflowHidden, diff --git a/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts b/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts index aad3a44fb0fe..764f79d70040 100644 --- a/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts +++ b/tests/unit/AutoGrowHeightInputStyleUtilsTest.ts @@ -8,7 +8,7 @@ import type {ViewStyle} from 'react-native'; import {StyleSheet} from 'react-native'; const mockStyles = createThemeStyles(defaultTheme); -const {getAutoGrowHeightInputStyle, getAutoGrowHeightInputVerticalInset, getVerticalPaddingFromStyle} = createStyleUtils(defaultTheme, mockStyles); +const {getAutoGrowHeightInputStyle, getAutoGrowHeightInputVerticalInset} = createStyleUtils(defaultTheme, mockStyles); const maxHeight = variables.textInputAutoGrowMaxHeight; @@ -40,16 +40,6 @@ describe('getAutoGrowHeightInputVerticalInset', () => { }); }); -describe('getVerticalPaddingFromStyle', () => { - it('uses the padding of the merged input style', () => { - // Given the base input style, and one where a caller overrides the vertical padding - // When the vertical padding is read from each - // Then it follows the override, so the auto grow height matches the hidden measurement - expect(getVerticalPaddingFromStyle(mockStyles.baseTextInput)).toBe(variables.inputPaddingTop + variables.inputPaddingBottom); - expect(getVerticalPaddingFromStyle([mockStyles.baseTextInput, {paddingTop: 10, paddingBottom: 2}])).toBe(12); - }); -}); - describe('getAutoGrowHeightInputStyle', () => { it('sizes the growing input so it matches the height it gets after the overflow flip', () => { const verticalInset = getAutoGrowHeightInputVerticalInset(getContainerStyle(true), true); @@ -85,26 +75,4 @@ describe('getAutoGrowHeightInputStyle', () => { expect(style.height).toBeUndefined(); expect(style.overflow).toBe(mockStyles.overflowAuto.overflow); }); - - it('caps a content sized input instead of pinning it', () => { - // Given a native input that sizes itself to its content - const verticalInset = getAutoGrowHeightInputVerticalInset(getContainerStyle(true), true); - - // When the style is built for it - const style = getAutoGrowHeightInputStyle(maxHeight - verticalInset, maxHeight, verticalInset, true); - - // Then the input is capped, not pinned, so its height follows the number of lines - expect(style.height).toBeUndefined(); - expect(style.maxHeight).toBe(maxHeight - verticalInset); - }); - - it('still scrolls a content sized input once it no longer fits', () => { - // Given a content sized input that has grown past the cap - // When the style is built for it - const style = getAutoGrowHeightInputStyle(maxHeight - 29 + 1, maxHeight, 29, true); - - // Then it switches to scrolling, like the pinned input - expect(style.maxHeight).toBeUndefined(); - expect(style.overflow).toBe(mockStyles.overflowAuto.overflow); - }); });