-
Notifications
You must be signed in to change notification settings - Fork 15
/
star-input-tag.vue
159 lines (151 loc) · 3.18 KB
/
star-input-tag.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!--
v-model="tagList" 标签内容,tagList为数组或者字符串,字符串每个标签以逗号隔开
theme="+ New Tag" 新增按钮名称,默认为+ New Tag
created(){ 字符串转数组,定义在父组件
if(typeof this.tagList =='string'){
this.tagList = this.tagList.split(',');
}
},
-->
<template>
<div>
<el-tag
v-for="(tag,index) in dynamicTags"
:key="index" closable
:disable-transitions="false"
@click="editTag(tag,index)"
@close="handleClose(tag)">
<span v-if="index!=num">{{tag}}</span>
<input
class="custom_input"
type="text" v-model="words"
v-if="index==num"
ref="editInput"
@keyup.enter.native="handleInput(tag,index)"
@blur="handleInput(tag,index)">
</el-tag>
<el-input
class="input-new-tag"
v-if="inputVisible"
v-model="inputValue"
ref="saveTagInput"
size="small"
@keyup.enter.native="handleInputConfirm"
@blur="handleInputConfirm">
</el-input>
<el-button
v-else
class="button-new-tag"
size="small"
@click="showInput">{{theme}}</el-button>
</div>
</template>
<script>
export default {
name: 'star-input-tag',
model: {
prop: 'tagList',
event: 'input'
},
props: {
tagList: {
type: Array,
default () {
return []
}
},
theme: {
type: String,
default: '+ 新标签'
}
},
mounted() {
console.log()
},
data() {
return {
inputVisible: false,
inputValue: '',
num: -1,
words: ''
}
},
computed: {
dynamicTags: {
get() {
return this.tagList;
},
set(tagList) {
this.$emit('input', tagList);
}
}
},
methods: {
// 数组去重
unique(arr) {
let x = new Set(arr);
return [...x];
},
handleClose(tag) {
this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
},
showInput() {
this.inputVisible = true;
this.$nextTick(_ => {
this.$refs.saveTagInput.$refs.input.focus();
});
},
handleInputConfirm() {
let inputValue = this.inputValue;
if (inputValue) {
this.dynamicTags.push(inputValue);
}
this.dynamicTags = this.unique(this.dynamicTags);
this.inputVisible = false;
this.inputValue = '';
},
editTag(tag, index) {
this.num = index;
this.$nextTick(_ => {
this.$refs.editInput[0].focus();
});
this.words = tag;
},
handleInput(tag, index) {
let words = this.words;
if (words) {
this.dynamicTags[index] = words;
}
this.dynamicTags = this.unique(this.dynamicTags);
this.words = '';
this.num = -1;
}
}
}
</script>
<style scoped lang="scss">
.el-tag+.el-tag {
margin-left: 10px;
}
.button-new-tag {
margin-left: 10px;
height: 32px;
line-height: 30px;
padding-top: 0;
padding-bottom: 0;
}
.input-new-tag {
width: 90px;
margin-left: 10px;
vertical-align: bottom;
}
.custom_input {
width: 80px;
height: 16px;
outline: none;
border: transparent;
background-color: transparent;
font-size: 12px;
color: #B59059;
}
</style>