antd vue 输入框高亮设置关键字

devtools/2024/10/23 4:47:38/
javascript">   <highlight-textareaplaceholder="请输入主诉"type="textarea"v-model="formModel.mainSuit":highlightKey="schema.componentProps.highlightKey"></highlight-textarea>

参考链接原生input,textarea

demo地址

默认为团队组织高亮

效果演示
vue3相关组件修改
javascript"><template><div class="highlight-box"><template v-if="type === 'textarea'"><div v-if="value"class="textarea-outer"ref="textareaOuter"><div ref="outerInner" class="outer-inner"v-html="highlightHtml(value)"></div></div><a-textarearef="textareaBox":autoSize="true":placeholder="placeholder"v-model:value="value"></a-textarea></template><template v-if="type === 'input'"><div v-if="value"class="input-outer"v-html="highlightHtml(value)"></div><a-input type="text" class="ant-input":placeholder="placeholder"v-model:value.trim="value"/></template></div>
</template><script >
export default {name: 'HighlightTextarea',data() {return {value: ''};},props: {placeholder: {type: String,required: false,default: '请输入'},text: {type: String,required: false,default: ''},highlightKey: {type: Array,require: false,default: () => ['团队']},type: {type: String,required: true,default: 'textarea'},maxHeight: {type: Number,required: false,default: 220},modelValue: {type: String,default: ''}},watch: {value (v) {console.log(v, 'value 3333')// this.value = this.text.replace(/(^\s*)|(\s*$)/g, '').replace(/<br \/>|<br\/>|<br>/g, '\n');this.$emit("update:modelValue", v);},modelValue: {deep: true,immediate: true,handler(v) {console.log(v, 'modelValue 3333')this.value = v ? this.modelValue : ''},},disabled: {deep: true,immediate: true,handler(v) {// this.editorConfig.readOnly = v// this.$nextTick(() => {//   if (this.editor) v ? this.editor.disable() : this.editor.enable();// })},}},created() {// this.value = this.text.replace(/(^\s*)|(\s*$)/g, '').replace(/<br \/>|<br\/>|<br>/g, '\n');},mounted() {// this.scrollMousewheel();},methods: {highlightHtml(str) {if ((!str || !this.highlightKey || this.highlightKey.length === 0) && this.type !== 'textarea') {return str;}let rebuild = str;if (this.highlightKey.filter(item => ~str.indexOf(item)).length) {let regStr = '';let regExp = null;this.highlightKey.forEach(list => {regStr = this.escapeString(list);regExp = new RegExp(regStr, 'g');rebuild = rebuild.replace(regExp, `<span>${list}</span>`);});}if (this.type === 'textarea') {rebuild = rebuild.replace(/\n/g, '<br/>').replace(/\s/g, '&nbsp;');// textarea有滚动条时,div底部不能和textarea重合,故加一个<br/>// const wrap = this.$refs.textareaBox;// if (wrap && wrap.scrollHeight > this.maxHeight) {//   rebuild = rebuild + '<br/>';// }}return rebuild;},syncScrollTop() {const wrap = this.$refs.textareaBox;const outerWrap = this.$refs.textareaOuter;const outerInner = this.$refs.outerInner;if (wrap.scrollHeight > this.maxHeight && outerInner.scrollHeight !== wrap.scrollHeight) {outerInner.style.height = `${wrap.scrollHeight}px`;}if (wrap.scrollTop !== outerWrap.scrollTop) {outerWrap.scrollTop = wrap.scrollTop;}},// scrollMousewheel() {//   if (this.type === 'input') {//     return;//   }//   this.$nextTick(() => {//     this.eventHandler('add');//   });// },// 处理字符串中可能对正则有影响的字符escapeString(value) {const characterss = ['(', ')', '[', ']', '{', '}', '^', '$', '|', '?', '*', '+', '.'];let str = value.replace(new RegExp('\\\\', 'g'), '\\\\');characterss.forEach(function (characters) {let r = new RegExp('\\' + characters, 'g');str = str.replace(r, '\\' + characters);});return str;},eventHandler(type) {const wrap = this.$refs.textareaBox;if (wrap) {let mousewheelevt = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';wrap[`${type}EventListener`](mousewheelevt, this.syncScrollTop);wrap[`${type}EventListener`]('scroll', this.syncScrollTop);}}},destroyed() {// this.eventHandler('remove');}
};
</script><style lang="less">
@width: 100%; // 500px
.highlight-box {font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;position: relative;display: flex;//font-size: 12px;width: @width;//position: relative;//color: #333333;background: #ffffff;border-radius: 2px;overflow: hidden;.textarea-outer,.input-outer {box-sizing: border-box;width: @width;position: absolute;top: 0;left: 0;right: 0;border: 1px solid transparent;border-top: 0;// 鼠标事件失效 ie6-10不支持pointer-events: none;cursor: text;span {color: red; // #F27C49}&:hover {border-color: #4C84FF;}}.textarea-outer {overflow-y: auto;//line-height: 20px;word-break: break-all;.outer-inner {padding: 5px 8px;width: 100%;box-sizing: border-box;}}textarea {width: @width;//line-height: 20px;resize: none;}.input-outer,input {width: @width;height: 32px;line-height: 32px;}.input-outer {bottom: 0;padding: 0 8px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;}textarea,input {font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;//font-size: 12px;// position: relative;// z-index: 2;// 光标的颜色//color: #333333;// 文本颜色text-shadow: 0 0 0 rgba(0, 0, 0, 0);-webkit-text-fill-color: transparent;background: transparent;border-radius: 2px;border: 1px solid #d9d9d9;padding: 4px 8px;box-sizing: border-box;//&::placeholder {//  -webkit-text-fill-color: #d5d5d5;//}//&:hover {//  border-color: #36cfc9;//}//&:focus {//  border-color:#36cfc9;//  box-shadow: 0 0 0 2px #DBE4FF;//  outline: none;//}}
}
</style>
 使用组件
   <highlight-textareaplaceholder="请输入主诉"type="textarea"v-model="formModel.mainSuit":highlightKey="schema.componentProps.highlightKey" >
// highlightKey 高亮关键字数组 如: ['团队']
// formModel.mainSuit 绑定字段</highlight-textarea>
参考说明 

 


http://www.ppmy.cn/devtools/128058.html

相关文章

循序渐进丨MogDB 5.0 远程访问 MogDB/Oracle 数据库的简便方法(使用@符号)

概述 早期的 MogDB 就提供了Postgres_fdw、Oracle_fdw、MySQL_fdw3个插件&#xff0c;用于远程访问 MogDB/Oracle/MySQL数据库。 旧的版本中&#xff0c;访问远程数据库的表&#xff0c;需要显式创建外部表&#xff0c;而在 MogDB 5.0当中&#xff0c;这种用法得到了简化&…

Android Camera2在textureView中的预览和拍照

Camera2预览和拍照 1、Camera2相机模型2、Camera2的重要类3、Camera2调用流程4、Camera2调用实现 1)定义TextureView作为预览界面2)设置相机参数3)开启相机4)开启相机预览5)实现PreviewCallback6)拍照 1、Camera2相机模型 解释上诉示意图&#xff0c;假如想要同时拍摄两张不同…

文件处理新纪元:微信小程序的‘快递员’与‘整理师’

嗨&#xff0c;我是中二青年阿佑&#xff0c;今天阿佑将带领大家如何通过巧妙的文件处理功能&#xff0c;让用户体验从‘杂乱无章’到‘井井有条’的转变&#xff01; 文章目录 微信小程序的文件处理文件上传&#xff1a;小程序的“快递服务”文件下载&#xff1a;小程序的“超…

WSL2 Linux子系统调整存储位置

WSL2 默认不支持修改Linux 安装路径&#xff0c;官方提供的方式&#xff0c;只有通过导出、导入的方式实现Linux子系统的迁移。 修改注册表的方式官方不推荐&#xff0c;没有尝试过&#xff0c;仅提供操作方式(自行评估风险&#xff0c;建议备份好数据) 1. 打开 **注册表编辑器…

源代码加密技术的一大新方向!

在当今这个信息爆炸的时代&#xff0c;企业所面临的数据安全挑战日益严峻。传统的文档加密方法已经无法满足日益复杂的安全需求。幸运的是&#xff0c;SDC沙盒加密系统以其革命性的安全理念和先进技术&#xff0c;为企业提供了一个更可靠、更高效的数据保护方案。 传统加密方案…

【学习笔记】网络设备(华为交换机)基础知识 9 —— 堆叠配置

提示&#xff1a;学习华为交换机堆叠配置&#xff0c;含堆叠的概念、功能、角色、ID和优先级&#xff1b;堆叠的建立过程以及注意事项&#xff1b;包含堆叠的配置命令&#xff0c;以及堆叠的配置案例 一、前期准备 1.已经可以正常访问交换机的命令行接口 Console口本地访问教…

D45【python 接口自动化学习】- python基础之类

day45 类与实例 学习日期&#xff1a;20241022 学习目标&#xff1a;类 -- 59 类与实例&#xff1a;如何使用面向对象的思想编写程序&#xff1f; 学习笔记&#xff1a; 定义一个类 类的实例化 类的属性 类的方法 # 定义一个类 class Coffee:water 0milk1# 类的方法def add…

技术经济学·技术经济分析指标体系与基本原则

第一节 经济效果的基本概念 一、经济效果的概念 1.什么是效果&#xff1f; 行为产生的结果。&#xff08;如政治效果、军事效果、科技效果等。&#xff09; 效果有好坏之分&#xff0c;具有双面刃性。 条件&#xff1a;投入——即要消耗一定的劳动&#xff08;活的或物化的劳…