Mentions 提及
多行輸入控件 + @ 前綴觸發成員建議,選中後把提及寫回文本。
基礎用法
基礎用法
已提及:—
import { useState } from 'react'
import { LhMentions } from '@lh-ui/react'
const members = [
{ label: '張三', value: 'zhangsan' },
{ label: '李四', value: 'lisi' },
]
export default function Demo() {
const [text, setText] = useState('')
return <LhMentions options={members} onValueChange={setText} />
}
Mentions Props
| 屬性 | 說明 | 類型 | 可選值 | 預設值 |
|---|---|---|---|---|
| options | 候選成員 | MentionOption[] | — | [] |
| value | 文本值(受控) | string | — | — |
| defaultValue | 預設文本值(非受控) | string | — | — |
| onValueChange | 文本變化回呼 | (value: string) => void | — | — |
| onSelect | 選中候選回呼(第二參數為寫回後的完整文本) | (option, insertText) => void | — | — |
| prefix | 觸發前綴 | string | — | '@' |
| rows | 多行輸入行數 | number | — | 3 |
| disabled | 停用 | boolean | — | false |
| name | 原生表單名(隱藏 input 承接提交) | string | — | — |
| placeholder / ariaLabel / sx | 通用屬性 | — | — | — |
MentionOption:{ label: string; value?: string; disabled?: boolean }。寫回的是人可讀的 @label(機器標識 value 通過 onSelect 傳給應用層)。
Mentions 事件
| 事件 | 說明 | 回呼簽名 | Vue 對照 |
|---|---|---|---|
| onValueChange | 文本變化 | (value: string) => void | @input |
| onSelect | 選中候選 | (option, insertText) => void | @select |
上下文識別規則
光標向前找到最近的前綴字符、且前綴與光標之間無空白時激活面板; 郵箱類偽前綴(a@b.com,前綴前緊貼 ASCII 字母數字 / ._%+-)不觸發;CJK 字符緊貼前綴(你好@張三)正常觸發。
鍵盤互動
面板展開時 ArrowUp / ArrowDown 移動高亮,Enter 選中建議(不換行),Esc 關閉面板。輸入控件按 combobox 模式輸出 aria 屬性。
樣式
import '@lh-ui/react/style/mentions'