InputTag 標籤輸入框
在輸入框輸入文字並按 Enter 產生標籤,適合關鍵字、電子郵件等一組短文字。輸入框為空時按退格可刪除最後一個標籤。
引入
import { HuiInputTag } from '@nscode/handy-ui'
原生屬性與標籤關聯
class、style 保留在可見外層;id、name、autocomplete、tabindex、ARIA 及其他原生屬性只落到輸入元素。顯式 ID 與 FormItem 標籤同步,使用者說明和表單說明/錯誤 ID 動態合併、去重。data-hui-field、data-invalid、data-disabled、data-open、data-filled 是保留狀態標記,只落在實際畫邊框的元素,外傳 value 不覆蓋實際模型。
name 對應目前輸入草稿;已產生的標籤陣列由 modelValue 提交,不會自動拼成原生表單值。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiInputTag } from '@nscode/handy-ui'
const version = ref(1)
const value = ref(['甲'])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="version++">更換欄位 ID</HuiButton>
<label :for="`native-inputtag-${version}`">欄位名稱</label>
<HuiInputTag
:id="`native-inputtag-${version}`"
v-model="value"
name="native-inputtag"
aria-describedby="native-inputtag-help"
autocomplete="on"
style="width: 280px; max-width: 100%"
/>
<span id="native-inputtag-help">標籤與說明始終關聯實際輸入欄位。</span>
</HuiSpace>
</template>
基礎用法
以 v-model 綁定字串陣列。輸入文字後按 Enter 新增標籤,點標籤的 × 刪除;輸入框為空時按退格刪除最後一項。placeholder 僅在沒有標籤時顯示。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag, HuiSpace } from '@nscode/handy-ui'
const tags = ref<string[]>(['Vue'])
const empty = ref<string[]>([])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInputTag v-model="tags" placeholder="按Enter添加標籤" />
<HuiInputTag v-model="empty" placeholder="還沒有標籤時顯示提示文字" />
</HuiSpace>
<p style="margin-top: 12px">綁定值:{{ tags }}</p>
</template>
自持狀態與受控回填
未傳入 modelValue 或僅監聽更新時,元件從空陣列自行保存標籤;父元件無關的重新呈現不會遺失。傳入屬性即為受控,包括顯式 undefined、null,兩者都顯示空清單;父元件不回填時清單不變。切換模式會發出開發警告。未提交文字是獨立草稿,外部改變清單不會清除它。新增或刪除先請求更新,再通知 add、remove;父拒絕更新時仍會通知事件。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInputTag, HuiSpace } from '@nscode/handy-ui'
import type { InputTagExpose } from '@nscode/handy-ui'
const localControl = ref<InputTagExpose>()
const redraw = ref(0)
const notification = ref<string[]>([])
const accepted = ref<string[] | null>(['固定'])
const allow = ref(false)
const request = ref('尚未提交')
const receive = (value: string[]) => {
request.value = JSON.stringify(value)
if (allow.value) accepted.value = value
}
</script>
<template>
<HuiSpace direction="vertical" fill class="input-tag-state-demo">
<HuiSpace wrap>
<HuiButton @click="redraw++">父重新呈現 {{ redraw }}</HuiButton>
<HuiButton @mousedown.prevent @click="localControl?.focus()">聚焦自持輸入</HuiButton>
<HuiButton @click="allow = !allow">父接受:{{ allow ? '是' : '否' }}</HuiButton>
</HuiSpace>
<span>未傳 modelValue:添加後自持,最多3個且去重</span>
<HuiInputTag
ref="localControl"
class="local-tags"
:data-redraw="redraw"
:max="3"
unique
placeholder="添加後點父重新呈現"
/>
<span>只有更新監聽:{{ notification }}</span>
<HuiInputTag
class="listener-tags"
@update:model-value="notification = $event"
placeholder="只記錄通知也會保存標籤"
/>
<span>受控清單:{{ accepted }};請求:{{ request }}</span>
<HuiInputTag
class="controlled-tags"
:model-value="accepted"
@update:model-value="receive"
placeholder="父接受後才修改清單"
/>
<span>顯式空值保持受控,父不回填時始終為空</span>
<HuiInputTag class="undefined-tags" :model-value="undefined" placeholder="顯式 undefined" />
<HuiInputTag class="null-tags" :model-value="null" placeholder="顯式 null" />
</HuiSpace>
</template>
最大數量
max 限制標籤數量。達到上限後輸入框變為唯讀,不能新增,但仍可聚焦並以退格刪除。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag } from '@nscode/handy-ui'
const tags = ref<string[]>(['Vue', 'React'])
</script>
<template>
<HuiInputTag v-model="tags" :max="3" placeholder="最多 3 個" />
<p style="margin-top: 12px">已添加 {{ tags.length }} / 3 個</p>
</template>
去重
unique=true 時已存在的標籤不會再次新增;按 Enter 沒有反應且文字留在輸入框。預設允許重複,刪除時只刪除點選的那一項。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag, HuiSpace } from '@nscode/handy-ui'
const uniqueTags = ref<string[]>(['Vue'])
const repeatTags = ref<string[]>(['Vue'])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">unique</span>
<HuiInputTag v-model="uniqueTags" unique style="width: 320px" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">預設</span>
<HuiInputTag v-model="repeatTags" style="width: 320px" />
</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>
禁用
disabled 會停用輸入,標籤也不顯示刪除按鈕;位於停用的 HuiForm 時同樣會停用。元件沒有 readonly 屬性,只展示標籤時請使用停用狀態或 HuiTag。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag, HuiSpace } from '@nscode/handy-ui'
const tags = ref<string[]>(['Vue', 'React'])
const empty = ref<string[]>([])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInputTag v-model="tags" disabled />
<HuiInputTag v-model="empty" placeholder="禁用時的提示文字" disabled />
</HuiSpace>
</template>
尺寸
size 可選 large、default、small,高度分別是 40、32、24px(標籤多了會換行加高),字級 16、14、12px,圓角 8、6、4px,和同尺寸的 HuiInput 一致。裡面的標籤和 HuiSelect 多選的標籤一樣:large 是 24px 高、14px 字、圓角 6px,default 和 small 是 18px 高、12px 字、圓角 4px(和 HuiTag 的 default、small 一樣大);標籤換行時離邊框 2px,上下兩行相距 4px。放在 HuiForm 裡且沒傳 size 時,跟隨表單項或表單的尺寸。
下面每一行最後放一個同尺寸的按鈕對比高度:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInputTag, HuiSpace } from '@nscode/handy-ui'
const tags = ref<string[]>(['Vue', 'React'])
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiInputTag v-model="tags" size="large" style="width: 320px" />
<HuiButton size="large">按鈕</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiInputTag v-model="tags" style="width: 320px" />
<HuiButton>按鈕</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiInputTag v-model="tags" size="small" style="width: 320px" />
<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>
事件
新增標籤後通知 add(參數為新標籤),刪除後通知 remove(參數為被刪標籤);兩種操作都先通知 update:modelValue。輸入框取得與離開焦點時通知 focus、blur。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag } from '@nscode/handy-ui'
const tags = ref<string[]>(['Vue'])
const logs = ref<string[]>([])
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onAdd = (tag: string) => log(`add:${tag}`)
const onRemove = (tag: string) => log(`remove:${tag}`)
</script>
<template>
<HuiInputTag
v-model="tags"
@add="onAdd"
@remove="onRemove"
@focus="log('focus')"
@blur="log('blur')"
/>
<p style="margin-top: 12px">最近的事件(新的在前):{{ logs.join(',') || '還沒有' }}</p>
</template>
驗證狀態
元件沒有獨立狀態屬性,驗證外觀由所在的 HuiFormItem 決定:驗證未通過或項目設定 error 時,邊框顯示危險色,下方顯示錯誤訊息,內部 <input> 帶有 aria-invalid。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiForm, HuiFormItem, HuiInputTag, HuiSwitch } from '@nscode/handy-ui'
const showError = ref(true)
const tags = ref<string[]>(['Vue'])
</script>
<template>
<HuiSwitch v-model="showError" active-text="顯示錯誤" />
<HuiForm label-width="80px" style="margin-top: 16px" @submit.prevent>
<HuiFormItem label="技能" :error="showError ? '至少填寫 2 個標籤' : ''">
<HuiInputTag v-model="tags" />
</HuiFormItem>
</HuiForm>
</template>
呼叫方法
通過 ref 呼叫 focus()、blur(),或者用 inputRef 直接拿到內部的 <input> 元素。點輸入框裡空白的地方也會聚焦輸入框。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInputTag, HuiSpace } from '@nscode/handy-ui'
const tagRef = ref<InstanceType<typeof HuiInputTag>>()
const tags = ref<string[]>(['Vue'])
const focused = ref(false)
</script>
<template>
<HuiInputTag ref="tagRef" v-model="tags" @focus="focused = true" @blur="focused = false" />
<div style="margin-top: 12px">
<HuiSpace wrap>
<HuiButton @click="tagRef?.focus()">聚焦</HuiButton>
<HuiButton @click="tagRef?.blur()">失焦</HuiButton>
</HuiSpace>
</div>
<p style="margin-top: 12px">輸入框{{ focused ? '已聚焦' : '沒有聚焦' }}</p>
</template>
在表單裡使用
在 HuiFormItem 內,標籤變更觸發 change 規則,失焦觸發 blur 規則;表單標籤會關聯輸入框。required 將空陣列視為未填,min、max 依標籤數量比較。不需驗證時設定 :validate-event="false"。
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiInputTag } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive({ keywords: [] as string[] })
const rules: Record<string, FormRule[]> = {
keywords: [
{ required: true, message: '請至少添加一個關鍵詞', trigger: 'blur' },
{ min: 2, max: 5, message: '關鍵詞 2 到 5 個', trigger: 'change' }
]
}
</script>
<template>
<HuiForm :model="model" :rules="rules" @submit.prevent>
<HuiFormItem label="關鍵詞" prop="keywords">
<HuiInputTag v-model="model.keywords" placeholder="輸入後按Enter" />
</HuiFormItem>
</HuiForm>
</template>
輸入分隔與失焦添加
尚未按 Enter 的文字會在失焦時新增;下例同時支援逗號、分號與剪貼簿換行。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag, HuiSpace } from '@nscode/handy-ui'
const tags = ref<string[]>([])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInputTag
v-model="tags"
:separator="[',', ';', '\n']"
:max="5"
unique
placeholder="輸入或貼上:蘋果,香蕉;葡萄"
/>
<p>結果:{{ tags }}</p>
<HuiInputTag separator="," placeholder="字串 separator:輸入逗號拆分" />
</HuiSpace>
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| modelValue | string[] | null | undefined | 標籤清單;不傳時自持空陣列,傳入屬性即受控(含 undefined/null) |
| max | number | - | 最多幾個標籤 |
| placeholder | string | - | 提示文字 |
| disabled | boolean | false | 是否禁用 |
| unique | boolean | false | 是否不允許重複標籤 |
| size | 'large' | 'default' | 'small' | - | 尺寸。不傳時跟隨表單,不在表單裡時是 'default' |
| validateEvent | boolean | true | 標籤變化和失焦時是否觸發所在表單項的驗證 |
| separator | string | string[] | - | 輸入或貼上時按分隔符批量添加;不設定時不拆分,仍遵守 max、unique |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| update:modelValue | (value: string[]) | 添加或刪除標籤後觸發 |
| add | (value: string) | 添加標籤後觸發 |
| remove | (value: string) | 刪除標籤後觸發,參數是被刪的標籤 |
| focus | (event: FocusEvent) | 輸入框獲得焦點時觸發 |
| blur | (event: FocusEvent) | 輸入框失去焦點時觸發 |
方法
| 名稱 | 類型 | 說明 |
|---|---|---|
| focus | () => void | 讓輸入框獲得焦點 |
| blur | () => void | 讓輸入框失去焦點 |
| inputRef | HTMLInputElement | 內部的 input 元素 |
類型
import type { InputTagProps, InputTagSize } from '@nscode/handy-ui'
type InputTagSize = 'large' | 'default' | 'small'
interface InputTagProps {
modelValue?: string[] | null
max?: number
placeholder?: string
disabled?: boolean
unique?: boolean
/** 輸入或貼上時拆分標籤的分隔符;不傳時僅Enter、失焦添加 */
separator?: string | string[]
size?: InputTagSize
/** 失焦、值變化時是否觸發所在表單項的驗證 */
validateEvent?: boolean
}
焦點方法與公開類型
元件ref使用InputTagExpose。focus()聚焦目前、已選或第一個可用控制項,不改值、不打開檔案選擇框;禁用或卸載後不聚焦,唯讀按控制項原約定保留可聚焦性。blur()只讓本元件所屬文件內的活動元素失焦,不影響外部焦點。組控制項的focus/blur只在整組進出時各觸發一次,內部移動不重複;既有表單驗證行為保持。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { InputTagExpose } from '@nscode/handy-ui'
const control = ref<InputTagExpose>()
const value = ref(['标签'])
const event = ref('等待操作')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
</HuiSpace>
<HuiInputTag ref="control" v-model="value" @focus="event = 'focus'" @blur="event = 'blur'" />
<span>{{ event }}</span>
</HuiSpace>
</template>
| 方法 | 簽名 |
|---|---|
| focus | () => void |
| blur | () => void |