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

Fix IOU amount input flicking #53740

Merged
merged 8 commits into from
Dec 11, 2024
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
2 changes: 1 addition & 1 deletion src/components/AmountTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type AmountTextInputProps = {

/** Hide the focus styles on TextInput */
hideFocusedState?: boolean;
} & Pick<BaseTextInputProps, 'autoFocus'>;
} & Pick<BaseTextInputProps, 'autoFocus' | 'autoGrowExtraSpace'>;

function AmountTextInput(
{
Expand Down
5 changes: 4 additions & 1 deletion src/components/MoneyRequestAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CONST from '@src/CONST';
import isTextInputFocused from './TextInput/BaseTextInput/isTextInputFocused';
import type {BaseTextInputRef} from './TextInput/BaseTextInput/types';
import TextInputWithCurrencySymbol from './TextInputWithCurrencySymbol';
import type {TextInputWithCurrencySymbolProps} from './TextInputWithCurrencySymbol/types';

type CurrentMoney = {amount: string; currency: string};

Expand Down Expand Up @@ -91,7 +92,7 @@ type MoneyRequestAmountInputProps = {

/** The width of inner content */
contentWidth?: number;
};
} & Pick<TextInputWithCurrencySymbolProps, 'autoGrowExtraSpace'>;

type Selection = {
start: number;
Expand Down Expand Up @@ -126,6 +127,7 @@ function MoneyRequestAmountInput(
hideFocusedState = true,
shouldKeepUserInput = false,
autoGrow = true,
autoGrowExtraSpace,
contentWidth,
...props
}: MoneyRequestAmountInputProps,
Expand Down Expand Up @@ -289,6 +291,7 @@ function MoneyRequestAmountInput(
return (
<TextInputWithCurrencySymbol
autoGrow={autoGrow}
autoGrowExtraSpace={autoGrowExtraSpace}
disableKeyboard={disableKeyboard}
formattedAmount={formattedAmount}
onChangeAmount={setNewAmount}
Expand Down
8 changes: 3 additions & 5 deletions src/components/TextInput/BaseTextInput/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function BaseTextInput(
autoFocus = false,
disableKeyboard = false,
autoGrow = false,
autoGrowExtraSpace = 0,
autoGrowHeight = false,
maxAutoGrowHeight,
hideFocusedState = false,
Expand Down Expand Up @@ -258,7 +259,8 @@ function BaseTextInput(
const newTextInputContainerStyles: StyleProp<ViewStyle> = StyleSheet.flatten([
styles.textInputContainer,
textInputContainerStyles,
(autoGrow || !!contentWidth) && StyleUtils.getWidthStyle(textInputWidth),
!!contentWidth && StyleUtils.getWidthStyle(textInputWidth),
autoGrow && StyleUtils.getAutoGrowWidthInputContainerStyles(textInputWidth, autoGrowExtraSpace),
!hideFocusedState && isFocused && styles.borderColorFocus,
(!!hasError || !!errorText) && styles.borderColorDanger,
autoGrowHeight && {scrollPaddingTop: typeof maxAutoGrowHeight === 'number' ? 2 * maxAutoGrowHeight : undefined},
Expand Down Expand Up @@ -449,14 +451,10 @@ function BaseTextInput(
)}
{/*
Text input component doesn't support auto grow by default.
We're using a hidden text input to achieve that.
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) && !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
<Text
style={[
inputStyle,
Expand Down
5 changes: 3 additions & 2 deletions src/components/TextInput/BaseTextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function BaseTextInput(
autoFocus = false,
disableKeyboard = false,
autoGrow = false,
autoGrowExtraSpace = 0,
autoGrowHeight = false,
maxAutoGrowHeight,
hideFocusedState = false,
Expand Down Expand Up @@ -257,7 +258,8 @@ function BaseTextInput(
const newTextInputContainerStyles: StyleProp<ViewStyle> = StyleSheet.flatten([
styles.textInputContainer,
textInputContainerStyles,
(autoGrow || !!contentWidth) && StyleUtils.getWidthStyle(textInputWidth),
!!contentWidth && StyleUtils.getWidthStyle(textInputWidth),
autoGrow && StyleUtils.getAutoGrowWidthInputContainerStyles(textInputWidth, autoGrowExtraSpace),
!hideFocusedState && isFocused && styles.borderColorFocus,
(!!hasError || !!errorText) && styles.borderColorDanger,
autoGrowHeight && {scrollPaddingTop: typeof maxAutoGrowHeight === 'number' ? 2 * maxAutoGrowHeight : undefined},
Expand Down Expand Up @@ -493,7 +495,6 @@ function BaseTextInput(
)}
{/*
Text input component doesn't support auto grow by default.
We're using a hidden text input to achieve that.
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.
*/}
Expand Down
3 changes: 3 additions & 0 deletions src/components/TextInput/BaseTextInput/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type CustomBaseTextInputProps = {
*/
autoGrow?: boolean;

/** If autoGrow is enabled, this reserves extra space for incoming characters to prevent flickering. */
autoGrowExtraSpace?: number;

/**
* Autogrow input container height based on the entered text
*/
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextInputWithCurrencySymbol/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type BaseTextInputWithCurrencySymbolProps = {

/** Hide the focus styles on TextInput */
hideFocusedState?: boolean;
} & Pick<BaseTextInputProps, 'autoFocus' | 'autoGrow' | 'contentWidth' | 'onPress'>;
} & Pick<BaseTextInputProps, 'autoFocus' | 'autoGrow' | 'autoGrowExtraSpace' | 'contentWidth' | 'onPress'>;

type TextInputWithCurrencySymbolProps = Omit<BaseTextInputWithCurrencySymbolProps, 'onSelectionChange'> & {
onSelectionChange?: (start: number, end: number) => void;
Expand Down
2 changes: 2 additions & 0 deletions src/pages/iou/MoneyRequestAmountForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as MoneyRequestUtils from '@libs/MoneyRequestUtils';
import Navigation from '@libs/Navigation/Navigation';
import variables from '@styles/variables';
import type {BaseTextInputRef} from '@src/components/TextInput/BaseTextInput/types';
import CONST from '@src/CONST';
import type {Route} from '@src/ROUTES';
Expand Down Expand Up @@ -259,6 +260,7 @@ function MoneyRequestAmountForm(
>
<MoneyRequestAmountInput
amount={amount}
autoGrowExtraSpace={variables.w80}
currency={currency}
isCurrencyPressable={isCurrencyPressable}
onCurrencyButtonPress={onCurrencyButtonPress}
Expand Down
10 changes: 10 additions & 0 deletions src/styles/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,16 @@ const createStyleUtils = (theme: ThemeColors, styles: ThemeStyles) => ({
};
},

/*
* Returns styles for the text input container, with extraSpace allowing overflow without affecting the layout.
*/
getAutoGrowWidthInputContainerStyles: (width: number, extraSpace: number): ViewStyle => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add comment explaining this function?

QichenZhu marked this conversation as resolved.
Show resolved Hide resolved
if (!!width && !!extraSpace) {
return {marginRight: -extraSpace, width: width + extraSpace};
}
return {width};
},

/*
* Returns the actual maxHeight of the auto-growing markdown text input.
*/
Expand Down
Loading