当前位置: 首页 > news >正文

vue2 web 多标签输入框 elinput是否当前焦点

又来分享一点点工作积累及解决方案

产品中需要用户输入一些文字后按下回车键生成标签来显示在页面上,经过尝试与改造完成如下:

<template><div class="tags-view" @click="beginInput"><el-tag :key="index" v-for="(tag, index) in dynamicTags" closable :disable-transitions="false"@close="handleClose(index)">{{ tag }}</el-tag><el-input v-if="inputVisible" class="input-new-tag" style="width: 100%;" 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">+</el-button> --></div>
</template><script>
export default {name: 'inputTag',props: {tags: {type: Array,default: []},},watch: {tags: {deep: true,immediate: true,handler(val) {this.dynamicTags = val || []}}},data() {return {dynamicTags: [],inputVisible: false,inputValue: ''};},methods: {handleClose(index) {this.dynamicTags.splice(index, 1);},showInput() {this.inputVisible = true;this.$nextTick(_ => {this.$refs.saveTagInput.$refs.input.focus();});},beginInput() {this.showInput();},handleInputConfirm() {let inputValue = this.inputValue;if (inputValue) {this.dynamicTags.push(inputValue);}const inputElement = this.$refs.saveTagInput.$refs.input; // 获取input DOM元素const isFocused = document.activeElement === inputElement; // 判断是否为当前焦点this.inputVisible = isFocused;this.inputValue = '';this.$emit('changed', this.dynamicTags)}}
}
</script><style lang="scss" scoped>
.tags-view {display: flex;flex-direction: row;justify-content: flex-start;align-items: center;flex-wrap: wrap;min-height: 32px;padding: 4px 5px;border: 1px solid #DCDFE6;border-radius: 4px;
}.button-new-tag {margin-left: 10px;height: 24px;line-height: 24px;padding-top: 0;padding-bottom: 0;
}.input-new-tag {height: 24px;line-height: 24px;width: 90px;//margin-left: 10px;vertical-align: bottom;
}::v-deep {.el-tag {margin-left: 5px;margin-top: 2px;margin-bottom: 2px;}.el-input__inner {height: 24px;line-height: 24px;border: none;padding: 0px 5px;}
}
</style>

组件的使用:

import InputTag from '../components/inputTag.vue'

tags用于默认值的回调,changed事件用于组件数据发生变化时的回调通知。 

<InputTag class="w-100" :tags="tagsValue" @changed="tagsChanged"></InputTag>

组件本身也比较简单,没有啥值得去细分和品评的技术点

enter事件和blur事件走了同一个事件,会导致输入不连续,为解决这个问题,我们只需要判断当前input是不是焦点,如果是,则不隐藏输入框即可,如下isFoucsed变量的判断即为是否本身自己是当前焦点的input!

handleInputConfirm() {let inputValue = this.inputValue;if (inputValue) {this.dynamicTags.push(inputValue);}const inputElement = this.$refs.saveTagInput.$refs.input; // 获取input DOM元素const isFocused = document.activeElement === inputElement; // 判断是否为当前焦点this.inputVisible = isFocused;this.inputValue = '';this.$emit('changed', this.dynamicTags)
}
http://www.lryc.cn/news/522873.html

相关文章:

  • 32单片机综合应用案例——物联网(IoT)环境监测站(四)(内附详细代码讲解!!!)
  • LabVIEW与WPS文件格式的兼容性
  • 小结: 路由协议的演进和分类
  • OpenCV相机标定与3D重建(60)用于立体校正的函数stereoRectify()的使用
  • Android wifi列表中去自身的热点
  • Windows环境本地配置pyspark环境详细教程
  • 《自动驾驶与机器人中的SLAM技术》ch9:自动驾驶车辆的离线地图构建
  • IP属地会随着人的移动而改变吗
  • openharmony应用开发快速入门
  • USB3020任意波形发生器4路16位同步模拟量输出卡1MS/s频率 阿尔泰科技
  • 云消息队列 Kafka 版 V3 系列荣获信通院“云原生技术创新标杆案例”
  • linux下的NFS和FTP部署
  • JS Clipboard API
  • MySQL中大量数据优化方案
  • 重拾Python学习,先从把python删除开始。。。
  • centos 安全配置基线
  • 高级编程语言的基本语法在CPU的眼中是什么样的呢?
  • Redis 性能优化:多维度技术解析与实战策略
  • .netframwork模拟启动webapi服务并编写对应api接口
  • MongoDB 学习指南与资料分享
  • 【Azure 架构师学习笔记】- Azure Function (2) --实操1
  • 扫描深度?滤光片和偏振片区别?
  • HJ4 字符串分隔(Java版)
  • 【脑机接口数据处理】matlab读取ns6 NS6 ns5NS5格式脑电数据
  • 用C++实现一个基于模板的观察者设计模式
  • 【华为路由/交换机的ftp文件操作】
  • 微信小程序 实现拼图功能
  • 深度学习项目--基于LSTM的火灾预测研究(pytorch实现)
  • AI时代下 | 通义灵码冲刺备战求职季
  • 当comfyui-reactor-node 安装失败urllib.error.HTTPError: HTTP Error 403: Forbidden解决方法