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

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

9 changes: 9 additions & 0 deletions src/components/MoneyRequestAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Browser from '@libs/Browser';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import getOperatingSystem from '@libs/getOperatingSystem';
import * as MoneyRequestUtils from '@libs/MoneyRequestUtils';
import shouldIgnoreSelectionWhenUpdatedManually from '@libs/shouldIgnoreSelectionWhenUpdatedManually';
import CONST from '@src/CONST';
import type {BaseTextInputRef} from './TextInput/BaseTextInput/types';
import TextInputWithCurrencySymbol from './TextInputWithCurrencySymbol';
Expand Down Expand Up @@ -140,6 +141,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 +165,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 +299,10 @@ function MoneyRequestAmountInput(
selectedCurrencyCode={currency}
selection={selection}
onSelectionChange={(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
if (shouldIgnoreSelectionWhenUpdatedManually && willSelectionBeUpdatedManually.current) {
willSelectionBeUpdatedManually.current = false;
return;
}
if (!shouldUpdateSelection) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,6 @@ function navigateToAndOpenReportWithAccountIDs(participantAccountIDs: number[])
*/
function navigateToAndOpenChildReport(childReportID = '0', parentReportAction: Partial<ReportAction> = {}, parentReportID = '0') {
if (childReportID !== '0') {
openReport(childReportID);
Copy link
Contributor

Choose a reason for hiding this comment

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

@bernhardoj I am confused why we removed the openReport call from here when I have approved the PR then this change was not here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, my bad. It's from my lint commit here: 6d0630d

I think I accidentally committed it when working on this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, it is from another PR, but it was intentional to remove it, so there is no need to revert this change, right?

Copy link
Contributor Author

@bernhardoj bernhardoj Jun 19, 2024

Choose a reason for hiding this comment

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

Yes, it was intentional to remove it, so no need to revert.

Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(childReportID));
} else {
const participantAccountIDs = [...new Set([currentUserAccountID, Number(parentReportAction.actorAccountID)])];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type ShouldIgnoreSelectionWhenUpdatedManually from './types';

const shouldIgnoreSelectionWhenUpdatedManually: ShouldIgnoreSelectionWhenUpdatedManually = true;

export default shouldIgnoreSelectionWhenUpdatedManually;
5 changes: 5 additions & 0 deletions src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type ShouldIgnoreSelectionWhenUpdatedManually from './types';

const shouldIgnoreSelectionWhenUpdatedManually: ShouldIgnoreSelectionWhenUpdatedManually = false;

export default shouldIgnoreSelectionWhenUpdatedManually;
3 changes: 3 additions & 0 deletions src/libs/shouldIgnoreSelectionWhenUpdatedManually/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type ShouldIgnoreSelectionWhenUpdatedManually = boolean;

export default ShouldIgnoreSelectionWhenUpdatedManually;
Loading