Skip to content

Commit

Permalink
Merge pull request #42897 from kaushiktd/reset-amount-keyboard-up
Browse files Browse the repository at this point in the history
Reset amount on reset button click
  • Loading branch information
youssef-lr authored Jul 11, 2024
2 parents 74d16f0 + b1eda3e commit 109d4aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/components/MoneyRequestAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ type MoneyRequestAmountInputProps = {
* Autogrow input container length based on the entered text.
*/
autoGrow?: boolean;

/**
* Determines whether the amount should be reset.
*/
shouldResetAmount?: boolean;

/**
* Callback function triggered when the amount is reset.
*
* @param resetValue - A boolean indicating whether the amount should be reset.
*/
onResetAmount?: (resetValue: boolean) => void;
};

type Selection = {
Expand Down Expand Up @@ -123,6 +135,8 @@ function MoneyRequestAmountInput(
hideFocusedState = true,
shouldKeepUserInput = false,
autoGrow = true,
shouldResetAmount,
onResetAmount,
...props
}: MoneyRequestAmountInputProps,
forwardedRef: ForwardedRef<BaseTextInputRef>,
Expand Down Expand Up @@ -202,10 +216,21 @@ function MoneyRequestAmountInput(
}));

useEffect(() => {
const frontendAmount = onFormatAmount(amount, currency);
setCurrentAmount(frontendAmount);
if (shouldResetAmount) {
setSelection({
start: frontendAmount.length,
end: frontendAmount.length,
});
onResetAmount?.(false);
return;
}

if ((!currency || typeof amount !== 'number' || (formatAmountOnBlur && isTextInputFocused(textInput))) ?? shouldKeepUserInput) {
return;
}
const frontendAmount = onFormatAmount(amount, currency);

setCurrentAmount(frontendAmount);

// Only update selection if the amount prop was changed from the outside and is not the same as the current amount we just computed
Expand All @@ -216,10 +241,7 @@ function MoneyRequestAmountInput(
end: frontendAmount.length,
});
}

// we want to re-initialize the state only when the amount changes
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [amount, shouldKeepUserInput]);
}, [amount, currency, formatAmountOnBlur, shouldKeepUserInput, onFormatAmount, shouldResetAmount, onResetAmount, currentAmount]);

// Modifies the amount to match the decimals for changed currency.
useEffect(() => {
Expand Down
6 changes: 5 additions & 1 deletion src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function MoneyRequestConfirmationList({
const isMerchantRequired = isPolicyExpenseChat && (!isScanRequest || isEditingSplitBill) && shouldShowMerchant;

const isCategoryRequired = !!policy?.requiresCategory;

const [shouldResetAmount, setShouldResetAmount] = useState(false);
useEffect(() => {
if (shouldDisplayFieldError && didConfirmSplit) {
setFormError('iou.error.genericSmartscanFailureMessage');
Expand Down Expand Up @@ -470,6 +470,8 @@ function MoneyRequestConfirmationList({
onFormatAmount={CurrencyUtils.convertToDisplayStringWithoutCurrency}
onAmountChange={(value: string) => onSplitShareChange(participantOption.accountID ?? -1, Number(value))}
maxLength={formattedTotalAmount.length}
shouldResetAmount={shouldResetAmount}
onResetAmount={(resetValue) => setShouldResetAmount(resetValue)}
/>
),
}));
Expand All @@ -492,6 +494,7 @@ function MoneyRequestConfirmationList({
transaction?.comment?.splits,
transaction?.splitShares,
onSplitShareChange,
shouldResetAmount,
]);

const isSplitModified = useMemo(() => {
Expand All @@ -509,6 +512,7 @@ function MoneyRequestConfirmationList({
<PressableWithFeedback
onPress={() => {
IOU.resetSplitShares(transaction);
setShouldResetAmount(true);
}}
accessibilityLabel={CONST.ROLE.BUTTON}
role={CONST.ROLE.BUTTON}
Expand Down

0 comments on commit 109d4aa

Please sign in to comment.