3 changed files with 127 additions and 6 deletions
@ -0,0 +1,101 @@ |
|||||
|
<template> |
||||
|
<div> |
||||
|
<el-tag |
||||
|
:key="tag" |
||||
|
v-for="tag in dynamicTags" |
||||
|
closable |
||||
|
:disable-transitions="false" |
||||
|
@close="handleClose(tag)"> |
||||
|
{{tag}} |
||||
|
</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">+ New Tag</el-button> |
||||
|
</div> |
||||
|
</template> |
||||
|
|
||||
|
<script> |
||||
|
export default { |
||||
|
name: 'editTags', |
||||
|
model: { |
||||
|
prop: 'dynamicTags', |
||||
|
event: 'change' |
||||
|
}, |
||||
|
props: { |
||||
|
// 值 |
||||
|
value: String, |
||||
|
// 是否禁用 |
||||
|
disabled: Boolean |
||||
|
}, |
||||
|
data() { |
||||
|
return { |
||||
|
// dynamicTags: ['标签一', '标签二', '标签三'], |
||||
|
inputVisible: false, |
||||
|
inputValue: '' |
||||
|
}; |
||||
|
}, |
||||
|
computed: { |
||||
|
dynamicTags() { |
||||
|
console.log(this.value) |
||||
|
if (this.value === '' || this.value === undefined) { |
||||
|
return [] |
||||
|
} else { |
||||
|
if (this.value.indexOf(',') !== -1) { |
||||
|
return [this.value] |
||||
|
} else { |
||||
|
return this.value.split(',') |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
methods: { |
||||
|
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.inputVisible = false; |
||||
|
this.inputValue = ''; |
||||
|
// this.value = this.dynamicTags.join(',') |
||||
|
this.$emit('change', this.dynamicTags.join(',')); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
</script> |
||||
|
|
||||
|
<style> |
||||
|
.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; |
||||
|
} |
||||
|
</style> |
||||
Loading…
Reference in new issue