@@ -27,7 +27,11 @@ export default defineComponent({
27
27
const instance = getCurrentInstance ( ) ;
28
28
29
29
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 ) {
31
35
showSuggestions . value = true ;
32
36
if ( props . position === 'top' ) {
33
37
setTimeout ( ( ) => {
@@ -38,12 +42,12 @@ export default defineComponent({
38
42
filteredSuggestions . value = ( suggestions . value as IMentionSuggestionItem [ ] ) . filter ( ( item : IMentionSuggestionItem ) =>
39
43
String ( item [ props . dmValueParse . value as keyof IMentionSuggestionItem ] )
40
44
. toLocaleLowerCase ( )
41
- . includes ( val . slice ( 1 ) . toLocaleLowerCase ( ) )
45
+ . includes ( lastWord . slice ( 1 ) . toLocaleLowerCase ( ) )
42
46
) ;
43
47
} else {
44
48
showSuggestions . value = false ;
45
49
}
46
- emit ( 'change' , val . slice ( 1 ) ) ;
50
+ emit ( 'change' , lastWord . slice ( 1 ) ) ;
47
51
} , 300 ) ;
48
52
49
53
const handleBlur = ( e : Event ) => {
@@ -67,7 +71,12 @@ export default defineComponent({
67
71
e . stopPropagation ( ) ;
68
72
e . preventDefault ( ) ;
69
73
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 ( ' ' ) ;
71
80
} ;
72
81
73
82
const arrowKeyDown = ( e : KeyboardEvent ) => {
@@ -102,9 +111,11 @@ export default defineComponent({
102
111
e . stopPropagation ( ) ;
103
112
e . preventDefault ( ) ;
104
113
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 ( ' ' ) ;
108
119
emit ( 'select' , filteredSuggestions . value [ currentIndex . value ] ) ;
109
120
}
110
121
}
0 commit comments