Skip to content
Form Checkbox

Checkbox

A native checkbox with a label and a description.

On this page

Examples

Checkbox

A short email when something ships.

Updates: no

Razor
<div class="flex flex-col gap-3">
    <KlyCheckbox @bind-Value="_updates" Label="Product updates" Description="A short email when something ships." />
    <KlyCheckbox Value="true" Label="Success color" Color="KlyColor.Success" />
    <KlyCheckbox Disabled Label="Disabled" />
    <p class="text-sm text-(--klyroo-text-muted)">Updates: @(_updates ? "yes" : "no")</p>
</div>

@code {
    private bool _updates;
}

Indeterminate

A select-all box in the mixed state; the parent owns it.

Selected: 1 of 3

Indeterminate stays true: every render puts the mixed state back.

Razor
<div class="flex flex-col gap-4">
    <div class="flex flex-col gap-2">
        <KlyCheckbox Value="AllSelected" ValueChanged="SelectAll" Indeterminate="SomeSelected" Label="Select all" data-testid="select-all" />
        <div class="flex flex-col gap-2 pl-6">
            @for (var i = 0; i < _items.Length; i++)
            {
                var index = i;
                <KlyCheckbox Value="_selected[index]" ValueChanged="(bool v) => Select(index, v)" Label="@_items[index]" />
            }
        </div>
        <p class="text-sm text-(--klyroo-text-muted)" data-testid="select-all-state">Selected: @_selected.Count(s => s) of @_selected.Length</p>
    </div>
    <div class="flex flex-col gap-2">
        <KlyCheckbox @bind-Value="_pinned" Indeterminate Label="Always mixed" Description="Indeterminate stays true: every render puts the mixed state back." data-testid="mixed" />
        <KlyButton Variant="ButtonVariant.Outline" Label="@($"Render again ({_renders})")" OnClick="() => _renders++" data-testid="rerender" />
    </div>
</div>

@code {
    private static readonly string[] _items = ["Email", "SMS", "Push"];
    private readonly bool[] _selected = [true, false, false];
    private bool _pinned;
    private int _renders;

    private bool AllSelected => _selected.All(s => s);

    // The parent owns the mixed state: it derives it from the items, so a click on the box (which selects everything)
    // leaves nothing mixed.
    private bool SomeSelected => _selected.Any(s => s) && !AllSelected;

    private void SelectAll(bool value) => Array.Fill(_selected, value);

    private void Select(int index, bool value) => _selected[index] = value;
}

API

KlyCheckbox

Checkbox with a label and a description, bindable to a bool and usable in an EditForm . It supports the indeterminate state.

Parameters

Name Type Default Description
Class string Classes added to the root element, merged last so they win over the theme.
Color KlyColor "primary" Semantic color of the control: a built-in KlyColor or the name of a color you defined. When not set, Color applies, else primary. Values: primary, secondary, success, info, warning, error, neutral.
Description string Text under the label of the checkbox.
Disabled bool false Disables the control: it cannot be focused or changed and is not submitted.
Id string The id of the native control. Defaults to the one of the enclosing KlyFormField , else a generated one.
Indeterminate bool false Shows the mixed state (a minus), for a "select all" box when only some items are selected. indeterminate is a DOM property that HTML cannot render, so the component renders data-indeterminate and the browser script of Klyroo sets the property (also in static rendering, and again after an enhanced navigation). The user's click clears the mixed state and toggles the box, as in any browser. The parameter is controlled: the parent decides what the box shows next, normally by setting it to false in its ValueChanged handler. While it stays true , every render of the parent puts the mixed state back. Static rendering has no .NET to render again, so a click there clears it until the page is rendered by the server again.
Label string Text next to the checkbox. A KlyFormField label also names it.
Name string Form field name outside an EditForm . Inside one, the name comes from the bound expression.
Required bool false Marks the control as required, for the browser and for assistive technology.
Size KlySize? Defaults to the size of the enclosing KlyFormField , or medium. Values: Xs, Sm, Md, Lg, Xl.
Ui CheckboxUi Per-instance class overrides of the slots of the checkbox, see CheckboxUi . They are merged after the theme and the Class parameter wins on the root slot.
Value (@bind-Value) bool false The value of the control. Bind it with @bind-Value .
ValueExpression Expression<Func<bool>> The expression of the bound field. The compiler sets it with @bind-Value , and validation uses it to find the field.

Ui slots

Ui takes a CheckboxUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlyCheckbox , after the theme and the app configuration.
Container Classes merged into the container slot of KlyCheckbox , after the theme and the app configuration.
Base Classes merged into the base slot of KlyCheckbox , after the theme and the app configuration.
Icon Classes merged into the icon slot of KlyCheckbox , after the theme and the app configuration.
IndeterminateIcon Classes merged into the indeterminateIcon slot of KlyCheckbox , after the theme and the app configuration.
Wrapper Classes merged into the wrapper slot of KlyCheckbox , after the theme and the app configuration.
Label Classes merged into the label slot of KlyCheckbox , after the theme and the app configuration.
Description Classes merged into the description slot of KlyCheckbox , after the theme and the app configuration.