Radio
Controlled native radios and radio groups with shared keyboard and form semantics.
Import
import { HuiRadio, HuiRadioGroup } from '@nscode/handy-ui'
import type { RadioProps, RadioGroupProps, RadioGroupExpose, RadioExpose } from '@nscode/handy-ui'
Examples
Local state and controlled updates
Omitting modelValue makes Radio and RadioGroup retain local state initially equal to undefined (no selection). 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, HuiRadio, HuiRadioGroup } 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="radio-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>
<HuiRadio class="local-single" value="a" :data-parent-render="parentRenders"
>Local option</HuiRadio
>
<HuiRadio
class="listener-single"
value="a"
@update:model-value="singleUpdate = JSON.stringify($event)"
>Update-only option</HuiRadio
>
<HuiRadio class="empty-single" value="a" :model-value="undefined"
>Explicit undefined</HuiRadio
>
<HuiRadio
class="fixed-single"
value="b"
:model-value="'a'"
@update:model-value="fixedRequest = JSON.stringify($event)"
>Fixed controlled option</HuiRadio
>
</HuiSpace>
<span class="single-notification">Standalone update: {{ singleUpdate }}</span>
<span>Group: omitted values are stored internally; Render parent preserves the selection</span>
<HuiRadioGroup class="local-group" :data-parent-render="parentRenders">
<HuiRadio value="a">Local A</HuiRadio><HuiRadio value="b">Local B</HuiRadio>
</HuiRadioGroup>
<HuiRadioGroup
class="listener-group"
@update:model-value="groupUpdate = JSON.stringify($event)"
>
<HuiRadio value="a">Update-only A</HuiRadio><HuiRadio value="b">Update-only B</HuiRadio>
</HuiRadioGroup>
<span class="group-notification">Group update: {{ groupUpdate }}</span>
<HuiRadioGroup class="empty-group" :model-value="undefined">
<HuiRadio value="a">Explicit empty A</HuiRadio><HuiRadio value="b">Explicit empty B</HuiRadio>
</HuiRadioGroup>
<HuiRadioGroup
class="fixed-group"
:model-value="'a'"
@update:model-value="fixedRequest = JSON.stringify($event)"
>
<HuiRadio value="a">Fixed A</HuiRadio><HuiRadio value="b">Parent rejects B</HuiRadio>
</HuiRadioGroup>
<span
>Fixed controls record requests while retaining the parent selection; request:
{{ fixedRequest }}</span
>
</HuiSpace>
</template>
Basic selection and label text
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const value = ref<string | number | boolean>('a')
</script>
<template>
<HuiSpace
><HuiRadio v-model="value" value="a" name="basic-radio">A</HuiRadio
><HuiRadio v-model="value" value="b" label="Option B" name="basic-radio" /><span>{{
value
}}</span></HuiSpace
>
</template>
Label as value
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const value = ref('A')
</script>
<template>
<HuiRadioGroup v-model="value"><HuiRadio label="A" /><HuiRadio label="B" /></HuiRadioGroup>
</template>
Disabled radios
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const value = ref('a')
</script>
<template>
<HuiSpace
><HuiRadio v-model="value" value="a" disabled>Selected disabled</HuiRadio
><HuiRadio v-model="value" value="b" disabled>Disabled</HuiRadio></HuiSpace
>
</template>
Radio groups
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const value = ref('b')
</script>
<template>
<HuiSpace direction="vertical"
><HuiRadioGroup v-model="value" @change="event = 'Changed: ' + $event"
><HuiRadio value="a">A</HuiRadio><HuiRadio value="b">B</HuiRadio
><HuiRadio value="c">C</HuiRadio></HuiRadioGroup
><span>{{ event }}</span></HuiSpace
>
</template>
Group disabled state
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const value = ref('a')
const disabled = ref(true)
</script>
<template>
<HuiSpace direction="vertical"
><HuiSwitch v-model="disabled" /><HuiRadioGroup v-model="value" :disabled="disabled"
><HuiRadio value="a">Inherited</HuiRadio
><HuiRadio value="b" :disabled="false">Explicit false</HuiRadio></HuiRadioGroup
></HuiSpace
>
</template>
Numbers and booleans
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const number = ref(1)
const flag = ref(false)
</script>
<template>
<HuiSpace direction="vertical"
><HuiRadioGroup v-model="number"
><HuiRadio :value="1">Number 1</HuiRadio
><HuiRadio :value="2">Number 2</HuiRadio></HuiRadioGroup
><HuiRadioGroup v-model="flag"
><HuiRadio :value="true">True</HuiRadio
><HuiRadio :value="false">False</HuiRadio></HuiRadioGroup
></HuiSpace
>
</template>
Sizes
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const value = ref('a')
</script>
<template>
<HuiSpace direction="vertical"
><HuiRadioGroup
v-for="size in ['large', 'default', 'small'] as const"
:key="size"
v-model="value"
:size="size"
><HuiRadio value="a">{{ size }} A</HuiRadio><HuiRadio value="b">B</HuiRadio></HuiRadioGroup
></HuiSpace
>
</template>
Button groups
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const value = ref('b')
</script>
<template>
<HuiSpace direction="vertical"
><HuiRadioGroup v-model="value" type="button"
><HuiRadio value="a">A</HuiRadio><HuiRadio value="b">B</HuiRadio
><HuiRadio value="c" disabled>Disabled</HuiRadio></HuiRadioGroup
><span>{{ value }}</span></HuiSpace
>
</template>
Form integration
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import {
HuiRadio,
HuiRadioGroup,
HuiSpace,
HuiForm,
HuiFormItem,
HuiSwitch
} from '@nscode/handy-ui'
const event = ref('Waiting')
const model = reactive({ role: '' })
const rules = { role: [{ required: true, message: 'Choose a role', trigger: 'change' as const }] }
</script>
<template>
<HuiForm :model="model" :rules="rules"
><HuiFormItem label="Role" prop="role"
><HuiRadioGroup v-model="model.role"
><HuiRadio value="admin">Administrator</HuiRadio
><HuiRadio value="member">Member</HuiRadio></HuiRadioGroup
></HuiFormItem
></HuiForm
>
</template>
HuiRadio Props
| Name | Type | Default | Description |
|---|---|---|---|
| modelValue | string | number | boolean | undefined | Local when omitted; controlled when supplied, including undefined. Controlled selection |
| value | string | number | boolean | — | Option value, preferred over label |
| label | string | number | boolean | — | Fallback value and visible text when the default slot is absent |
| disabled | boolean | — | Disabled; inherits group and form state |
| 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 |
HuiRadioGroup Props
| Name | Type | Default | Description |
|---|---|---|---|
| modelValue | string | number | boolean | undefined | Local when omitted; controlled when supplied, including undefined. Controlled group selection |
| disabled | boolean | undefined | Default child disabled state; Form disabled remains authoritative |
| name | string | Generated with useId | 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 |
HuiRadio Events
| Event | Arguments | Description |
|---|---|---|
| update:modelValue | value: string | number | boolean | Selection update request |
| change | value: string | number | boolean | User selection changed |
| focus | event: FocusEvent | Native radio gained focus |
| blur | event: FocusEvent | Native radio lost focus |
HuiRadioGroup Events
| Event | Arguments | Description |
|---|---|---|
| update:modelValue | value: string | number | boolean | Selection update request |
| change | value: string | number | boolean | User selection changed |
| focus | event: FocusEvent | Focus enters the whole group once |
| blur | event: FocusEvent | Focus leaves the whole group once; internal moves do not repeat it |
Slots
| Component | Slot | Description |
|---|---|---|
| Radio | default | Option label; overrides label text |
| RadioGroup | default | Child Radio controls |
Focus methods and exposed type
Use RadioGroupExpose 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.
Individual radios also expose RadioExpose with focus()/blur().
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRadioGroup, HuiRadio, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { RadioGroupExpose } from '@nscode/handy-ui'
const control = ref<RadioGroupExpose>()
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>
<HuiRadioGroup ref="control" v-model="value" @focus="event = 'focus'" @blur="event = 'blur'"
><HuiRadio value="a">A</HuiRadio><HuiRadio value="b">B</HuiRadio></HuiRadioGroup
>
<span>{{ event }}</span>
</HuiSpace>
</template>
| Method | Signature |
|---|---|
| focus | () => void |
| blur | () => void |
Types
export interface RadioGroupExpose {
focus: () => void
blur: () => void
}
export interface RadioExpose extends RadioGroupExpose {}
Props, emits, group context and injection-key types are also exported from @nscode/handy-ui.