1 Combobox
Byron edited this page 2026-10-02 22:41:06 +02:00

Warning

This component currently only works correctly in Interactive rendering mode.

Basic

<CleanCombobox @bind-Value="@(value)"
          Options="@(Basic.options)"
          GetOptionValue="@(static (option) => option.Value)"
          GetOptionLabel="@(static (option) => option.Label)"
          GetOptionIcon="@(static (option) => option.Icon)"
          PrefixContent="@(Icons.Burger)" />

@code {

	private string? value;

	private static readonly SelectOption[] options =
	[
		new("apple", "Apple", Icons.Apple),
		new("banana", "Banana"),
		new("cherry", "Cherry", Icons.Cherry),
		new("mango", "Mango"),
		new("strawberry", "Strawberry"),
		new("lemon", "Lemon", Icons.Lemon2),
		new("watermelon", "Watermelon", Icons.Lemon),
	];

	private readonly record struct SelectOption(string Value, string Label, RenderFragment? Icon = null);

}

Multiple

Important

When using an array as the Value parameter, make sure it initially isn't null. Always initialize it with an empty array.

<CleanCombobox @bind-Value="@(value)"
          Options="@(Multiple.options)"
          GetOptionValue="@(static (option) => option.Value)"
          GetOptionLabel="@(static (option) => option.Label)" />

@code {

	private string[] value = [Multiple.options[0].Value, Multiple.options[^1].Value];

	private static readonly SelectOption[] options =
	[
		new("apple", "Apple"),
		new("banana", "Banana"),
		new("cherry", "Cherry"),
		new("mango", "Mango"),
		new("strawberry", "Strawberry"),
		new("lemon", "Lemon"),
		new("watermelon", "Watermelon"),
	];

	private readonly record struct SelectOption(string Value, string Label);

}

Custom formatting

Sometimes, you want to overwrite the label that displayed inside the combobox.

<CleanCombobox @bind-Value="@(value)"
          Options="@(CustomLabelFormatting.options)"
          GetOptionValue="@(static (option) => option.Value)"
          GetOptionLabel="@(static (option) => option.Label)"
          GetOptionIcon="@(static (option) => option.Icon)"
          FormatLabel="@(CustomLabelFormatting.FormatLabel)" />

@code {

	private string[] value = [];

	private static readonly SelectOption[] options =
	[
		new("apple", "Apple", Icons.Apple),
		new("banana", "Banana"),
		new("cherry", "Cherry", Icons.Cherry),
		new("mango", "Mango"),
		new("strawberry", "Strawberry"),
		new("lemon", "Lemon", Icons.Lemon2),
		new("watermelon", "Watermelon", Icons.Lemon),
	];

	private static RenderFragment FormatLabel(SelectOption[] selected) =>
		RenderFragment.FromString($"{selected.Length} item(s)");

	private readonly record struct SelectOption(string Value, string Label, RenderFragment? Icon = null);

}

Filtering

By binding a string variable to the Search parameter, users can interact with a search field in the popover. Adding a callback using @@bind-Search:after="…", you can update the current search results displayed to the user.


<CleanCombobox @bind-Value="@(value)"
          @bind-Search="@(search)"
          @bind-Search:after="@(UpdateOptionsAsync)"
          Options="@(options)"
          GetOptionValue="@(static (option) => option.Value)"
          GetOptionLabel="@(static (option) => option.Label)" />

@code {

	private string? value;
	private string[] values = [];
	private string? search;
	private SelectOption[] options = Filtering.defaultOptions;

	private async Task UpdateOptionsAsync()
	{
		// Simulate loading data from a database.
		await Task.Delay(TimeSpan.FromMilliseconds(250));

		if (string.IsNullOrEmpty(search))
		{
			options = Filtering.defaultOptions;
		}
		else
		{
			options =
			[
				new SelectOption(search.ToLowerInvariant(), search),
				new SelectOption($"{search.ToLowerInvariant()}-2", $"{search} 2"),
				new SelectOption($"{search.ToLowerInvariant()}-3", $"{search} 3"),
				new SelectOption($"{search.ToLowerInvariant()}-4", $"{search} 4"),
			];
		}
	}

	private static readonly SelectOption[] defaultOptions =
	[
		new("apple", "Apple"),
		new("banana", "Banana"),
		new("cherry", "Cherry"),
		new("mango", "Mango"),
	];

	private readonly record struct SelectOption(string Value, string Label);

}