SelectInput
A selection field with a fully custom popup. Show a label or tags while the panel manages selection. The component owns popup visibility and query notifications; it does not own selected values or options.
Import
import { HuiSelectInput } from '@nscode/handy-ui'
import type { SelectInputExpose, SelectInputTag } from '@nscode/handy-ui'
Examples
Custom panel and a single label
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSelectInput, HuiSpace } from '@nscode/handy-ui'
const label = ref('')
const visible = ref(false)
const choose = (value: string) => {
label.value = value
visible.value = false
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSelectInput v-model:visible="visible" :label="label" placeholder="Choose a team">
<template #panel="{ close }">
<HuiSpace style="padding: 12px">
<HuiButton @click="choose('Design')">Design</HuiButton>
<HuiButton @click="choose('Engineering')">Engineering</HuiButton>
<HuiButton @click="close">Close panel</HuiButton>
</HuiSpace>
</template>
</HuiSelectInput>
<span>Selected: {{ label || '(empty)' }}; visible: {{ visible }}</span>
</HuiSpace>
</template>
Paged table, search and confirmed removal
The page owns selection across table pages. It accepts query changes through inputChange. Clear requests wait for confirmation, so cancellation preserves both selection and query. Tag removal, table deselection and clearing share the same pending and stale confirmation protection.
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import {
HuiButton,
HuiCheckbox,
HuiMessageBox,
HuiPagination,
HuiSelectInput,
HuiSpace,
HuiTable
} from '@nscode/handy-ui'
import type { TableColumnConfig } from '@nscode/handy-ui'
const initialRows = [
{ id: 1, name: 'Lin Ke', team: 'Design' },
{ id: 2, name: 'Chen An', team: 'Engineering' },
{ id: 3, name: 'Zhou Ning', team: 'Product' },
{ id: 4, name: 'He Qing', team: 'Engineering' },
{ id: 5, name: 'Su He', team: 'Operations' },
{ id: 6, name: 'Fang Yuan', team: 'Design' },
{ id: 7, name: 'Lu Chuan', team: 'Engineering' },
{ id: 8, name: 'Shen Yue', team: 'Product' }
]
const rows = ref(initialRows.map((row) => ({ ...row })))
const selected = ref<(string | number)[]>([1, 3])
const visible = ref(false)
const query = ref('')
const page = ref(1)
const pending = ref(false)
const readonly = ref(false)
const collapsed = ref(true)
const status = ref(
'The page owns tags and cross-page selection; confirm before removal or clearing'
)
const pageSize = 4
const columns: TableColumnConfig[] = [
{ prop: 'name', label: 'Name', width: 130 },
{ prop: 'team', label: 'Team', width: 130 }
]
const filtered = computed(() =>
rows.value.filter((row) => `${row.name}${row.team}`.includes(query.value.trim()))
)
const pageRows = computed(() =>
filtered.value.slice((page.value - 1) * pageSize, page.value * pageSize)
)
const pageKeys = computed(() =>
selected.value.filter((key) => pageRows.value.some((row) => row.id === key))
)
const tags = computed(() =>
selected.value.flatMap((key) => {
const row = rows.value.find((item) => item.id === key)
return row ? [{ value: key, label: row.name }] : []
})
)
let alive = true
let revision = 0
watch([selected, rows, query, readonly], () => revision++, { deep: true, flush: 'sync' })
watch(query, () => {
page.value = 1
})
onBeforeUnmount(() => {
alive = false
revision++
})
const confirmSelection = async (
next: (string | number)[],
object: string,
clearQuery = false
) => {
if (!alive || pending.value || readonly.value) return
const snapshot = selected.value.slice()
const collection = rows.value
const request = revision
pending.value = true
status.value = `Confirming ${object},selection stays unchanged`
try {
const result = await HuiMessageBox.confirm(
`Confirm ${object}? This changes selection only; member records are not deleted. Other selections are preserved.`,
'Confirm selection change',
{ confirmButtonText: 'Confirm removal', confirmButtonType: 'danger' }
)
if (!alive) return
if (
request !== revision ||
readonly.value ||
rows.value !== collection ||
snapshot.length !== selected.value.length ||
snapshot.some((key, index) => key !== selected.value[index])
) {
status.value = 'Selection, query or data changed; stale confirmation was discarded'
return
}
if (result.action !== 'confirm') {
status.value = 'Cancelled; selection is unchanged'
return
}
selected.value = next.slice()
if (clearQuery) query.value = ''
status.value = `Completed: ${object}`
} catch {
if (alive && request === revision)
status.value = 'Cancelled or closed; selection and query are unchanged'
} finally {
if (alive) pending.value = false
}
}
const removeTag = (key: string | number) => {
const tag = tags.value.find((item) => item.value === key)
if (tag)
void confirmSelection(
selected.value.filter((value) => value !== key),
`Remove member“${tag.label}”`
)
}
const clear = () => {
if (!selected.value.length) {
query.value = ''
return
}
void confirmSelection([], `Clear all ${selected.value.length} selected members`, true)
}
const updatePage = (keys: (string | number)[]) => {
if (pending.value || readonly.value) return
const pageIds = new Set(pageRows.value.map((row) => row.id))
const next = [...selected.value.filter((key) => !pageIds.has(key as number)), ...keys]
const removed = selected.value.filter((key) => !next.includes(key))
if (removed.length) {
const names = tags.value
.filter((tag) => removed.includes(tag.value))
.map((tag) => tag.label)
.join('、')
void confirmSelection(next, `Remove ${removed.length} members“${names}”`)
} else selected.value = next
}
// 查询只接受inputChange;clear发出的文本更新请求等确认后再接受。
const inputChange = (value: string) => {
if (!pending.value && !readonly.value) query.value = value
}
const refresh = () => {
revision++
rows.value = rows.value.map((row) => ({ ...row }))
status.value = 'Data refreshed; stale confirmation is invalid and selection is unchanged'
}
const toggleReadonly = () => {
readonly.value = !readonly.value
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiCheckbox v-model="collapsed">Collapse tags</HuiCheckbox>
<HuiButton class="select-input-demo-refresh" size="small" @click="refresh"
>Refresh data</HuiButton
>
<HuiButton class="select-input-demo-readonly" size="small" @click="toggleReadonly">{{
readonly ? 'Enable selection' : 'Set readonly'
}}</HuiButton>
</HuiSpace>
<HuiSelectInput
v-model:visible="visible"
:input-value="query"
:tags="tags"
:collapse-tags="collapsed"
:max-collapse-tags="2"
:disabled="pending || readonly"
:loading="pending"
:popup-width="420"
multiple
filterable
clearable
placeholder="Search by name or team"
aria-label="Choose members"
@input-change="inputChange"
@remove-tag="removeTag"
@clear="clear"
>
<template #panel>
<HuiSpace direction="vertical" fill style="padding: 12px">
<HuiTable
:data="pageRows"
:columns="columns"
:selected-keys="pageKeys"
:row-selectable="() => !pending && !readonly"
row-key="id"
selectable
@update:selected-keys="updatePage"
/>
<HuiPagination
v-model:current-page="page"
:total="filtered.length"
:page-size="pageSize"
:disabled="pending"
layout="prev,pager,next"
/>
</HuiSpace>
</template>
</HuiSelectInput>
<span class="select-input-demo-status" role="status">{{ status }}</span>
<span class="select-input-demo-selected"
>Selected: {{ tags.map((tag) => tag.label).join('、') || '(empty)' }}</span
>
<span
>While confirmation is pending, removal cannot repeat. Cancel, close or Escape preserves
selection and query. Data refresh, readonly changes or unmount invalidate stale
confirmation.</span
>
</HuiSpace>
</template>
Tree panel
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSelectInput, HuiSpace, HuiTree } from '@nscode/handy-ui'
import type { TreeNodeData } from '@nscode/handy-ui'
const visible = ref(false)
const label = ref('')
const selected = ref<string | number | null>(null)
const nodes: TreeNodeData[] = [
{
id: 'design',
label: 'Design',
children: [
{ id: 'visual', label: 'Visual design' },
{ id: 'interaction', label: 'Interaction design' }
]
},
{ id: 'engineering', label: 'Engineering' }
]
// node-click passes { node, key }
const choose = ({ node, key }: { node: TreeNodeData; key: string | number }) => {
selected.value = key
label.value = node.label ?? ''
visible.value = false
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSelectInput v-model:visible="visible" :label="label" placeholder="Choose a team">
<template #panel
><HuiTree
:data="nodes"
:current-key="selected"
default-expand-all
highlight-current
@node-click="choose"
/></template>
</HuiSelectInput>
<span>Node: {{ selected ?? '(empty)' }}</span>
</HuiSpace>
</template>
Sizes, widths, placement and panel clicks
trigger sets a minimum width equal to the field, auto follows content and numbers specify pixels. The viewport limits panel width. With keepOpenOnPanelClick=false, normal panel clicks request closing. A prevented click or a callback that already reopens the popup keeps the new session.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiCheckbox, HuiSelect, HuiSelectInput, HuiSpace } from '@nscode/handy-ui'
import type { PopperPlacement } from '@nscode/handy-ui'
const sizes = ['large', 'default', 'small'] as const
const widths = ['trigger', 'auto', 420] as const
const positions: PopperPlacement[] = [
'top',
'top-start',
'top-end',
'bottom',
'bottom-start',
'bottom-end',
'left',
'left-start',
'left-end',
'right',
'right-start',
'right-end'
]
const placement = ref<PopperPlacement>('bottom-start')
const keep = ref(true)
const loading = ref(false)
const disabled = ref(false)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><HuiCheckbox v-model="keep">Keep open on panel click</HuiCheckbox
><HuiCheckbox v-model="loading">loading</HuiCheckbox
><HuiCheckbox v-model="disabled">disabled</HuiCheckbox></HuiSpace
>
<HuiSelect v-model="placement" :options="positions.map((value) => ({ label: value, value }))" />
<HuiSelectInput
v-for="(size, index) in sizes"
:key="size"
:size="size"
:popup-width="widths[index]"
:placement="placement"
:keep-open-on-panel-click="keep"
:loading="loading"
:disabled="disabled"
:placeholder="`${size} / ${widths[index]}`"
>
<template #panel="{ close }"
><HuiSpace style="padding: 12px"
><span>{{ size }} / {{ widths[index] }}</span
><HuiButton @click="close">Close</HuiButton></HuiSpace
></template
>
</HuiSelectInput>
</HuiSpace>
</template>
Tags, prefix, clear requests and empty slot
The page owns the tags: the component only emits removeTag and clear requests, and the page updates tags itself. tag.close() has the same behavior as the default remove button. closable=false blocks removal requests for that tag, and the page keeps it when clearing. To confirm before changing the selection, follow the table example.
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiButton,
HuiCheckbox,
HuiIcon,
HuiSelect,
HuiSelectInput,
HuiSpace
} from '@nscode/handy-ui'
import type { SelectInputTag } from '@nscode/handy-ui'
const initialTags: SelectInputTag[] = [
{ value: 1, label: 'Number 1' },
{ value: '1', label: 'String 1' },
{ value: 2, label: 'Fixed', closable: false },
{ value: 3, label: 'Third' }
]
const tags = ref(initialTags.slice())
const types = ['primary', 'success', 'warning', 'danger', 'info', 'help'] as const
const tagType = ref<(typeof types)[number]>('primary')
const collapse = ref(true)
const event = ref('Waiting for a request')
// 1 and '1' are different tags, so compare strictly
const remove = (value: string | number, source: string) => {
tags.value = tags.value.filter((tag) => tag.value !== value)
event.value = `${source} requested removing ${typeof value} ${value}; the page removed it`
}
// Keep the fixed tag that cannot be removed
const clearTags = () => {
tags.value = tags.value.filter((tag) => tag.closable === false)
event.value = 'clear: the page cleared the tags and kept the fixed one'
}
const reset = () => {
tags.value = initialTags.slice()
event.value = 'All tags restored'
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><HuiCheckbox v-model="collapse">collapseTags</HuiCheckbox
><HuiSelect
v-model="tagType"
:options="types.map((value) => ({ label: value, value }))"
/><HuiButton @click="reset">Restore tags</HuiButton></HuiSpace
>
<HuiSelectInput
:tags="tags"
:tag-type="tagType"
:collapse-tags="collapse"
:max-collapse-tags="2"
multiple
clearable
@remove-tag="(value) => remove(value, 'Default tag')"
@clear="clearTags"
>
<template #prefix><HuiIcon name="search" /></template>
<template #empty>Provide custom content with the panel slot</template>
</HuiSelectInput>
<HuiSelectInput
:tags="tags"
multiple
:collapse-tags="false"
@remove-tag="(value) => remove(value, 'Custom tag')"
>
<template #tag="{ tag, close }"
><HuiButton size="small" :disabled="tag.closable === false" @click.stop="close($event)">{{
tag.label
}}</HuiButton></template
>
</HuiSelectInput>
<span role="status">{{ event }}</span>
</HuiSpace>
</template>
Controlled queries, normalization and rejection
inputValue and visible are independently controlled by prop presence. Passing a prop without accepting updates retains the parent value. Explicit undefined is also controlled. Omit the prop to use internal query or visibility state. The example accepts input and uppercases it by default. Uncheck the box and the parent stops accepting updates, so the field keeps the parent value.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiSelectInput, HuiSpace } from '@nscode/handy-ui'
const query = ref('ABC')
const accept = ref(true)
const event = ref('Waiting for input')
const update = (value: string) => {
if (accept.value) query.value = value.toUpperCase()
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCheckbox v-model="accept">Accept and uppercase input; otherwise reject it</HuiCheckbox>
<HuiSelectInput
:input-value="query"
filterable
@update:input-value="update"
@input-change="event = $event"
><template #panel
><span>Application query: {{ query }}</span></template
></HuiSelectInput
>
<span>Parent value: {{ query }}; latest request: {{ event }}</span>
</HuiSpace>
</template>
Form, input group and public methods
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiCheckbox,
HuiForm,
HuiFormItem,
HuiInputGroup,
HuiSelectInput,
HuiSpace
} from '@nscode/handy-ui'
import type { SelectInputExpose } from '@nscode/handy-ui'
const model = reactive({ team: '' })
const control = ref<SelectInputExpose>()
const readonly = ref(false)
const validateEvent = ref(true)
const event = ref('Waiting for an action')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><HuiCheckbox v-model="readonly">Readonly form</HuiCheckbox
><HuiCheckbox v-model="validateEvent">validateEvent</HuiCheckbox></HuiSpace
>
<HuiForm :model="model" :readonly="readonly" size="small">
<HuiFormItem
label="Team"
prop="team"
tip="The page owns panel selection"
:rules="[{ required: true, message: 'Choose a team', trigger: 'blur' }]"
>
<HuiInputGroup>
<HuiSelectInput
ref="control"
:label="model.team"
:validate-event="validateEvent"
@focus="event = 'focus'"
@blur="event = 'blur'"
@clear="event = 'clear request'"
>
<template #panel="{ close }"
><HuiButton
@click="
() => {
model.team = 'Design'
close()
}
"
>Choose Design</HuiButton
></template
>
</HuiSelectInput>
<HuiButton @click="control?.open()">open()</HuiButton>
</HuiInputGroup>
</HuiFormItem>
</HuiForm>
<HuiSpace wrap
><HuiButton @click="control?.close()">close()</HuiButton
><HuiButton @click="control?.focus()">focus()</HuiButton
><HuiButton @click="control?.blur()">blur()</HuiButton
><HuiButton @click="control?.clear()">clear(): request only</HuiButton></HuiSpace
>
<span role="status">{{ event }}</span>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | — | Single selection display text, not a selected value |
| tags | SelectInputTag[] | [] | External tags; never mutated by the component |
| multiple | boolean | false | Display tags |
| visible | boolean | Internal false when omitted | Controlled by prop presence; v-model:visible |
| inputValue | string | Internal '' when omitted | Query text; v-model:input-value |
| filterable | boolean | false | Allow query input; no filtering or requests |
| placeholder | string | Localized selection placeholder | Empty string explicitly overrides it |
| clearable | boolean | false | Show the clear request button |
| disabled | boolean | false | Disable interactions; inherit group and form disabled state |
| loading | boolean | false | Loading icon and aria-busy; panel content stays intact |
| size | 'large' | 'default' | 'small' | Inherited | Explicit > InputGroup > FormItem > Form > defaults > global size |
| collapseTags | boolean | true | Collapse tags beyond the limit |
| maxCollapseTags | number | 3 | Visible tag count; 0 shows only +N |
| tagType | TagType | 'primary' | primary/success/warning/danger/info/help |
| popupWidth | 'trigger' | 'auto' | number | 'trigger' | Trigger minimum width, content width or fixed px |
| placement | PopperPlacement | 'bottom-start' | Twelve placements with automatic flipping |
| keepOpenOnPanelClick | boolean | true | Keep the panel open after a click |
| validateEvent | boolean | true | Validate display changes and closed group blur |
Events
| Event | Parameters | Description |
|---|---|---|
| update:visible | visible: boolean | Open/close request in controlled and uncontrolled modes |
| update:inputValue | value: string | Query update request |
| inputChange | value: string | Query after composition; emitted after update |
| removeTag | value: string | number | Tag removal request; external selection stays intact |
| clear | — | After update:inputValue(''); no selection change event |
| focus | event: FocusEvent | Once when focus enters the complete control |
| blur | event: FocusEvent | Once when focus leaves the field, panel and owned child popups |
Use kebab-case listeners in templates: @input-change, @remove-tag, @update:input-value. Event declarations use camelCase.
Slots
| Slot | Parameters | Description |
|---|---|---|
| panel | { close: () => void } | Arbitrary focusable and clickable content |
| prefix | — | Field prefix |
| tag | { tag: SelectInputTag, close: (event?: Event) => void } | Custom tag; close requests removal |
| empty | — | Fallback only when panel is absent |
Methods
Access through ref<SelectInputExpose>().
| Method | Description |
|---|---|
| open() | Request opening without moving existing focus |
| close() | Request closing; parent rejection keeps it open |
| focus() | Focus the query input or semantic trigger; no-op when disabled or unmounted |
| blur() | Blur only the currently focused element owned by this control |
| clear() | Same clear request; never changes label/tags or steals outside focus |
Types
interface SelectInputTag {
label: string
value: string | number
closable?: boolean
}
interface SelectInputExpose {
open: () => void
close: () => void
focus: () => void
blur: () => void
clear: () => void
}
Also exports SelectInputProps, SelectInputEmits, SelectInputSlots, SelectInputPanelSlotProps and SelectInputTagSlotProps.