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: split amount is put into a second line #43624

Merged
merged 13 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions src/components/MoneyRequestAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ type MoneyRequestAmountInputProps = {
* Autogrow input container length based on the entered text.
*/
autoGrow?: boolean;

/** The width of inner content */
contentWidth?: number;
};

type Selection = {
Expand Down Expand Up @@ -123,6 +126,7 @@ function MoneyRequestAmountInput(
hideFocusedState = true,
shouldKeepUserInput = false,
autoGrow = true,
contentWidth,
...props
}: MoneyRequestAmountInputProps,
forwardedRef: ForwardedRef<BaseTextInputRef>,
Expand Down Expand Up @@ -325,6 +329,7 @@ function MoneyRequestAmountInput(
hideFocusedState={hideFocusedState}
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
contentWidth={contentWidth}
/>
);
}
Expand Down
14 changes: 6 additions & 8 deletions src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {useIsFocused} from '@react-navigation/native';
import lodashIsEqual from 'lodash/isEqual';
import React, {memo, useCallback, useEffect, useMemo, useState} from 'react';
import type {TextStyle} from 'react-native';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
Expand All @@ -11,7 +10,6 @@ import useLocalize from '@hooks/useLocalize';
import {MouseProvider} from '@hooks/useMouseContext';
import usePermissions from '@hooks/usePermissions';
import usePrevious from '@hooks/usePrevious';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
Expand Down Expand Up @@ -209,7 +207,6 @@ function MoneyRequestConfirmationList({
const policyCategories = policyCategoriesReal ?? policyCategoriesDraft;

const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate, toLocaleDigit} = useLocalize();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const {canUseP2PDistanceRequests} = usePermissions(iouType);
Expand Down Expand Up @@ -440,9 +437,10 @@ function MoneyRequestConfirmationList({
}

const currencySymbol = currencyList?.[iouCurrencyCode ?? '']?.symbol ?? iouCurrencyCode;
const prefixPadding = StyleUtils.getCharacterPadding(currencySymbol ?? '');
const formattedTotalAmount = CurrencyUtils.convertToDisplayStringWithoutCurrency(iouAmount, iouCurrencyCode);
const amountWidth = StyleUtils.getWidthStyle(formattedTotalAmount.length * 9 + prefixPadding);
const maxSplitAmount = Math.max(
...[payeeOption, ...selectedParticipants].map((participantOption: Participant) => transaction?.splitShares?.[participantOption.accountID ?? -1]?.amount ?? -1),
);

return [payeeOption, ...selectedParticipants].map((participantOption: Participant) => ({
...participantOption,
Expand All @@ -460,12 +458,13 @@ function MoneyRequestConfirmationList({
hideCurrencySymbol
formatAmountOnBlur
prefixContainerStyle={[styles.pv0]}
inputStyle={[styles.optionRowAmountInput, amountWidth] as TextStyle[]}
containerStyle={[styles.textInputContainer, amountWidth]}
inputStyle={[styles.optionRowAmountInput]}
containerStyle={[styles.textInputContainer]}
touchableInputWrapperStyle={[styles.ml3]}
onFormatAmount={CurrencyUtils.convertToDisplayStringWithoutCurrency}
onAmountChange={(value: string) => onSplitShareChange(participantOption.accountID ?? -1, Number(value))}
maxLength={formattedTotalAmount.length}
contentWidth={CurrencyUtils.convertToDisplayStringWithoutCurrency(maxSplitAmount).length * 9}
Copy link
Contributor Author

@tienifr tienifr Jul 2, 2024

Choose a reason for hiding this comment

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

Earlier we assumed each digit occupies 9px then multiply the total amount with 9 to compute the content width. That's to make sure the all the split input widths are equal and are the longest possible number length which assumingly is the total amount.

But since the paddings, margins... are no longer inclusive in this content width, in case the total amount has more digits than the individual split amount, we would have a larger input width.

So I kept the old requirement: the split input widths are equal and are the longest possible number length but the longest possible number now is not based on the total amount but the individual split amount.

/>
),
}));
Expand All @@ -475,7 +474,6 @@ function MoneyRequestConfirmationList({
shouldShowReadOnlySplits,
currencyList,
iouCurrencyCode,
StyleUtils,
iouAmount,
selectedParticipants,
styles.flexWrap,
Expand Down
30 changes: 28 additions & 2 deletions src/components/TextInput/BaseTextInput/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function BaseTextInput(
shouldShowClearButton = false,
prefixContainerStyle = [],
prefixStyle = [],
contentWidth,
...props
}: BaseTextInputProps,
ref: ForwardedRef<BaseTextInputRef>,
Expand Down Expand Up @@ -251,12 +252,14 @@ function BaseTextInput(
const newTextInputContainerStyles: StyleProp<ViewStyle> = StyleSheet.flatten([
styles.textInputContainer,
textInputContainerStyles,
autoGrow && StyleUtils.getWidthStyle(textInputWidth),
(autoGrow || !!contentWidth) && StyleUtils.getWidthStyle(textInputWidth),
!hideFocusedState && isFocused && styles.borderColorFocus,
(!!hasError || !!errorText) && styles.borderColorDanger,
autoGrowHeight && {scrollPaddingTop: typeof maxAutoGrowHeight === 'number' ? 2 * maxAutoGrowHeight : undefined},
]);

const inputPaddingLeft = !!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft);

return (
<>
<View style={[containerStyles]}>
Expand Down Expand Up @@ -341,7 +344,7 @@ function BaseTextInput(
styles.w100,
inputStyle,
(!hasLabel || isMultiline) && styles.pv0,
!!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft),
inputPaddingLeft,
inputProps.secureTextEntry && styles.secureInput,

!isMultiline && {height, lineHeight: undefined},
Expand Down Expand Up @@ -411,6 +414,29 @@ function BaseTextInput(
/>
)}
</View>
{contentWidth && (
<View
style={[inputStyle as ViewStyle, styles.hiddenElementOutsideOfWindow, styles.visibilityHidden, styles.wAuto, inputPaddingLeft]}
onLayout={(e) => {
if (e.nativeEvent.layout.width === 0 && e.nativeEvent.layout.height === 0) {
return;
}
setTextInputWidth(e.nativeEvent.layout.width);
setTextInputHeight(e.nativeEvent.layout.height);
}}
>
<Text
style={[
inputStyle,
autoGrowHeight && styles.autoGrowHeightHiddenInput(width ?? 0, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : undefined),
{width: contentWidth},
]}
>
{/* \u200B added to solve the issue of not expanding the text input enough when the value ends with '\n' (https://github.com/Expensify/App/issues/21271) */}
{value ? `${value}${value.endsWith('\n') ? '\u200B' : ''}` : placeholder}
</Text>
</View>
)}
{/*
Text input component doesn't support auto grow by default.
We're using a hidden text input to achieve that.
Expand Down
30 changes: 28 additions & 2 deletions src/components/TextInput/BaseTextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function BaseTextInput(
shouldShowClearButton = false,
prefixContainerStyle = [],
prefixStyle = [],
contentWidth,
...inputProps
}: BaseTextInputProps,
ref: ForwardedRef<BaseTextInputRef>,
Expand Down Expand Up @@ -248,7 +249,7 @@ function BaseTextInput(
const newTextInputContainerStyles: StyleProp<ViewStyle> = StyleSheet.flatten([
styles.textInputContainer,
textInputContainerStyles,
autoGrow && StyleUtils.getWidthStyle(textInputWidth),
(autoGrow || !!contentWidth) && StyleUtils.getWidthStyle(textInputWidth),
!hideFocusedState && isFocused && styles.borderColorFocus,
(!!hasError || !!errorText) && styles.borderColorDanger,
autoGrowHeight && {scrollPaddingTop: typeof maxAutoGrowHeight === 'number' ? 2 * maxAutoGrowHeight : undefined},
Expand All @@ -274,6 +275,8 @@ function BaseTextInput(
return undefined;
}, [inputStyle]);

const inputPaddingLeft = !!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft);

return (
<>
<View
Expand Down Expand Up @@ -362,7 +365,7 @@ function BaseTextInput(
styles.w100,
inputStyle,
(!hasLabel || isMultiline) && styles.pv0,
!!prefixCharacter && StyleUtils.getPaddingLeft(StyleUtils.getCharacterPadding(prefixCharacter) + styles.pl1.paddingLeft),
inputPaddingLeft,
inputProps.secureTextEntry && styles.secureInput,

// Explicitly remove `lineHeight` from single line inputs so that long text doesn't disappear
Expand Down Expand Up @@ -440,6 +443,29 @@ function BaseTextInput(
/>
)}
</View>
{contentWidth && (
<View
style={[inputStyle as ViewStyle, styles.hiddenElementOutsideOfWindow, styles.visibilityHidden, styles.wAuto, inputPaddingLeft]}
onLayout={(e) => {
if (e.nativeEvent.layout.width === 0 && e.nativeEvent.layout.height === 0) {
return;
}
setTextInputWidth(e.nativeEvent.layout.width);
setTextInputHeight(e.nativeEvent.layout.height);
}}
>
<Text
style={[
inputStyle,
autoGrowHeight && styles.autoGrowHeightHiddenInput(width ?? 0, typeof maxAutoGrowHeight === 'number' ? maxAutoGrowHeight : undefined),
{width: contentWidth},
]}
>
{/* \u200B added to solve the issue of not expanding the text input enough when the value ends with '\n' (https://github.com/Expensify/App/issues/21271) */}
{value ? `${value}${value.endsWith('\n') ? '\u200B' : ''}` : placeholder}
</Text>
</View>
)}
{/*
Text input component doesn't support auto grow by default.
We're using a hidden text input to achieve that.
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 @@ -120,6 +120,9 @@ type CustomBaseTextInputProps = {

/** Style for the prefix container */
prefixContainerStyle?: StyleProp<ViewStyle>;

/** The width of inner content */
contentWidth?: number;
};

type BaseTextInputRef = HTMLFormElement | AnimatedTextInputRef;
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,6 +77,6 @@ type TextInputWithCurrencySymbolProps = {

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

export default TextInputWithCurrencySymbolProps;
Loading