# 单选列表

## 使用方法

`RadioList` 在一组互斥选项里选一个。默认插槽放 `RadioListItem`。选项只是纯文本时，也可以传 `options` 数组。

```vue
<script setup lang="ts">
import { RadioList, RadioListItem } from '@astryx-vue/core/RadioList'
import { shallowRef } from 'vue'

const channel = shallowRef('')
</script>

<template>
  <RadioList v-model="channel" label="通知方式">
    <RadioListItem label="邮件" value="email" />
    <RadioListItem label="短信" value="sms" />
  </RadioList>
</template>
```

指示器默认 24px，`size="sm"` 时是 20px。`orientation="horizontal"` 把选项排成一行，间距 20px。`end` 插槽靠行的尾端。

### 结构

- **页眉**: 单选列表上方的可选标题。
- **子项**: 作为可选项渲染的单选列表项。
- **标签和值**: 每个单选项的文字标签，以及对应的值。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 选项保持较少，通常 2 到 7 个。 |
| 避免 | 需要多选时使用单选列表。应改用 CheckboxList。 |
| 避免 | 超过大约 4 个选项时用水平排列。 |

## 示例

### 水平排列

小号、中号、大号排成一行。

```vue
<script setup lang="ts">
import { RadioList, RadioListItem } from '@astryx-vue/core/RadioList'
import { shallowRef } from 'vue'

const value = shallowRef('md')
</script>

<template>
  <RadioList v-model="value" label="Size" orientation="horizontal">
    <RadioListItem label="Small" value="sm" />
    <RadioListItem label="Medium" value="md" />
    <RadioListItem label="Large" value="lg" />
  </RadioList>
</template>
```
### 价格方案

每个方案在行尾显示价格。

```vue
<script setup lang="ts">
import { RadioList, RadioListItem } from '@astryx-vue/core/RadioList'
import { Text } from '@astryx-vue/core/Text'
import { shallowRef } from 'vue'

const value = shallowRef('')
</script>

<template>
  <RadioList v-model="value" label="Plan">
    <RadioListItem label="Free" value="free">
      <template #end>
        <Text type="body" color="secondary">
          $0/mo
        </Text>
      </template>
    </RadioListItem>
    <RadioListItem label="Pro" value="pro">
      <template #end>
        <Text type="body" color="secondary">
          $9/mo
        </Text>
      </template>
    </RadioListItem>
    <RadioListItem label="Enterprise" value="enterprise">
      <template #end>
        <Text type="body" color="secondary">
          Custom
        </Text>
      </template>
    </RadioListItem>
  </RadioList>
</template>
```
### 带说明

每个选项下面有一段说明。

```vue
<script setup lang="ts">
import { RadioList, RadioListItem } from '@astryx-vue/core/RadioList'
import { shallowRef } from 'vue'

const value = shallowRef('')
</script>

<template>
  <RadioList v-model="value" label="Notification preference" description="Choose how you would like to be notified">
    <RadioListItem label="Email" value="email" description="Receive notifications via email" />
    <RadioListItem label="SMS" value="sms" description="Standard messaging rates apply" />
    <RadioListItem label="Push notification" value="push" description="Instant alerts on your device" />
  </RadioList>
</template>
```
### 带校验

必填且未选择时显示错误。

```vue
<script setup lang="ts">
import { RadioList, RadioListItem } from '@astryx-vue/core/RadioList'
import { computed, shallowRef } from 'vue'

const value = shallowRef('')
const status = computed(() => value.value === ''
  ? { type: 'error' as const, message: 'Please select a notification method' }
  : undefined)
</script>

<template>
  <RadioList v-model="value" label="Notification preference" is-required :status="status">
    <RadioListItem label="Email" value="email" />
    <RadioListItem label="SMS" value="sms" />
    <RadioListItem label="Push notification" value="push" />
  </RadioList>
</template>
```

## RadioList 属性

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `v-model` | `string \| number` | — | 必填。当前选中的值。 |
| `label` | `string` | — | 必填。这一组的无障碍名称。 |
| `options` | `RadioOption[]` | — | 纯文本选项。行尾需要内容时改用子项。 |
| `orientation` | `'vertical' \| 'horizontal'` | `'vertical'` | 纵向排列，或排成一行。 |
| `size` | `'sm' \| 'md'` | `'md'` | 指示器尺寸，20px 或 24px。单独的 Radio 仍是 18px。 |
| `description` | `string` | — | 组标签下的说明。 |
| `status` | `FieldStatusInput` | — | 错误、警告或成功。 |
| `statusVariant` | `'attached' \| 'detached' \| 'tooltip'` | `'detached'` | 状态消息的位置。 |
| `isDisabled` | `boolean` | `false` | 禁用整组。 |
| `disabledMessage` | `string` | — | 整组被禁用的原因。和 `isDisabled` 一起用时，悬停或键盘聚焦会显示这段提示，每个选项仍可聚焦（带 `aria-disabled`），但选不中。 |
| `isRequired` | `boolean` | `false` | 标记为必填。 |
| `containerClass` | `string` | — | 加到字段外壳上的 class。横向标签的表单里没有外壳，这时不生效。 |
| `containerStyle` | `StyleValue` | — | 加到字段外壳上的行内样式。 |
| `controlClass` | `string` | — | 加到单选组（`role="radiogroup"`）上的 class。 |
| `controlStyle` | `StyleValue` | — | 加到单选组上的行内样式。 |

## RadioListItem 属性

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `value` | `string \| number` | — | 必填。选中后提交的值。 |
| `label` | `string` | — | 必填。行标题。需要链接等富内容时用 `label` 插槽，点链接不会选中这一行。 |
| `description` | `string` | — | 标题下的说明。富内容用 `description` 插槽。 |
| `isDisabled` | `boolean` | `false` | 禁用这一行。 |

`end` 插槽画在标题后面。纵向列表里它靠行的尾端对齐。

## 无障碍

### 单选圆

- 准则: 1.4.11 Non-text Contrast
- 要求: 3:1
- 适用状态: Rest, Hover, Pointer down, Selected

未选中时的圆边、选中后的填充，都要和后面的表面至少 3:1。悬停和按下，量的是着色和按下叠层加上去之后的最终颜色。
