@@ -26,13 +26,18 @@ export default defineComponent({
2626 ClickOutside,
2727 } ,
2828 props : tagInputProps ,
29- emits : [ 'update:tags ' , 'update:suggestionList' , 'change' ] ,
29+ emits : [ 'update:modelValue ' , 'update:suggestionList' , 'change' ] ,
3030 setup ( props : TagInputProps , ctx : SetupContext ) {
3131 const app = getCurrentInstance ( ) ;
3232 const t = createI18nTranslate ( 'DTagInput' , app ) ;
3333
3434 const ns = useNamespace ( 'tag-input' ) ;
3535
36+ const selectedTags = ref < Array < Suggestion > > ( [ ] ) ;
37+ watch ( ( ) => props . modelValue , ( ) => {
38+ selectedTags . value = props . modelValue ;
39+ } , { immediate : true , deep : true } ) ;
40+
3641 const add = ( arr : Suggestion [ ] , target : Suggestion ) => {
3742 const res = Object . assign ( { } , target ) ;
3843 delete res . __index ;
@@ -96,7 +101,7 @@ export default defineComponent({
96101 if ( tagInputVal . value === '' && mergedSuggestions . value . length === 0 ) {
97102 return false ;
98103 }
99- if ( props . tags . findIndex ( ( item ) => item [ props . displayProperty ] === tagInputVal . value ) > - 1 ) {
104+ if ( selectedTags . value . findIndex ( ( item ) => item [ props . displayProperty ] === tagInputVal . value ) > - 1 ) {
100105 tagInputVal . value = '' ;
101106 return false ;
102107 }
@@ -113,20 +118,20 @@ export default defineComponent({
113118 ctx . emit ( 'update:suggestionList' , remove ( props . suggestionList , target . __index ) ) ;
114119 }
115120
116- const newTags = add ( props . tags , res ) ;
117- ctx . emit ( 'change' , props . tags , newTags ) ;
118- ctx . emit ( 'update:tags ' , newTags ) ;
121+ const newTags = add ( selectedTags . value , res ) ;
122+ ctx . emit ( 'change' , selectedTags . value , newTags ) ;
123+ ctx . emit ( 'update:modelValue ' , newTags ) ;
119124 mergedSuggestions . value . length === 0 && ( tagInputVal . value = '' ) ;
120125 } ;
121126
122127 const { onInputKeydown } = useInputKeydown ( props , handleEnter , onSelectIndexChange ) ;
123128
124129 const removeTag = ( $event : Event , tagIdx : number ) => {
125130 $event . preventDefault ( ) ;
126- ctx . emit ( 'update:suggestionList' , add ( props . suggestionList , props . tags [ tagIdx ] ) ) ;
127- const newTags = remove ( props . tags , tagIdx ) ;
128- ctx . emit ( 'change' , props . tags , newTags ) ;
129- ctx . emit ( 'update:tags ' , newTags ) ;
131+ ctx . emit ( 'update:suggestionList' , add ( props . suggestionList , selectedTags . value [ tagIdx ] ) ) ;
132+ const newTags = remove ( selectedTags . value , tagIdx ) ;
133+ ctx . emit ( 'change' , selectedTags . value , newTags ) ;
134+ ctx . emit ( 'update:modelValue ' , newTags ) ;
130135
131136 nextTick ( ( ) => {
132137 tagInputRef . value ?. focus ( ) ;
@@ -137,14 +142,14 @@ export default defineComponent({
137142 const onSuggestionItemClick = ( $event : Event , itemIndex : number ) => {
138143 $event . preventDefault ( ) ;
139144 const target = mergedSuggestions . value [ itemIndex ] ;
140- const newTags = add ( props . tags , target ) ;
145+ const newTags = add ( selectedTags . value , target ) ;
141146 const newSuggestions = remove ( props . suggestionList , target . __index ) ;
142- ctx . emit ( 'change' , props . tags , newTags ) ;
143- ctx . emit ( 'update:tags ' , newTags ) ;
147+ ctx . emit ( 'change' , selectedTags . value , newTags ) ;
148+ ctx . emit ( 'update:modelValue ' , newTags ) ;
144149 ctx . emit ( 'update:suggestionList' , newSuggestions ) ;
145150 } ;
146151
147- const isTagsLimit = computed ( ( ) => props . maxTags <= props . tags . length ) ;
152+ const isTagsLimit = computed ( ( ) => props . maxTags <= selectedTags . value . length ) ;
148153 const isShowSuggestion = computed ( ( ) => {
149154 return ! props . disabled && ! isTagsLimit . value && isInputBoxFocus . value ;
150155 } ) ;
@@ -153,7 +158,7 @@ export default defineComponent({
153158 // 已选择 tags 列表
154159 const chosenTags = ( ) => {
155160 return < ul class = "devui-tag-list" title = { props . disabled ? props . disabledText : '' } >
156- { props . tags . map ( ( tag , tagIdx ) => {
161+ { selectedTags . value . map ( ( tag , tagIdx ) => {
157162 return (
158163 < li class = "devui-tag-item" >
159164 < span > { tag [ props . displayProperty ] } </ span >
0 commit comments