Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更改了input事件部分的内容 #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
300 changes: 162 additions & 138 deletions star-input-tag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,152 +8,176 @@ created(){ 字符串转数组,定义在父组件
},
-->
<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">
<div class="input-tag">
<el-tag
v-for="(tag, index) in dynamicTags"
:key="tag"
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.trim="words"
v-if="index == num"
ref="editInput"
@keyup.enter="$event.target.blur()"
@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>
<div class="add-icon" :class="tagList.length>0 || inputVisible ?'ml10':''" v-if="tagList.length < max ">
<i class="el-icon-circle-plus-outline" @click="showInput"></i>
</div>
</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];
},
export default {
name: "input-tag",
model: {
prop: "tagList",
event: "input",
},
props: {
tagList: {
type: Array,
default() {
return [];
},
},
theme: {
type: String,
default: "+ 新标签",
},
max:{
type:Number,
default:1000
}
},
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="less">
.input-tag {
display: flex;
align-items: center;
height: 40px;
}

handleClose(tag) {
this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
},
.add-icon {
width: 40px;
height: 40px;
font-size: 20px;
display: flex;
align-items: center;

showInput() {
this.inputVisible = true;
this.$nextTick(_ => {
this.$refs.saveTagInput.$refs.input.focus();
});
},
i {
cursor: pointer;
}
}
.el-tag {
// margin-top: 4px ;
}
.el-tag + .el-tag {
margin-left: 10px;
}

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;

.button-new-tag {
margin-left: 10px;
height: 32px;
line-height: 30px;
padding-top: 0;
padding-bottom: 0;
}
&:first-child {
margin-left: 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;
}
.custom_input {
width: 80px;
height: 16px;
outline: none;
border: transparent;
background-color: transparent;
font-size: 12px;
color: #409eff;
}
</style>