Dropdown
Description
The Dropdown component renders an interactive list of options with a single visible selected item by default. When a user clicks the dropdown, all available options become visible. It supports both single-select and multi-select modes, customizable arrow icons, asynchronous item loading, validation, and styling options such as disabled, required, no-border, or no-background. Use the Dropdown when you want to allow users to choose from a list of items while keeping the UI compact.
Usage
Instantiate a Dropdown component using the static helper method from Tesserae.UI. Items can be provided directly or loaded asynchronously. The following sample demonstrates a basic single-select Dropdown with two options and custom validation logic.
Sample Code
API reference
public sealed class Dropdown : Layer<Dropdown>, ICanValidate<Dropdown>, IObservableListComponent<Dropdown.Item>, ITabIndex, IRoundedStyleA select-style form input for picking one or many values from a list, with filtering, async loading and rich item rendering.
Constructors
Properties
public SelectMode Mode { get ; set ; }Gets or sets the operating mode of the component.
public string SelectedText { get ; }Gets the text of the currently selected item.
public string Error { get ; set ; }Gets or sets the validation error message displayed beneath the component.
public bool HasBorder { get ; set ; }Gets or sets a value indicating whether the component has a visible border.
public bool IsInvalid { get ; set ; }Gets or sets a value indicating whether the component is currently in an invalid state.
public bool IsEnabled { get ; set ; }Gets or sets a value indicating whether the component is interactive (enabled).
Methods
public Dropdown SuppressSelectedOnChangingItemSelections()Configures the component to not fire selection-changed callbacks while selections are being mutated in bulk.
public async Task LoadItemsAsync()This will initiate the last async data retrieval specified via a call to the Items method overload that takes a Func-of-Task-of-array-of-T. If there has not been call to that method (or if LoadItemsAsync has already been called after it was called) then this will throw an InvalidOperationException. If another async data retrieval had already been initiated but has not completed yet, its results will be ignored when it DOES complete because this call came after it and it is presumed that this data will be more current).
public void Attach(ComponentEventHandler<Dropdown> handler)Attaches a handler to the component's value-changed event.
public Dropdown Searchable(string placeholder = "Search")Enables a built-in search box on the component.
public Dropdown SetArrowIcon(UIcons icon, TextSize size = TextSize.Tiny, UIconsWeight weight = UIconsWeight.Regular)Sets the arrow icon of the component.
public Dropdown Items(params Item[] children)This will set items to the available options, replacing any that are already rendered (and meaning that any async data retrievals that have started but not completed yet will be ignored when they DO complete because this call came after it and it is presumed that this data will be more current)
public Dropdown Items(Func<Task<Item[]>> itemsSource)This will specify an asynchronous callback that describes how to get available options - note that they will not be retrieved until LoadItemsAsync is called (when that successfully gets new item data, any existing items will be removed first and the list will be completely replace with the new data). LoadItemsAsync may be called explicitly (and immediately after setting this) - if not, it will be called automatically when the User clicks to open the dropdown.
public Dropdown NoBackground()Removes / disables the background on the component.
public Dropdown Placeholder(string text)Gets or sets the placeholder text shown when the component is empty.
public Dropdown Placeholder(IComponent placeholder)Gets or sets the placeholder text shown when the component is empty.
public Dropdown WithCustomSelectionRender(Func<Item[], IComponent> renderSelectedItems)Returns the component configured with the given custom selection render.
public IObservable<IReadOnlyList<Item>> AsObservable()Returns the component's state as a(n) observable.
private void EnsureAsyncLoadingStateEnabled()When a LoadItemsAsync call starts, there should be a spinner to indicate that something is happening in the background - but if a further async request comes in before the previous one has completed then there is no need to change anything as the spinner state will already be enabled. When multiple async requests overlap, which was started later will take precedence and the results of the earlier-started one will be ignored - when the 'winning' request completes, it will call the synchronous Items method and that will ensure that any loading state is disabled.
private void EnsureAsyncLoadingStateDisabled()When data is successfully retrieved from a LoadItemsAsync call, it will call the synchronous Items method that will call this and ensure that any spinner state is disabled. This happens in the simple case where there is only that single async retrieval and it runs to completion and it also happens if it is superceded by a more recent LoadItemsAsync call or by a separate synchronous Items call.
Samples
Basic Dropdown
The following sample demonstrates the creation of a basic single-select Dropdown with two options and a custom validation handler.
Asynchronous Items Loading
The next example shows how to configure async item loading for a Dropdown. The items will be loaded after a 5-second delay once the dropdown is opened.