Skip to content

Commit

Permalink
Merge pull request #42158 from bernhardoj/fix/41762-wrong-selection-v…
Browse files Browse the repository at this point in the history
…alue-after-replacing-split-value

Fix cursor appears in front of digit when highlighting amount and entering digit
  • Loading branch information
youssef-lr authored Jun 10, 2024
2 parents 4c24663 + 6d0630d commit b52a303
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
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
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;

0 comments on commit b52a303

Please sign in to comment.