Skip to content

Radio Group

A set of checkable buttons where no more than one can be checked at a time. Composed of RadioGroup and RadioGroupItem.

When to use

  • Selecting a single option from a mutually exclusive set.
  • Choosing between a small number of alternatives.

API

RadioGroup::new()

A container for radio items. Add items with .child(...).

MethodTypeDescription
.child(child)impl IntoElementAppend a RadioGroupItem

RadioGroupItem::new(id)

An individual radio button.

MethodTypeDefaultDescription
.label(text)impl Into<SharedString>Text label
.checked(checked)boolfalseWhether this item is selected
.focus_handle(&handle)&FocusHandleFocus handle (enables keyboard activation with Space/Enter)
.on_click(fn)Fn(&ClickEvent, &mut Window, &mut App)Click handler

Usage

rust
use gpui::{Component, div, prelude::*};
use components::{RadioGroup, RadioGroupItem};

div().child(Component::new(
    RadioGroup::new()
        .child(Component::new(
            RadioGroupItem::new("light-mode")
                .label("Light")
                .checked(this.theme == "light")
                .on_click(cx.listener(|this, _, _, cx| {
                    this.theme = "light".to_string();
                    cx.notify();
                })),
        )),
))

Copy the source

crates/components/src/radio_group.rs — self-contained, stateless, and free to modify.

Released under the MIT License.