From a7964d3ff2efd3161857e21672c5880c9d2519d9 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Fri, 13 Sep 2024 17:13:11 +0200 Subject: [PATCH] Remove selection from PercentageForm --- src/components/PercentageForm.tsx | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/components/PercentageForm.tsx b/src/components/PercentageForm.tsx index 8d9ca950f49c..76e3b19891c4 100644 --- a/src/components/PercentageForm.tsx +++ b/src/components/PercentageForm.tsx @@ -1,6 +1,5 @@ import type {ForwardedRef} from 'react'; -import React, {forwardRef, useCallback, useMemo, useRef, useState} from 'react'; -import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native'; +import React, {forwardRef, useCallback, useMemo, useRef} from 'react'; import useLocalize from '@hooks/useLocalize'; import * as MoneyRequestUtils from '@libs/MoneyRequestUtils'; import CONST from '@src/CONST'; @@ -21,14 +20,6 @@ type PercentageFormProps = { label?: string; }; -/** - * Returns the new selection object based on the updated amount's length - */ -const getNewSelection = (oldSelection: {start: number; end: number}, prevLength: number, newLength: number) => { - const cursorPosition = oldSelection.end + (newLength - prevLength); - return {start: cursorPosition, end: cursorPosition}; -}; - function PercentageForm({value: amount, errorText, onInputChange, label, ...rest}: PercentageFormProps, forwardedRef: ForwardedRef) { const {toLocaleDigit, numberFormat} = useLocalize(); @@ -36,13 +27,6 @@ function PercentageForm({value: amount, errorText, onInputChange, label, ...rest const currentAmount = useMemo(() => (typeof amount === 'string' ? amount : ''), [amount]); - const [selection, setSelection] = useState({ - start: currentAmount.length, - end: currentAmount.length, - }); - - const forwardDeletePressedRef = useRef(false); - /** * Sets the selection and the amount accordingly to the value passed to the input * @param newAmount - Changed amount from user input @@ -55,16 +39,13 @@ function PercentageForm({value: amount, errorText, onInputChange, label, ...rest // Use a shallow copy of selection to trigger setSelection // More info: https://github.com/Expensify/App/issues/16385 if (!MoneyRequestUtils.validatePercentage(newAmountWithoutSpaces)) { - setSelection((prevSelection) => ({...prevSelection})); return; } const strippedAmount = MoneyRequestUtils.stripCommaFromAmount(newAmountWithoutSpaces); - const isForwardDelete = currentAmount.length > strippedAmount.length && forwardDeletePressedRef.current; - setSelection(getNewSelection(selection, isForwardDelete ? strippedAmount.length : currentAmount.length, strippedAmount.length)); onInputChange?.(strippedAmount); }, - [currentAmount, onInputChange, selection], + [onInputChange], ); const formattedAmount = MoneyRequestUtils.replaceAllDigits(currentAmount, toLocaleDigit); @@ -84,10 +65,6 @@ function PercentageForm({value: amount, errorText, onInputChange, label, ...rest } textInput.current = ref; }} - selection={selection} - onSelectionChange={(e: NativeSyntheticEvent) => { - setSelection(e.nativeEvent.selection); - }} suffixCharacter="%" keyboardType={CONST.KEYBOARD_TYPE.DECIMAL_PAD} // eslint-disable-next-line react/jsx-props-no-spreading