Mentions 提及輸入
在文字區域輸入前綴後選取提及項目,例如 @ 選使用者或 # 選標籤。
引入
import { HuiMentions } from '@nscode/handy-ui'
基礎用法
以 v-model 綁定文字,輸入 @ 後從建議清單選取使用者,選取結果會插入原文字並保留前綴。
<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>
篩選
預設 filterable=true,只比對選項 label 且不區分大小寫。設為 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 } 可限制範圍,超過上限後在文字區內捲動。
<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 規則,表單標籤會關聯文字區。不需驗證時設定 :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>
自持文字與受控回填
未傳 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>
鍵盤操作
上下鍵循環移動高亮並跳過禁用項;Enter 或 Tab 插入高亮項,Shift+Tab 不確認。Home/End、左右及 PageUp/PageDown 留給文字游標。輸入法組合、鍵碼 229、Alt/Ctrl/Meta 組合與已消費事件不處理,上屏後只提交一次。
高亮按嚴格 value 比對,數字與字串分開。重排保留目前項;篩選、移除或禁用時就近回退,不依過期索引選取。關閉後移除活動項的 ARIA 關聯;全禁用或無符合項時,Enter 保持正常換行。
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
}