该组件输入、换行、变换光标可以实时给出提示

效果:

textarea.vue

<template>
<div>
<el-input
id="user-input"
type="textarea"
placeholder="请换行输入不同的通知用户"
:autosize="{minRows: 2, maxRows: 10}"
v-model="inputValue"
@blur="closeHint"
@input="settingHint"
@click.native="settingHint"
@keyup.native="disposeKey">
</el-input>
<input-hint
:all-items="hintItems"
:position = 'hintPosition'
@select = "replaceStr"
></input-hint>
</div>
</template> <script lang="ts">
import { Vue, Component, Prop } from "vue-property-decorator";
import InputHint from "./inputHint.vue";
import $ from "jquery"; @Component({
components: {
InputHint
}
})
export default class AdvancedTextarea extends Vue {
inputValue: string = '';
Seprator = "\n";
allUsers: string[] = [];
hintItems: string[] = []; //传入提示框的项,可以是html字符串;为空则表示不显示提示框
initPosition = { //输入框的信息,用于计算提示框位置
left: 15,
top: 5,
rowHeight: 20, //一行的高度
fontSize: 7 //一个字的宽度
}
hintPosition = {
left: this.initPosition.left,
top: this.initPosition.top
} //按上下左右键时,重置提示框
disposeKey(e) {
if (e.keyCode === 37 || e.keyCode === 38 || e.keyCode === 39 || e.keyCode === 40) {
this.settingHint();
}
} settingHint(val?) {
let cursorLocation = $('#user-input').caret(); //光标位置
let newStr = this.inputValue.slice(0, cursorLocation); //输入框光标前的字符
let newArr = newStr.split(this.Seprator);
let searchKey = newArr.length === 0 ? "" : newArr[newArr.length - 1];
let regExp = new RegExp(searchKey, 'ig');
this.hintItems = searchKey ?
this.allUsers.filter(item => item.indexOf(searchKey) !== -1).map(item => item.replace(regExp, `<strong>${searchKey}</strong>`)) :
this.allUsers;
this.hintPosition.left = this.initPosition.left + this.initPosition.fontSize * (searchKey.length > 0 ? searchKey.length - 1 : 0);
this.hintPosition.top = this.initPosition.top + this.initPosition.rowHeight * (newArr.length > 10 ? 10 : newArr.length);
} closeHint() {
//延后关闭是因为立即关闭的话,点击提示框内容就无法触发点击事件
window.setTimeout(() => {
this.hintItems = null;
window.clearTimeout();
}, 200); } //将光标当前值替换为选中值
replaceStr(val) {
let cursorLocation = $('#user-input').caret(); //光标位置
let newStr = this.inputValue.slice(0, cursorLocation); //输入框光标前的字符
let row = newStr.split(this.Seprator).length - 1; //光标所在行
let oriArr = this.inputValue.split(this.Seprator);
oriArr[row] = val;
this.inputValue = oriArr.join(this.Seprator);
$('#user-input').focus();
} getAllUsers() {
this.allUsers = [
'xiaoming@qq.com',
'daming@163.com',
'liuxioawei@gridsum.com',
'432454354@qq.com',
'zhangzheng@qq.com',
'mostlove@163.com',
'wangweihao@gridsum.com',
'67900332@qq.com',
'xiaosi@qq.com',
'loveshuang@163.com',
'liuxioawei@gridsum.com',
'87456563@qq.com',
'yaru@qq.com',
'wuyuetian@163.com',
'junjun@gridsum.com',
'67576889@qq.com',
'shuanger@qq.com',
'she@163.com',
'ruiji@gridsum.com',
'45454334@qq.com',
]
} mounted() {
if (this.allUsers.length === 0) {
this.getAllUsers();
}
}
} </script>

inputHint.vue

<template>
<div v-show="allItems&&allItems.length!==0">
<ul class="el-dropdown-menu el-popper max-height new-scoll-bar" :style="{left: position.left+'px', top: position.top+'px'}">
<li class="el-dropdown-menu__item" v-for="(item,index) in allItems" :key="index" v-html="item" @click="selectItem(item)"></li>
</ul>
</div>
</template> <style lang="postcss" scoped>
.max-height {
max-height: 250px;
overflow-y: auto;
}
</style> <script lang="ts">
import { Vue, Component } from "vue-property-decorator"; @Component({
props: {
allItems: {
type: Array,
default: []
},
position: {
type: Object,
default: {
left: 0,
top: 0
}
}
}
}) export default class InputHint extends Vue {
allItems = this.allItems;
selectItem(item: string) {
let regExp = /<strong>|<\/strong>/g;
let str = item.replace(regExp, '');
this.$emit('select', str)
}
}
</script>

实现一个可以实时提示的textarea组件的更多相关文章

  1. 写一个可插入自定义标签的 Textarea 组件

    - “插入自定义标签是什么鬼?” - “比如你要插入一个<wise></wise>的标签...” - “什么情况下会有这种需求?” - “得罪了产品的情况下...” 一.需求背 ...

  2. Vue父组件向子组件传递一个动态的值,子组件如何保持实时更新实时更新?

    原文:https://blog.csdn.net/zhouweixue_vivi/article/details/78550738 2017年11月16日 14:22:50 zhouweixue_vi ...

  3. uni-app中textarea组件

    textarea组件,官方给出的监听事件有以下事件: 其中一定要注意,当使用 v-model 对表单内容进行双向绑定的时候,@input 事件是在绑定变量变化前触发的,所以如果在input事件内打印绑 ...

  4. jquery的实时触发事件(textarea实时获取中文个数)

    jquery的实时触发事件(textarea实时获取中文个数) (2014-09-16 11:49:50) 转载▼ 标签: 实时触发事件 中文个数 onpropertychange oninput o ...

  5. 使用SignalR ASP.NET Core来简单实现一个后台实时推送数据给Echarts展示图表的功能

    什么是 SignalR ASP.NET Core ASP.NET Core SignalR 是一种开放源代码库,可简化将实时 web 功能添加到应用程序的功能. 实时 web 功能使服务器端代码可以立 ...

  6. 微信小程序input组件抖动及textarea组件光标错位解决方案

    问题一: 使用微信小程序input组件时,在移动端唤起focus或blur事件时,因光标占位导致内容出现叠影及抖动现象. 解决方案: 用<textarea>组件代替了<input/& ...

  7. artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口

    artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应 ...

  8. Puer是一个可以实时编辑刷新的前端服务器

    ##Puer是一个可以实时编辑刷新的前端服务器 确保你安装了nodejs(现在还有没nodejs环境的前端? 拖出去喂狗吧) 使用npm全局安装puer命令 npm install puer -g 输 ...

  9. JStorm 是一个分布式实时计算引擎

    alibaba/jstorm JStorm 是一个分布式实时计算引擎. JStorm 是一个类似Hadoop MapReduce的系统, 用户按照指定的接口实现一个任务,然后将这个任务递交给JStor ...

随机推荐

  1. 『Python基础-12』各种推导式(列表推导式、字典推导式、集合推导式)

    # 『Python基础-12』各种推导式(列表推导式.字典推导式.集合推导式) 推导式comprehensions(又称解析式),是Python的一种独有特性.推导式是可以从一个数据序列构建另一个新的 ...

  2. python学习笔记:第15天 初识面向对象

    目录 1. 面向对象和面向过程 2. 面向对象如何编写: 3. 面向对象的三大特征 封装 继承 多态 1. 面向对象和面向过程 面向对象和面向过程的理解: ⾯向过程: ⼀切以事物的流程为核⼼. 核⼼是 ...

  3. SQL盲注

    一.首先输入1和-1 查看输入正确和不正确两种情况 二.三种注入POC LOW等级 ... where user_id =$id 输入      真  and  假 = 假 (1)...where u ...

  4. Node.js的Formidable模块的使用,方便快捷

    服务用的是express ,如果不是很老的express框架,都有自带formidable  如果没有就下载一个  npm i formidable var formidable = require( ...

  5. 无偏方差为什么除以n-1

    设样本均值为,样本方差为,总体均值为,总体方差为,那么样本方差有如下公式:. 很多人可能都会有疑问,为什么要除以n-1,而不是n,但是翻阅资料,发现很多都是交代到,如果除以n,对样本方差的估计不是无偏 ...

  6. hive中 udf,udaf,udtf

    1.hive中基本操作: DDL,DML 2.hive中函数 User-Defined Functions : UDF(用户自定义函数,简称JDF函数)UDF: 一进一出  upper  lower ...

  7. android 8.0 Account行为变更 账号系统

    我们有个方法,是判断系统的账号有没有登录. public static boolean isAccountLogin(Context context) { String df = "com. ...

  8. PS 给天空添加蓝天白云<转载>

    https://jingyan.baidu.com/article/b2c186c8e83b1cc46ef6ffee.html 给图片添加蓝天白云的步骤: 1.打开要加蓝天白云的照片.(如图一) [图 ...

  9. 40套PSD欧美扁平化网页模板,可二次编辑开发,精品

    40套PSD欧美扁平化网页模板,可二次编辑开发,绝对精品,下载地址:百度网盘, https://pan.baidu.com/s/1uMF4MM_3UC2Q6mbyNomLfQ 模板内容预览:   小

  10. Qt-QML-Repeater-导航条

    上篇文章中,我写了一个自己的Button,也就是美化了一下QML自带的Button 就是上面的这个,剩下的就是放三张图片在上面就可以了,当然了,这个Button在后期,还是会加入更让多的美化,比如,可 ...