单选列表 · RadioList
单选列表用于表单输入场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。
安装与前置条件
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0
需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。
示例与使用方式
- 单选列表 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
- 单选列表 · 综合展示 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 单选列表 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 单选列表 · 基础用法 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 单选列表 · 交互示例 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 单选列表 · 交互示例 6:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 单选列表 · 输入校验 7:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
API 与上游详细参考
优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。
Radio List
A group of options where only one can be selected at a time. All options are visible at once, making it easy to compare choices. Use it when users need to pick one option from a small set.
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| label * | string | Label text for the radio group (always rendered for accessibility). | |
| value * | string | The currently selected value. | |
| onChange * | (value: string) => void | Callback fired when the selected value changes. | |
| children * | ReactNode | RadioListItem elements. | |
| isLabelHidden | boolean | false | Whether to visually hide the label. |
| description | string | Description text displayed below the label. | |
| orientation | 'vertical' | 'horizontal' | 'vertical' | Layout direction of the radio items. |
| isDisabled | boolean | false | Whether all radio items are disabled. |
| htmlName | string | The HTML name attribute shared by the radio inputs, useful for form submissions. When omitted, a unique internal name still groups the radios. | |
| disabledMessage | string | Explains why the group is disabled. Applies to the whole-group disabled state (isDisabled), not per item. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the radios focusable via aria-disabled (selection stays blocked). Use this instead of wrapping a disabled RadioList in Tooltip. Disabled controls swallow the hover events an external Tooltip needs. | |
| isRequired | boolean | false | Whether the radio group is required. |
| isOptional | boolean | false | Whether the field is optional (mutually exclusive with isRequired). |
| status | {type: 'warning' | 'error' | 'success', message?: string} | Status indicator ({ type, message }). | |
| size | 'sm' | 'md' | 'md' | Size of the radio controls. |
| labelTooltip | string | Tooltip text for an info icon next to the label. | |
| width | SizeValue | Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned. | |
| containerXstyle | StyleXStyles | StyleX styles for the field container (label, control, and status together). No effect inside InputGroup (style the InputGroup instead) or in a horizontal-labels FormLayout. Root xstyle, className, and style are not accepted. | |
| containerClassName | string | Class name for the field container. Same targeting as containerXstyle. | |
| containerStyle | CSSProperties | Inline styles for the field container. Same targeting as containerXstyle. | |
| controlXstyle | StyleXStyles | StyleX styles for the visual control box: the element that paints the border and background and holds icons and end controls. Never the native input. | |
| controlClassName | string | Class name for the visual control box. | |
| controlStyle | CSSProperties | Inline styles for the visual control box. |
使用建议
- Keep the number of options small: typically 2 to 7 choices.
- Use clear, concise labels that differentiate each option at a glance.
- Pre-select a default option when there's a sensible default; don't leave the group empty unless the choice is optional.
- 避免:Use when multiple selections are needed; use CheckboxList instead.
- 避免:Use for long lists; use Selector for better discoverability.
- 避免:Use horizontal layout with more than 4 options; it wraps awkwardly.
- 避免:Wrap a disabled RadioList in Tooltip to explain why it is disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead.
主题 API
{
"targets": [
{
"className": "astryx-radio-list",
"visualProps": [
"orientation",
"size"
]
},
{
"className": "astryx-radio-list-item",
"visualProps": [
"size"
],
"states": [
"selected",
"disabled"
]
},
{
"className": "astryx-radio-indicator",
"visualProps": [
"size"
],
"states": [
"checked",
"disabled"
]
},
{
"className": "astryx-radio-indicator-dot",
"visualProps": [
"size"
]
}
]
}
RadioListItem
无障碍
- Radio circle: The circle edge (unselected) and fill (selected) must have at least 3:1 contrast with the surface behind them. For Hover and Pointer down, measure the final colors after the tint and the pressed overlay are applied. — 3:1
组件结构
- Header: Optional heading above the radio list.
- Children: The radio list items rendered as selectable options.
- Label/Value: The text label and associated value for each radio item.
Radio List Item
单个单选选项,包含标签、描述和内容插槽。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| label * | ReactNode | 单选选项的主标签。富内容标签可包含链接或按钮,它们保留自身行为且不会选中该选项。标签文本用于命名单选框;仅当可见文本缺失时使用 aria-label,否则覆盖值必须保留全部可见文字。 | |
| aria-label | string | 单选框的纯文本无障碍名称,应用于控件本身而非整行。它会覆盖由标签推导出的名称。仅当富标签没有可见文本时才完全替代;否则必须保留全部可见文字。 | |
| value * | string | 此单选选项的值。 | |
| description | ReactNode | 显示在标签下方的次要内容。链接和按钮保留自身的点击行为——整行仅将非交互区域的点击委派给单选框。 | |
| isDisabled | boolean | false | 是否禁用此单个单选选项。 |
| startContent | ReactNode | 在单选圆圈前渲染的内容。 | |
| endContent | ReactNode | 在标签后渲染的内容。 |