Skip to content

Fix cursor appears in front of digit when highlighting amount and entering digit #42158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8 changes: 8 additions & 0 deletions src/components/MoneyRequestAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ function MoneyRequestAmountInput(
});

const forwardDeletePressedRef = useRef(false);
// The ref is used to ignore any onSelectionChange event that happens while we are updating the selection manually in setNewAmount
const willSelectionBeUpdatedManually = useRef(false);

/**
* Sets the selection and the amount accordingly to the value passed to the input
Expand All @@ -162,13 +164,15 @@ function MoneyRequestAmountInput(

// setCurrentAmount contains another setState(setSelection) making it error-prone since it is leading to setSelection being called twice for a single setCurrentAmount call. This solution introducing the hasSelectionBeenSet flag was chosen for its simplicity and lower risk of future errors https://github.com/Expensify/App/issues/23300#issuecomment-1766314724.

willSelectionBeUpdatedManually.current = true;
let hasSelectionBeenSet = false;
setCurrentAmount((prevAmount) => {
const strippedAmount = MoneyRequestUtils.stripCommaFromAmount(finalAmount);
const isForwardDelete = prevAmount.length > strippedAmount.length && forwardDeletePressedRef.current;
if (!hasSelectionBeenSet) {
hasSelectionBeenSet = true;
setSelection((prevSelection) => getNewSelection(prevSelection, isForwardDelete ? strippedAmount.length : prevAmount.length, strippedAmount.length));
willSelectionBeUpdatedManually.current = false;
}
onAmountChange?.(strippedAmount);
return strippedAmount;
Expand Down Expand Up @@ -294,6 +298,10 @@ function MoneyRequestAmountInput(
selectedCurrencyCode={currency}
selection={selection}
onSelectionChange={(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
if (willSelectionBeUpdatedManually.current) {
willSelectionBeUpdatedManually.current = false;
return;
}
if (!shouldUpdateSelection) {
return;
}
Expand Down
Loading