Skip to content

Mention 提及输入框

基础用法

  • sn-input 基础上扩展的 @ 提及输入框,继承其全部属性与事件。
  • 输入提及符号(默认 @,可通过 mentionChar 自定义)时触发 mention 事件,此时通常收起键盘并弹出选择面板。
  • 在面板中选中后调用 insertMention(name) 方法,自动在提及符号处插入 @name 标签并重新聚焦。
  • 在标签后按退格键会整段删除标签并触发 removemention 事件;beforeRemove 可校验标签是否允许删除。
vue
<template>
	<sn-mention ref="mentionRef" v-model="text" placeholder="输入 @ 选择好友" @mention="onMention"
		@removemention="onRemove"></sn-mention>
</template>

<script lang="uts" setup>
const text = ref<string>('')
const mentionRef = ref<SnMentionComponentPublicInstance | null>(null)

function onMention(): void {
	uni.hideKeyboard()
	openFriendPanel()
}

function onRemove(tag: string): void {
	console.log('已删除标签:@' + tag)
}

function selectFriend(name: string): void {
	mentionRef.value?.$callMethod('insertMention', name)
}
</script>

更多演示请下载 demo 查看

拦截标签删除

beforeRemove 返回 false 时,退格仅按普通字符删除,不触发 removemention;返回 true 时整段删除标签。

vue
<template>
	<sn-mention ref="mentionRef" v-model="text" :before-remove="beforeRemove" @removemention="onRemove"></sn-mention>
</template>

<script lang="uts" setup>
const text = ref<string>('')
const mentionRef = ref<SnMentionComponentPublicInstance | null>(null)

function beforeRemove(tag: string): boolean {
	return isFriend(tag)
}

function onRemove(tag: string): void {
	console.log('已整段删除标签 @' + tag)
}
</script>

自定义提及符号

vue
<template>
	<sn-mention ref="mentionRef" v-model="text" mention-char="#" placeholder="输入 # 触发话题提及"></sn-mention>
</template>

<script lang="uts" setup>
const text = ref<string>('')
const mentionRef = ref<SnMentionComponentPublicInstance | null>(null)
</script>

属性

参数说明类型默认值可选值
v-model绑定值String''-
mentionChar触发提及的符号,设为空字符串时关闭提及功能String@-
beforeRemove删除标签前的校验函数,返回 false 则按普通字符删除((tag: string) => boolean) | nullnull-
name表单的控件名称,作为键值对的一部分与表单一同提交String''-
type输入框类型Stringtexttext | number | digit | tel | idcard | nickname
password是否密码类型,为 true 时右侧显示明文/密文切换按钮Booleanfalsetrue | false
placeholder输入框为空时的占位符String请输入内容,@选择-
placeholderStyle占位符内联样式String''-
placeholderClass占位符外部类String''-
maxlength最大输入长度,-1 表示不限制String | Number-1-
focus获取焦点Booleanfalsetrue | false
autoFocus自动获取焦点,与 focus 相比只在首次生效Booleanfalsetrue | false
cursor指定聚焦时的光标位置,-1 表示不指定String | Number-1-
cursorColor光标颜色String''(默认主题 primary 色)-
cursorSpacing光标与键盘的距离(px)String | Number0-
selectionStart光标起始位置,自动聚焦时有效,需与 selectionEnd 搭配String | Number-1-
selectionEnd光标结束位置,自动聚焦时有效,需与 selectionStart 搭配String | Number-1-
confirmType键盘右下角按钮的文字Stringdonesend | search | next | go | done
confirmHold点击键盘右下角按钮时是否保持键盘不收起Booleanfalsetrue | false
holdKeyboard聚焦时点击页面不收起键盘Booleanfalsetrue | false
adjustPosition键盘弹起时是否自动上推页面Booleantruetrue | false
controlled是否为受控属性Booleanfalsetrue | false
disabled是否禁用Booleanfalsetrue | false
readonly是否只读Booleanfalsetrue | false
loading是否加载中,右侧显示加载指示器Booleanfalsetrue | false
loadingClass加载指示器外部类String''-
loadingStyle加载指示器样式UTSJSONObject | String''-
clearable是否显示清除按钮Booleanfalsetrue | false
showBorder是否显示边框Booleantruetrue | false
showActiveBorder聚焦时是否切换为激活边框颜色Booleantruetrue | false
borderColor边框颜色String'$line'-
activeBorderColor聚焦时的边框颜色String'$line'-
bgColor背景颜色String'$info'-
activeBgColor聚焦时的背景颜色String'$info'-
disabledBgColor禁用时的背景颜色String''(默认主题 disabled 色)-
textColor文本颜色String''(默认主题 text 色)-
disabledTextColor禁用时的文本颜色String''(默认主题 disabledText 色)-
iconColor前后置图标颜色String''(默认主题 text 色)-
borderRadius圆角大小String | Number''(默认 8px × 圆角乘数)-
borderWidth边框宽度String | Number1px-
padding内边距String8px 12px-
textSize文本字体大小String | Number''(默认 14px × 字体乘数)-
textFont文本字体String''-
align文本对齐方式Stringleftleft | center | right
prefixIcon前置图标名称,存在 prefix 插槽时不显示String''-
suffixIcon后置图标名称,存在 suffix 插槽时不显示String''-
iconSize图标大小String | Number''(默认 16px × 字体乘数)-
containStyle自定义内部输入框容器样式UTSJSONObject | String''-
inputStyle自定义输入框本体样式UTSJSONObject | String''-
inputClass输入框本体的外部类String''-
prefixIconStyle自定义前置图标样式UTSJSONObject | String''-
prefixIconClass前置图标的外部类String''-
suffixIconStyle自定义后置图标样式UTSJSONObject | String''-
suffixIconClass后置图标的外部类String''-
customStyle自定义根节点样式UTSJSONObject | String''-
customClass根节点外部类String''-

事件

名称类型说明
mention() => Void输入提及符号时触发,通常在此收起键盘并打开选择面板
removemention(tag: string) => Void整段删除一个提及标签时触发,参数为标签内容(不含提及符号)
change(value: string) => Void绑定值变化时触发,参数为当前值
input(event: UniInputEvent) => Void键盘输入时触发
focus(event: UniInputFocusEvent) => Void输入框聚焦时触发
blur(event: UniInputBlurEvent) => Void输入框失去焦点时触发
confirm(event: UniInputConfirmEvent) => Void点击键盘右下角按钮时触发
keyboardheightchange(event: UniInputKeyboardHeightChangeEvent) => Void键盘高度变化时触发
prefix-click() => Void点击前置图标时触发
suffix-click() => Void点击后置图标时触发

方法

名称参数返回值描述
setFocusval: boolean-设置输入框聚焦状态
insertMentionname: string-在提及符号处插入 @name 标签并重新聚焦,一般由选择面板选中后调用

插槽

名称说明
prefix自定义前置内容,透传给内部输入框,使用后前置图标不显示
suffix自定义后置内容,透传给内部输入框,使用后后置图标不显示

使用 MIT 协议