Table of Contents

Class TwAutocomplete<T>

Namespace
TwBlazor.Components
Assembly
TwBlazor.dll

Represents a text field that suggests matching options as the user types, and binds the option they pick.

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

Type Parameters

T

The type of the options.

Inheritance
TwAutocomplete<T>
Implements
Inherited Members

Remarks

The field follows the WAI-ARIA combobox pattern with list autocomplete: focus stays in the text field while the arrow keys move through the suggestions, which are exposed through aria-activedescendant. It shares its look with TwSelect<T>, so its listbox is styled by the same TwInputTheme values. Options come from Values, filtered on the client by FilterMode, or from SearchFunc for server side search.

Constructors

TwAutocomplete()

public TwAutocomplete()

Properties

Clearable

Gets or sets whether a clear button is shown at the end of the field.

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

Property Value

bool

DebounceInterval

Gets or sets how long, in milliseconds, to wait after the last keystroke before calling SearchFunc. Defaults to 0, which searches on every keystroke.

[Parameter]
public int DebounceInterval { get; set; }

Property Value

int

FilterMode

Gets or sets how the typed text is matched against each of Values. Defaults to Contains. Matching ignores case and accents.

[Parameter]
public AutocompleteFilterMode FilterMode { get; set; }

Property Value

AutocompleteFilterMode

ItemTemplate

Gets or sets the content rendered for each suggestion. Defaults to the option's display text.

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

Property Value

RenderFragment<T>

Remarks

Unlike TwSelect<T>, the autocomplete never switches to the browser's native picker, on mobile or anywhere else (PreferNativePicker has no effect), so the template is always used in the list. The text field itself can only hold text, so it always shows the selected option's display text from PropertyName or ToString(), and the typed text is matched against that display text, not against what the template renders.

MaxItems

Gets or sets the most suggestions to list at once, or null to list every match.

[Parameter]
public int? MaxItems { get; set; }

Property Value

int?

MinCharacters

Gets or sets how many characters must be typed before suggestions are shown. Defaults to 0, so every option is listed as soon as the list opens.

[Parameter]
public int MinCharacters { get; set; }

Property Value

int

NoResultsText

Gets or sets the text shown when nothing matches the typed text. Defaults to the localized Common_NoResults text ("No results").

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

Property Value

string

Placeholder

Gets or sets the placeholder text shown while the field is empty.

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

Property Value

string

PropertyName

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

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

Property Value

string

Remarks

If null or empty, the option's ToString() is used. The displayed text is also the text that the typed text is matched against.

Required

Gets or sets whether a value is required. A required field cannot be cleared by emptying its text.

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

Property Value

bool

SearchFunc

Gets or sets a function that returns the options for the typed text, for searching on a server or a large data set. When set, Values and FilterMode are not used.

[Parameter]
public Func<string, CancellationToken, Task<IEnumerable<T>>>? SearchFunc { get; set; }

Property Value

Func<string, CancellationToken, Task<IEnumerable<T>>>

Remarks

The token is cancelled when the user types again before the search completes, so a slow, stale search never replaces newer results.

SelectedValue

Gets or sets the selected option.

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

Property Value

T

SelectedValueChanged

Gets or sets the callback that is invoked when the selected option changes. Clearing the field 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>>

Values

Gets or sets the options to suggest. Ignored when SearchFunc is set.

[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()

Empties the field and clears the selection, then moves focus back to the field.

public Task ClearAsync()

Returns

Task

A task that completes once the field 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)

Lifts the background inert-ing if it is still on after the panel has closed.

protected override Task OnAfterRenderAsync(bool firstRender)

Parameters

firstRender bool

Returns

Task

Remarks

Picking a value closes the panel through ReleasePanelTrapAsync(), which awaits several JS calls. The component re-renders while those are in flight, still with the panel open, and that render's OnAfterRenderAsync arms the trap again after the release has already cleared it. Nothing runs after the panel is gone to undo that, so the rest of the page (inside a dialog, its Save and Cancel buttons) stayed inert and unclickable. The render that removes the panel always follows, so checking here catches it.

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.

OpenFromKeyboardAsync(string?)

Opens the list from the keyboard (Arrow Down or Arrow Up on the field, called by twAutocomplete.attach) and highlights its first or last suggestion.

[JSInvokable("OpenFromKeyboard")]
public Task OpenFromKeyboardAsync(string? highlight)

Parameters

highlight string

first or last, or null to open without a highlight.

Returns

Task

A task that completes once the list is open.

SelectOptionAsync(int)

Selects a suggestion, by click or by Enter on the highlighted one (see twAutocomplete.attach), puts its text in the field and closes the list. Focus stays in the field.

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

Parameters

key int

The suggestion's key in the list shown.

Returns

Task

A task that completes once the selection is committed.