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

效果:

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. golang 并发执行函数func类型slice

    golang的slice支持func.使用func slice要注意func要完整描述入参出参. 如果需要执行一系列类型相同(入参出参格式相同)的函数,可以动态添加到一个slice里面.range s ...

  2. 前端必备的Nginx学习

    由于机缘巧合,认识了一些朋友,给我介绍了搬瓦工的网站.买了一个国外的服务器,既可以FQ又拥有了一个搭载 Centos 6 的服务器.一年19.99美元,折合人民币也就130左右,一键搭建.有兴趣可以点 ...

  3. Hibernate学习笔记一

    1 框架体系结构 2 hibernate入门 2.1 ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对象,java对象,此处特指JavaBean Relational: ...

  4. 20145234黄斐《Java程序设计》第八周学习总结

    教材学习内容总结 第十五章部分 - 通用API 通用API 日志: 日志对信息安全意义重大,审计.取证.入侵检测等都会用到日志信息 日志API Logger: 注意无法使用构造方法生成Logger对象 ...

  5. React-精华版

    现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领 ...

  6. Eclipse - 配置优化

    去除不需要的启动加载项 Window --> Preferences -->General --> Startup and Shutdown 关闭自动更新 Window --> ...

  7. jenkins通过maven指定testng的xml文件,并给testng代码传参

    1.jenkins设置参数化构建,设置要传的参数名和值 2.指定testng的xml文件,在jenkins的输入以下 3.在pom.xml文件分别引用jenkins的参数,设置两个property & ...

  8. 第四十篇 Python之设计模式总结-简单工厂、工厂方法、抽象工厂、单例模式

    一. 简单工厂 简单工厂模式(Simple Factory Pattern):是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类. 简单工厂的用处不大,主要就是一个if... ...

  9. sqlalchemy 转json 的几种常用方式

    sqlalchemy 转json 的几种常用方式 # -*- coding:utf-8 -*- import datetime from flask import Flask, json, jsoni ...

  10. HDU - 6438(贪心+思维)

    链接:HDU - 6438 题意:给出 n ,表示 n 天.给出 n 个数,a[i] 表示第 i 天,物品的价格是多少.每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少 ...