InputTag
An editable tag list supporting maximum counts, duplicate checks, separators, pasted batches, form validation, and focus methods.
Import
import { HuiInputTag } from '@nscode/handy-ui'
import type { InputTagProps, InputTagEmits, InputTagExpose, InputTagSize } from '@nscode/handy-ui'
Native attributes and labels
class and style remain on the visible wrapper. id, name, autocomplete, tabindex, ARIA attributes and other native attributes are applied once to the input. Explicit IDs update the FormItem label target; user descriptions are merged with the form hint and error IDs without duplicates. The reserved data-hui-field, data-invalid, data-disabled, data-open and data-filled markers are applied only to the element that draws the border, and a native value attribute does not replace the actual model.
name identifies the current input draft. Submit completed tags through modelValue; the array is not automatically serialized as a native form value.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiInputTag } from '@nscode/handy-ui'
const version = ref(1)
const value = ref(['Alpha'])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="version++">Change field ID</HuiButton>
<label :for="`native-inputtag-${version}`">Field label</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">The label and description follow the input ID.</span>
</HuiSpace>
</template>
Examples
Basic Binding, Maximum Count, and Uniqueness
Press Enter or leave the input to add trimmed text. Empty text is ignored. Duplicate text is allowed unless unique is enabled. At max, the field becomes readonly, while Backspace can still remove the last tag when the draft is empty.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag, HuiSpace } from '@nscode/handy-ui'
const tags = ref(['Vue', 'TypeScript'])
const limited = ref(['One', 'Two'])
const unique = ref(['Existing'])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInputTag v-model="tags" placeholder="Enter a tag, then press Enter" />
<HuiInputTag v-model="limited" :max="3" placeholder="Maximum three tags" />
<HuiInputTag v-model="unique" unique placeholder="Try Existing again" />
<span>Tags: {{ tags }}</span>
</HuiSpace>
</template>
Local State and Controlled Updates
Omitting modelValue, including when only an update listener is supplied, keeps a local list initially equal to []. Unrelated parent renders retain the tags. Supplying the prop makes it controlled, including explicit undefined and null, both displayed as an empty list. A parent that rejects an update keeps its list unchanged. Changing controlled status produces a development warning and uses the new mode. Pending text is a separate draft retained when the external list changes. Updates still precede add or remove, including when the parent rejects the request.
<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>(['Fixed'])
const allow = ref(false)
const request = ref('No request yet')
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++">Render parent {{ redraw }}</HuiButton>
<HuiButton @mousedown.prevent @click="localControl?.focus()">Focus local input</HuiButton>
<HuiButton @click="allow = !allow">Accept updates: {{ allow ? 'yes' : 'no' }}</HuiButton>
</HuiSpace>
<span>No modelValue: local list, maximum three unique tags</span>
<HuiInputTag
ref="localControl"
class="local-tags"
:data-redraw="redraw"
:max="3"
unique
placeholder="Add a tag, then click Render parent"
/>
<span>Update listener only: {{ notification }}</span>
<HuiInputTag
class="listener-tags"
@update:model-value="notification = $event"
placeholder="Record updates while retaining local tags"
/>
<span>Controlled list: {{ accepted }}; request: {{ request }}</span>
<HuiInputTag
class="controlled-tags"
:model-value="accepted"
@update:model-value="receive"
placeholder="Parent acceptance changes the list"
/>
<span>Explicit empty values stay controlled and empty without parent updates</span>
<HuiInputTag class="undefined-tags" :model-value="undefined" placeholder="Explicit undefined" />
<HuiInputTag class="null-tags" :model-value="null" placeholder="Explicit null" />
</HuiSpace>
</template>
Separators and Pasted Batches
A string or array of separators adds complete tags during typing and pasting. Newlines in clipboard text are recognized before browser input normalization. Unfinished text, duplicate text, and overflow text stay in the draft. IME composition does not split tags mid-composition.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag, HuiSpace } from '@nscode/handy-ui'
const comma = ref<string[]>([])
const batch = ref<string[]>([])
const separators = [',', '\n']
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInputTag v-model="comma" separator="," placeholder="Comma-separated tags" />
<HuiInputTag
v-model="batch"
:separator="separators"
unique
:max="4"
placeholder="Paste comma/newline-separated text"
/>
<span>Batch: {{ batch }}</span>
</HuiSpace>
</template>
Sizes and Disabled State
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputTag, HuiSpace } from '@nscode/handy-ui'
import type { InputTagSize } from '@nscode/handy-ui'
const sizes: InputTagSize[] = ['large', 'default', 'small']
const tags = ref(['Tag'])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInputTag
v-for="size in sizes"
:key="size"
v-model="tags"
:size="size"
:placeholder="size"
/>
<HuiInputTag :model-value="['Disabled tag']" disabled />
</HuiSpace>
</template>
Events and Focus Methods
Adding emits update before add; removing emits update before remove. There is no change event. InputTagExpose provides focus and blur; blur affects only this component's native input and retains the existing blur-to-add behavior.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInputTag, HuiSpace } from '@nscode/handy-ui'
import type { InputTagExpose } from '@nscode/handy-ui'
const control = ref<InputTagExpose>()
const tags = ref(['Initial'])
const event = ref('Waiting')
</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="tags"
@add="event = 'add: ' + $event"
@remove="event = 'remove: ' + $event"
@focus="event = 'focus'"
@blur="event = 'blur'"
/>
<span>{{ event }}; tags: {{ tags }}</span>
</HuiSpace>
</template>
Form Validation
The component follows form disabled and size settings. Tag changes validate change rules; blur adds a valid draft before validating blur rules. Pending text is separate from the model and is not cleared by resetting only the bound form value.
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiInputTag, HuiSpace } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive({ tags: [] as string[] })
const rules: Record<string, FormRule[]> = {
tags: [{ required: true, message: 'Add at least one tag', trigger: 'blur' }]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiForm :model="model" :rules="rules" @submit.prevent>
<HuiFormItem label="Tags" prop="tags"
><HuiInputTag v-model="model.tags" unique
/></HuiFormItem>
</HuiForm>
<HuiInputTag :model-value="[]" :validate-event="false" placeholder="No automatic validation" />
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string[] | null | undefined | Local empty list when omitted; supplying the prop controls it, including undefined/null |
| max | number | — | Maximum number of tags |
| placeholder | string | — | Empty list input placeholder |
| disabled | boolean | false | Prevent input and removal |
| unique | boolean | false | Reject duplicate tag values |
| separator | string | string[] | — | Separators for typing and pasting; respects max and unique |
| size | 'large' | 'default' | 'small' | — | Inherited control size |
| validateEvent | boolean | true | Validate on tag changes and blur |
Events
| Event | Payload | Description |
|---|---|---|
| update:modelValue | (value: string[]) | Tags are added or removed |
| add | (value: string) | One tag was added |
| remove | (value: string) | One tag was removed |
| focus | (event: FocusEvent) | Focus enters the component |
| blur | (event: FocusEvent) | Focus leaves the component |
Slots
No slots.
Methods
| Name | Type | Description |
|---|---|---|
| focus | () => void | Focus the current or first available control unless disabled or unmounted |
| blur | () => void | Blur only an active element inside this component in its owner document |
| inputRef | HTMLInputElement | undefined | Current native input; undefined after unmount |
Types
import type { InputTagSize, InputTagProps, InputTagEmits, InputTagExpose } from '@nscode/handy-ui'
export type InputTagSize = 'large' | 'default' | 'small'
export interface InputTagProps {
modelValue?: string[] | null
max?: number
placeholder?: string
disabled?: boolean
unique?: boolean
separator?: string | string[]
size?: InputTagSize
validateEvent?: boolean
}
export interface InputTagEmits {
'update:modelValue': [value: string[]]
add: [value: string]
remove: [value: string]
focus: [event: FocusEvent]
blur: [event: FocusEvent]
}
export interface InputTagExpose {
focus: () => void
blur: () => void
inputRef: HTMLInputElement | undefined
}