Table of Contents

Class TwSelect<T>

Namespace
TwBlazor.Components
Assembly
TwBlazor.dll

Represents a generic select dropdown component that supports two-way data binding.

public class TwSelect<T> : TwPopoverPickerComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, ITwInputComponent, IAsyncDisposable

Type Parameters

T

The type of values in the dropdown options.

Inheritance
TwSelect<T>
Implements
Inherited Members

Remarks

The TwSelect component provides a flexible dropdown control that can bind to collections of any type. It supports customizable styling, placeholder text, required validation, and property-based display text. The component implements two-way binding through the SelectedValue and SelectedValueChanged parameters.

Constructors

TwSelect()

public TwSelect()

Properties

Clearable

Gets or sets whether a clear button is shown at the end of the field, which clears the selection: the single select's SelectedValue becomes default, and a Multiple select's SelectedValues becomes empty.

[Parameter]
public bool Clearable { get; set; }

Property Value

bool

Remarks

Shown in every mode, the native picker included, and disabled while nothing is selected. It is left out of the Tab order, as the text field's is; pressing Delete or Backspace on the field clears it from the keyboard. Has no effect on a Required single select, which always keeps a value, or while the select is disabled or read only.

ItemTemplate

Gets or sets the content rendered for each option in the listbox.

[Parameter]
public RenderFragment<T>? ItemTemplate { get; set; }

Property Value

RenderFragment<T>

Remarks

Used only by the custom listbox popover. When the browser's native picker is used instead (on iOS and Android by default, or with PreferNativePicker set to true), a native <option> can only hold text, so each option falls back to its display text from PropertyName or ToString(). The closed field and the chips of a Multiple select always show that display text too.

Multiple

Gets or sets whether more than one option can be selected at once.

[Parameter]
public bool Multiple { get; set; }

Property Value

bool

Remarks

A closed trigger (matching the single-select look) is always shown. On a device that prefers its own native picker (see PreferNativePicker), tapping it opens a real, invisible <select multiple> layered on top - so the platform's own multi-select UI (e.g. iOS/Android's full-screen sheet) still handles the interaction. Everywhere else, it opens a listbox popover (the same rows and keyboard handling as the single select) instead, since a native multi-select can't render as a closed, single-row trigger the way a single-select can. Bind SelectedValues/SelectedValuesChanged rather than SelectedValue/SelectedValueChanged when this is true.

OnClear

Gets or sets the callback that is invoked after the clear button, or Delete or Backspace, clears the selection.

[Parameter]
public EventCallback OnClear { get; set; }

Property Value

EventCallback

Placeholder

Gets or sets the placeholder text displayed when no value is selected. Defaults to the localized Select_Placeholder text ("Select an option...").

[Parameter]
public string? Placeholder { get; set; }

Property Value

string

Remarks

When Multiple is true, this is shown on the closed trigger only while nothing is selected - it's replaced by a chip per selected option once at least one is picked.

PropertyName

Gets or sets the name of the property to display for complex objects.

[Parameter]
public string PropertyName { get; set; }

Property Value

string

Remarks

When Values contains complex objects, specify the property name to display as the option text. If null or empty, the object's ToString() method will be used.

Required

Gets or sets whether the select is required (no empty option will be shown).

[Parameter]
public bool Required { get; set; }

Property Value

bool

SelectedValue

Gets or sets the currently selected value.

[Parameter]
public T SelectedValue { get; set; }

Property Value

T

Remarks

Not used when Multiple is true - bind SelectedValues instead.

SelectedValueChanged

Gets or sets the callback that is invoked when the selected value changes. Choosing the placeholder clears the selection and passes default, such as null for a reference type.

[Parameter]
public EventCallback<T> SelectedValueChanged { get; set; }

Property Value

EventCallback<T>

SelectedValueExpression

Gets or sets the expression that identifies the bound value. Set automatically by @bind-SelectedValue, it lets an enclosing EditForm or TwForm validate the property the field edits.

[Parameter]
public Expression<Func<T>>? SelectedValueExpression { get; set; }

Property Value

Expression<Func<T>>

SelectedValues

Gets or sets the currently selected values when Multiple is true.

[Parameter]
public IEnumerable<T> SelectedValues { get; set; }

Property Value

IEnumerable<T>

SelectedValuesChanged

Gets or sets the callback that is invoked when the selection changes while Multiple is true.

[Parameter]
public EventCallback<IEnumerable<T>> SelectedValuesChanged { get; set; }

Property Value

EventCallback<IEnumerable<T>>

SelectedValuesExpression

Gets or sets the expression that identifies the bound values of a Multiple select. Set automatically by @bind-SelectedValues, it lets an enclosing EditForm or TwForm validate the property the field edits.

[Parameter]
public Expression<Func<IEnumerable<T>>>? SelectedValuesExpression { get; set; }

Property Value

Expression<Func<IEnumerable<T>>>

Values

Gets or sets the collection of values to display in the dropdown.

[Parameter]
public IEnumerable<T> Values { get; set; }

Property Value

IEnumerable<T>

Methods

BuildRenderTree(RenderTreeBuilder)

Renders the component to the supplied RenderTreeBuilder.

protected override void BuildRenderTree(RenderTreeBuilder __builder)

Parameters

__builder RenderTreeBuilder

ClearAsync()

Clears the selection, as the Clearable clear button does, then moves focus back to the field.

public Task ClearAsync()

Returns

Task

A task that completes once the selection has been cleared.

Close()

Closes the picker's popover panel and cleans up JavaScript event handlers.

[JSInvokable("Close")]
public override Task Close()

Returns

Task

Remarks

This method is invoked from JavaScript when a click outside the picker is detected. It sets isFocused to false, unregisters the outside click handler, and triggers a UI refresh.

CreateFieldIdentifier()

Creates the identifier of the model property the field's value is bound to. Input components override this to build it from their value expression (supplied by @bind-Value).

protected override FieldIdentifier? CreateFieldIdentifier()

Returns

FieldIdentifier?

The field identifier, or null when the value is not bound to a property.

DisposeAsync()

Disposes of the component's resources asynchronously.

public override ValueTask DisposeAsync()

Returns

ValueTask

Remarks

This method ensures that JavaScript event handlers are unregistered and the .NET object reference is disposed, even if the component is removed from the DOM without Close() being called. This prevents memory leaks and orphaned event listeners.

OnAfterRenderAsync(bool)

Determines, via PreferNativePicker or JS-based device detection, whether Multiple's closed trigger should hand taps/clicks to a real invisible native <select multiple> instead of opening the custom listbox popover.

protected override Task OnAfterRenderAsync(bool firstRender)

Parameters

firstRender bool

Returns

Task

OnInitialized()

Method invoked when the component is ready to start, having received its initial parameters from its parent in the render tree.

protected override void OnInitialized()

OnParametersSet()

Invoked when the component's parameters have been set. Ensures that the component has a unique identifier by generating one if none is provided.

protected override void OnParametersSet()

Remarks

If the component's identifier is not set, a unique ID is generated based on the component's type name (with a 'Tw' prefix removed if present) and a GUID. This helps ensure that each component instance can be uniquely identified, which is useful for scenarios such as associating labels with form elements or targeting elements in client-side scripts.

SelectOptionAsync(int)

Commits an option chosen in the custom listbox, by click or by the keyboard handling in twSelect.attachListbox, then closes it and returns focus to the trigger.

[JSInvokable("SelectOption")]
public Task SelectOptionAsync(int id)

Parameters

id int

The option's id in TwBlazor.Components.TwSelect`1.parsedValues, or 0 for the placeholder.

Returns

Task

ToggleOptionAsync(int)

Toggles one option in the multi-select's listbox, by click or by the keyboard handling in twSelect.attachListbox. The listbox stays open so several options can be picked in a row.

[JSInvokable("ToggleOption")]
public Task ToggleOptionAsync(int id)

Parameters

id int

The option's id in TwBlazor.Components.TwSelect`1.parsedValues.

Returns

Task