# Blazor Checkbox, Radio Group, Switch and Rating

URL: https://flexcoreui.com/demo/checks  
Section: Components  
Controls: CheckBoxControl, RadioGroupControl, SwitchControl, RatingControl  
Keywords: checkbox, radio button, radio group, switch, toggle switch, rating

Blazor checkbox, radio group, switch and star rating: check off a list with a select-all box, pick one of a few, switch an option on or give stars.

## Documentation

### Check box

| Name | Type | Default | Description |
|---|---|---|---|
| Checked | bool | false | State. Two-way with @bind-Checked. |
| Indeterminate | bool | false | Draws a dash and sets aria-checked="mixed". Visual only; Checked still decides what a click toggles to. |
| Label | string? | null | Text after the box, inside the same <label>. |
| Disabled | bool | false | Disables the input and dims the label. |
| CssClass | string? | null | Appended to the label; fx-checkbox-theme-blue picks the native look. |
| TabIndex | int? | null | Tab order of the input. |
| StopClickPropagation / StopMouseDownPropagation / StopKeyDownPropagation | bool | false | Stop those events reaching ancestors. |
| PreventKeyDownDefault | bool | false | Cancels the browser's default for key presses on the input. |
| (no base class) |  |  | This control does not derive from FlexControlBase: no Enabled, Visible, Style or unmatched attributes. |

### Radio group

| Name | Type | Default | Description |
|---|---|---|---|
| Items | IReadOnlyList<RadioGroupItem<TValue>> | empty | One radio per item, in order. |
| Value | TValue? | default | Selected item's value. Two-way with @bind-Value; picking a disabled item is ignored, but a bound value equal to a disabled item's value still shows that radio checked. |
| ValueExpression | Expression<Func<TValue>>? | null | Set by @bind-Value; marks the field changed on the cascaded EditContext. |
| AriaLabel | string? | null | Accessible name of the group. |
| Vertical | bool | false | Stacks the radios in a column. |
| Spacing | string | "12px" | Flex gap between radios. |
| Enabled (base) | bool | true | false disables every radio. |
| Visible (base) | bool | true | false hides the group with display:none. |
| CssClass / Style (base) | string? | null | On the group element; Style is appended after the layout styles. |
| Id / Title / Tag / Data / Parent (base) |  | null | Accepted but not rendered. A written id binds to Id, so it does not reach the group. |
| (unmatched attributes) | Dictionary<string,object> |  | Written on the group <div>. |

### Radio item

| Name | Type | Default | Description |
|---|---|---|---|
| RadioGroupItem.Value / Text / Disabled | TValue / string / bool | — / — / false | Positional record: new(value, text, disabled). A disabled item renders but cannot be chosen. |
| RadioControl (standalone) |  |  | Value, SelectedValue (two-way), Name, Label, Disabled, TabIndex, CssClass, ChildContent. Give every button in a group the same Name. |

### Switch

| Name | Type | Default | Description |
|---|---|---|---|
| Checked | bool | false | State. Two-way with @bind-Checked. |
| Label | string? | null | Text after the track. Takes precedence over child content. |
| ChildContent | RenderFragment? | null | Markup label, used when Label is empty. |
| Disabled | bool | false | Disables the input and dims the control. The base Enabled is not read. |
| CssClass / Style (base) | string? | null | On the <label> host. The base Visible is not applied. |
| Id / Title / Tag / Data / Parent (base) |  | null | Accepted but not rendered. A written id binds to Id, so it does not reach the input. |
| (unmatched attributes) | Dictionary<string,object> |  | Written on the hidden <input>, such as aria-label, name and data-*. |

### Rating

| Name | Type | Default | Description |
|---|---|---|---|
| Value | int | 0 | Filled stars, 0 to MaxStars. Two-way with @bind-Value. |
| MaxStars | int | 5 | Number of stars. |
| ReadOnly | bool | false | Disables the star buttons without dimming them. |
| Disabled | bool | false | Disables and dims; also blocks pointer events. The base Enabled is not read. |
| CssClass / Style (base) | string? | null | On the fx-rating root. The base Visible is not applied. |
| Id / Title / Tag / Data / Parent (base) |  | null | Accepted but not rendered. A written id binds to Id, so it does not reach the root. |
| (unmatched attributes) | Dictionary<string,object> |  | Written on the root <div>. |

### Events

| Name | Type | Description |
|---|---|---|
| CheckBoxControl.CheckedChanged | bool | On the input's change event, with the new state. |
| CheckBoxControl.OnMouseDown / OnFocus / OnKeyDown | MouseEventArgs / FocusEventArgs / KeyboardEventArgs | Raw events from the input, for hosts such as grid cells. |
| RadioGroupControl.ValueChanged | TValue? | When an enabled radio is selected, by click or arrow key. |
| SwitchControl.CheckedChanged | bool | On the hidden input's change event, with the new state. |
| RatingControl.ValueChanged | int | When a star is clicked, or pressed with Enter or Space; again if the current star is chosen again. The stars cannot clear the rating to 0. |

### Methods

| Name | Type | Description |
|---|---|---|
| CheckBoxControl.FocusAsync() | Task | Focuses the input without scrolling. |

### Css

| Name | Description |
|---|---|
| fx-checkbox · .fx-checkbox-indeterminate · .fx-checkbox-theme-blue · fx-checkbox-label | Label host; the classic 13px square box, the dash state, the native blue variant, and the text. |
| fx-radio-group · fx-radio · fx-radio-label | Flex group; each radio label with a native 16px button; focus draws a dotted outline on the text. |
| fx-switch-host · .fx-switch-disabled · fx-switch-input · fx-switch-track · fx-switch-thumb · fx-switch-label | Label host, hidden input, 38×22px track (blue when checked), 18px thumb that slides 16px, and the text. |
| fx-rating · .fx-rating-readonly · .fx-rating-disabled · fx-rating-item · .fx-rating-filled / .fx-rating-empty | Row of 20px star buttons; filled stars are amber, empty ones gray; hover scales a star by 15%. |

### Keys

| Name | Description |
|---|---|
| Space (checkbox, switch) | Toggles. |
| Tab (radio group) | Enters the group on the selected radio, or the first enabled one. |
| Arrow keys (radio group) | Move the selection to the previous or next enabled radio; the browser does this because the radios share a name. |
| Tab / Enter / Space (rating) | Tab moves through the stars one by one; Enter or Space selects the focused star. |

## Examples

### Checks

```razor
@using Fx.ControlKit

<div class="ck">
    <div class="stack">
        <CheckBoxControl Checked="@AllChecked" Indeterminate="@SomeChecked" CheckedChanged="SetAll" Label="Include in Neville Longbutton's benefits" />
        <div class="stack indent">
            <CheckBoxControl @bind-Checked="_dental" Label="Dental plan, $32" />
            <CheckBoxControl @bind-Checked="_vision" Label="Vision plan, $12" />
            <CheckBoxControl @bind-Checked="_gym" Label="Gym membership, $40" />
            <CheckBoxControl Checked="true" Disabled="true" Label="Health plan, $85 (always included)" />
        </div>
    </div>
    <p class="result" role="status">Monthly deduction: <strong>@Total.ToString("C0")</strong></p>
</div>
```

```csharp
// Checkboxes
private bool _dental = true, _vision = true, _gym;
private bool AllChecked => _dental && _vision && _gym;
private bool SomeChecked => !AllChecked && (_dental || _vision || _gym);
private void SetAll(bool value) => _dental = _vision = _gym = value;
private decimal Total => 85 + (_dental ? 32 : 0) + (_vision ? 12 : 0) + (_gym ? 40 : 0);
```

### Radio

```razor
@using Fx.ControlKit

<div class="ck">
    <div class="demo-row top">
        <div class="stack">
            <span class="field-label">Work pattern</span>
            <RadioGroupControl TValue="string" Items="_patternItems" @bind-Value="_pattern" Vertical="true" AriaLabel="Work pattern" />
        </div>
        <div class="stack">
            <span class="field-label">Options</span>
            <SwitchControl @bind-Checked="_parking" Label="Parking pass" />
            <SwitchControl @bind-Checked="_standingDesk" Label="Standing desk" />
            <SwitchControl Checked="true" Disabled="true" Label="Laptop (included)" />
        </div>
    </div>
    <p class="result" role="status">@Setup</p>
</div>
```

```csharp
// Radio group and switches
private readonly RadioGroupItem<string>[] _patternItems =
{
    new("office", "In the office"), new("hybrid", "Hybrid"), new("remote", "Remote, not offered", true),
};
private string? _pattern = "hybrid";
private bool _parking = true, _standingDesk;
private string Setup => $"{_patternItems.First(i => i.Value == _pattern).Text}; parking pass {(_parking ? "requested" : "not requested")}; standing desk {(_standingDesk ? "requested" : "not requested")}; laptop included.";
```

### Rating

```razor
@using Fx.ControlKit

<div class="ck">
    <div class="demo-row">
        <span class="field-label">Your rating of onboarding week</span>
        <RatingControl Value="@_stars" ValueChanged="Rate" />
        <span class="result inline" role="status">@Stars</span>
    </div>
    <div class="demo-row mt">
        <RatingControl Value="@Rounded" ReadOnly="true" />
        <span class="field-label">Average @Average.ToString("0.0") of @_ratings new hire ratings</span>
    </div>
</div>
```

```csharp
// Rating: each click adds one rating
private int _stars;                                    // 0 until the visitor rates
private int _ratings = 8, _sum = 28;                   // 8 ratings averaging 3.5 to start
private double Average => (double)_sum / _ratings;
private int Rounded => (int)Math.Round(Math.Round(Average, 1), MidpointRounding.AwayFromZero);   // rounds the average as shown
private void Rate(int value)
{
    _stars = value;
    _ratings++;
    _sum += value;
}
private string Stars => _stars switch
{
    0 => "Not rated",
    1 => "1 star, poor",
    2 => "2 stars, fair",
    3 => "3 stars, good",
    4 => "4 stars, very good",
    _ => "5 stars, excellent",
};
```

