Skip to content

Commit

Permalink
Read amount from ref in onSelectionChange in MoneyRequestAmountInput
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Oct 22, 2024
1 parent 04214cd commit 69704c1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/components/MoneyRequestAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ function MoneyRequestAmountInput(

const textInput = useRef<BaseTextInputRef | null>(null);

const amountRef = useRef<string | undefined>(undefined);

const decimals = CurrencyUtils.getCurrencyDecimals(currency);
const selectedAmountAsString = amount ? onFormatAmount(amount, currency) : '';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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});
Expand Down

0 comments on commit 69704c1

Please sign in to comment.