Forms
TwAutocomplete
TwAutocomplete is a Blazor autocomplete component. Filter suggestions as you type, search a server with SearchFunc, and bind the chosen SelectedValue.
Last updated
Please be aware this component is new, may have bugs and/or be subject to change. Please report any bugs at
https://github.com/twblazor/twblazor/issues.
Basic usage
Bind
SelectedValue and pass the options in Values. Typing opens a list of the options that contain the typed text. Pick one with the mouse, or with the arrow keys and Enter. The list shares its look with TwSelect, and Variant switches between the default, filled and outlined styles.Choosing a value
Only an option from the list can be selected. When focus leaves the field, text that matches an option in full (ignoring case) selects it, and any other text is replaced by the selected option's text.
Emptying the field clears the selection, setting
SelectedValue back to its default, such as null, unless the field is Required.
Filter mode
Set
FilterMode to AutocompleteFilterMode.StartsWith to list only the options that start with the typed text. The default, Contains, matches anywhere. Matching ignores case and accents, so "cafe" finds "Café".Complex objects and item templates
Use
PropertyName to show, and match against, one property of a complex object. Add an ItemTemplate to draw each suggestion your own way.The field always shows the display text
UnlikeTwSelect, the autocomplete never switches to the browser's native picker on iOS or Android, so the template is used for the suggestions on every device, and PreferNativePicker has no effect.
The text field can only hold text, though: once an option is chosen it shows that option's display text from PropertyName, or ToString() when no property is named, and typing is matched against that text rather than what the template draws.
Server side search
Set
SearchFunc to fetch suggestions yourself, such as from an API. It is given the typed text and a CancellationToken that is cancelled when the user types again, so a slow search never overwrites newer results.
DebounceInterval waits for a pause in typing before searching, and MinCharacters keeps the list closed until enough has been typed. "Loading..." is shown while a search runs.
Clearable, max items and no results text
Set
Clearable for a button that empties the field and clears the selection. MaxItems caps how many suggestions are listed, and NoResultsText replaces the "No results" message.Dense, required, disabled and read only
Set
Dense for a more compact field. Required marks the field as required, Disabled turns it off, and ReadOnly keeps it focusable while stopping it from changing.RAZOR
<div class="grid gap-4 sm:grid-cols-2">
<TwAutocomplete T="string" Label="Dense" Values="@_countries" Dense @bind-SelectedValue="@denseCountry" />
<TwAutocomplete T="string" Label="Required" Values="@_countries" Required @bind-SelectedValue="@requiredCountry" />
<TwAutocomplete T="string" Label="Disabled" Values="@_countries" SelectedValue="@("France")" Disabled />
<TwAutocomplete T="string" Label="Read only" Values="@_countries" SelectedValue="@("Germany")" ReadOnly />
</div>Accessibility
The field follows the WAI-ARIA combobox pattern with list autocomplete. It has the
combobox role with aria-autocomplete="list", aria-expanded and aria-controls, and focus never leaves it: the highlighted suggestion is exposed through aria-activedescendant, so you can keep typing while you browse.
Each suggestion is an option in a labelled listbox, with aria-selected on the chosen one, and a polite live region announces how many suggestions are available as they change.
The chevron and clear buttons are left out of the Tab order, as the APG pattern recommends.
Theme configuration
Styling is controlled by
TwBlazorTheme.Components (a TwInputTheme), configured via the
AddTwBlazor() options callback described on the Get Started page.
Every property is a set of Tailwind classes, so changes are picked up by your own Tailwind build.
Inherited from TwTextfield - this component reuses that theme rather than defining its own.
Full reference: TwInputTheme on the API docs.