Tags Input
可增删标签的输入控件。基于 Reka UI TagsInput。可以单独用,也可以放进 FormField。有 FormField 时会合并 id / aria-* / data-*。支持前缀、清除与分隔符添加。
Installation
pnpm dlx @weme-ui/weme-ui add tags-inputUsage
Vueexamples/usage.vueExpand codeHide code
<script lang="ts" setup>
import type { TagsInputValue } from '../tags-input.props'
import * as v from 'valibot'
import Button from '../../button/button.vue'
import FormField from '../../form-field/form-field.vue'
import Form from '../../form/form.vue'
import TagsInput from '../tags-input.vue'
const schema = v.object({
tags: v.pipe(v.array(v.string()), v.minLength(1, '至少加一个标签')),
})
const defaultValues = {
tags: [] as TagsInputValue[],
}
function onFormSubmit(values: { tags: TagsInputValue[] }) {
// eslint-disable-next-line no-alert
alert(JSON.stringify(values))
}
</script>
<template>
<Form
class="max-w-sm w-full"
:default-values="defaultValues"
:schema="schema"
@form-submit="onFormSubmit"
>
<FormField v-slot="{ field }" name="tags" label="Tags" required>
<TagsInput
:model-value="field.state.value"
placeholder="Add a tag…"
prefix-icon="ri:search-line"
clearable
@blur="field.api.handleBlur"
@update:model-value="field.api.handleChange"
/>
</FormField>
<Button type="submit"> Submit </Button>
</Form>
</template>Examples
Variants
soft
outline
Vueexamples/variants.vueExpand codeHide code
<script lang="ts" setup>
import type { TagsInputValue } from '../tags-input.props'
import { ref } from 'vue'
import TagsInput from '../tags-input.vue'
const soft = ref<TagsInputValue[]>(['soft'])
const outline = ref<TagsInputValue[]>(['outline'])
</script>
<template>
<div class="flex flex-col gap-3 max-w-sm w-full">
<TagsInput v-model="soft" variant="soft" placeholder="Soft" />
<TagsInput v-model="outline" variant="outline" placeholder="Outline" />
</div>
</template>Sizes
sm
md
lg
Vueexamples/sizes.vueExpand codeHide code
<script lang="ts" setup>
import type { TagsInputValue } from '../tags-input.props'
import { ref } from 'vue'
import TagsInput from '../tags-input.vue'
const sm = ref<TagsInputValue[]>(['sm'])
const md = ref<TagsInputValue[]>(['md'])
const lg = ref<TagsInputValue[]>(['lg'])
</script>
<template>
<div class="flex flex-col gap-3 max-w-sm w-full">
<TagsInput v-model="sm" size="sm" placeholder="Small" />
<TagsInput v-model="md" size="md" placeholder="Medium" />
<TagsInput v-model="lg" size="lg" placeholder="Large" />
</div>
</template>With Prefix
label
icon
Vueexamples/with-prefix.vueExpand codeHide code
<script lang="ts" setup>
import type { TagsInputValue } from '../tags-input.props'
import { ref } from 'vue'
import TagsInput from '../tags-input.vue'
const tags = ref<TagsInputValue[]>(['label', 'icon'])
</script>
<template>
<div class="max-w-sm w-full">
<TagsInput v-model="tags" prefix-icon="ri:price-tag-3-line" placeholder="Add a tag…" />
</div>
</template>Clearable
alpha
beta
gamma
Vueexamples/clearable.vueExpand codeHide code
<script lang="ts" setup>
import type { TagsInputValue } from '../tags-input.props'
import { ref } from 'vue'
import TagsInput from '../tags-input.vue'
const tags = ref<TagsInputValue[]>(['alpha', 'beta', 'gamma'])
</script>
<template>
<div class="max-w-sm w-full">
<TagsInput v-model="tags" clearable placeholder="Add a tag…" />
</div>
</template>Disabled
locked
readonly
Vueexamples/disabled.vueExpand codeHide code
<script lang="ts" setup>
import type { TagsInputValue } from '../tags-input.props'
import { ref } from 'vue'
import TagsInput from '../tags-input.vue'
const tags = ref<TagsInputValue[]>(['locked', 'readonly'])
</script>
<template>
<div class="max-w-sm w-full">
<TagsInput v-model="tags" disabled placeholder="Disabled" />
</div>
</template>Invalid
Vueexamples/invalid.vueExpand codeHide code
<script lang="ts" setup>
import TagsInput from '../tags-input.vue'
</script>
<template>
<div class="flex flex-col gap-3 max-w-sm w-full">
<TagsInput placeholder="Default" />
<TagsInput invalid placeholder="Invalid" />
</div>
</template>Unstyled
custom
Vueexamples/unstyled.vueExpand codeHide code
<script lang="ts" setup>
import type { TagsInputValue } from '../tags-input.props'
import { ref } from 'vue'
import TagsInput from '../tags-input.vue'
const tags = ref<TagsInputValue[]>(['custom'])
</script>
<template>
<div class="max-w-sm w-full">
<TagsInput
v-model="tags"
unstyled
placeholder="Fully custom…"
class="text-sm py-1 outline-none border-b b-default flex flex-wrap gap-1 w-full focus-within:b-accent"
/>
</div>
</template>Props
PropTypeDefault
asany'div'
根节点渲染为的元素或组件(Reka UI
Primitive)any'div'asChildbooleanfalse
是否合并到子元素(Reka UI
Primitive)booleanfalsemodelValueTagsInputValue[] | null-
受控标签列表;可用
v-model;TagsInputValue 为 stringTagsInputValue[] | null-
defaultValueTagsInputValue[]-
非受控初始标签列表
TagsInputValue[]-
variant'soft' | 'outline''soft'
外观变体
'soft' | 'outline''soft'
size'sm' | 'md' | 'lg''md'
尺寸
'sm' | 'md' | 'lg''md'
placeholderstring-
输入框占位文案
string-prefixstring-
前缀文案(无
prefix 插槽且无 prefixIcon 时展示)string-prefixIconIconifyIconProps['icon']-
前缀图标(Iconify)
IconifyIconProps['icon']-
itemIconIconifyIconProps['icon']-
标签项图标(Iconify);当前模板未渲染
IconifyIconProps['icon']-
clearIconIconifyIconProps['icon']'ri:close-line'
标签删除与清除全部共用的图标(Iconify)
IconifyIconProps['icon']'ri:close-line'
clearableboolean-
有标签且未禁用时显示清除全部控件
boolean-delimiterstring | RegExp','
触发添加标签的字符或正则;也用于粘贴拆分
string | RegExp','
addOnPasteboolean-
粘贴时是否按
delimiter 拆分并添加标签boolean-addOnTabboolean-
按
Tab 时是否添加当前输入为标签boolean-addOnBlurboolean-
失焦时是否添加当前输入为标签
boolean-duplicateboolean-
是否允许重复标签
boolean-maxnumber0
最大标签数;
0 表示不限制number0convertValue(value: string) => TagsInputValue-
将输入字符串转为标签值(对象值场景必填)
(value: string) => TagsInputValue-
displayValue(value: TagsInputValue) => string-
标签展示文案
(value: TagsInputValue) => string-
dir'ltr' | 'rtl'-
阅读方向;未传时继承
ConfigProvider'ltr' | 'rtl'-
unstyledboolean-
移除样式变体类,仅保留结构
boolean-idstring-
控件
id;未传时回退到 FormField Contextstring-namestring-
控件
name;未传时回退到 FormField Contextstring-requiredboolean-
是否必填;会与 FormField Context 的
required 合并boolean-disabledboolean-
是否禁用;会与 FormField Context 的
disabled 合并boolean-invalidboolean-
是否无效;会与 FormField Context 的
invalid 合并boolean-classany-
根节点额外 class
any-uiPartial<TagsInputStyleSlots>-
覆盖 slots 样式(
root / prefix / prefixIcon / item / itemIcon / itemText / itemDelete / input / clear)Partial<TagsInputStyleSlots>-
Events
EventType
update:modelValuevalues: TagsInputValue[]
标签列表变更时触发(含
v-model;透传自 Reka UI Root)values: TagsInputValue[]
addTagvalue: TagsInputValue
添加标签时触发(透传自 Reka UI Root)
value: TagsInputValue
removeTagvalue: TagsInputValue
移除标签时触发(透传自 Reka UI Root)
value: TagsInputValue
invalidvalue: TagsInputValue
添加无效(如超限、重复且未允许)时触发(透传自 Reka UI Root)
value: TagsInputValue
focusevent: FocusEvent
输入框获得焦点
event: FocusEvent
blurevent: FocusEvent
输入框失去焦点
event: FocusEvent
changevalues: TagsInputValue[] | null
类型里声明了。脚本没有调用
emits('change'),界面不会发出这个事件values: TagsInputValue[] | null
Slots
SlotDescription
prefix自定义前缀;未传时回退到 prefixIcon / prefix
自定义前缀;未传时回退到
prefixIcon / prefixcleanIcon自定义清除全部图标;未传时回退到 clearIcon
自定义清除全部图标;未传时回退到
clearIconAccessibility
基于 Reka UI TagsInput(TagsInputRoot / TagsInputInput / TagsInputItem / TagsInputItemDelete / TagsInputClear)。通过 useFormFieldControl 将 FormField Context 的 id、name、disabled、required、aria-labelledby、aria-describedby、aria-invalid 与字段 data-* 合并到输入框,并向 FormField 注册控件。根节点设置 data-slot="tags-input",并透传 FormField 的 data-*。
Keyboard Interactions
KeyDescription
Enter将当前输入添加为标签
将当前输入添加为标签
TabaddOnTab 为真时将当前输入添加为标签
addOnTab 为真时将当前输入添加为标签,(或 delimiter)输入分隔符时添加标签
输入分隔符时添加标签
Backspace光标在输入开头且无选中标签时选中末个标签;已选中时删除该标签
光标在输入开头且无选中标签时选中末个标签;已选中时删除该标签
Delete已选中标签时删除该标签
已选中标签时删除该标签
ArrowLeft / ArrowRight光标在输入开头时在标签间移动选中(Roving);离开末个标签后回到输入框
光标在输入开头时在标签间移动选中(Roving);离开末个标签后回到输入框
Files
src/components/tags-input/tags-input.vuesrc/components/tags-input/tags-input.props.tssrc/components/tags-input/tags-input.style.ts