-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tags.vue
119 lines (115 loc) · 2.81 KB
/
Tags.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
<template>
<div class="tag-container">
<tag
v-for="(tag, index) in tags"
:tag="tag"
:index="index"
:key="index"
:tagColor="color"
@removeOneTagEvent="clicktToRemove($event)"
/>
<input
type="text"
@keydown.enter="addTag"
@keydown.backspace="removeTag"
placeholder="Anahtar Kelime Giriniz"
/>
<transition name="fade">
<p class="error" v-if="error">Bu etiket daha önceden eklenmiş</p>
</transition>
</div>
</template>
<script>
import tag from './Tag';
export default {
name: 'tags',
props : {
value : {
required : false
},
color : {
type : String,
required : false,
default : 'primary'
}
},
data () {
return {
tags : [],
error : false
}
},
methods : {
addTag(event){
let data = event.target;
let matchedTag = false;
if (data.value.length > 0) {
this.tags.forEach(tag => {
if (tag.toLowerCase() === data.value.toLowerCase()) {
matchedTag = true;
}
});
if(!matchedTag){
this.tags.push(data.value);
data.value = null;
}else{
this.error = true;
setTimeout(() => {
this.error = false;
}, 1500);
}
}
},
removeTag(event){
if (event.target.value.length <= 0) {
this.tags.splice(this.tags.length - 1, 1);
}
},
clicktToRemove(index){
this.tags.splice(index, 1);
}
},
components : {
tag
},
created(){
if(this.value){
if(this.value.length > 0){
this.tags = this.value.split(',');
}
}
},
watch : {
tags(){
this.$emit('input', this.tags.join(','));
}
}
}
</script>
<style scoped>
.fade-enter-active, .fade-leave-active {
transition: opacity .5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
}
.tag-container {
border: 1px solid #cccccc;
padding: 20px;
margin-bottom: 10px;
}
input {
outline: none;
height: 40px;
width: 160px;
border: 1px solid #cccccc;
border-radius: 5px;
padding: 12px 12px;
box-sizing: border-box;
}
.error {
font-size: 12px;
color: red;
margin-top: 5px;
}
</style>