Skip to content

Commit b52a303

Browse files
authored
Merge pull request #42158 from bernhardoj/fix/41762-wrong-selection-value-after-replacing-split-value
Fix cursor appears in front of digit when highlighting amount and entering digit
2 parents 4c24663 + 6d0630d commit b52a303

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/components/MoneyRequestAmountInput.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Browser from '@libs/Browser';
77
import * as CurrencyUtils from '@libs/CurrencyUtils';
88
import getOperatingSystem from '@libs/getOperatingSystem';
99
import * as MoneyRequestUtils from '@libs/MoneyRequestUtils';
10+
import shouldIgnoreSelectionWhenUpdatedManually from '@libs/shouldIgnoreSelectionWhenUpdatedManually';
1011
import CONST from '@src/CONST';
1112
import type {BaseTextInputRef} from './TextInput/BaseTextInput/types';
1213
import TextInputWithCurrencySymbol from './TextInputWithCurrencySymbol';
@@ -140,6 +141,8 @@ function MoneyRequestAmountInput(
140141
});
141142

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

144147
/**
145148
* Sets the selection and the amount accordingly to the value passed to the input
@@ -162,13 +165,15 @@ function MoneyRequestAmountInput(
162165

163166
// 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.
164167

168+
willSelectionBeUpdatedManually.current = true;
165169
let hasSelectionBeenSet = false;
166170
setCurrentAmount((prevAmount) => {
167171
const strippedAmount = MoneyRequestUtils.stripCommaFromAmount(finalAmount);
168172
const isForwardDelete = prevAmount.length > strippedAmount.length && forwardDeletePressedRef.current;
169173
if (!hasSelectionBeenSet) {
170174
hasSelectionBeenSet = true;
171175
setSelection((prevSelection) => getNewSelection(prevSelection, isForwardDelete ? strippedAmount.length : prevAmount.length, strippedAmount.length));
176+
willSelectionBeUpdatedManually.current = false;
172177
}
173178
onAmountChange?.(strippedAmount);
174179
return strippedAmount;
@@ -294,6 +299,10 @@ function MoneyRequestAmountInput(
294299
selectedCurrencyCode={currency}
295300
selection={selection}
296301
onSelectionChange={(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
302+
if (shouldIgnoreSelectionWhenUpdatedManually && willSelectionBeUpdatedManually.current) {
303+
willSelectionBeUpdatedManually.current = false;
304+
return;
305+
}
297306
if (!shouldUpdateSelection) {
298307
return;
299308
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type ShouldIgnoreSelectionWhenUpdatedManually from './types';
2+
3+
const shouldIgnoreSelectionWhenUpdatedManually: ShouldIgnoreSelectionWhenUpdatedManually = true;
4+
5+
export default shouldIgnoreSelectionWhenUpdatedManually;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type ShouldIgnoreSelectionWhenUpdatedManually from './types';
2+
3+
const shouldIgnoreSelectionWhenUpdatedManually: ShouldIgnoreSelectionWhenUpdatedManually = false;
4+
5+
export default shouldIgnoreSelectionWhenUpdatedManually;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type ShouldIgnoreSelectionWhenUpdatedManually = boolean;
2+
3+
export default ShouldIgnoreSelectionWhenUpdatedManually;

0 commit comments

Comments
 (0)