v0.1.0-alpha
官网Home
components/radio
操作与选择Actions · 04 / 64

Radio

ui.widgets.RadioGroup

从互斥选项中选择唯一值。Pick exactly one value from mutually exclusive options.

BEHAVIOR CONTRACT

组内选择、value、name、键盘移动与焦点顺序由组件统一管理。Group selection, value, name, arrow-key movement and focus order are managed by the group.

RECORDED INTERACTION

Harness 选择 Option Y,组内其余项自动取消。The harness selects Option Y; the rest of the group deselects.

examples/storybook/stories.zig:433
pub fn buildRadio(scope: *ui.Scope, cx: *ui.Cx) anyerror!*ui.Node {
    const a = cx.allocator;
    const c = try col(cx, 16);

    const status = try ui.text(cx, "selected → (none)", .{ .font_size = 13, .color = light.color.fg_secondary });
    status.meta.ownership.meta.test_id = "story.radio.status";
    try c.appendChild(a, status);
    const radio_story = try cx.bindState(RadioStory, .{ .status = status });

    try c.appendChild(a, try label(cx, "Radio"));
    try c.appendChild(a, try W.Radio(.{ .label_text = "Selected", .checked = true, .value = "a", .name = "g" }).mount(scope, cx));
    try c.appendChild(a, try W.Radio(.{ .label_text = "Unselected", .checked = false, .value = "b", .name = "g" }).mount(scope, cx));

    try c.appendChild(a, try label(cx, "RadioGroup"));
    const opts = [_]ui.widgets.checkbox.RadioOption{
        .{ .value = "x", .label_text = "Option X" },
        .{ .value = "y", .label_text = "Option Y" },
        .{ .value = "z", .label_text = "Option Z" },
    };
    const group = try W.RadioGroup(.{
        .options = &opts,
        .value = "x",
        .on_change = ui.Cx.strHandlerFrom(RadioStory, radio_story, RadioStory.onChange),
    }).mount(scope, cx);
    group.meta.ownership.meta.test_id = "story.radio.group";
    try c.appendChild(a, group);
    return c;
}
在 GitHub 查看源码View source on GitHubexamples/storybook/stories.zig:433

这是 Storybook 中该 story 的原样代码(zenit 5f9add5+wip 2026-09-30)。其中 W 即 ui.widgets,col / row / label 是 Storybook 内的布局小工具。The story's verbatim source from the Storybook (zenit 5f9add5+wip 2026-09-30). W is ui.widgets; col / row / label are small Storybook layout helpers.

zenit · 双授权Dual-licensed开源项目可按 GPL-3.0-only 免费使用;闭源或商业产品需要商业授权。Free for open-source projects under GPL-3.0-only; closed-source or commercial products need a commercial license.可联系作者:Contact the author: zongyi.xzy#gmail.com(# 换成 @) (replace # with @)zenit 5f9add5+wip 2026-09-30