我为我的应用程序创建了一个自定义@提及系统,我唯一的问题是开始输入@...
,然后选择我要提及的用户,发生的情况是所选用户的名称刚刚添加到末尾textarea
所以这个工作如果我只输入@
但如果我输入@ja
然后选择jason smith
它现在会说@jaJason Smith
我如何删除在@之后输入的任何文本然后添加我的字符串。仅供参考,这是在textarea
所以我当前的功能看起来像这样..
addMention(user: any): void {
const textarea = document.querySelector('textarea');
const mentionSelect = <HTMLElement>document.querySelector('.mention-container');
const value = textarea.value; // Store the value
textarea.value = `${textarea.value}${user.value}`; // Current Value
mentionSelect.style.display = 'none'; // Hide the mention list
this.message = textarea.value; // Make sure the new textarea value is stored in message
textarea.focus(); // refocus
textarea.value = ''; // clear
textarea.value = value; // add the value back in
}
所以发生的事情是我只是通过添加用户名来更新 textarea 的值
编辑
我注意到这不是最好的方法,因为如果用户将光标移动到中间并键入@,名称将被添加到字符串的末尾而不是@所以我的问题是如何我替换了 @ 之后的字符串,但可以在 textarea 的任何地方进行操作??
任何帮助,将不胜感激!