Skip to content
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

Simplify single line TextInput styling to allow for different heights #55343

Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/components/TextInput/BaseTextInput/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ function BaseTextInput(

const inputPaddingLeft = !!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft);
const inputPaddingRight = !!suffixCharacter && StyleUtils.getPaddingRight(StyleUtils.getCharacterPadding(suffixCharacter) + styles.pr1.paddingRight);

// Height fix is needed only for Text single line inputs
const shouldApplyHeight = !isMultiline && !isMarkdownEnabled;
return (
<>
<View style={[containerStyles]}>
Expand Down Expand Up @@ -356,7 +359,9 @@ function BaseTextInput(
inputPaddingRight,
inputProps.secureTextEntry && styles.secureInput,

!isMultiline && {height, lineHeight: undefined},
// Explicitly remove `lineHeight` from single line inputs so that long text doesn't disappear
// once it exceeds the input space on iOS (See https://github.com/Expensify/App/issues/13802)
shouldApplyHeight && {height, lineHeight: undefined},

// Stop scrollbar flashing when breaking lines with autoGrowHeight enabled.
...(autoGrowHeight && !isAutoGrowHeightMarkdown
Expand Down
38 changes: 6 additions & 32 deletions src/components/TextInput/BaseTextInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Str} from 'expensify-common';
import type {ForwardedRef, MutableRefObject} from 'react';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import React, {forwardRef, useCallback, useEffect, useRef, useState} from 'react';
import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, TextInput, TextInputFocusEventData, ViewStyle} from 'react-native';
import {ActivityIndicator, StyleSheet, View} from 'react-native';
import {useSharedValue, withSpring} from 'react-native-reanimated';
Expand All @@ -24,8 +24,8 @@ import useMarkdownStyle from '@hooks/useMarkdownStyle';
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 {isMobileChrome, isMobileSafari, isSafari} from '@libs/Browser';
import {scrollToRight} from '@libs/InputUtils';
import isInputAutoFilled from '@libs/isInputAutoFilled';
import variables from '@styles/variables';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -98,7 +98,6 @@ function BaseTextInput(
const [passwordHidden, setPasswordHidden] = useState(inputProps.secureTextEntry);
const [textInputWidth, setTextInputWidth] = useState(0);
const [textInputHeight, setTextInputHeight] = useState(0);
const [height, setHeight] = useState<number>(variables.componentSizeLarge);
const [width, setWidth] = useState<number | null>(null);

const labelScale = useSharedValue<number>(initialActiveLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE);
Expand Down Expand Up @@ -187,7 +186,6 @@ function BaseTextInput(
const layout = event.nativeEvent.layout;

setWidth((prevWidth: number | null) => (autoGrowHeight ? layout.width : prevWidth));
setHeight((prevHeight: number) => (!multiline ? layout.height : prevHeight));
},
[autoGrowHeight, multiline],
);
Expand Down Expand Up @@ -263,25 +261,6 @@ function BaseTextInput(
]);
const isMultiline = multiline || autoGrowHeight;

/**
* To prevent text jumping caused by virtual DOM calculations on Safari and mobile Chrome,
* make sure to include the `lineHeight`.
* Reference: https://github.com/Expensify/App/issues/26735
* For other platforms, explicitly remove `lineHeight` from single-line inputs
* to prevent long text from disappearing once it exceeds the input space.
* See https://github.com/Expensify/App/issues/13802
*/
const lineHeight = useMemo(() => {
if (Browser.isSafari() || Browser.isMobileChrome()) {
const lineHeightValue = StyleSheet.flatten(inputStyle).lineHeight;
if (lineHeightValue !== undefined) {
return lineHeightValue;
}
}

return undefined;
}, [inputStyle]);

const inputPaddingLeft = !!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft);
const inputPaddingRight = !!suffixCharacter && StyleUtils.getPaddingRight(StyleUtils.getCharacterPadding(suffixCharacter) + styles.pr1.paddingRight);

Expand Down Expand Up @@ -330,7 +309,6 @@ function BaseTextInput(
/>
</>
) : null}

<View style={[styles.textInputAndIconContainer, isMultiline && hasLabel && styles.textInputMultilineContainer, styles.pointerEventsBoxNone]}>
{!!iconLeft && (
<View style={[styles.textInputLeftIconContainer, !isReadOnly ? styles.cursorPointer : styles.pointerEventsNone]}>
Expand Down Expand Up @@ -380,15 +358,11 @@ function BaseTextInput(
inputPaddingRight,
inputProps.secureTextEntry && styles.secureInput,

// Explicitly remove `lineHeight` from single line inputs so that long text doesn't disappear
// once it exceeds the input space (See https://github.com/Expensify/App/issues/13802)
!isMultiline && {height, lineHeight},

// Explicitly change boxSizing attribute for mobile chrome in order to apply line-height
// for the issue mentioned here https://github.com/Expensify/App/issues/26735
// Set overflow property to enable the parent flexbox to shrink its size
// (See https://github.com/Expensify/App/issues/41766)
!isMultiline && Browser.isMobileChrome() && {boxSizing: 'content-box', height: undefined, ...styles.overflowAuto},
!isMultiline && isMobileChrome() && {boxSizing: 'content-box', height: undefined, ...styles.overflowAuto},

// Stop scrollbar flashing when breaking lines with autoGrowHeight enabled.
...(autoGrowHeight && !isAutoGrowHeightMarkdown
Expand Down Expand Up @@ -431,7 +405,7 @@ function BaseTextInput(
if (didScrollToEndRef.current || !input.current) {
return;
}
InputUtils.scrollToRight(input.current);
scrollToRight(input.current);
didScrollToEndRef.current = true;
}}
>
Expand Down Expand Up @@ -524,7 +498,7 @@ function BaseTextInput(
return;
}
let additionalWidth = 0;
if (Browser.isMobileSafari() || Browser.isSafari() || Browser.isMobileChrome()) {
if (isMobileSafari() || isSafari() || isMobileChrome()) {
additionalWidth = 2;
}
setTextInputWidth(e.nativeEvent.layout.width + additionalWidth);
Expand Down
2 changes: 0 additions & 2 deletions src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,6 @@ const styles = (theme: ThemeColors) =>
},

textInputAndIconContainer: {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this style is only used inside TextInput and the changes were tested as part of this task - it does not affect anything else

flex: 1,
height: '100%',
zIndex: -1,
flexDirection: 'row',
},
Expand Down
Loading