Radio单选框
单选框用于在同时可见的多个选项中选出一个,便于比较,因此选项不宜过多。它在 <label> 中渲染原生 <input type="radio">。RadioButton 把同样的选择画成按钮,RadioGroup 负责保存值。
何时使用
- 当用户需要并排查看少量选项并只选其中一项时,使用单选框。
- 选项很多或空间有限时,Select 更合适;需要同时选择多项时,请使用多选框。
- 优先使用带
options的RadioGroup;需要紧凑的按钮式选择时使用optionType="button"。
代码演示
The radio is not selected no change yet
<script lang="ts">
import { Radio } from 'svelte-ant-design';
let selected = $state(false);
let last = $state('no change yet');
</script>
<div class="stack">
<!-- The text between the tags is the children of the radio, and its accessible name. -->
<Radio bind:checked={selected} onchange={(checked) => (last = `onchange(${checked})`)}>
Send me the newsletter
</Radio>
<span>The radio is {selected ? 'selected' : 'not selected'}</span>
<span role="status">{last}</span>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
<script lang="ts">
import { Button, Radio, RadioGroup } from 'svelte-ant-design';
let disabled = $state(true);
</script>
<div class="stack">
<div class="row">
<Button onclick={() => (disabled = !disabled)}>{disabled ? 'Enable' : 'Disable'}</Button>
<Radio {disabled}>Not selected</Radio>
<Radio {disabled} checked>Selected</Radio>
</div>
<!-- disabled disables the whole group. -->
<RadioGroup {disabled} value="Apple" options={['Apple', 'Pear']} aria-label="Fruits" />
<!-- An option can be disabled alone. -->
<RadioGroup
value="Apple"
aria-label="Stock"
options={['Apple', { label: 'Pear (out of stock)', value: 'Pear', disabled: true }, 'Orange']}
/>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Radio, RadioGroup } from 'svelte-ant-design';
let fruit = $state('Apple');
let last = $state('no change yet');
</script>
<div class="stack">
<!-- The children of the group are Radio elements; each one needs a value. -->
<RadioGroup
bind:value={fruit}
name="fruit"
aria-label="Fruit"
onchange={(value) => (last = `onchange(${value})`)}
>
<Radio value="Apple">Apple</Radio>
<Radio value="Pear">Pear</Radio>
<Radio value="Orange">Orange</Radio>
</RadioGroup>
<span>The value is {fruit}</span>
<span role="status">{last}</span>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
no change yet
<script lang="ts">
import { RadioGroup } from 'svelte-ant-design';
let fruit = $state<string | undefined>('Apple');
let size = $state<number | undefined>(2);
let agree = $state<boolean | undefined>(false);
let extra = $state<string | undefined>();
let last = $state('no change yet');
</script>
{#snippet cherry()}<strong>Cherry</strong>{/snippet}
<div class="stack">
<!-- Strings, numbers and booleans are both the value and the label. -->
<RadioGroup bind:value={fruit} options={['Apple', 'Pear', 'Orange']} aria-label="Fruit" />
<RadioGroup bind:value={size} options={[1, 2, 3]} aria-label="Size" />
<!-- A boolean value stays a boolean in onchange; the form gets its text. -->
<RadioGroup
bind:value={agree}
aria-label="Agree"
options={[
{ label: 'Yes', value: true },
{ label: 'No', value: false }
]}
/>
<!-- Objects add a snippet label, title, id, required, disabled and an onchange of the option. -->
<RadioGroup
bind:value={extra}
aria-label="Extras"
options={[
{ label: cherry, value: 'cherry', id: 'extra-cherry' },
{
label: 'Pear',
value: 'pear',
title: 'Juicy',
required: true,
onchange: (checked) => (last = `pear: ${checked}`)
},
{ label: 'Plum', value: 'plum', disabled: true }
]}
/>
<span role="status">{last}</span>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
<script lang="ts">
import { RadioButton, RadioGroup } from 'svelte-ant-design';
let city = $state('Hangzhou');
let solidCity = $state('Shanghai');
let mode = $state('day');
</script>
<div class="stack">
<!-- optionType="button" asks options for buttons instead of plain radios. -->
<RadioGroup
bind:value={city}
optionType="button"
options={['Hangzhou', 'Shanghai', { label: 'Beijing', value: 'Beijing', disabled: true }]}
aria-label="City"
/>
<!-- buttonStyle="solid" fills the checked button. -->
<RadioGroup
bind:value={solidCity}
optionType="button"
buttonStyle="solid"
options={['Hangzhou', 'Shanghai', 'Chengdu']}
aria-label="City, solid"
/>
<!-- Or put RadioButton elements in the group yourself. -->
<RadioGroup bind:value={mode} buttonStyle="solid" aria-label="Mode">
<RadioButton value="day">Day</RadioButton>
<RadioButton value="week">Week</RadioButton>
<RadioButton value="month">Month</RadioButton>
</RadioGroup>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
<script lang="ts">
import { RadioGroup } from 'svelte-ant-design';
const cities = ['Hangzhou', 'Shanghai', 'Beijing'];
</script>
<div class="stack">
<!-- size changes the buttons only; medium is the default, middle and default are aliases of it.
A plain value is the initial value, so each group keeps its own selection. -->
<RadioGroup value="Hangzhou" optionType="button" size="large" options={cities} aria-label="Large" />
<RadioGroup value="Hangzhou" optionType="button" size="medium" options={cities} aria-label="Medium" />
<RadioGroup value="Hangzhou" optionType="button" size="small" options={cities} aria-label="Small" />
<RadioGroup value="Hangzhou" optionType="button" size="default" options={cities} aria-label="Default (medium)" />
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
<script lang="ts">
import { Radio, RadioGroup } from 'svelte-ant-design';
let a = $state('Apple');
let b = $state('Pear');
let c = $state('Hangzhou');
let d = $state('x');
const fruits = ['Apple', 'Pear', 'Orange'];
</script>
<div class="layout">
<!-- orientation="vertical" puts one option on each line. -->
<RadioGroup bind:value={a} orientation="vertical" options={fruits} aria-label="Vertical" />
<!-- vertical is a shorthand for it; orientation wins when both are set. -->
<RadioGroup bind:value={b} vertical optionType="button" options={fruits} aria-label="Vertical buttons" />
<!-- block fills the width of the parent and shares it between the items. -->
<RadioGroup
bind:value={c}
block
optionType="button"
options={['Hangzhou', 'Shanghai', 'Beijing']}
aria-label="Block"
/>
<!-- Your own markup around the radios: they still share the group. -->
<RadioGroup bind:value={d} aria-label="Grid">
<div class="grid">
<Radio value="x">Top left</Radio>
<Radio value="y">Top right</Radio>
<Radio value="z">Bottom left</Radio>
<Radio value="w">Bottom right</Radio>
</div>
</RadioGroup>
</div>
<style>
.layout {
display: flex;
flex-direction: column;
gap: 16px;
align-items: stretch;
width: 100%;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 140px);
gap: 8px;
}
</style>
Plan: free, refused 0 times
<script lang="ts">
import { Button, Radio, RadioGroup } from 'svelte-ant-design';
let plan = $state<string | undefined>('free');
let refused = $state(0);
let locked = $state(false);
let agreed = $state(false);
</script>
<div class="stack">
<!-- A function binding: the setter decides whether a choice is accepted. The previous radio is put back. -->
<RadioGroup
bind:value={
() => plan,
(next) => {
if (next === 'team' && !agreed) {
refused++;
return;
}
plan = next;
}
}
options={[
{ label: 'Free', value: 'free' },
{ label: 'Team (needs the terms)', value: 'team' }
]}
aria-label="Plan"
/>
<div class="row">
<Radio bind:checked={agreed}>I accept the terms</Radio>
<span role="status">Plan: {plan ?? 'none'}, refused {refused} times</span>
</div>
<!-- preventDefault() in a forwarded onclick cancels the selection and onchange. -->
<Radio
bind:checked={locked}
onclick={(event) => {
if (!agreed) event.preventDefault();
}}
>
Lock the plan
</Radio>
<!-- Setting the value to undefined clears the selection. -->
<Button onclick={() => (plan = undefined)}>Clear the plan</Button>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px 16px;
}
</style>
<script lang="ts">
import { Radio, RadioButton, RadioGroup } from 'svelte-ant-design';
let value = $state('b');
</script>
<div class="stack">
<div class="row">
<!-- Radio: the parts root, icon and label. -->
<Radio
checked
class="radio-demo-mine"
classNames={{ label: 'radio-demo-strong' }}
styles={{ icon: { '--ads-radio-checked-bg': '#52c41a' }, label: { 'font-weight': 600 } }}
>
Green mark
</Radio>
<!-- RadioButton: the parts root and label. -->
<RadioButton checked styles={{ root: { 'border-radius': '16px' } }}>Round</RadioButton>
</div>
<!-- RadioGroup: root, item, itemIcon and itemLabel reach every radio inside. -->
<RadioGroup
bind:value
options={[
{ label: 'A', value: 'a' },
{ label: 'B', value: 'b' }
]}
aria-label="Parts"
class="radio-demo-group"
style="padding: 4px 8px"
classNames={{ item: 'radio-demo-item', itemIcon: 'radio-demo-icon', itemLabel: 'radio-demo-label' }}
styles={{
root: { 'background-color': 'rgba(22, 119, 255, 0.08)', 'border-radius': '8px' },
itemLabel: { 'letter-spacing': '0.04em' }
}}
/>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px 24px;
}
:global(.radio-demo-strong) {
text-decoration: underline;
}
:global(.radio-demo-item) {
padding-block: 2px;
}
</style>
No visible text, named by aria-label
Named by a heading
Uses the radio of your device.
Pick a colour
<script lang="ts">
import { Radio, RadioGroup } from 'svelte-ant-design';
let color = $state('red');
</script>
<div class="stack">
<!-- children: the text of the radio is its accessible name. -->
<Radio>Visible text</Radio>
<!-- aria-label: for a radio without a visible text. -->
<div class="row">
<Radio aria-label="Hidden label" />
<span>No visible text, named by aria-label</span>
</div>
<!-- aria-labelledby: another element names it. -->
<div class="row">
<Radio aria-labelledby="radio-name" />
<span id="radio-name">Named by a heading</span>
</div>
<!-- An external <label for> works with the id of the input. -->
<div class="row">
<Radio id="radio-external" />
<label for="radio-external">External label</label>
</div>
<!-- aria-describedby and title. -->
<div class="row">
<Radio aria-describedby="radio-hint" title="Wireless">Wi-Fi</Radio>
<small id="radio-hint">Uses the radio of your device.</small>
</div>
<!-- A group is named with aria-labelledby (or aria-label), so a screen reader says what the choice is about. -->
<div class="field">
<span id="color-title">Pick a colour</span>
<RadioGroup bind:value={color} options={['red', 'green', 'blue']} aria-labelledby="color-title" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 4px 12px;
}
.field {
display: grid;
justify-items: start;
gap: 4px;
}
</style>
<script lang="ts">
import { Button, Radio, RadioGroup } from 'svelte-ant-design';
let size = $state<string | undefined>();
let gift = $state(false);
let result = $state('');
function submit(event: SubmitEvent) {
event.preventDefault();
const data = new FormData(event.currentTarget as HTMLFormElement);
result = `Submitted: ${JSON.stringify([...data.entries()])}`;
}
// A native reset does not change the state of the components: reset it here.
function reset() {
size = undefined;
gift = false;
result = '';
}
</script>
<form class="stack" onsubmit={submit} onreset={reset}>
<!-- A named group sends the value of the selected radio. -->
<RadioGroup bind:value={size} name="size" options={['S', 'M', 'L']} aria-label="Size" />
<Radio bind:checked={gift} name="gift" value="yes" required>Gift wrap</Radio>
<!-- A group without a name is never submitted, so a form stays free of made-up keys. -->
<RadioGroup value="a" options={['a', 'b']} aria-label="Not submitted" />
<div class="row">
<Button htmlType="submit" type="primary">Save</Button>
<Button htmlType="reset">Reset</Button>
</div>
<span role="status">{result}</span>
</form>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
.row {
display: flex;
gap: 8px;
}
</style>
no change yet
<script lang="ts">
import { RadioGroup } from 'svelte-ant-design';
let first = $state('Apple');
let second = $state<string | undefined>();
let last = $state('no change yet');
</script>
<!-- Tab enters a group once, on the selected radio (or the first when none is). Arrow keys move and select; Space
selects the focused one. The browser does it: the inputs of a group share one name. -->
<div class="stack">
<input aria-label="Before the groups" placeholder="Tab from here" />
<RadioGroup
bind:value={first}
options={['Apple', 'Pear', 'Orange']}
aria-label="First group"
onchange={(value) => (last = `first: ${value}`)}
/>
<RadioGroup
bind:value={second}
options={['Red', 'Green', 'Blue']}
aria-label="Second group"
onchange={(value) => (last = `second: ${value}`)}
/>
<span role="status">{last}</span>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 12px;
}
</style>
API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| checked | 单选框是否被选中。使用 bind:checked;单独写 checked={x} 只是初始值。位于组内时被忽略。 | boolean | false |
| disabled | 禁用单选框。组的 disabled 同样生效。 | boolean | false |
| value | 在 RadioGroup 中的标识,同时是 input 的原生 value(以文本形式写入)。位于组内时必填。 | string | number | boolean | — |
| name | input 的原生 name。位于组内时以组的名称为准。同名的独立单选框不会相互同步,请使用组。 | string | — |
| children | 标签内容,同时是可访问名称;没有它时请用 aria-label 或外部标签命名。 | Snippet | — |
| onchange | 用户选中单选框时调用一次,参数为 true 与原生 change 事件。父组件修改或禁用状态下不会调用。 | (checked: boolean, event: Event) => void | — |
| classNames | 为单选框各部分追加的类名。 | Partial<Record<'root' | 'icon' | 'label', string>> | — |
| styles | 单选框各部分的内联样式;键为 kebab-case 的 CSS 属性名或自定义属性,数字原样写入。 | Partial<Record<'root' | 'icon' | 'label', Record<string, string | number>>> | — |
| class | 根元素的额外类名,位于 classNames.root 之后。 | string | — |
| style | 根元素的内联样式;styles.root 写在其后。 | string | — |
| title | 根 label 的原生 title,因此提示同时覆盖圆点与文字。 | string | — |
| native attributes | 其余属性都会传给 <input>:id、required、autofocus、tabindex、aria-*、data-*、onclick、onfocus、onblur 等。组件自己管理 type、checked、value、disabled、name 与 onchange。 | HTMLInputAttributes | — |
与 Ant Design 的差异:单选框的
onchange 接收 (checked, event)(checked 恒为 true),组的接收 (value, event),传入的是原生事件而非事件对象;没有 defaultChecked 与 defaultValue,由 bind:checked 和 bind:value 替代;RadioButton 是独立组件,optionType 只是让 options 生成它;value 接受字符串、数字或布尔值,而非任意类型;没有名称的组不会随表单提交;size 同时接受 middle 与 default。RadioButton 的 API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| checked | 单选框是否被选中。使用 bind:checked;单独写 checked={x} 只是初始值。位于组内时被忽略。 | boolean | false |
| disabled | 禁用单选框。组的 disabled 同样生效。 | boolean | false |
| value | 在 RadioGroup 中的标识,同时是 input 的原生 value(以文本形式写入)。位于组内时必填。 | string | number | boolean | — |
| name | input 的原生 name。位于组内时以组的名称为准。同名的独立单选框不会相互同步,请使用组。 | string | — |
| children | 标签内容,同时是可访问名称;没有它时请用 aria-label 或外部标签命名。 | Snippet | — |
| onchange | 用户选中单选框时调用一次,参数为 true 与原生 change 事件。父组件修改或禁用状态下不会调用。 | (checked: boolean, event: Event) => void | — |
| classNames | 为单选按钮各部分追加的类名。 | Partial<Record<'root' | 'label', string>> | — |
| styles | 单选按钮各部分的内联样式;键为 kebab-case 的 CSS 属性名或自定义属性,数字原样写入。 | Partial<Record<'root' | 'label', Record<string, string | number>>> | — |
| class | 根元素的额外类名,位于 classNames.root 之后。 | string | — |
| style | 根元素的内联样式;styles.root 写在其后。 | string | — |
| title | 根 label 的原生 title,因此提示同时覆盖圆点与文字。 | string | — |
| native attributes | 其余属性都会传给 <input>:id、required、autofocus、tabindex、aria-*、data-*、onclick、onfocus、onblur 等。组件自己管理 type、checked、value、disabled、name 与 onchange。 | HTMLInputAttributes | — |
RadioGroup 的 API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 选中的值;未设置或为 null 表示未选中。使用 bind:value;单独写 value={x} 只是初始值。不匹配任何单选框的值不会选中任何项,但会被保留。 | string | number | boolean | null | — |
| options | 每个条目渲染一个单选框。字符串、数字或布尔值既是值也是标签;对象为 RadioOption。值必须唯一。有 options 时忽略 children。 | ReadonlyArray<string | number | boolean | RadioOption> | [] |
| optionType | options 渲染的类型:普通单选框或按钮。子元素保持其自身类型。 | 'default' | 'button' | 'default' |
| buttonStyle | 选中按钮的样式:彩色描边或实心填充。对组内每个 RadioButton 生效。 | 'outline' | 'solid' | 'outline' |
| size | 按钮高度(24、32 与 40 px);普通单选框忽略它。middle 与 default 是 medium 的别名。 | 'large' | 'medium' | 'middle' | 'small' | 'default' | 'medium' |
| orientation | 条目的排列方向。设置后优先于 vertical;两者都未设置时为水平排列。 | 'horizontal' | 'vertical' | — |
| vertical | orientation="vertical" 的简写。 | boolean | false |
| block | 组撑满父元素宽度,各条目平分宽度。 | boolean | false |
| disabled | 禁用组内全部单选框;条目自己的 disabled 只禁用该条目。 | boolean | false |
| name | 所有 input 的原生 name,优先于条目自己的 name。未设置时组会生成唯一名称,且不会随表单提交。 | string | — |
| children | 包含 Radio 与 RadioButton 的自定义标记;每个都需要 value。options 非空时忽略。 | Snippet | — |
| onchange | 用户每次选择时调用一次,参数为新值与被选中 input 的原生 change 事件。父组件修改时不会调用。 | (value: string | number | boolean, event: Event) => void | — |
| classNames | 额外类名:root 是组本身;item、itemIcon、itemLabel 作用于组内每个单选框的对应部分(itemIcon 对按钮无效)。 | Partial<Record<'root' | 'item' | 'itemIcon' | 'itemLabel', string>> | — |
| styles | 同样各部分的内联样式;条目自己的 styles 写在组的样式之后。 | Partial<Record<'root' | 'item' | 'itemIcon' | 'itemLabel', Record<string, string | number>>> | — |
| class | 组根元素的额外类名。 | string | — |
| style | 组根元素的内联样式;styles.root 写在其后。 | string | — |
| native attributes | 其余属性都会传给根 <div>:id、aria-label、aria-labelledby、aria-describedby、aria-required、data-* 与事件处理函数。role="radiogroup" 由组件管理。 | HTMLAttributes<HTMLDivElement> | — |
RadioOption:options 中的条目
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| label | 标签内容:纯文本,或用于图标与标记的 snippet。 | string | Snippet | — |
| value | 选项的值;在组内必须唯一。 | string | number | boolean | — |
| disabled | 仅禁用该选项。 | boolean | — |
| title | 该选项根 label 的原生 title。 | string | — |
| id | 该选项 input 的原生 id,外部的 <label for> 或 aria-labelledby 可据此引用。 | string | — |
| required | 该选项 input 的原生 required;只要有一个必填,整个原生分组都必填。 | boolean | — |
| class | 该选项根元素的额外类名。 | string | — |
| style | 该选项根元素的内联样式。 | string | — |
| onchange | 用户选中该选项时调用,早于组的 onchange。 | (checked: boolean, event: Event) => void | — |
方法
| 方法 | 说明 | 类型 |
|---|---|---|
| focus | 把焦点移到单选框。Radio 与 RadioButton 都可通过 bind:this 调用。 | (options?: FocusOptions) => void |
| blur | 让单选框失去焦点。 | () => void |
设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-radio-bg | 圆点标记的背景色。 | #ffffff |
| --ads-radio-border | 未选中标记的边框色。 | #d9d9d9 |
| --ads-radio-border-hover | 未选中标记悬停时的边框色。 | #1677ff |
| --ads-radio-border-width | 圆点标记的边框宽度。 | 1px |
| --ads-radio-button-bg | 按钮的背景色。 | #ffffff |
| --ads-radio-button-border | 未选中按钮的边框色。 | #d9d9d9 |
| --ads-radio-button-checked-bg | 选中的描边按钮的背景色。 | #ffffff |
| --ads-radio-button-checked-disabled-bg | 禁用且选中按钮的背景色。 | rgba(0, 0, 0, 0.15) |
| --ads-radio-button-checked-disabled-color | 禁用且选中按钮的文字颜色。 | rgba(0, 0, 0, 0.25) |
| --ads-radio-button-color | 未选中按钮的文字颜色。 | rgba(0, 0, 0, 0.88) |
| --ads-radio-button-disabled-bg | 禁用按钮的背景色。 | rgba(0, 0, 0, 0.04) |
| --ads-radio-button-disabled-border | 禁用按钮的边框色。 | #d9d9d9 |
| --ads-radio-button-disabled-color | 禁用按钮的文字颜色。 | rgba(0, 0, 0, 0.25) |
| --ads-radio-button-padding-inline | 中号与大号按钮的水平内边距。 | 15px |
| --ads-radio-button-padding-inline-sm | 小号按钮的水平内边距。 | 7px |
| --ads-radio-checked-bg | 选中标记的填充与边框色。 | #1677ff |
| --ads-radio-checked-bg-hover | 选中标记悬停时的填充色。 | #4096ff |
| --ads-radio-disabled-bg | 禁用标记的背景色。 | rgba(0, 0, 0, 0.04) |
| --ads-radio-disabled-border | 禁用标记的边框色。 | #d9d9d9 |
| --ads-radio-disabled-dot-color | 禁用单选框中圆点的颜色。 | rgba(0, 0, 0, 0.25) |
| --ads-radio-dot-color | 圆点的颜色。 | #ffffff |
| --ads-radio-dot-size | 选中单选框中圆点的直径。 | 6px |
| --ads-radio-font-size | 标签与组的字号。 | 14px |
| --ads-radio-group-gap | 组内单选框之间的间距。 | 8px |
| --ads-radio-label-padding | 标签两侧的内边距。 | 8px |
| --ads-radio-line-height | 标签的行高。 | 1.5714 |
| --ads-radio-size | 圆点标记的直径。 | 16px |
无障碍
- 单选框是
<label>中的原生<input type="radio">。同一组的 input 共用一个名称(未设置时自动生成),因此浏览器自带单选组的键盘行为:只占一个 Tab 停靠点(落在选中项上),方向键移动并选中(到达两端时 Chromium 与 Firefox 会循环,WebKit 则停住),空格键选中。 - 组的角色是
role="radiogroup"。请用aria-label或aria-labelledby(或带<legend>的<fieldset>)为它命名;单选框可由自身文字、aria-label、aria-labelledby或外部<label for>命名。 - 用户只能选中单选框,不能取消选中。没有选中值的组由第一个单选框承担 Tab 停靠点(部分浏览器在反向 Tab 时会停在最后一个)。
- 默认颜色与 Ant Design 一致。使用
tokens-accessible.css以及在暗色主题下,边框、圆点、按钮边框与焦点环在所有状态下的对比度不低于 3:1,按钮文字不低于 4.5:1。 - 在 Windows 高对比度等强制颜色模式下,选中的单选框保留圆点,选中的按钮使用选中色,因此各状态仍可区分;禁用控件使用禁用色。
- 尚未验证:NVDA、JAWS、VoiceOver 与真实触屏设备。关于 Ant Design 单选框的公开报告称,当 label 同时包裹控件与文字时,VoiceOver 可能停留两次;本组件结构相同。Orca 由自动化测试覆盖。