SelectMenu
A select with a filter field inside its list, for one or several values. Each value posts through a hidden input.
On this page
Examples
Single
Selected: none
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Framework">
<KlySelectMenu TValue="string" Items="Frameworks" Value="@_single" ValueChanged="@(v => _single = v ?? [])" Placeholder="Pick a framework" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="single-state">Selected: @(_single.Count > 0 ? string.Join(", ", _single) : "none")</p>
</div>
@code {
private IReadOnlyList<string> _single = [];
private static readonly KlySelectItem[] Frameworks =
[
new("blazor", "Blazor"), new("react", "React"), new("vue", "Vue"), new("svelte", "Svelte"), new("angular", "Angular") { Disabled = true },
];
}Multiple with creation
Type a label that is not in the list to add it as a value.
Selected: csharp
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Languages">
<KlySelectMenu Items="Languages" Multiple Create @bind-Value="_values" Placeholder="Pick languages" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="multiple-state">Selected: @(_values.Count > 0 ? string.Join(", ", _values) : "none")</p>
</div>
@code {
private IReadOnlyList<string> _values = ["csharp"];
private static readonly KlySelectItem[] Languages =
[
new("csharp", "C#"), new("fsharp", "F#"), new("ts", "TypeScript"), new("rust", "Rust"), new("go", "Go"),
];
}In an EditForm
Posts in static rendering too: every selected value becomes one form entry.
@using System.ComponentModel.DataAnnotations
<EditForm Model="Model" FormName="select-menu-form" OnValidSubmit="Save" class="flex w-full max-w-sm flex-col items-start gap-4" novalidate>
<DataAnnotationsValidator />
<KlyFormField Label="Stack" Field="FieldIdentifier.Create(() => Model!.Stack)" class="w-full">
<KlySelectMenu Items="Stack" Multiple @bind-Value="Model!.Stack" Placeholder="Pick technologies" />
</KlyFormField>
<KlyFormField Label="Keywords" Field="FieldIdentifier.Create(() => Model!.Keywords)" class="w-full">
<KlyInputTags @bind-Value="Model!.Keywords" Placeholder="Type, then Enter" />
</KlyFormField>
<KlyButton Type="ButtonType.Submit" Label="Save profile" />
@if (_saved)
{
<p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="saved">Saved: @string.Join(", ", Model!.Stack) / @string.Join(", ", Model!.Keywords)</p>
}
</EditForm>
@code {
[SupplyParameterFromForm(FormName = "select-menu-form")]
private Profile? Model { get; set; }
private bool _saved;
private static readonly KlySelectItem[] Stack = [new("dotnet", ".NET"), new("node", "Node.js"), new("python", "Python"), new("go", "Go")];
protected override void OnInitialized() => Model ??= new();
private void Save() => _saved = true;
private sealed class Profile
{
public IReadOnlyList<string> Stack { get; set; } = [];
public IReadOnlyList<string> Keywords { get; set; } = [];
}
}Clearing
Emptying a multiple selection posts one empty value, so a default list on the server is replaced instead of kept: ignore empty entries.
@using System.ComponentModel.DataAnnotations
<EditForm Model="Model" FormName="select-menu-clear" OnValidSubmit="Save" class="flex w-full max-w-sm flex-col items-start gap-4" novalidate>
<KlyFormField Label="Toolset" Field="FieldIdentifier.Create(() => Model!.Stack)" class="w-full">
<KlySelectMenu Items="Stack" Multiple @bind-Value="Model!.Stack" Placeholder="Pick technologies" />
</KlyFormField>
<KlyButton Type="ButtonType.Submit" Label="Save selection" />
@if (_saved)
{
<p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="clear-saved">Saved: @(Picked.Count == 0 ? "none" : string.Join(", ", Picked))</p>
}
</EditForm>
@code {
[SupplyParameterFromForm(FormName = "select-menu-clear")]
private Profile? Model { get; set; }
private bool _saved;
// Emptying the selection posts one empty value (a form cannot post an empty list): ignore it.
private List<string> Picked => Model!.Stack.Where(value => value.Length > 0).ToList();
private static readonly KlySelectItem[] Stack = [new("dotnet", ".NET"), new("node", "Node.js"), new("go", "Go")];
protected override void OnInitialized() => Model ??= new();
private void Save() => _saved = true;
private sealed class Profile
{
public IReadOnlyList<string> Stack { get; set; } = ["dotnet", "go"];
}
}Not bound
Without a binding the browser keeps the selection and posts it under the name.
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Editors">
<KlySelectMenu Items="Editors" Name="editors" Multiple Value="@(["vim"])" Placeholder="Pick editors" />
</KlyFormField>
</div>
@code {
private static readonly KlySelectItem[] Editors = [new("vim", "Vim"), new("code", "VS Code"), new("rider", "Rider")];
}Groups and counts
Group puts items under a heading (role group, named by it); typing in the filter hides a heading together with its last matching item.
Selected: none
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Destination">
<KlySelectMenu TValue="string" Items="Countries" Value="@_values" ValueChanged="@(v => _values = v ?? [])" Placeholder="Pick a country" data-testid="destination" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="destination-state">Selected: @(_values.Count > 0 ? string.Join(", ", _values) : "none")</p>
</div>
@code {
private IReadOnlyList<string> _values = [];
// The filter hides a heading with the last of its items.
private static readonly KlySelectItem[] Countries =
[
new("fr", "France") { Group = "Europe", Count = 48 },
new("de", "Germany") { Group = "Europe", Count = 36 },
new("it", "Italy") { Group = "Europe", Count = 21 },
new("jp", "Japan") { Group = "Asia", Count = 17, Icon = "globe" },
new("kr", "South Korea") { Group = "Asia", Count = 9, Icon = "globe" },
new("br", "Brazil") { Group = "Americas", Count = 12 },
new("ca", "Canada") { Group = "Americas", Count = 8 },
];
}Remote options
OptionsProvider loads the options from a source (simulated here, 400 ms per call): the filter field searches it after Debounce (250 ms by default), the next page loads as the list scrolls, ResolveSelected names values that were never listed. Interactive only: a statically rendered page shows the field disabled, with the values it was given.
Selected: none
Source calls: 0
Last call: none
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Country">
<KlySelectMenu TValue="string" OptionsProvider="_source.ByPage" ResolveSelected="_source.Resolve" PageSize="20" Placeholder="Search a country"
Value="_values" ValueChanged="@(v => _values = v ?? [])" data-testid="remote-select" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-state">Selected: @(_values.Count > 0 ? string.Join(", ", _values) : "none")</p>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-requests">Source calls: @_source.Requests</p>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-last">Last call: @_source.Last</p>
</div>
@code {
private RemoteDirectory _source = default!;
private IReadOnlyList<string> _values = [];
protected override void OnInitialized() => _source = new RemoteDirectory { Changed = () => _ = InvokeAsync(StateHasChanged) };
}Cursor paging and restored values
The provider returns NextCursor instead of counting with Skip, MinLength keeps short texts from being searched, and the two initial values are named by ResolveSelected without their page being loaded.
Selected: jp, br
Source calls: 0
Last call: none
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Offices">
<KlySelectMenu TValue="string" OptionsProvider="_source.ByCursor" ResolveSelected="_source.Resolve" Multiple PageSize="20" Debounce="150" MinLength="2"
Placeholder="Type two letters to search" Value="_values" ValueChanged="@(v => _values = v ?? [])" data-testid="remote-multiple" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-multiple-state">Selected: @string.Join(", ", _values)</p>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-multiple-requests">Source calls: @_source.Requests</p>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-multiple-last">Last call: @_source.Last</p>
</div>
@code {
private RemoteDirectory _source = default!;
// Restored values (from the address, a saved form): their pages were never loaded, so ResolveSelected names them.
private IReadOnlyList<string> _values = ["jp", "br"];
protected override void OnInitialized() => _source = new RemoteDirectory { Changed = () => _ = InvokeAsync(StateHasChanged) };
}Loading and error states
The list is aria-busy while it loads, announces it, and shows a translated error with a Retry button (Enter in the filter field also retries) when the provider throws. Turn the switch on, open the list, turn it off and retry.
Source calls: 0
<div class="flex w-full max-w-xs flex-col gap-3">
<KlySwitch @bind-Value="_failing" Label="The source is down" data-testid="remote-fail" />
<KlyFormField Label="Country">
<KlySelectMenu OptionsProvider="_source.ByPage" PageSize="20" Placeholder="Search a country" @bind-Value="_values" data-testid="remote-error-select" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-error-requests">Source calls: @_source.Requests</p>
</div>
@code {
private RemoteDirectory _source = default!;
private IReadOnlyList<string> _values = [];
// The switch flips what the simulated source does: with it on, every call throws and the list shows its error and Retry button.
private bool _failing
{
get => _source.Fail;
set => _source.Fail = value;
}
protected override void OnInitialized() => _source = new RemoteDirectory { Changed = () => _ = InvokeAsync(StateHasChanged), LatencyMs = 200 };
}Named values
Items are not listed with a provider but name values: France shows as France in every render mode, with no call to the source.
Selected: fr
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Home country">
<KlySelectMenu TValue="string" OptionsProvider="_source.ByPage" Items="Known" PageSize="20" Value="_values" ValueChanged="@(v => _values = v ?? [])" data-testid="remote-named" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-named-state">Selected: @string.Join(", ", _values)</p>
</div>
@code {
private readonly RemoteDirectory _source = new();
private IReadOnlyList<string> _values = ["fr"];
// Items are not listed when there is a provider, but they name values: France shows as France in every render mode, without a call to the source.
private static readonly KlySelectItem[] Known = [new("fr", "France")];
}Create with remote options
The option that creates the typed text follows the options that came back: it disappears when the source returns that exact option, and a text that is already a value shows no results instead of an empty list.
Selected:
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Countries visited">
<KlySelectMenu TValue="string" OptionsProvider="_source.ByPage" Multiple Create PageSize="20" Debounce="150" Placeholder="Search or add a country"
Value="_values" ValueChanged="@(v => _values = v ?? [])" data-testid="remote-create" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="remote-create-state">Selected: @string.Join(", ", _values)</p>
</div>
@code {
private readonly RemoteDirectory _source = new() { LatencyMs = 250 };
private IReadOnlyList<string> _values = [];
}OptionsKey: cities of a country
OptionsKey is what the options depend on besides the text, here the country. When it changes the loaded pages, the search text, the cursor and the resolved labels are forgotten and the list loads again when it opens; a provider that is a new delegate on every render (as here) keeps what was loaded as long as the key is the same.
City: none
Source calls: 0
Last call: none
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Country of the city">
<KlySelect TValue="string" Items="Countries" Value="_country" ValueChanged="OnCountryChanged" data-testid="key-country" />
</KlyFormField>
@* OptionsKey is the country: when it changes, the pages loaded for the other country are forgotten and the list loads again when it opens. The provider is a new
delegate on every render here, which alone would keep what was loaded. *@
<KlyFormField Label="City">
<KlySelectMenu TValue="string" OptionsProvider="@((request, ct) => _source.CitiesOf(_country, request, ct))" OptionsKey="@_country" PageSize="5" Placeholder="Pick a city"
Value="_cities" ValueChanged="@(v => _cities = v ?? [])" data-testid="key-city" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="key-state">City: @(_cities.Count > 0 ? string.Join(", ", _cities) : "none")</p>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="key-requests">Source calls: @_source.Requests</p>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="key-last">Last call: @_source.Last</p>
</div>
@code {
private RemoteDirectory _source = default!;
private string? _country = "fr";
private IReadOnlyList<string> _cities = [];
private static readonly KlySelectItem[] Countries = [new("fr", "France"), new("de", "Germany"), new("it", "Italy")];
protected override void OnInitialized() => _source = new RemoteDirectory { Changed = () => _ = InvokeAsync(StateHasChanged), LatencyMs = 200 };
private void OnCountryChanged(string? country)
{
_country = country;
// A city of the other country is no longer a valid value.
_cities = [];
}
}Bound to ints
The values of the items have the type of the bound property: a list of ints for a multiple select menu, an int? for an input menu, which is then always strict (only a suggestion is a value). The options of a provider can carry any value type too (KlySelectItem<int>), and ResolveSelected receives the text of the values.
Tags: 10, country: none
<div class="flex w-full max-w-xs flex-col gap-3">
<KlyFormField Label="Tags (several ids)">
<KlySelectMenu @bind-Value="_tagIds" Items="Tags" Multiple Placeholder="Pick tags" data-testid="tag-ids" />
</KlyFormField>
<KlyFormField Label="Country (typed suggestion, int key)">
<KlyInputMenu @bind-Value="_countryId" Items="Countries" Placeholder="Type a country" data-testid="country-id" />
</KlyFormField>
<p class="text-sm text-(--klyroo-text-muted)" data-testid="typed-menu-state">Tags: @(_tagIds.Count == 0 ? "none" : string.Join(", ", _tagIds)), country: @(_countryId?.ToString() ?? "none")</p>
</div>
@code {
private sealed record Tag(int Id, string Name);
private static readonly Tag[] Rows = [new(10, "Urgent"), new(11, "Billing"), new(12, "Shipping"), new(13, "Returns")];
// Lists bind to a list of the key type: IReadOnlyList<int>.
private IReadOnlyList<int> _tagIds = [10];
private int? _countryId;
private static readonly IReadOnlyList<KlySelectItem<int>> Tags = KlySelectItems.From(Rows, t => t.Id, t => t.Name);
// An input menu of anything but strings only accepts a suggestion, so the field is strict: the value is the key of the chosen option.
private static readonly IReadOnlyList<KlySelectItem<int?>> Countries =
KlySelectItems.From([(250, "France"), (276, "Germany"), (380, "Italy"), (724, "Spain")], c => (int?)c.Item1, c => c.Item2);
}API
KlySelectMenu<TValue>
Select with a searchable popup list, single or multiple selection, bound to a list of strings, numbers, enums, Guid s or any value that identifies an option, and options that can be fetched from .NET.
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. |
Create |
bool |
false |
Offers the typed text as a new value when no item has that label. A created value is kept in the value list but is not added to Items . |
Debounce |
int |
250 |
With OptionsProvider : the quiet time in milliseconds after typing before the source is searched. |
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 |
IReadOnlyList |
The options. Not needed when OptionsProvider loads them; given together with it, these items are not listed but their labels are known (the label of a value the provider has not returned yet). The parameter must be passed when there is no provider, and a null value ( Items="@_list" while the list is still loading) is an empty list, before and after it loads. | |
MinLength |
int |
0 |
With OptionsProvider : the shortest text that is searched. A shorter, non-empty text shows a hint and lists nothing. |
Multiple |
bool |
false |
Several values can be selected; each one posts under Name . Otherwise the list holds at most one value. |
Name |
string |
Form field name outside an EditForm . Inside one, the name comes from the bound expression. | |
OptionsKey |
string |
With OptionsProvider : what the options depend on besides the search text (the country whose cities are listed). When it changes (compared ordinally), the loaded pages, the search text, the cursor and the resolved labels are forgotten, the selected values are resolved again, and the list loads again the next time it opens. A new delegate with the same key keeps what was loaded: the provider is only asked for what was not loaded yet. Without a key the source is assumed not to change. | |
OptionsProvider |
KlyOptions |
Loads the options from a remote source, one page at a time, instead of listing Items . The filter field searches the source (the browser does not filter): the text is trimmed, sent after Debounce milliseconds and only when it has at least MinLength characters (an empty text lists the unfiltered options); an older request is cancelled and an old answer never replaces a newer one. The next page loads as the list scrolls, a bit before its end, and by the "Load more" button. While loading the list is aria-busy and a polite status says so; when the provider throws, the list shows a translated error with a Retry button (also reached with Enter in the filter field). Interactive only: a statically rendered page (and the prerender before the page becomes interactive) has nobody to call the provider, so the field renders its current values (as their raw values, unless names them) with a disabled trigger, and still posts them with the form. The provider is first called when the list opens. A parent that renders again with an equal or a new delegate keeps the loaded state; give the component a @key to start from scratch. | |
PageSize |
int |
25 |
With OptionsProvider : the number of options requested per page. |
Placeholder |
string |
Hint shown while the field is empty. | |
Required |
bool |
false |
Marks the control as required, for the browser and for assistive technology. |
ResolveSelected |
KlySelected |
With OptionsProvider : returns the options of selected values the loaded pages do not contain (values restored from the address or a saved form), so the field shows their labels. Called once for each such value, as soon as the component is interactive. Without it, such values show their raw value. | |
SearchLabel |
string |
Label of the filter field. Defaults to SelectMenuSearch . | |
Size |
KlySize? |
Defaults to the size of the enclosing KlyFormField , or medium. Values: Xs, Sm, Md, Lg, Xl. |
|
Ui |
SelectMenuUi |
Per-instance class overrides of the slots of the select menu, see SelectMenuUi . 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. | |
Variant |
FieldVariant |
FieldVariant. |
Look of the field: Outline , Soft , Subtle , Ghost or None . Values: Outline, Soft, Subtle, Ghost, None. |
Ui slots
Ui takes a SelectMenuUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlySelectMenu`1 , after the theme and the app configuration. |
Base |
Classes merged into the base slot of KlySelectMenu`1 , after the theme and the app configuration. |
Label |
Classes merged into the label slot of KlySelectMenu`1 , after the theme and the app configuration. |
TrailingIcon |
Classes merged into the trailingIcon slot of KlySelectMenu`1 , after the theme and the app configuration. |
Content |
Classes merged into the content slot of KlySelectMenu`1 , after the theme and the app configuration. |
SearchWrapper |
Classes merged into the searchWrapper slot of KlySelectMenu`1 , after the theme and the app configuration. |
Search |
Classes merged into the search slot of KlySelectMenu`1 , after the theme and the app configuration. |
Listbox |
Classes merged into the listbox slot of KlySelectMenu`1 , after the theme and the app configuration. |
Item |
Classes merged into the item slot of KlySelectMenu`1 , after the theme and the app configuration. |
ItemLabel |
Classes merged into the itemLabel slot of KlySelectMenu`1 , after the theme and the app configuration. |
ItemIcon |
Classes merged into the itemIcon slot of KlySelectMenu`1 , after the theme and the app configuration. |
ItemLeadingIcon |
Classes merged into the itemLeadingIcon slot of KlySelectMenu`1 , after the theme and the app configuration. |
ItemCount |
Classes merged into the itemCount slot of KlySelectMenu`1 , after the theme and the app configuration. |
Group |
Classes merged into the group slot of KlySelectMenu`1 , after the theme and the app configuration. |
GroupLabel |
Classes merged into the groupLabel slot of KlySelectMenu`1 , after the theme and the app configuration. |
LeadingIcon |
Classes merged into the leadingIcon slot of KlySelectMenu`1 , after the theme and the app configuration. |
Empty |
Classes merged into the empty slot of KlySelectMenu`1 , after the theme and the app configuration. |
Create |
Classes merged into the create slot of KlySelectMenu`1 , after the theme and the app configuration. |
Scroller |
Classes merged into the scroller slot of KlySelectMenu`1 , after the theme and the app configuration. |
Loading |
Classes merged into the loading slot of KlySelectMenu`1 , after the theme and the app configuration. |
Spinner |
Classes merged into the spinner slot of KlySelectMenu`1 , after the theme and the app configuration. |
Error |
Classes merged into the error slot of KlySelectMenu`1 , after the theme and the app configuration. |
Announcer |
Classes merged into the announcer slot of KlySelectMenu`1 , after the theme and the app configuration. |