File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed
src/components/AutoCompleteTextarea Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -204,12 +204,20 @@ class ReactTextareaAutocomplete extends React.Component {
204204
205205 const textToModify = textareaValue . slice ( 0 , selectionEnd ) ;
206206
207- const startOfTokenPosition = textToModify . search (
208- /**
209- * It's important to escape the currentTrigger char for chars like [, (,...
210- */
211- new RegExp ( `\\${ currentTrigger } ${ `[^\\${ currentTrigger } ${ '\\s' } ]` } *$` ) ,
207+ // It's important to escape the currentTrigger char for chars like [, (,...
208+ const findTriggerRegExp = new RegExp (
209+ `\\${ currentTrigger } ${ `[^\\${ currentTrigger } ${ '\\s' } ]` } *` ,
212210 ) ;
211+ // reverse the string so we can easily find the first index of the currentTrigger
212+ const reversedString = textToModify . split ( '' ) . reverse ( ) . join ( '' ) ;
213+ // find the first instance of currentTrigger (-1 if not found)
214+ const distanceFromEndOfString =
215+ reversedString . search ( findTriggerRegExp ) + 1 ;
216+ // if found, distract from the length of the string to revert the position back to an actual string index.
217+ const startOfTokenPosition =
218+ distanceFromEndOfString === - 1
219+ ? 0
220+ : textToModify . length - distanceFromEndOfString ;
213221
214222 // we add space after emoji is selected if a caret position is next
215223 const newTokenString =
You can’t perform that action at this time.
0 commit comments