InputTag 标签输入框
在输入框里输入文字、按回车生成标签,用来录入关键词、邮箱等一组短文本。
引入
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 绑定一个字符串数组。输入文字后按回车添加标签,点标签上的 × 删除;输入框为空时按退格删除最后一个标签。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="按回车添加标签" />
<HuiInputTag v-model="empty" placeholder="还没有标签时显示占位文字" />
</HuiSpace>
<p style="margin-top: 12px">绑定值:{{ tags }}</p>
</template>
自持状态与受控回填
不传 modelValue 或只有 update: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 时,已经存在的标签不会再添加(按回车没有反应,文字留在输入框里)。默认允许重复,删除时只删点中的那一个。在下面两个输入框里都再输入一次“Vue”试试:
<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 里也会被禁用。组件没有只读属性,要只展示一组标签时用禁用状态或 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 规则;表单项的 label 会关联到输入框。规则里的 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="输入后按回车" />
</HuiFormItem>
</HuiForm>
</template>
输入分隔与失焦添加
未按回车的文字在失焦时添加;下例同时支持逗号、分号和剪贴板换行。
<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
/** 输入或粘贴时拆分标签的分隔符;不传时仅回车、失焦添加 */
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 |