Skip to content

Mention 提及

用于在输入文本时,通过触发字符(如 @#)弹出建议列表,选择后插入到文本中。支持多触发字符、input/textarea 模式、自定义渲染、异步搜索、选项分组、Split 标签模式等高级功能。

基础用法

默认触发字符为 @,输入 @ 后会弹出建议列表。

多触发字符

通过 trigger 属性传入数组,可同时支持多个触发字符(如 @ 提及用户、# 提及话题)。

Input 模式

通过 type="input" 可使用单行输入框替代默认的多行文本域。

自定义选项渲染

通过 #option 插槽完全自定义下拉项的内容和样式。

自定义过滤函数

通过 filter 属性传入自定义匹配函数,替代内置的 includes 匹配。

异步远程搜索

通过 loading 显示加载状态,结合 @search 事件调用远程 API。

选项分组

通过 optionGroups 按部门或类别分组展示选项。选项通过 group 字段关联到对应的分组。

空状态自定义

通过 #empty 插槽自定义无匹配结果时的展示内容。

Teleport 弹层定位

通过 teleport 属性将下拉面板 Teleport 到 body,避免在 overflow: hidden 容器中被裁剪。

overflow: hidden 容器内 — Teleport 确保下拉不被裁剪

Split 标签模式

通过 split 启用标签渲染模式,选中的提及会显示为带样式的标签(contenteditable)。

Blur 行为配置

通过 blurBehavior 配置输入框失去焦点时的行为:

  • 'clear'(默认):清空搜索,关闭下拉
  • 'select-first':自动选中第一个匹配项
  • 'keep-open':保持下拉打开

maxHeight 配置

通过 maxHeight 自定义下拉面板的最大高度,支持数值(px)或 CSS 字符串。

Mention API

Props

参数说明类型默认值
modelValue绑定值 (v-model)string''
options建议选项列表MentionOption[][]
trigger触发字符,支持单个字符或数组string | string[]'@'
type输入元素类型'textarea' | 'input''textarea'
disabled是否禁用booleanfalse
placeholder占位文本string'请输入内容'
placement下拉弹出位置'bottom' | 'top''bottom'
filterable是否启用内置过滤booleantrue
filter自定义过滤函数,替代内置 includes 匹配(option, keyword, trigger) => boolean-
loading是否显示加载状态booleanfalse
loadingText加载状态文案string'加载中…'
optionGroups选项分组配置MentionOptionGroup[][]
blurBehavior失焦时行为'clear' | 'select-first' | 'keep-open''clear'
split是否启用 Split 标签模式booleanfalse
maxHeight下拉面板最大高度number | string240
teleport是否将下拉面板 Teleport 到 bodyboolean | stringfalse

Events

事件名说明回调参数
update:modelValue值变化时触发 (v-model)(value: string)
change值变化时触发(value: string)
select选中建议项时触发(option: MentionOption)
search搜索时触发,携带搜索词和触发字符(query: string, trigger: string)
focus获取焦点时触发(event: FocusEvent)
blur失去焦点时触发(event: FocusEvent)

Slots

插槽名说明
option自定义下拉项渲染
empty自定义无匹配结果时的内容
loading自定义加载状态内容

Exposed Methods

方法名说明参数
closeSuggestions关闭建议下拉()
visible下拉是否可见 (Ref<boolean>)-
searchText当前搜索文本 (Ref<string>)-
filteredOptions过滤后的选项列表-

Types

ts
interface MentionOption {
  value: string
  label?: string
  avatar?: string
  disabled?: boolean
  group?: string
}

interface MentionOptionGroup {
  value: string
  label: string
  options?: MentionOption[]
}

type MentionPlacement = 'top' | 'bottom'
type MentionType = 'textarea' | 'input'
type MentionBlurBehavior = 'clear' | 'select-first' | 'keep-open'
type MentionFilterFunc = (option: MentionOption, keyword: string, trigger: string) => boolean

基于 MIT 许可发布