Skip to content

Commit

Permalink
Merge pull request Expensify#51306 from software-mansion-labs/fix/sel…
Browse files Browse the repository at this point in the history
…ection-on-ios

iOS - Hybrid app - Split - Cursor jumps in front of digit when deleting participant's amount
  • Loading branch information
mountiny authored Oct 30, 2024
2 parents f806997 + 47969e5 commit ffc8c3d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
10 changes: 8 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 @@ -313,7 +316,10 @@ function MoneyRequestAmountInput(
if (!shouldUpdateSelection) {
return;
}
const maxSelection = formattedAmount.length;

// 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);
const end = Math.min(e.nativeEvent.selection.end, maxSelection);
setSelection({start, end});
Expand Down

0 comments on commit ffc8c3d

Please sign in to comment.