From 2263ee97af35ca6ddd2dfdab2e32ddd67190581f Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Tue, 14 May 2024 22:16:29 +0800 Subject: [PATCH 1/3] fix wrong selection after replacing all values --- src/components/MoneyRequestAmountInput.tsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/components/MoneyRequestAmountInput.tsx b/src/components/MoneyRequestAmountInput.tsx index a59b50e5bdb7..6201df3e8e6f 100644 --- a/src/components/MoneyRequestAmountInput.tsx +++ b/src/components/MoneyRequestAmountInput.tsx @@ -123,6 +123,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 @@ -145,6 +147,7 @@ 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); @@ -152,6 +155,7 @@ function MoneyRequestAmountInput( if (!hasSelectionBeenSet) { hasSelectionBeenSet = true; setSelection((prevSelection) => getNewSelection(prevSelection, isForwardDelete ? strippedAmount.length : prevAmount.length, strippedAmount.length)); + willSelectionBeUpdatedManually.current = false; } onAmountChange?.(strippedAmount); return strippedAmount; @@ -266,6 +270,10 @@ function MoneyRequestAmountInput( selectedCurrencyCode={currency} selection={selection} onSelectionChange={(e: NativeSyntheticEvent) => { + if (willSelectionBeUpdatedManually.current) { + willSelectionBeUpdatedManually.current = false; + return; + } if (!shouldUpdateSelection) { return; } From 7481eb3a985328883717a6c09df066f7686a51bb Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Fri, 7 Jun 2024 17:07:21 +0800 Subject: [PATCH 2/3] only apply the fix for android --- src/components/MoneyRequestAmountInput.tsx | 3 ++- .../index.android.tsx | 5 +++++ src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx | 5 +++++ src/libs/shouldIgnoreSelectionWhenUpdatedManually/types.ts | 3 +++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx create mode 100644 src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx create mode 100644 src/libs/shouldIgnoreSelectionWhenUpdatedManually/types.ts diff --git a/src/components/MoneyRequestAmountInput.tsx b/src/components/MoneyRequestAmountInput.tsx index d97d6ef39af4..fb17297dc642 100644 --- a/src/components/MoneyRequestAmountInput.tsx +++ b/src/components/MoneyRequestAmountInput.tsx @@ -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'; @@ -298,7 +299,7 @@ function MoneyRequestAmountInput( selectedCurrencyCode={currency} selection={selection} onSelectionChange={(e: NativeSyntheticEvent) => { - if (willSelectionBeUpdatedManually.current) { + if (shouldIgnoreSelectionWhenUpdatedManually && willSelectionBeUpdatedManually.current) { willSelectionBeUpdatedManually.current = false; return; } diff --git a/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx new file mode 100644 index 000000000000..d786760e8691 --- /dev/null +++ b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx @@ -0,0 +1,5 @@ +import type ShouldIgnoreSelectionWhenUpdatedManually from './types.ts'; + +const shouldIgnoreSelectionWhenUpdatedManually: ShouldIgnoreSelectionWhenUpdatedManually = true; + +export default shouldIgnoreSelectionWhenUpdatedManually; diff --git a/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx new file mode 100644 index 000000000000..0e7c69b39386 --- /dev/null +++ b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx @@ -0,0 +1,5 @@ +import type ShouldIgnoreSelectionWhenUpdatedManually from './types.ts'; + +const shouldIgnoreSelectionWhenUpdatedManually: ShouldIgnoreSelectionWhenUpdatedManually = false; + +export default shouldIgnoreSelectionWhenUpdatedManually; diff --git a/src/libs/shouldIgnoreSelectionWhenUpdatedManually/types.ts b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/types.ts new file mode 100644 index 000000000000..56394183ef7d --- /dev/null +++ b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/types.ts @@ -0,0 +1,3 @@ +type ShouldIgnoreSelectionWhenUpdatedManually = boolean; + +export default ShouldIgnoreSelectionWhenUpdatedManually; From 6d0630d35122717a484469b25e78fad35dbaa825 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Fri, 7 Jun 2024 17:17:35 +0800 Subject: [PATCH 3/3] lint --- src/libs/actions/Report.ts | 1 - .../shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx | 2 +- src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 1262e8af7d44..01025f21aa78 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1027,7 +1027,6 @@ function navigateToAndOpenReportWithAccountIDs(participantAccountIDs: number[]) */ function navigateToAndOpenChildReport(childReportID = '0', parentReportAction: Partial = {}, parentReportID = '0') { if (childReportID !== '0') { - openReport(childReportID); Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(childReportID)); } else { const participantAccountIDs = [...new Set([currentUserAccountID, Number(parentReportAction.actorAccountID)])]; diff --git a/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx index d786760e8691..289c56ad69be 100644 --- a/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx +++ b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.android.tsx @@ -1,4 +1,4 @@ -import type ShouldIgnoreSelectionWhenUpdatedManually from './types.ts'; +import type ShouldIgnoreSelectionWhenUpdatedManually from './types'; const shouldIgnoreSelectionWhenUpdatedManually: ShouldIgnoreSelectionWhenUpdatedManually = true; diff --git a/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx index 0e7c69b39386..744a94aa1f32 100644 --- a/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx +++ b/src/libs/shouldIgnoreSelectionWhenUpdatedManually/index.tsx @@ -1,4 +1,4 @@ -import type ShouldIgnoreSelectionWhenUpdatedManually from './types.ts'; +import type ShouldIgnoreSelectionWhenUpdatedManually from './types'; const shouldIgnoreSelectionWhenUpdatedManually: ShouldIgnoreSelectionWhenUpdatedManually = false;