From 49fc180982c55bb5d39ea61930f054746b43da27 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Fri, 13 Jan 2023 21:06:16 +0200 Subject: [PATCH] Fix the caret position when using the search phrase modifier (#6972) --- .../search/initial/modifiers/modifier.tsx | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/screens/home/search/initial/modifiers/modifier.tsx b/app/screens/home/search/initial/modifiers/modifier.tsx index b58df28bd..bb75cb3f9 100644 --- a/app/screens/home/search/initial/modifiers/modifier.tsx +++ b/app/screens/home/search/initial/modifiers/modifier.tsx @@ -2,7 +2,7 @@ // See LICENSE.txt for license information. import React, {Dispatch, RefObject, SetStateAction, useCallback} from 'react'; -import {StyleSheet} from 'react-native'; +import {Platform, StyleSheet} from 'react-native'; import OptionItem from '@components/option_item'; import {SearchRef} from '@components/search'; @@ -33,6 +33,12 @@ const Modifier = ({item, searchRef, searchValue, setSearchValue}: Props) => { addModifierTerm(item.term); }, [item.term, searchValue]); + const setNativeCursorPositionProp = (position?: number) => { + setTimeout(() => { + searchRef.current?.setNativeProps({selection: {start: position, end: position}}); + }, 50); + }; + const addModifierTerm = preventDoubleTap((modifierTerm) => { let newValue = ''; if (!searchValue) { @@ -46,9 +52,16 @@ const Modifier = ({item, searchRef, searchValue, setSearchValue}: Props) => { setSearchValue(newValue); if (item.cursorPosition) { const position = newValue.length + item.cursorPosition; - setTimeout(() => { - searchRef.current?.setNativeProps({selection: {start: position, end: position}}); - }, 50); + setNativeCursorPositionProp(position); + + if (Platform.OS === 'android') { + // on Android the selection set by setNativeProps is permanent thus the caret returns to the same + // position after we stop typing for a few ms. By setting the position to undefined, + // then the caret remains in place. + setTimeout(() => { + setNativeCursorPositionProp(undefined); + }, 50); + } } });