﻿# Radio group {#radio-group}

A segmented button selector using Element Plus Tooltip, with single/multiple selection, three sizes and disabled states. Labels and hints are supplied by the application; use the header to switch component themes.

## Single and multiple selection {#selection}

### Single and multiple selection
```vue
<script setup>
import { ref } from 'vue'
const groups = [{ name: 'Today', value: 1 }, { name: 'Day', value: 2, tip: 'Last 7 days' }, { name: 'Week', value: 3, tip: 'Last 4 weeks' }, { name: 'Month', value: 4, tip: 'Last 6 months' }]
const value = ref(2)
const selected = ref([2])
</script>

<template>
  <nf-radio-group v-model:value="value" :groups="groups" />
  <nf-radio-group v-model="selected" :groups="groups" multiple style="margin-top: 12px" />
  <p>single: {{ value }} / multiple: {{ selected }}</p>
</template>
```

## Sizes {#sizes}

### Sizes
```vue
<script setup>
import { ref } from 'vue'
const groups = [{ name: 'Today', value: 1 }, { name: 'Day', value: 2, tip: 'Last 7 days' }, { name: 'Week', value: 3, tip: 'Last 4 weeks' }, { name: 'Month', value: 4, tip: 'Last 6 months' }]
const value = ref(2)
</script>

<template>
  <div style="display: grid; gap: 12px">
    <nf-radio-group v-model:value="value" :groups="groups" size="large" />
    <nf-radio-group v-model:value="value" :groups="groups" size="medium" />
    <nf-radio-group v-model:value="value" :groups="groups" size="small" />
  </div>
</template>
```

## Disabled states {#disabled}

### Disabled states
```vue
<script setup>
import { ref } from 'vue'
const groups = [{ name: 'Today', value: 1 }, { name: 'Day', value: 2, tip: 'Last 7 days' }, { name: 'Week', value: 3, tip: 'Last 4 weeks' }, { name: 'Month', value: 4, tip: 'Last 6 months' }]
const value = ref(2)
</script>

<template>
  <nf-radio-group v-model:value="value" :groups="groups.map((item, index) => ({ ...item, disabled: index === 0 }))" />
  <nf-radio-group v-model:value="value" :groups="groups" disabled style="margin-top: 12px" />
</template>
```

## Custom tooltips {#tooltip}

### Custom tooltips
```vue
<script setup>
import { ref } from 'vue'
const groups = [{ name: 'Today', value: 1 }, { name: 'Day', value: 2, tip: 'Last 7 days' }, { name: 'Week', value: 3, tip: 'Last 4 weeks' }, { name: 'Month', value: 4, tip: 'Last 6 months' }]
const value = ref(2)
</script>

<template>
  <nf-radio-group v-model:value="value" :groups="groups" placement="bottom" effect="light" :visible-arrow="false" :open-delay="200" :hide-after="3000">
    <template #content="{ item, index }"><strong>{{ index }} / {{ item.tip }}</strong></template>
  </nf-radio-group>
</template>
```

## Interactive example {#interactive}


See expanded example example-5 (RadioGroupExamples).


## Installation {#installation}

```ts
import NfRadioGroup from '@94ai-plus/nf-radio-group'
import 'element-plus/dist/index.css'
import '@94ai-plus/nf-theme-chalk/nf-radio-group.css'

app.use(NfRadioGroup)
```

## Types {#types}

```ts
import type { RadioGroupOption, RadioGroupModelValue, RadioGroupChange } from '@94ai-plus/nf-radio-group'
// RadioGroupOption: { name: string; value: string | number; tip?: string; disabled?: boolean }
// RadioGroupModelValue: string | number | (string | number)[]
// RadioGroupChange: { index: number; value: string | number }
```

## Props {#props}

Text props follow the library language when omitted; defaults below use this language.

| Parameter | Description | Type | Options | Default |
| --- | --- | --- | --- | --- |
| `groups` | Option list with name, value, disabled and tip; business text is not translated automatically. | `RadioGroupOption[]` | — | `[]` |
| `value` | Current value: string/number for single selection, array for multiple; supports v-model:value. | `RadioGroupModelValue` | — | `''` |
| `modelValue` | Current value; Vue 3 v-model takes precedence over the compatibility prop. | `RadioGroupModelValue` | — | `undefined` |
| `multiple` | Enable multiple selection; use an array for multiple values. | `boolean` | — | `false` |
| `disabled` | Whether to disable the component. | `boolean` | — | `false` |
| `size` | Option size. | `string` | small / medium / large | `'medium'` |
| `placement` | Popup placement. | `string` | top / top-start / top-end / bottom / bottom-start / bottom-end / left / left-start / left-end / right / right-start / right-end | `'top'` |
| `effect` | Tooltip theme. | `string` | dark / light | `'dark'` |
| `offset` | Popup offset in pixels. | `number` | — | `0` |
| `transition` | Popup transition name. | `string` | — | `'el-fade-in-linear'` |
| `visibleArrow` | Whether to show the popup arrow. | `boolean` | — | `true` |
| `popperOptions` | Popper 2 options; use modifiers to customize positioning. | `RadioGroupPopperOptions` | — | `{ boundariesElement: 'body', gpuAcceleration: false }` |
| `popperClass` | Custom class for the popup. | `string` | — | `''` |
| `openDelay` | Show delay for hover, in milliseconds. | `number` | — | `0` |
| `enterable` | Whether the pointer can enter the Tooltip. | `boolean` | — | `true` |
| `hideAfter` | Tooltip auto-close delay after showing, in milliseconds; 0 disables auto-close. | `number` | — | `0` |
| `tabindex` | tabindex of the trigger. | `number` | — | `0` |

## Events {#events}

| Event | Description | Payload |
| --- | --- | --- |
| `update:value` | Update the bound value. | `(value: RadioGroupModelValue)` |
| `update:modelValue` | Update the bound value. | `(value: RadioGroupModelValue)` |
| `change` | Fires when an option is clicked; value is the operated option, not the whole multiple-selection array. | `({ index: number, value: string \| number })` |

## Slots {#slots}

| Parameter | Description | Payload |
| --- | --- | --- |
| `content` | Custom Tooltip content; only shown when tip exists and the option is enabled. | `{ item: RadioGroupOption, index: number }` |

## Migration notes {#migration}

Replace Vue 2 `:value.sync` with `v-model:value`. Default `v-model` is also supported; modelValue wins when both are supplied. Multiple selection requires an array. Inputs are not mutated; current values are filtered in group order before toggling. Clicking the selected single item emits nothing. The change payload always contains the clicked item value.

Legacy openDelay/visibleArrow map to showAfter/showArrow. Legacy hideAfter retains auto-close semantics via autoClose, starting when shown. The mouseleave delay is 200ms; enterable controls pointer entry. Legacy boundariesElement/gpuAcceleration become Popper 2 modifiers. Rewrite other Popper 1 settings for Popper 2; explicit modern modifiers win.

Disabled items can no longer be selected. Non-array multiple values no longer throw or match string substrings. Native buttons support Enter/Space, arrows and Home/End, skipping disabled items. Arrows select in single mode and only focus in multiple mode. Missing tips or disabled items/groups suppress tooltips even with a content slot. The slot now receives item/index. Business text is not auto-translated.

## Expanded component examples

### RadioGroupExamples

Source: docs/.vitepress/theme/components/RadioGroupExamples.vue

```vue
<script setup lang="ts">
import { computed, ref } from 'vue'
import { NfRadioGroup, type RadioGroupChange, type RadioGroupModelValue, type RadioGroupOption, type RadioGroupProps, type RadioGroupSize } from '@94ai-plus/common-ui'
import { useDocsLanguage } from '../locale'

const { english } = useDocsLanguage()
const value = ref<RadioGroupModelValue>(2)
const multiple = ref(false)
const disabled = ref(false)
const itemDisabled = ref(true)
const size = ref<RadioGroupSize>('medium')
const placement = ref<RadioGroupProps['placement']>('top')
const effect = ref<'light' | 'dark'>('dark')
const openDelay = ref(0)
const hideAfter = ref(0)
const offset = ref(0)
const tabindex = ref(0)
const visibleArrow = ref(true)
const enterable = ref(true)
const customContent = ref(false)
const transition = ref('el-fade-in-linear')
const popperClass = ref('radio-example-popper')
const events = ref(0)
const last = ref<RadioGroupChange>()
const groups = computed<RadioGroupOption[]>(() => [
  { name: english.value ? 'Today' : '今日', value: 1 },
  { name: english.value ? 'Day' : '按日', value: 2, tip: english.value ? 'Last 7 days' : '最近七天' },
  { name: english.value ? 'Week' : '按周', value: 3, tip: english.value ? 'Last 4 weeks' : '最近四周', disabled: itemDisabled.value },
  { name: english.value ? 'Month' : '按月', value: 4, tip: english.value ? 'Last 6 months' : '最近六个月' },
])
function change(payload: RadioGroupChange) { events.value++; last.value = payload }
function toggleMultiple() { value.value = multiple.value ? [2] : 2 }
</script>

<template>
  <section class="radio-examples">
    <div class="radio-controls">
      <label><input v-model="multiple" type="checkbox" data-testid="radio-multiple" @change="toggleMultiple">multiple</label>
      <label><input v-model="disabled" type="checkbox" data-testid="radio-disabled">disabled</label>
      <label><input v-model="itemDisabled" type="checkbox" data-testid="radio-item-disabled">item.disabled</label>
      <label>size <select v-model="size" data-testid="radio-size"><option>small</option><option>medium</option><option>large</option></select></label>
      <label>placement <select v-model="placement" data-testid="radio-placement"><option>top</option><option>bottom</option><option>left</option><option>right</option></select></label>
      <label>effect <select v-model="effect" data-testid="radio-effect"><option>dark</option><option>light</option></select></label>
      <label>openDelay <input v-model.number="openDelay" type="number" min="0" data-testid="radio-open-delay"></label>
      <label>hideAfter <input v-model.number="hideAfter" type="number" min="0" data-testid="radio-hide-after"></label>
      <label>offset <input v-model.number="offset" type="number" data-testid="radio-offset"></label>
      <label>tabindex <input v-model.number="tabindex" type="number" data-testid="radio-tabindex"></label>
      <label>transition <input v-model="transition" data-testid="radio-transition"></label>
      <label>popperClass <input v-model="popperClass" data-testid="radio-popper-class"></label>
      <label><input v-model="visibleArrow" type="checkbox" data-testid="radio-arrow">visibleArrow</label>
      <label><input v-model="enterable" type="checkbox" data-testid="radio-enterable">enterable</label>
      <label><input v-model="customContent" type="checkbox" data-testid="radio-content">content slot</label>
    </div>
    <NfRadioGroup v-model:value="value" data-testid="interactive-radio" aria-label="选择周期 / Select period" :groups="groups" :multiple="multiple" :disabled="disabled" :size="size" :placement="placement" :effect="effect" :open-delay="openDelay" :hide-after="hideAfter" :offset="offset" :tabindex="tabindex" :transition="transition" :popper-class="popperClass" :visible-arrow="visibleArrow" :enterable="enterable" @change="change">
      <template v-if="customContent" #content="{ item, index }"><strong data-testid="radio-custom-content">{{ index }} / {{ item.tip }}</strong></template>
    </NfRadioGroup>
    <output data-testid="radio-result">value: {{ JSON.stringify(value) }}; changes: {{ events }}; last: {{ JSON.stringify(last) }}</output>
    <button type="button" data-testid="radio-reset" @click="value = multiple ? [2] : 2">Reset value</button>
  </section>
</template>

<style scoped>
.radio-controls { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 20px; }
.radio-controls label { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; }
.radio-controls input:not([type='checkbox']) { width: 100px; }
.radio-controls input, .radio-controls select { border: 1px solid var(--site-border); border-radius: 4px; padding: 4px; background: var(--site-background); color: var(--site-text); }
.radio-examples output { display: block; margin-top: 12px; font-size: 12px; overflow-wrap: anywhere; }
.radio-examples > button { font-size: 12px; margin-top: 8px; cursor: pointer; }
</style>
```

Source: docs/.vitepress/theme/locale.ts

```ts
import { computed, inject, type InjectionKey, type Ref } from 'vue'
import { withBase } from 'vitepress'
import { localizePath, type LocaleName } from '../../../packages/common-locale/src/languages'
import { additionalMessages } from '../locales/ui'
export { languageDefinitions, localeFromPath, isLocaleName } from '../../../packages/common-locale/src/languages'

export type DocsLanguage = LocaleName
export const languageStorageKey = 'common-ui-plus:docs-language'
export const docsLanguageKey: InjectionKey<Ref<DocsLanguage>> = Symbol('docs-language')

const zh = {
  brand: '94智能通用组件库', home: '94智能公共组件库首页', skip: '跳至正文',
  toggleSidebar: '切换侧边导航', closeNav: '关闭导航', navigation: '顶部导航', sidebar: '文档导航', outline: '当前页面目录',
  guide: '指南', packages: '分包集合', theme: '主题', language: '语言', links: '边缘库',
  chalk: 'Chalk 默认主题', oem: 'OEM 定制主题', playground: '演练场',
  dark: '切换为暗色模式', light: '切换为亮色模式',
  search: '搜索文档', searchPlaceholder: '搜索文档，索引至标题，定位至组件', noResults: '没有匹配结果',
  example: '示例', loadingDemo: '正在加载示例…', preview: '实时预览', actions: '示例操作',
  edit: '在线编辑', editTitle: '在演练场中编辑', copy: '复制代码', copied: '代码已复制',
  expand: '展开代码', collapse: '收起代码', switchJs: '切换为 JS', switchTs: '切换为 TS',
  editor: '示例代码编辑器', switchFailed: '语言切换失败，代码未更改：',
  manualCopy: '请展开代码后手动复制。', copyFailed: '无法访问剪贴板，请在展开的编辑器中手动复制。',
}
const en: typeof zh = {
  brand: '94AI Common UI', home: '94AI Common UI home', skip: 'Skip to content',
  toggleSidebar: 'Toggle sidebar', closeNav: 'Close navigation', navigation: 'Top navigation', sidebar: 'Documentation navigation', outline: 'On this page',
  guide: 'Guide', packages: 'Packages', theme: 'Theme', language: 'Language', links: 'Links',
  chalk: 'Chalk default theme', oem: 'OEM custom theme', playground: 'Playground',
  dark: 'Switch to dark mode', light: 'Switch to light mode',
  search: 'Search docs', searchPlaceholder: 'Search docs, headings and components', noResults: 'No matching results',
  example: 'Example', loadingDemo: 'Loading example…', preview: ' live preview', actions: 'Example actions',
  edit: 'Edit online', editTitle: 'Edit in playground', copy: 'Copy code', copied: 'Code copied',
  expand: 'Expand code', collapse: 'Collapse code', switchJs: 'Switch to JS', switchTs: 'Switch to TS',
  editor: 'Example code editor', switchFailed: 'Language switch failed; code unchanged: ',
  manualCopy: 'Expand the code and copy it manually.', copyFailed: 'Clipboard unavailable. Copy the code manually from the editor.',
}
export const docsMessages: Record<DocsLanguage, typeof zh> = { 'zh-CN': zh, 'en-US': en, ...additionalMessages }

export function localizedPath(path: string, locale: DocsLanguage): string {
  return localizePath(path, locale)
}

export function useDocsLanguage() {
  const locale = inject(docsLanguageKey)!
  const english = computed(() => locale.value !== 'zh-CN')
  const ui = computed(() => docsMessages[locale.value])
  const docLink = (path: string) => withBase(localizedPath(path, locale.value))
  return { locale, english, ui, docLink }
}
```
