Skip to content
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

Resolve Cursor Position Issue on Long-Press in Android Chrome #30082

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/pages/iou/steps/MoneyRequestAmountForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,19 @@ function MoneyRequestAmountForm({amount, currency, isEditing, forwardedRef, onCu
if (!_.isEmpty(formError)) {
setFormError('');
}

// There was an issue where long-pressing the back button in Android Chrome removed the last digit but moved the cursor ahead two positions instead of one. This occurred because setCurrentAmount contains another setState, making it impure and error-prone. Various solutions were suggested, including merging currentAmount and selection; however, this solution introducing the hasSelectionBeenSet flag was chosen for its simplicity and lower risk of future errors https://github.com/Expensify/App/issues/23300.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super long. Can we make it short and crisp and link the GH comment..


let hasSelectionBeenSet = false;
setCurrentAmount((prevAmount) => {
const strippedAmount = MoneyRequestUtils.stripCommaFromAmount(newAmountWithoutSpaces);
const isForwardDelete = prevAmount.length > strippedAmount.length && forwardDeletePressedRef.current;
setSelection((prevSelection) => getNewSelection(prevSelection, isForwardDelete ? strippedAmount.length : prevAmount.length, strippedAmount.length));
if (!hasSelectionBeenSet) {
setSelection((prevSelection) => {
hasSelectionBeenSet = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to set it inside the setSelection, wouldn't having it after the if statement more meaningful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that makes more sense. That's what I had intially tried but saw some issues. I don't see those now, so i have moved the hasSelectionBeenSet out of setSelection

return getNewSelection(prevSelection, isForwardDelete ? strippedAmount.length : prevAmount.length, strippedAmount.length);
});
}
return strippedAmount;
});
},
Expand Down
Loading