1- import React , { useContext , useEffect , useRef , useState } from 'react' ;
1+ import React , { useContext , useEffect , useRef } from 'react' ;
22import styled from '@stream-io/styled-components' ;
33import PropTypes from 'prop-types' ;
44
@@ -43,8 +43,7 @@ const AutoCompleteInput = ({
4343 const { t } = useContext ( TranslationContext ) ;
4444
4545 const isTrackingStarted = useRef ( false ) ;
46-
47- const [ selectionEnd , setSelectionEnd ] = useState ( 0 ) ;
46+ const selectionEnd = useRef ( 0 ) ;
4847
4948 useEffect ( ( ) => {
5049 handleChange ( value , true ) ;
@@ -72,7 +71,6 @@ const AutoCompleteInput = ({
7271 if ( query !== queryCallback ) {
7372 return ;
7473 }
75-
7674 updateSuggestionsContext ( {
7775 data,
7876 onSelect : ( item ) => onSelectSuggestion ( { item, trigger } ) ,
@@ -94,7 +92,7 @@ const AutoCompleteInput = ({
9492 selection : { end } ,
9593 } ,
9694 } ) => {
97- setSelectionEnd ( end ) ;
95+ selectionEnd . current = end ;
9896 } ;
9997
10098 const onSelectSuggestion = ( { item, trigger } ) => {
@@ -104,7 +102,7 @@ const AutoCompleteInput = ({
104102 return ;
105103 }
106104
107- const textToModify = value . slice ( 0 , selectionEnd ) ;
105+ const textToModify = value . slice ( 0 , selectionEnd . current ) ;
108106
109107 const startOfTokenPosition = textToModify . search (
110108 /**
@@ -128,7 +126,7 @@ const AutoCompleteInput = ({
128126 stopTracking ( ) ;
129127 onChange ( value . replace ( textToModify , modifiedText ) ) ;
130128
131- setSelectionEnd ( newCaretPosition || 0 ) ;
129+ selectionEnd . current = newCaretPosition || 0 ;
132130
133131 if ( triggerSettings [ trigger ] . callback ) {
134132 triggerSettings [ trigger ] . callback ( item ) ;
@@ -190,12 +188,12 @@ const AutoCompleteInput = ({
190188 const handleSuggestions = ( text ) => {
191189 setTimeout ( async ( ) => {
192190 if (
193- text . slice ( selectionEnd - 1 , selectionEnd ) === ' ' &&
191+ text . slice ( selectionEnd . current - 1 , selectionEnd . current ) === ' ' &&
194192 ! isTrackingStarted . current
195193 ) {
196194 stopTracking ( ) ;
197195 } else if ( ! ( await handleCommand ( text ) ) ) {
198- handleMentions ( { selectionEnd, text } ) ;
196+ handleMentions ( { selectionEnd : selectionEnd . current , text } ) ;
199197 }
200198 } , 100 ) ;
201199 } ;
0 commit comments