Mentions 提及
在多行文本里输入 @ 等前缀时弹出候选列表,选中后把提及内容插入到光标处。用于评论、消息里提及用户或话题。
引入
import { HuiMentions } from '@nscode/handy-ui'
自持文本与受控回填
未传 modelValue 或仅监听更新时,组件从空文本自行保存,输入、插入提及及父组件无关重绘都保留实际文本。传入属性即受控,显式 undefined 表示受控空文本;父接受、改写或拒绝后,显示与表单校验按实际值收敛,拒绝或外部改值会作废旧提及范围。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiButton, HuiSpace } from '@nscode/handy-ui'
const redraw = ref(0)
const recent = ref('等待操作')
const options = [
{ value: 'a', label: '甲' },
{ value: 'b', label: '乙' },
{ value: 'c', label: '丙(禁用)', disabled: true }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="redraw++">父重绘 {{ redraw }}</HuiButton>
<HuiMentions :options="options" @select="recent = String($event.value)" />
<HuiMentions model-value="固定文本" :options="options" />
<HuiMentions :model-value="undefined" :options="options" placeholder="受控空文本" />
<span>最近选择:{{ recent }}</span>
</HuiSpace>
</template>
基础用法
用 v-model 绑定文本,用 options 传候选项。输入 @ 后弹出列表,用上下键选择,回车或 Tab 选中,Esc 关闭,也可以用鼠标点选。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
</script>
<template>
<HuiMentions v-model="value" :options="users" placeholder="输入 @ 提及用户" />
</template>
选中“张三”后插入的是 @zhangsan :前缀 + 选项的 value + 分隔符。
自定义前缀
prefix 改触发前缀,默认是 @。前缀前面必须是空白或者在开头,下面输入 # 提及话题:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const topics: MentionsOption[] = [
{ value: '新功能', label: '新功能' },
{ value: '问题反馈', label: '问题反馈' },
{ value: '周报', label: '周报' }
]
</script>
<template>
<HuiMentions v-model="value" :options="topics" prefix="#" placeholder="输入 # 提及话题" />
</template>
多个前缀
prefix 传数组时,每个前缀都能触发。用 search 或 select 事件的第二个参数区分是哪个前缀。下面输入 @ 提及用户,输入 # 提及小组:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const prefix = ref('@')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
const teams: MentionsOption[] = [
{ value: '前端组', label: '前端组' },
{ value: '测试组', label: '测试组' }
]
const onSearch = (_query: string, p: string) => {
prefix.value = p
}
</script>
<template>
<HuiMentions
v-model="value"
:options="prefix === '#' ? teams : users"
:prefix="['@', '#']"
placeholder="输入 @ 提及用户,输入 # 提及小组"
@search="onSearch"
/>
</template>
过滤
默认按输入的文字过滤选项(只匹配 label,不区分大小写)。filterable 设为 false 时始终显示全部选项,适合在 search 事件里自己请求接口:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const options = ref<MentionsOption[]>([])
// 用 setTimeout 模拟接口,按姓名或拼音查用户
const allUsers: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' },
{ value: 'wangwu', label: '王五' }
]
const searchUsers = (query: string) =>
new Promise<MentionsOption[]>((resolve) => {
setTimeout(() => {
resolve(
allUsers.filter(
(user) => user.label.includes(query) || String(user.value).includes(query)
)
)
}, 300)
})
const handleSearch = async (query: string) => {
options.value = await searchUsers(query)
}
</script>
<template>
<HuiMentions
v-model="value"
:options="options"
:filterable="false"
placeholder="输入 @ 后再输入 zhang 或 wang"
@search="handleSearch"
/>
</template>
左边是默认的 filterable,输入 @张 只剩张三;右边 :filterable="false",输入什么都显示全部选项:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const other = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' },
{ value: 'wangwu', label: '王五' }
]
</script>
<template>
<HuiSpace wrap>
<HuiMentions v-model="value" :options="users" placeholder="过滤(true)" style="width: 240px" />
<HuiMentions
v-model="other"
:options="users"
:filterable="false"
placeholder="不过滤(false)"
style="width: 240px"
/>
</HuiSpace>
</template>
禁用的选项
选项设 disabled: true 后不出现在下拉列表里(直接隐藏,不显示成灰色)。没有可选的选项时显示“暂无匹配选项”。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四(已离职)', disabled: true },
{ value: 'wangwu', label: '王五' }
]
</script>
<template>
<HuiMentions v-model="value" :options="users" placeholder="输入 @,列表里没有李四" />
</template>
分隔符
选中后会在提及后面追加 split(默认一个空格)。光标后面本来就是分隔符时不再重复追加,光标落在分隔符之后。输入分隔符也会结束当前提及,下拉层随之关闭。
左边是默认的空格,右边是逗号,分别选一个人看插入的内容:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const comma = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
</script>
<template>
<HuiSpace wrap>
<HuiMentions
v-model="value"
:options="users"
placeholder="split 默认空格"
style="width: 240px"
/>
<HuiMentions
v-model="comma"
:options="users"
split=","
placeholder="split 是逗号"
style="width: 240px"
/>
</HuiSpace>
<p style="margin-top: 12px">
左边:{{ JSON.stringify(value) }};右边:{{ JSON.stringify(comma) }}
</p>
</template>
行数和自适应高度
rows 指定行数,默认 1 行,可以纵向拖动调整高度。autosize 为 true 时高度跟着内容变化;传 { minRows, maxRows } 可以限制行数范围,内容超过 maxRows 后在框里滚动。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const rows = ref('')
const auto = ref('')
const limited = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMentions v-model="rows" :options="users" :rows="3" placeholder="固定 3 行(rows)" />
<HuiMentions v-model="auto" :options="users" autosize placeholder="随内容增高(autosize)" />
<HuiMentions
v-model="limited"
:options="users"
:autosize="{ minRows: 2, maxRows: 5 }"
placeholder="2 到 5 行(autosize: { minRows: 2, maxRows: 5 })"
/>
</HuiSpace>
</template>
最大长度
maxlength 限制最多能输入几个字。选中选项插入的内容不受它限制。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
</script>
<template>
<HuiMentions v-model="value" :options="users" :maxlength="20" placeholder="最多输入 20 个字" />
<p style="margin-top: 12px">已输入 {{ value.length }} / 20</p>
</template>
弹出位置
placement 是下拉层的位置,默认 bottom-start,共 6 个:top-start、top、top-end、bottom-start、bottom、bottom-end。下拉层对齐的是整个输入框,放不下时自动翻到另一侧。在下面的输入框里输入 @ 看效果:
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption, MentionsPlacement } from '@nscode/handy-ui'
const values = reactive<Record<string, string>>({})
const tops: MentionsPlacement[] = ['top-start', 'top', 'top-end']
const bottoms: MentionsPlacement[] = ['bottom-start', 'bottom', 'bottom-end']
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace v-for="row in [tops, bottoms]" :key="row[0]" wrap>
<HuiMentions
v-for="item in row"
:key="item"
v-model="values[item]"
:options="users"
:placement="item"
:placeholder="`输入 @(${item})`"
style="width: 240px"
/>
</HuiSpace>
</HuiSpace>
</template>
禁用和只读
disabled 禁用后不能编辑;readonly 只读时可以选中、复制文字,但不能修改,光标放进已有的提及里也不会弹出候选列表。在禁用的 HuiForm 里也会被禁用。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('@zhangsan 请帮忙看一下这个问题')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMentions v-model="value" :options="users" disabled />
<HuiMentions v-model="value" :options="users" readonly />
</HuiSpace>
</template>
尺寸
size 可选 large、default、small,单行时高度分别是 40、32、24px(多行时每行加 24、21、18px),字号 16、14、12px,圆角 8、6、4px,左右内边距 16、12、8px,和同尺寸的 HuiInput 一致,文本框部分和同尺寸的 HuiTextarea 一样。下拉层的选项跟着变:行高 40、36、28px,字号 16、14、12px,左右内边距 16、12、8px。放在 HuiForm 里且没传 size 时,跟随表单项或表单的尺寸。
每一行是一种尺寸,依次是单行的提及、同尺寸的输入框和按钮:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const text = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiMentions
v-model="value"
:options="users"
size="large"
placeholder="输入 @"
style="width: 240px"
/>
<HuiInput v-model="text" size="large" placeholder="输入框" style="width: 160px" />
<HuiButton size="large">按钮</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiMentions v-model="value" :options="users" placeholder="输入 @" style="width: 240px" />
<HuiInput v-model="text" placeholder="输入框" style="width: 160px" />
<HuiButton>按钮</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiMentions
v-model="value"
:options="users"
size="small"
placeholder="输入 @"
style="width: 240px"
/>
<HuiInput v-model="text" size="small" placeholder="输入框" style="width: 160px" />
<HuiButton size="small">按钮</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
事件
输入 @ 打开候选列表、继续输入查询词时触发 search;选中选项时触发 select,第二个参数是前缀;change 是原生的,文本框失焦且内容有变化时触发;获得、失去焦点时触发 focus、blur。最近的事件显示在下方:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const logs = ref<string[]>([])
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onSearch = (query: string, prefix: string) => log(`search:“${query}”,前缀 ${prefix}`)
const onSelect = (option: MentionsOption, prefix: string) =>
log(`select:${option.label},前缀 ${prefix}`)
const onChange = (val: string) => log(`change:${val}`)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMentions
v-model="value"
:options="users"
:prefix="['@', '#']"
placeholder="输入 @ 或 #"
@search="onSearch"
@select="onSelect"
@change="onChange"
@focus="log('focus')"
@blur="log('blur')"
/>
<div class="logs">
<div v-if="logs.length === 0">还没有触发事件</div>
<div v-for="(item, index) in logs" :key="index">{{ item }}</div>
</div>
</HuiSpace>
</template>
<style scoped>
.logs {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
调用方法
通过 ref 调用 focus()、blur();textareaRef 是内部的 <textarea> 元素,可以直接调用它的原生方法,例如 select() 选中全部文字。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const mentionsRef = ref<InstanceType<typeof HuiMentions>>()
const value = ref('@zhangsan 请帮忙看一下')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMentions ref="mentionsRef" v-model="value" :options="users" />
<HuiSpace wrap>
<HuiButton @click="mentionsRef?.focus()">聚焦(focus)</HuiButton>
<HuiButton @click="mentionsRef?.blur()">失焦(blur)</HuiButton>
<HuiButton @click="mentionsRef?.textareaRef?.select()"
>选中文字(textareaRef.select)</HuiButton
>
</HuiSpace>
</HuiSpace>
</template>
在表单里使用
放在 HuiFormItem 里时,值变化触发 change 规则,失焦触发 blur 规则;表单项的 label 会关联到文本框。不需要校验时设 :validate-event="false"。下面的评论点进去再点出来,就会提示必填:
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiForm,
HuiFormItem,
HuiMentions,
HuiMessage,
HuiSpace
} from '@nscode/handy-ui'
import type { FormRule, MentionsOption } from '@nscode/handy-ui'
const formRef = ref<InstanceType<typeof HuiForm>>()
const model = reactive({ comment: '' })
const rules: Record<string, FormRule[]> = {
comment: [
{ required: true, message: '请填写评论', trigger: 'blur' },
{ max: 50, message: '最多 50 个字', trigger: 'change' }
]
}
const users: MentionsOption[] = [
{ value: 'zhangsan', label: '张三' },
{ value: 'lisi', label: '李四' }
]
const submit = async () => {
const result = await formRef.value?.validate()
if (result?.valid) HuiMessage.success('校验通过')
}
</script>
<template>
<HuiForm ref="formRef" :model="model" :rules="rules" label-width="80px" style="max-width: 480px">
<HuiFormItem label="评论" prop="comment">
<HuiMentions
v-model="model.comment"
:options="users"
:rows="3"
placeholder="输入 @ 提及同事"
/>
</HuiFormItem>
<HuiFormItem>
<HuiSpace>
<HuiButton type="primary" @click="submit">提交</HuiButton>
<HuiButton @click="formRef?.resetFields()">重置</HuiButton>
</HuiSpace>
</HuiFormItem>
</HuiForm>
</template>
键盘操作
上下键循环移动高亮,跳过禁用项;Enter/Tab插入高亮项,Shift+Tab不确认,Home/End、左右及PageUp/PageDown留给文本光标。输入法组合中(含229兼容码)、Alt/Ctrl/Meta组合键和已消费事件不处理,上屏后只提交一次。
高亮按严格匹配的 value 保存,数值和字符串分开;候选重排后保持当前项,过滤、删除或禁用时就近回退,不会因旧下标选错项。关闭后移除活动项的ARIA关联;全禁用或无匹配时回车正常换行。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| modelValue | string | undefined | 属性存在即受控;未传时内部从空文本保存,显式undefined受控为空 |
| options | MentionsOption[] | [] | 候选项。禁用的选项不显示 |
| prefix | string | string[] | '@' | 触发前缀 |
| split | string | ' ' | 选中后追加在提及后面的分隔符;输入它也会结束当前提及 |
| filterable | boolean | true | 是否按输入的文字过滤选项 |
| placement | 'top' | 'bottom' | 'top-start' | 'bottom-start' | 'top-end' | 'bottom-end' | 'bottom-start' | 下拉层的位置,放不下时自动翻到另一侧 |
| popperClass | string | - | 下拉浮层的附加类名 |
| placeholder | string | - | 占位文字 |
| disabled | boolean | false | 是否禁用 |
| readonly | boolean | false | 是否只读 |
| size | 'large' | 'default' | 'small' | - | 尺寸。不传时跟随表单,不在表单里时是 'default' |
| maxlength | number | - | 最大输入长度 |
| rows | number | 1 | 行数 |
| autosize | boolean | { minRows?: number; maxRows?: number } | false | 自适应高度 |
| validateEvent | boolean | true | 值变化和失焦时是否触发所在表单项的校验 |
Events
| 事件 | 回调参数 | 说明 |
|---|---|---|
| update:modelValue | (value: string) | 输入或选中选项后触发 |
| change | (value: string) | 文本框失焦且内容有变化时触发(原生 change) |
| focus | (event: FocusEvent) | 获得焦点时触发 |
| blur | (event: FocusEvent) | 失去焦点时触发。点选项不会让文本框失焦 |
| select | (option: MentionsOption, prefix: string) | 选中选项时触发 |
| search | (query: string, prefix: string) | 打开下拉或输入的查询词变化时触发,query 不含前缀 |
方法
| 名称 | 类型 | 说明 |
|---|---|---|
| focus | () => void | 让文本框获得焦点 |
| blur | () => void | 让文本框失去焦点 |
| textareaRef | HTMLTextAreaElement | 内部的 textarea 元素 |
类型
import type {
MentionsOption,
MentionsPlacement,
MentionsSize,
MentionsExpose
} from '@nscode/handy-ui'
interface MentionsOption {
value: string | number
label: string
disabled?: boolean
}
type MentionsPlacement = 'top' | 'bottom' | 'top-start' | 'bottom-start' | 'top-end' | 'bottom-end'
type MentionsSize = 'large' | 'default' | 'small'
interface MentionsExpose {
focus: () => void
blur: () => void
readonly textareaRef: HTMLTextAreaElement | undefined
}