Skip to content

Commit

Permalink
Refactor onFlipAmount to handle amount parsing and update selection i…
Browse files Browse the repository at this point in the history
…n MoneyRequestAmountForm
  • Loading branch information
pasyukevich committed Jan 30, 2025
1 parent 0fe0e70 commit 7f331c8
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/pages/iou/MoneyRequestAmountForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,17 @@ function MoneyRequestAmountForm(
);

const onFlipAmount = useCallback(() => {
const currentAmount = moneyRequestAmountInput.current?.getAmount() ?? '';
const newAmount = convertToFrontendAmountAsInteger(-Number(currentAmount), currency);
initializeAmount(newAmount);
}, [currency, initializeAmount]);
const currentAmount = moneyRequestAmountInput.current?.getAmount() ?? '0';
const parsedAmount = Number(currentAmount.replace(/\./g, '')); // Remove any commas for parsing
const newAmount = parsedAmount * -1;
const frontendAmount = convertToFrontendAmountAsString(newAmount, currency);

moneyRequestAmountInput.current?.changeAmount(frontendAmount);
moneyRequestAmountInput.current?.changeSelection({
start: frontendAmount.length,
end: frontendAmount.length,
});
}, [currency]);

useEffect(() => {
if (!currency || typeof amount !== 'number') {
Expand Down Expand Up @@ -302,15 +309,13 @@ function MoneyRequestAmountForm(
<View style={[styles.flexRow, styles.justifyContentCenter, styles.mt5, styles.gap2]}>
<Button
allowBubble={!isEditing}
pressOnEnter
shouldShowRightIcon
iconRight={Expensicons.DownArrow}
onPress={onCurrencyButtonPress}
text={currency}
/>
<Button
allowBubble={!isEditing}
pressOnEnter
shouldShowRightIcon
iconRight={Expensicons.PlusMinus}
onPress={onFlipAmount}
Expand Down

0 comments on commit 7f331c8

Please sign in to comment.