日本語

Radio group

A group of radio buttons that sets a signal to the chosen option's value.

Result

radio_group.rs
use hypersig::html::{div, p};
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals, if_else};
use hypersig_ui::radio_group;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Copy, PartialEq)]
pub enum Plan {
    Hobby,
    Pro,
    Team,
}

#[derive(Signals, Serialize, Deserialize)]
pub struct Billing {
    plan: Plan,
}

pub fn page() -> Page<Billing> {
    let plan = Billing::plan();
    Page::new(
        "Billing",
        Billing { plan: Plan::Pro },
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(radio_group(
                "Plan",
                plan,
                [
                    (
                        Plan::Hobby,
                        "Hobby",
                    ),
                    (
                        Plan::Pro,
                        "Pro",
                    ),
                    (
                        Plan::Team,
                        "Team",
                    ),
                ],
            ))
            .child(p().bind_text(if_else(
                plan.equals(&Plan::Hobby),
                "Free for personal projects.",
                "Billed monthly.",
            ))),
    )
}

Accessibility

The radios share a name derived from the signal's path, so the browser gives the group one Tab stop and moves between its radios with the arrow keys. The <fieldset> and <legend> name the group for a screen reader.

KeyWhat it does
TabMoves focus into the group, to the checked radio.
Arrow keysMove to the next or previous radio and check it.

API

ItemSummary
radio_groupA group of radio buttons with title in its <legend>, one per option, that sets selected to the option's value.
radio_group_in_formA group of radio buttons with title in its <legend>, bound to field of form.