Listbox
An always visible list of options for one or several values, with typeahead.
Examples
Single and multiple
Apple
Banana
Cherry
Grape
Fruit: none
Cheese
Ham
Olives
Pepper
Razor
<div class="flex w-full flex-col gap-6 sm:flex-row sm:gap-12">
<div class="w-full max-w-xs">
<KlyFormField Label="Fruit">
<KlyListbox Items="Fruits" @bind-Value="_fruit" />
</KlyFormField>
<p class="mt-2 text-sm text-(--klyroo-text-muted)" data-testid="listbox-state">Fruit: @(_fruit.Count > 0 ? _fruit[0] : "none")</p>
</div>
<div class="w-full max-w-xs">
<KlyFormField Label="Toppings">
<KlyListbox Items="Toppings" Multiple @bind-Value="_toppings" />
</KlyFormField>
</div>
</div>
@code {
private IReadOnlyList<string> _fruit = [];
private IReadOnlyList<string> _toppings = ["cheese"];
private static readonly KlySelectItem[] Fruits = [new("apple", "Apple"), new("banana", "Banana"), new("cherry", "Cherry"), new("grape", "Grape")];
private static readonly KlySelectItem[] Toppings = [new("cheese", "Cheese"), new("ham", "Ham"), new("olives", "Olives"), new("pepper", "Pepper")];
}Not bound
Without a binding the browser keeps the selection.
Red
Green
Blue
Razor
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Color">
<KlyListbox Items="Colors" Name="color" Value="@(["green"])" />
</KlyFormField>
</div>
@code {
private static readonly KlySelectItem[] Colors = [new("red", "Red"), new("green", "Green"), new("blue", "Blue")];
}Groups, icons and counts
Group lists the items under headings; the arrow keys and the typeahead skip the headings.
Europe
Paris,
2
London,
1
Asia
Tokyo,
3
Pacific
Sydney,
1
Zone: none
Razor
<div class="w-full max-w-xs">
<KlyFormField Label="Time zone">
<KlyListbox Items="Zones" @bind-Value="_zone" />
</KlyFormField>
<p class="mt-2 text-sm text-(--klyroo-text-muted)" data-testid="zone-state">Zone: @(_zone.Count > 0 ? _zone[0] : "none")</p>
</div>
@code {
private IReadOnlyList<string> _zone = [];
private static readonly KlySelectItem[] Zones =
[
new("paris", "Paris") { Group = "Europe", Icon = "clock", Count = 2 },
new("london", "London") { Group = "Europe", Icon = "clock", Count = 1 },
new("tokyo", "Tokyo") { Group = "Asia", Icon = "clock", Count = 3 },
new("sydney", "Sydney") { Group = "Pacific", Icon = "clock", Count = 1 },
];
}API
KlyListbox<TValue>
Always visible list of options with single or multiple selection, bound to a list of strings, numbers, enums, Guid s or any value that identifies an option.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
AriaLabel |
string |
Accessible name when no KlyFormField label is around. | |
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. |
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. | |
Items (required) |
IReadOnlyList |
The options of the list, as KlySelectItem`1 ( KlySelectItem for strings). | |
Multiple |
bool |
false |
Several values can be selected, each posting under Name . |
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 |
ListboxUi |
Per-instance class overrides of the slots of the listbox, see ListboxUi . They are merged after the theme and the Class parameter wins on the root slot. | |
Value (@bind-Value) |
IReadOnlyList |
The value of the control. Bind it with @bind-Value . | |
ValueExpression |
Expression |
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 ListboxUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyListbox`1 , after the theme and the app configuration. |
Base |
Classes merged into the base slot of KlyListbox`1 , after the theme and the app configuration. |
Item |
Classes merged into the item slot of KlyListbox`1 , after the theme and the app configuration. |
ItemLabel |
Classes merged into the itemLabel slot of KlyListbox`1 , after the theme and the app configuration. |
ItemIcon |
Classes merged into the itemIcon slot of KlyListbox`1 , after the theme and the app configuration. |
ItemLeadingIcon |
Classes merged into the itemLeadingIcon slot of KlyListbox`1 , after the theme and the app configuration. |
ItemCount |
Classes merged into the itemCount slot of KlyListbox`1 , after the theme and the app configuration. |
Group |
Classes merged into the group slot of KlyListbox`1 , after the theme and the app configuration. |
GroupLabel |
Classes merged into the groupLabel slot of KlyListbox`1 , after the theme and the app configuration. |