Tag Tag
Display categories and states, with an optional close guard.
Import
Use <hui-tag> with uni-app easycom. Import public types from @nscode/handy-uni. For ordinary Vue 3, install the preview plugin from @nscode/handy-uni/web and register its components.
Examples
Interact with the controls to see values and event results on the page.
<template>
<view>
<hui-page-host /><hui-tag
v-if="shown"
:key="record.id"
:label="record.name"
type="primary"
closable
@click="event0"
@close="
(event) => {
shown = false
event1(event)
}
"
>待处理 / Pending</hui-tag
><hui-button v-if="!shown" @click="shown = true">重新添加 / Add again</hui-button>
<hui-tag v-for="type in types" :key="type" :type="type">{{ type }}</hui-tag
><hui-tag effect="dark">dark</hui-tag><hui-tag effect="plain">plain</hui-tag
><hui-tag round disabled closable>禁用 / Disabled</hui-tag>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('No event yet')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const shown = ref(true)
const record = { id: 'pending-1', name: '待处理 / Pending' }
const types = ['default', 'primary', 'success', 'warning', 'danger', 'info'] as const
const event0 = (arg0: unknown) => {
lastEvent.value = 'click' + ': ' + [describe(arg0)].join(', ')
}
const event1 = (arg0: unknown) => {
lastEvent.value = 'close' + ': ' + [describe(arg0)].join(', ')
}
</script>
Without beforeClose, close is emitted directly and the parent removes the item. Call showConfirmDialog in beforeClose when confirmation is needed, and return false on cancellation. Unmounting or changing the target invalidates pending work.
Confirm before closing
Confirm the target in the guard. Cancelling or dismissing the dialog keeps the tag; the close handler removes it only after confirmation.
<script setup lang="ts">
import { ref } from 'vue'
import { showConfirmDialog } from '@nscode/handy-uni'
const shown = ref(true)
const result = ref('The tag has not been removed')
async function beforeClose() {
try {
await showConfirmDialog({
message: 'Remove the “Pending” tag? This only removes this tag from the current list.'
})
return true
} catch {
result.value = 'Cancelled; the tag remains'
return false
}
}
function removeTag(_event: unknown) {
shown.value = false
result.value = 'Confirmed; the tag was removed'
}
</script>
<template>
<view>
<hui-page-host />
<hui-tag v-if="shown" label="Pending" closable :before-close="beforeClose" @close="removeTag" />
<hui-button v-else @click="shown = true">Add again</hui-button>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
type | TagType | 'default' | Semantic type or input mode; see the type definition. |
effect | TagEffect | 'light' | Light, dark or outlined tag appearance. |
size | ComponentSize | — | Component size; inherits configuration when omitted. |
round | boolean | false | Use rounded or pill styling. |
icon | string | — | Registered icon name. |
color | string | — | Color override; prefer theme variables. |
label | string | — | Visible label. |
closable | boolean | false | Show the tag removal action. |
beforeClose | (() => unknown) | — | Close guard; false or rejection prevents closing. |
disabled | boolean | false | Disable interaction. |
Root attributes: common props. A Boolean default of undefined supports inheritance; explicit false overrides it. Other Boolean props default to false unless listed otherwise.
Events
| Event | Parameters | Description |
|---|---|---|
click | (event: unknown) | Emitted when the interactive area is clicked. |
close | (event: unknown) | Closing started after guards passed. |
Slots
| Slot | Parameters | Description |
|---|---|---|
icon | — | Custom icon region; replaces its fallback content. |
default | — | Main content. |
Methods
None.
Types
TagProps、TagType、TagEffect、TagEmits are available from @nscode/handy-uni.
export type TagType = 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info'
export type TagEffect = 'light' | 'dark' | 'plain'
Platform notes
| Behavior | uni-app H5 | WeChat Mini Program | App-vue | Ordinary Vue 3 Web (preview) |
|---|---|---|---|---|
| Basic interaction | Supported; device checks pending | Supported; device checks pending | Supported; device checks pending | Supported by the web platform adapter |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.