@@ -27,7 +27,11 @@ export default defineComponent({
2727 const instance = getCurrentInstance ( ) ;
2828
2929 const handleUpdate = debounce ( ( val : string ) => {
30- if ( props . trigger . includes ( val [ 0 ] ) ) {
30+ const wordsWithoutSpace = val . split ( ' ' ) ;
31+ const lastWordIndex = wordsWithoutSpace . length - 1 ;
32+ const lastWord = wordsWithoutSpace [ lastWordIndex ] ;
33+ const shouldBeActive = lastWord && props . trigger . includes ( lastWord [ 0 ] ) ;
34+ if ( shouldBeActive ) {
3135 showSuggestions . value = true ;
3236 if ( props . position === 'top' ) {
3337 setTimeout ( ( ) => {
@@ -38,12 +42,12 @@ export default defineComponent({
3842 filteredSuggestions . value = ( suggestions . value as IMentionSuggestionItem [ ] ) . filter ( ( item : IMentionSuggestionItem ) =>
3943 String ( item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] )
4044 . toLocaleLowerCase ( )
41- . includes ( val . slice ( 1 ) . toLocaleLowerCase ( ) )
45+ . includes ( lastWord . slice ( 1 ) . toLocaleLowerCase ( ) )
4246 ) ;
4347 } else {
4448 showSuggestions . value = false ;
4549 }
46- emit ( 'change' , val . slice ( 1 ) ) ;
50+ emit ( 'change' , lastWord . slice ( 1 ) ) ;
4751 } , 300 ) ;
4852
4953 const handleBlur = ( e : Event ) => {
@@ -67,7 +71,12 @@ export default defineComponent({
6771 e . stopPropagation ( ) ;
6872 e . preventDefault ( ) ;
6973 showSuggestions . value = false ;
70- textContext . value = textContext . value . substring ( 0 , 1 ) + item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
74+ const wordsWithoutSpace = textContext . value . split ( ' ' ) ;
75+ const lastWordIndex = wordsWithoutSpace . length - 1 ;
76+ const lastWord = wordsWithoutSpace [ lastWordIndex ] ;
77+ const selectedWord = item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
78+ wordsWithoutSpace [ lastWordIndex ] = `${ lastWord [ 0 ] } ${ selectedWord } ` ;
79+ textContext . value = wordsWithoutSpace . join ( ' ' ) ;
7180 } ;
7281
7382 const arrowKeyDown = ( e : KeyboardEvent ) => {
@@ -102,9 +111,11 @@ export default defineComponent({
102111 e . stopPropagation ( ) ;
103112 e . preventDefault ( ) ;
104113 showSuggestions . value = false ;
105- textContext . value =
106- textContext . value . substring ( 0 , 1 ) +
107- filteredSuggestions . value [ currentIndex . value ] [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
114+ const wordsWithoutSpace = textContext . value . split ( ' ' ) ;
115+ const lastWordIndex = wordsWithoutSpace . length - 1 ;
116+ const selectedWord = filteredSuggestions . value [ currentIndex . value ] [ props . dmValueParse . value as keyof IMentionSuggestionItem ] ;
117+ wordsWithoutSpace [ lastWordIndex ] = `@${ selectedWord } ` ;
118+ textContext . value = wordsWithoutSpace . join ( ' ' ) ;
108119 emit ( 'select' , filteredSuggestions . value [ currentIndex . value ] ) ;
109120 }
110121 }
0 commit comments