Checkbox
A controlled checkbox, checkbox group and connected button group.
Import
import { HuiCheckbox, HuiCheckboxGroup } from '@nscode/handy-ui'
import type { CheckboxProps, CheckboxGroupProps, CheckboxGroupExpose } from '@nscode/handy-ui'
Examples
String and boolean business values
value, the fallback label, modelValue, trueValue and falseValue preserve their string values, including an empty string and a string matching the prop name. Use explicit bindings such as :value="true" and :value="false" for boolean business values; value="" remains an empty string. Selection compares the original values. The boolean props disabled, readonly and indeterminate retain their usual behavior.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace } from '@nscode/handy-ui'
import type { CheckboxValueType } from '@nscode/handy-ui'
const selected = ref<CheckboxValueType[]>([])
const single = ref<CheckboxValueType>('off')
</script>
<template>
<HuiSpace direction="vertical" fill class="checkbox-value-demo">
<span>The empty string, number 0 and boolean false are distinct option values</span>
<HuiCheckboxGroup v-model="selected">
<HuiCheckbox class="value-empty" value="">Empty string</HuiCheckbox>
<HuiCheckbox class="value-zero" :value="0">Number 0</HuiCheckbox>
<HuiCheckbox class="value-false" :value="false">Boolean false</HuiCheckbox>
</HuiCheckboxGroup>
<span class="value-array-result">Array value: {{ JSON.stringify(selected) }}</span>
<HuiCheckbox v-model="single" class="value-custom" true-value="" false-value="off">
Standalone: an empty string when checked, off when unchecked
</HuiCheckbox>
<span class="value-single-result">Standalone value: {{ JSON.stringify(single) }}</span>
<HuiCheckbox class="value-controlled-empty" :model-value="''">
An empty string does not equal the default checked value true
</HuiCheckbox>
</HuiSpace>
</template>
Reactive object values
When an option uses an object, selecting and unchecking match the original object reference, including a reactive proxy of that object after a ref array updates. A different object with identical fields remains a separate option. Retain the original option objects when updating the model.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace } from '@nscode/handy-ui'
import type { CheckboxValueType } from '@nscode/handy-ui'
const options = [
{ id: 1, name: 'A' },
{ id: 2, name: 'B' }
]
const grouped = ref<CheckboxValueType[]>([])
const standalone = ref<CheckboxValueType[]>([])
</script>
<template>
<HuiSpace direction="vertical" fill class="checkbox-object-demo">
<span>Group: object values can be unchecked after v-model updates</span>
<HuiCheckboxGroup v-model="grouped" class="object-group">
<HuiCheckbox v-for="option in options" :key="option.id" :value="option">
Object {{ option.name }}
</HuiCheckbox>
</HuiCheckboxGroup>
<span class="object-group-result">Group value: {{ JSON.stringify(grouped) }}</span>
<span>Standalone array: selection matches the original object</span>
<HuiSpace class="object-array" wrap>
<HuiCheckbox v-for="option in options" :key="option.id" v-model="standalone" :value="option">
Object {{ option.name }}
</HuiCheckbox>
</HuiSpace>
<span class="object-array-result">Array value: {{ JSON.stringify(standalone) }}</span>
</HuiSpace>
</template>
Local state and controlled updates
Omitting modelValue makes a standalone Checkbox retain local state initially equal to falseValue (default false), and a CheckboxGroup retain a local []. An update listener alone does not make them controlled. Supplying the prop makes them controlled, including explicit undefined; refusing the update preserves the parent selection. Unrelated parent renders retain local state. Switching controlled status produces a development warning. Group state takes precedence over child models. No defaultValue or defaultChecked prop is added.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiCheckbox, HuiCheckboxGroup } from '@nscode/handy-ui'
const parentRenders = ref(0)
const singleUpdate = ref('No update yet')
const groupUpdate = ref('No update yet')
const fixedRequest = ref('No request yet')
</script>
<template>
<HuiSpace direction="vertical" fill class="checkbox-state-demo">
<HuiButton class="state-render-parent" @click="parentRenders++"
>Render parent {{ parentRenders }}</HuiButton
>
<span>Standalone: omitted values and update-only listeners keep local state</span>
<HuiSpace wrap>
<HuiCheckbox class="local-single" :data-parent-render="parentRenders"
>Local option</HuiCheckbox
>
<HuiCheckbox
class="listener-single"
@update:model-value="singleUpdate = JSON.stringify($event)"
>Update-only option</HuiCheckbox
>
<HuiCheckbox class="empty-single" :model-value="undefined">Explicit undefined</HuiCheckbox>
<HuiCheckbox
class="fixed-single"
:model-value="true"
@update:model-value="fixedRequest = JSON.stringify($event)"
>Fixed controlled option</HuiCheckbox
>
</HuiSpace>
<span class="single-notification">Standalone update: {{ singleUpdate }}</span>
<span>Group: omitted values are stored internally; Render parent preserves the selection</span>
<HuiCheckboxGroup class="local-group" :max="2" :data-parent-render="parentRenders">
<HuiCheckbox value="a">Local A</HuiCheckbox><HuiCheckbox value="b">Local B</HuiCheckbox>
</HuiCheckboxGroup>
<HuiCheckboxGroup
class="listener-group"
@update:model-value="groupUpdate = JSON.stringify($event)"
>
<HuiCheckbox value="a">Update-only A</HuiCheckbox
><HuiCheckbox value="b">Update-only B</HuiCheckbox>
</HuiCheckboxGroup>
<span class="group-notification">Group update: {{ groupUpdate }}</span>
<HuiCheckboxGroup class="empty-group" :model-value="undefined">
<HuiCheckbox value="a">Explicit empty A</HuiCheckbox
><HuiCheckbox value="b">Explicit empty B</HuiCheckbox>
</HuiCheckboxGroup>
<HuiCheckboxGroup
class="fixed-group"
:model-value="['a']"
@update:model-value="fixedRequest = JSON.stringify($event)"
>
<HuiCheckbox value="a">Fixed A</HuiCheckbox
><HuiCheckbox value="b">Parent rejects B</HuiCheckbox>
</HuiCheckboxGroup>
<span
>Fixed controls record requests while retaining the parent selection; request:
{{ fixedRequest }}</span
>
</HuiSpace>
</template>
Basic values and label text
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const checked = ref(false)
const values = ref(['a'])
</script>
<template>
<HuiSpace wrap
><HuiCheckbox v-model="checked">Agreement</HuiCheckbox
><HuiCheckbox v-model="values" value="a" label="Option A" /><HuiCheckbox
v-model="values"
value="b"
>Option B</HuiCheckbox
></HuiSpace
>
</template>
Disabled and readonly
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const checked = ref(true)
</script>
<template>
<HuiSpace wrap
><HuiCheckbox v-model="checked" disabled>Disabled</HuiCheckbox
><HuiCheckbox v-model="checked" readonly>Readonly but focusable</HuiCheckbox
><HuiCheckbox v-model="checked">Editable</HuiCheckbox></HuiSpace
>
</template>
Indeterminate and select all
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const values = ref<string[]>([])
const all = computed(() => values.value.length === 3)
const half = computed(() => values.value.length > 0 && !all.value)
const select = (next: unknown) => (values.value = next ? ['a', 'b', 'c'] : [])
</script>
<template>
<HuiSpace direction="vertical"
><HuiCheckbox :model-value="all" :indeterminate="half" @update:model-value="select"
>Select all</HuiCheckbox
><HuiCheckboxGroup v-model="values"
><HuiCheckbox value="a">A</HuiCheckbox><HuiCheckbox value="b">B</HuiCheckbox
><HuiCheckbox value="c">C</HuiCheckbox></HuiCheckboxGroup
><span>{{ values }}</span></HuiSpace
>
</template>
Custom checked and unchecked values
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const value = ref<string | number>('off')
</script>
<template>
<HuiSpace
><HuiCheckbox v-model="value" true-value="on" false-value="off">Status</HuiCheckbox
><span>{{ value }}</span></HuiSpace
>
</template>
Array values
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const values = ref(['red'])
</script>
<template>
<HuiSpace
><HuiCheckbox v-model="values" value="red">Red</HuiCheckbox
><HuiCheckbox v-model="values" value="blue">Blue</HuiCheckbox
><span>{{ values }}</span></HuiSpace
>
</template>
Checkbox group and selection limits
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const values = ref(['b'])
</script>
<template>
<HuiSpace direction="vertical"
><HuiCheckboxGroup v-model="values" :min="1" :max="2" @limit-reach="event = 'Limit: ' + $event"
><HuiCheckbox value="a">A</HuiCheckbox><HuiCheckbox value="b">B</HuiCheckbox
><HuiCheckbox value="c">C</HuiCheckbox></HuiCheckboxGroup
><span>{{ values }} · {{ event }}</span></HuiSpace
>
</template>
Group state and explicit child overrides
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const values = ref(['a'])
const readonly = ref(true)
const disabled = ref(false)
</script>
<template>
<HuiSpace direction="vertical"
><HuiSpace
><HuiCheckbox v-model="readonly">Group readonly</HuiCheckbox
><HuiCheckbox v-model="disabled">Group disabled</HuiCheckbox></HuiSpace
><HuiCheckboxGroup v-model="values" :readonly="readonly" :disabled="disabled"
><HuiCheckbox value="a">Inherited</HuiCheckbox
><HuiCheckbox value="b" :readonly="false" :disabled="false"
>Explicit child false</HuiCheckbox
></HuiCheckboxGroup
></HuiSpace
>
</template>
Sizes
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const values = ref(['a'])
</script>
<template>
<HuiSpace direction="vertical"
><HuiCheckboxGroup
v-for="size in ['large', 'default', 'small'] as const"
:key="size"
v-model="values"
:size="size"
><HuiCheckbox value="a">{{ size }} A</HuiCheckbox
><HuiCheckbox value="b">B</HuiCheckbox></HuiCheckboxGroup
></HuiSpace
>
</template>
Button appearance
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const values = ref(['b'])
</script>
<template>
<HuiSpace direction="vertical"
><HuiCheckboxGroup v-model="values" type="button"
><HuiCheckbox value="a">A</HuiCheckbox><HuiCheckbox value="b">B</HuiCheckbox
><HuiCheckbox value="c" disabled>Disabled</HuiCheckbox></HuiCheckboxGroup
><span>{{ values }}</span></HuiSpace
>
</template>
Form integration
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { HuiCheckbox, HuiCheckboxGroup, HuiSpace, HuiForm, HuiFormItem } from '@nscode/handy-ui'
const event = ref('Waiting')
const model = reactive({ permissions: [] as string[] })
const rules = {
permissions: [{ required: true, message: 'Select permissions', trigger: 'change' as const }]
}
</script>
<template>
<HuiForm :model="model" :rules="rules"
><HuiFormItem label="Permissions" prop="permissions"
><HuiCheckboxGroup v-model="model.permissions"
><HuiCheckbox value="read">Read</HuiCheckbox
><HuiCheckbox value="write">Write</HuiCheckbox></HuiCheckboxGroup
></HuiFormItem
></HuiForm
>
</template>
HuiCheckbox Props
| Name | Type | Default | Description |
|---|---|---|---|
| modelValue | CheckboxValueType | CheckboxValueType[] | undefined | Local when omitted; controlled when supplied, including undefined. Controlled selection |
| value | CheckboxValueType | — | Option value, preferred over label |
| label | CheckboxValueType | — | Fallback value and visible text when the default slot is absent |
| disabled | boolean | — | Disabled; inherits group and form state |
| readonly | boolean | — | Readonly while remaining focusable |
| indeterminate | boolean | false | Visual mixed state; controlled independently |
| trueValue | CheckboxValueType | true | Standalone checked value |
| falseValue | CheckboxValueType | false | Standalone unchecked value |
| name | string | — | Native form name; group supplies its name |
| size | 'large' | 'default' | 'small' | — | Own value overrides inherited group/form/global size |
| validateEvent | boolean | true | Automatic value-change validation; the group handles its children |
HuiCheckboxGroup Props
| Name | Type | Default | Description |
|---|---|---|---|
| modelValue | CheckboxValueType[] | undefined | Local when omitted; controlled when supplied, including undefined. Controlled group selection |
| disabled | boolean | false | Default child disabled state; Form disabled remains authoritative |
| readonly | boolean | false | Default child readonly state |
| min | number | — | Minimum selected count; 0 imposes no limit |
| max | number | — | Maximum selected count; 0 imposes no limit |
| name | string | — | Shared native name |
| type | 'default' | 'button' | default | Standard controls or connected buttons |
| size | 'large' | 'default' | 'small' | — | Group size inherited by children |
| validateEvent | boolean | true | Automatic change validation |
HuiCheckbox Events
| Event | Arguments | Description |
|---|---|---|
| update:modelValue | value: CheckboxValueType | CheckboxValueType[] | Selection update request |
| change | value: CheckboxValueType | CheckboxValueType[] | User selection changed |
HuiCheckboxGroup Events
| Event | Arguments | Description |
|---|---|---|
| update:modelValue | value: CheckboxValueType[] | Selection update request |
| change | value: CheckboxValueType[] | User selection changed |
| limitReach | limit: 'min' | 'max' | A configured selection limit was reached |
| focus | event: FocusEvent | Focus enters the whole group once |
| blur | event: FocusEvent | Focus leaves the whole group once; internal moves do not repeat it |
The public limit event is declared and emitted as limitReach, once after update:modelValue and change. Vue templates may keep @limit-reach; render functions use onLimitReach. The old event name is not emitted alongside it. Limit values remain 'min' or 'max'; selection and limit rules are unchanged.
Slots
| Component | Slot | Description |
|---|---|---|
| Checkbox | default | Option label; overrides label text |
| CheckboxGroup | default | Child Checkbox controls |
Focus methods and exposed type
Use CheckboxGroupExpose for the component ref. focus() targets the current/selected or first available control, without changing values or opening a file dialog. Disabled or unmounted controls ignore focus requests; readonly controls remain focusable when their existing contract allows it. blur() only affects the active element inside this component, using its owner document, and does not blur external elements. Group focus/blur events fire once on entering/leaving the whole component; internal movement does not repeat them. Existing validation behavior is preserved.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckboxGroup, HuiCheckbox, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { CheckboxGroupExpose } from '@nscode/handy-ui'
const control = ref<CheckboxGroupExpose>()
const value = ref(['b'])
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>
<HuiCheckboxGroup ref="control" v-model="value" @focus="event = 'focus'" @blur="event = 'blur'"
><HuiCheckbox value="a">A</HuiCheckbox
><HuiCheckbox value="b">B</HuiCheckbox></HuiCheckboxGroup
>
<span>{{ event }}</span>
</HuiSpace>
</template>
| Method | Signature |
|---|---|
| focus | () => void |
| blur | () => void |
Types
export type CheckboxValueType = string | number | boolean | object
export interface CheckboxGroupExpose {
focus: () => void
blur: () => void
}
Props, emits, group context and injection-key types are also exported from @nscode/handy-ui. Checkbox's existing focus()/blur()/inputRef remain available via the component ref.