﻿# 按钮选择器 {#radio-group}

基于 Element Plus Tooltip 的分段按钮选择器，支持单选、多选、三种尺寸和禁用状态。选项名称及提示由业务传入，可使用右上角切换组件主题。

## 单选与多选 {#selection}

### 单选与多选
```vue
<script setup>
import { ref } from 'vue'
const groups = [{ name: '今日', value: 1 }, { name: '按日', value: 2, tip: '最近七天' }, { name: '按周', value: 3, tip: '最近四周' }, { name: '按月', value: 4, tip: '最近六个月' }]
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}

### 尺寸
```vue
<script setup>
import { ref } from 'vue'
const groups = [{ name: '今日', value: 1 }, { name: '按日', value: 2, tip: '最近七天' }, { name: '按周', value: 3, tip: '最近四周' }, { name: '按月', value: 4, tip: '最近六个月' }]
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}

### 禁用状态
```vue
<script setup>
import { ref } from 'vue'
const groups = [{ name: '今日', value: 1 }, { name: '按日', value: 2, tip: '最近七天' }, { name: '按周', value: 3, tip: '最近四周' }, { name: '按月', value: 4, tip: '最近六个月' }]
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>
```

## 自定义提示 {#tooltip}

### 自定义提示
```vue
<script setup>
import { ref } from 'vue'
const groups = [{ name: '今日', value: 1 }, { name: '按日', value: 2, tip: '最近七天' }, { name: '按周', value: 3, tip: '最近四周' }, { name: '按月', value: 4, tip: '最近六个月' }]
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}


See expanded example example-5 (RadioGroupExamples).


## 按需引入 {#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}

```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}

文案属性未传时跟随组件库当前语言；下表列出本语言的默认文案。

| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| --- | --- | --- | --- | --- |
| `groups` | 选项数据列表，包含 name、value、disabled、tip；业务文案不自动翻译。 | `RadioGroupOption[]` | — | `[]` |
| `value` | 当前值；单选为字符串或数字，多选为数组，支持 v-model:value。 | `RadioGroupModelValue` | — | `''` |
| `modelValue` | 当前值；支持 Vue 3 v-model，优先于兼容属性。 | `RadioGroupModelValue` | — | `undefined` |
| `multiple` | 是否启用多选模式；多选值使用数组。 | `boolean` | — | `false` |
| `disabled` | 是否禁用组件。 | `boolean` | — | `false` |
| `size` | 选项尺寸。 | `string` | small / medium / large | `'medium'` |
| `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 主题。 | `string` | dark / light | `'dark'` |
| `offset` | 浮层位置偏移量，单位 px。 | `number` | — | `0` |
| `transition` | 定义浮层渐变动画。 | `string` | — | `'el-fade-in-linear'` |
| `visibleArrow` | 是否显示浮层箭头。 | `boolean` | — | `true` |
| `popperOptions` | Popper 2 配置，使用 modifiers 定制定位。 | `RadioGroupPopperOptions` | — | `{ boundariesElement: 'body', gpuAcceleration: false }` |
| `popperClass` | 为浮层添加类名。 | `string` | — | `''` |
| `openDelay` | hover 触发时的显示延迟，单位毫秒。 | `number` | — | `0` |
| `enterable` | 鼠标是否可以进入 Tooltip。 | `boolean` | — | `true` |
| `hideAfter` | Tooltip 显示后自动隐藏的延时，单位毫秒；0 表示不自动隐藏。 | `number` | — | `0` |
| `tabindex` | 触发元素的 tabindex。 | `number` | — | `0` |

## 事件 {#events}

| 事件名称 | 说明 | 回调参数 |
| --- | --- | --- |
| `update:value` | 更新绑定值。 | `(value: RadioGroupModelValue)` |
| `update:modelValue` | 更新绑定值。 | `(value: RadioGroupModelValue)` |
| `change` | 点击选项触发；value 为本次操作项的值，不是整个多选数组。 | `({ index: number, value: string \| number })` |

## 插槽 {#slots}

| 参数 | 说明 | 回调参数 |
| --- | --- | --- |
| `content` | 自定义 Tooltip 内容；仅有 tip 且未禁用时显示。 | `{ item: RadioGroupOption, index: number }` |

## 迁移说明 {#migration}

Vue 2 的 `:value.sync` 改为 `v-model:value`。也支持普通 `v-model`，两者同时传入时 `modelValue` 优先。多选值必须使用数组；组件不修改传入数组，且保持旧版按 groups 过滤当前值再切换的顺序。单选重复点击不触发事件，change 的 value 始终为本次操作项。

旧 `openDelay`／`visibleArrow` 分别适配 `showAfter`／`showArrow`；`hideAfter` 保留自动关闭语义，映射 `autoClose`，从提示实际显示起计时。鼠标移出后 200ms 关闭，enterable 控制是否可进入提示。默认 Popper 1 的 `boundariesElement`／`gpuAcceleration` 转为 Popper 2 modifiers，其他旧配置需按 Popper 2 改写，显式现代 modifiers 优先。

修复局部禁用项仍可点击，以及多选传入非数组时异常／字符串误匹配的问题。选项改为原生按钮，支持 Enter／空格、方向键、Home／End，跳过禁用项。单选方向键同时选中，多选仅移动焦点。没有 tip 或禁用时不显示提示，content 插槽不能绕过该条件；插槽新增 item／index 参数。业务名称和提示不会自动翻译。

## 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 }
}
```
