# RadioGroup A group of radio buttons sharing the same `name` attribute. Supports horizontal or vertical layout. One option can be pre-selected. --- ## HTML structure ``` div.flex.flex-col.gap-1.5 label.text-sm.font-medium ← group label (omitted when empty) {label} div.flex.{direction}.gap-3 ← flex-col or flex-row label.flex.items-center.gap-2.cursor-pointer ← one per option input[type=radio, name, value, class, $$Checked$$] span.text-sm {option label} ``` --- ## CSS mechanics | Class | Effect | |---|---| | `accent-primary` | Radio circle color follows `--color-primary` CSS variable | | `h-4 w-4` | 16×16 radio circle | | `cursor-pointer` | Pointer cursor on the label | | `flex-col` (default) | Stacks options vertically | | `flex-row` | Places options side by side | --- ## Constructor signature ```csharp public RadioGroup( string name, IEnumerable<(string Value, string Label, bool Selected)> options, string label = "", string direction = "flex-col") ``` | Parameter | Description | |---|---| | `name` | Shared `name` attribute for all radio inputs in the group | | `options` | List of `(Value, Label, Selected)` tuples | | `label` | Optional visible group heading above the options | | `direction` | `"flex-col"` (vertical, default) or `"flex-row"` (horizontal) | --- ## Usage examples ### Vertical list ```csharp new RadioGroup( name: "plan", label: "Select a plan", options: new[] { ("free", "Free", true), ("pro", "Pro", false), ("teams", "Teams", false), }) ``` ### Horizontal inline options ```csharp new RadioGroup( name: "size", label: "Size", direction: "flex-row", options: new[] { ("sm", "S", false), ("md", "M", true), ("lg", "L", false), ("xl", "XL", false), }) ``` ### Reading in a form handler ```csharp public record Command([property: FromForm] string Plan); // command.Plan == "free" | "pro" | "teams" ``` ### Dynamic options from database ```csharp var options = categories .Select((cat, i) => (cat.Slug, cat.Name, i == 0)) .ToArray(); new RadioGroup(name: "category", label: "Category", options: options) ``` --- ## Tips and tricks - Only one option in the group can have `Selected = true`; if multiple are marked selected the last one wins (standard HTML behavior). - An unselected `RadioGroup` submits nothing — validate server-side that the field is present. - For a "none of the above" option, add a tuple with the intended empty value: `("", "None", false)`. - To conditionally show additional fields when a radio is selected, add an `htmx` attribute via inline HTML after the component — or use a custom slot that includes both the radio and a reveal div. - To conditionally show additional fields when a radio is selected, add an `htmx` attribute via inline HTML after the component — or use a custom slot that includes both the radio and a reveal div. --- ## Complete page example **`Templates/SurveyPage.htmx`** ```html
Help us improve BeepBoop.
$$SuccessAlert$$