From 69704c108f4cc2154101fa6175de8943deb8f43d Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Tue, 22 Oct 2024 15:20:10 +0200 Subject: [PATCH 1/2] Read amount from ref in onSelectionChange in MoneyRequestAmountInput --- src/components/MoneyRequestAmountInput.tsx | 8 ++++++-- .../{index.android.tsx => index.native.tsx} | 0 2 files changed, 6 insertions(+), 2 deletions(-) rename src/libs/shouldIgnoreSelectionWhenUpdatedManually/{index.android.tsx => index.native.tsx} (100%) diff --git a/src/components/MoneyRequestAmountInput.tsx b/src/components/MoneyRequestAmountInput.tsx index 702e6c384b58..f0c83a5e776f 100644 --- a/src/components/MoneyRequestAmountInput.tsx +++ b/src/components/MoneyRequestAmountInput.tsx @@ -135,6 +135,8 @@ function MoneyRequestAmountInput( const textInput = useRef(null); + const amountRef = useRef(undefined); + const decimals = CurrencyUtils.getCurrencyDecimals(currency); const selectedAmountAsString = amount ? onFormatAmount(amount, currency) : ''; @@ -172,8 +174,9 @@ function MoneyRequestAmountInput( willSelectionBeUpdatedManually.current = true; let hasSelectionBeenSet = false; + const strippedAmount = MoneyRequestUtils.stripCommaFromAmount(finalAmount); + amountRef.current = strippedAmount; setCurrentAmount((prevAmount) => { - const strippedAmount = MoneyRequestUtils.stripCommaFromAmount(finalAmount); const isForwardDelete = prevAmount.length > strippedAmount.length && forwardDeletePressedRef.current; if (!hasSelectionBeenSet) { hasSelectionBeenSet = true; @@ -312,7 +315,8 @@ function MoneyRequestAmountInput( if (!shouldUpdateSelection) { return; } - const maxSelection = formattedAmount.length; + const maxSelection = amountRef.current?.length ?? formattedAmount.length; + amountRef.current = undefined; const start = Math.min(e.nativeEvent.selection.start, maxSelection); const end = Math.min(e.nativeEvent.selection.end, maxSelection); setSelection({start, end}); diff --git a/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.native.tsx similarity index 100% rename from src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx rename to src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.native.tsx From 47969e5341dd47148ff1495e17b86a8441391bc7 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Thu, 24 Oct 2024 11:39:49 +0200 Subject: [PATCH 2/2] Add comment to onSelectionChange in MoneyRequestAmountInput --- src/components/MoneyRequestAmountInput.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/components/MoneyRequestAmountInput.tsx b/src/components/MoneyRequestAmountInput.tsx index f0c83a5e776f..b06968ed7219 100644 --- a/src/components/MoneyRequestAmountInput.tsx +++ b/src/components/MoneyRequestAmountInput.tsx @@ -315,6 +315,8 @@ function MoneyRequestAmountInput( if (!shouldUpdateSelection) { return; } + + // When the amount is updated in setNewAmount on iOS, in onSelectionChange formattedAmount stores the value before the update. Using amountRef allows us to read the updated value const maxSelection = amountRef.current?.length ?? formattedAmount.length; amountRef.current = undefined; const start = Math.min(e.nativeEvent.selection.start, maxSelection);