DwsAutoComplete
DwsAutoComplete<TItem> suggests items while the user types - from a local list or an asynchronous, cancelable search. It binds a typed value (or several), groups, limits and virtualizes its suggestions, and takes part in form validation.
Basic
Items is the list to search, @bind-Value the picked item. Typing filters the list (Contains, case-insensitive) and marks the match; Clearable adds a clear button. Leaving the field puts the selection's text back, so the field never shows text that is not a value.
Show Code
AutoComplete.razor
<DwsAutoComplete Name="AutoCompleteDemo_Basic" Items="_fruits" @bind-Value="_fruit"
Label="Fruit" Placeholder="Search for a fruit..." HelperText="Try 'an' or 'berry'."
Clearable="true" MaxWidth="360" />
<DwsTypography Name="AutoCompleteDemo_Basic_Value" Typography="DwsTypographyType.Paragraph" Class="mt-2 mb-0">Value: <code>@(_fruit ?? "null")</code></DwsTypography>AutoComplete.razor.cs
private static readonly string[] _fruits = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon", "Nectarine", "Orange", "Papaya", "Passion fruit",
"Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Quince", "Raspberry", "Strawberry", "Tangerine", "Watermelon"];
private string? _fruit;Typed items
TItem can be any type. ItemText picks the text shown and searched.
Show Code
AutoComplete.razor
<DwsAutoComplete TItem="Country" Name="AutoCompleteDemo_Typed" Items="_countries" @bind-Value="_country" ItemText="CountryText"
Label="Country" Placeholder="Search a country..." Clearable="true" MaxWidth="360" />
<DwsTypography Name="AutoCompleteDemo_Typed_Value" Typography="DwsTypographyType.Paragraph" Class="mt-2 mb-0">Value: <code>@(_country is null ? "null" : $"{_country.Name} ({_country.Code})")</code></DwsTypography>AutoComplete.razor.cs
public sealed record Country(string Code, string Name, string Continent, double Population, bool Available = true);
private static readonly Country[] _countries =
[
new("AT", "Austria", "Europe", 9.1),
new("DE", "Germany", "Europe", 84.5),
new("NO", "Norway", "Europe", 5.5, Available: false),
new("CH", "Switzerland", "Europe", 8.8, Available: false),
new("BR", "Brazil", "Americas", 216.4),
new("US", "United States", "Americas", 334.9),
new("JP", "Japan", "Asia", 124.5),
new("KE", "Kenya", "Africa", 55.1, Available: false),
new("TN", "Tunisia", "Africa", 12.5),
new("AU", "Australia", "Oceania", 26.6),
];
// Assigned once, so the component does not see new delegates on every render.
private static readonly Func<Country, string> CountryText = country => country.Name;
private Country? _country;Appearance
Appearance picks Outline (default), Filled or Text.
Show Code
AutoComplete.razor
<DwsAutoComplete Name="AutoCompleteDemo_Outline" Items="_fruits" @bind-Value="_appearanceOutline" Label="Outline" Appearance="DwsAppearance.Outline" Width="200" />
<DwsAutoComplete Name="AutoCompleteDemo_Filled" Items="_fruits" @bind-Value="_appearanceFilled" Label="Filled" Appearance="DwsAppearance.Filled" Width="200" />
<DwsAutoComplete Name="AutoCompleteDemo_Text" Items="_fruits" @bind-Value="_appearanceText" Label="Text" Appearance="DwsAppearance.Text" Width="200" />AutoComplete.razor.cs
private static readonly string[] _fruits = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon", "Nectarine", "Orange", "Papaya", "Passion fruit",
"Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Quince", "Raspberry", "Strawberry", "Tangerine", "Watermelon"];
private string? _appearanceOutline;
private string? _appearanceFilled;
private string? _appearanceText;Dense
Dense makes the field compact.
Show Code
AutoComplete.razor
<DwsAutoComplete Name="AutoCompleteDemo_DenseField" Items="_fruits" @bind-Value="_appearanceDense" Label="Dense field" Dense="true" Width="200" />AutoComplete.razor.cs
private static readonly string[] _fruits = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon", "Nectarine", "Orange", "Papaya", "Passion fruit",
"Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Quince", "Raspberry", "Strawberry", "Tangerine", "Watermelon"];
private string? _appearanceDense;Color
Color sets the accent (focus border, highlight, chips) through the theme's CSS variables.
Show Code
AutoComplete.razor
<DwsAutoComplete Name="AutoCompleteDemo_ColorField" Items="_fruits" @bind-Value="_appearanceColor" Label="Secondary color" Color="DwsColor.Secondary" Width="200" />AutoComplete.razor.cs
private static readonly string[] _fruits = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon", "Nectarine", "Orange", "Papaya", "Passion fruit",
"Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Quince", "Raspberry", "Strawberry", "Tangerine", "Watermelon"];
private string? _appearanceColor;Readonly and disabled
Readonly and Disabled keep the list closed.
Show Code
AutoComplete.razor
<DwsAutoComplete Name="AutoCompleteDemo_Readonly" Items="_fruits" Value="@("Mango")" Label="Readonly" Readonly="true" Width="200" />
<DwsAutoComplete Name="AutoCompleteDemo_Disabled" Items="_fruits" Value="@("Kiwi")" Label="Disabled" Disabled="true" Width="200" />AutoComplete.razor.cs
private static readonly string[] _fruits = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon", "Nectarine", "Orange", "Papaya", "Passion fruit",
"Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Quince", "Raspberry", "Strawberry", "Tangerine", "Watermelon"];Size, border and list height
Width, MinWidth, MaxWidth, Margin, Padding, BorderWidth, BorderRadius and ListMaxHeight take pixels.
Show Code
AutoComplete.razor
<DwsAutoComplete Name="AutoCompleteDemo_Sized" Items="_fruits" @bind-Value="_appearanceSized" Label="Rounded, short list"
BorderRadius="20" BorderWidth="2" Padding="8" ListMaxHeight="120" Width="200" />AutoComplete.razor.cs
private static readonly string[] _fruits = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon", "Nectarine", "Orange", "Papaya", "Passion fruit",
"Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Quince", "Raspberry", "Strawberry", "Tangerine", "Watermelon"];
private string? _appearanceSized;Multiple values
SelectionMode Multiple binds @bind-Values and shows the picks as chips; the list stays open for the next one and Backspace in the empty field removes the last chip.
Show Code
AutoComplete.razor
<DwsAutoComplete Name="AutoCompleteDemo_Multiple" Items="_fruits" @bind-Values="_fruitBasket" SelectionMode="DwsSelectionMode.Multiple"
Label="Fruit basket" Placeholder="Add fruits..." Clearable="true" Width="360" />
<DwsTypography Name="AutoCompleteDemo_Multiple_Values" Typography="DwsTypographyType.Caption" Class="d-block mt-2 mb-0">Basket: <code>@string.Join(", ", _fruitBasket)</code></DwsTypography>AutoComplete.razor.cs
private static readonly string[] _fruits = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon", "Nectarine", "Orange", "Papaya", "Passion fruit",
"Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Quince", "Raspberry", "Strawberry", "Tangerine", "Watermelon"];
private IReadOnlyList<string> _fruitBasket = ["Apple", "Mango"];Custom value in single mode
A single-value field with AllowCustomValues takes any text. With Items the whole list is checked, also what MaxItems cut from the suggestions; for types other than string, CustomValueFactory turns the text into an item.
Show Code
AutoComplete.razor
<DwsAutoComplete Name="AutoCompleteDemo_CustomSingle" Items="_fruits" @bind-Value="_customFruit" AllowCustomValues="true"
Label="Fruit or anything else" Placeholder="Type a new fruit..." Width="260" />
<DwsTypography Name="AutoCompleteDemo_CustomSingle_Value" Typography="DwsTypographyType.Caption" Class="d-block mt-2 mb-0">Value: <code>@(_customFruit ?? "null")</code></DwsTypography>AutoComplete.razor.cs
private static readonly string[] _fruits = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Coconut", "Cranberry", "Date",
"Fig", "Grape", "Grapefruit", "Guava", "Kiwi", "Lemon", "Lime", "Lychee", "Mango", "Melon", "Nectarine", "Orange", "Papaya", "Passion fruit",
"Peach", "Pear", "Pineapple", "Plum", "Pomegranate", "Quince", "Raspberry", "Strawberry", "Tangerine", "Watermelon"];
private string? _customFruit;
// For a TItem other than string, tell the component how to turn text into an item:
// CustomValueFactory="@(text => new Country("??", text, "Other", 0))"Interface-Description
| Name | Typ | Default | Description |
|---|---|---|---|
| IsOpen | bool | false | Whether the suggestion list is open. |
| IsLoading | bool | false | Whether SearchFunc is running. |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| LoadMoreAsync | Task | Loads the next page of a paged list, as reaching its end does. | |
| OpenAsync | Task | Opens the suggestion list. | |
| CloseAsync | Task | Closes the suggestion list. | |
| ClearAsync | Task | Clears the text and the selection. | |
| CancelLoadingAsync | Task | Cancels a running SearchFunc. | |
| Validate | bool | Shows the Required message now if nothing is picked. Returns whether the field is valid. | |
| FocusAsync | ValueTask | Focuses the field. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Items | IEnumerable<TItem>? | null | The local items to search. Ignored when SearchFunc is set. |
| SearchFunc | Func<string, CancellationToken, Task<IEnumerable<TItem>>>? | null | Loads suggestions for the typed text. The token is canceled when a newer search starts, the list closes, the user cancels (Escape or the spinner) or the component is disposed. |
| DebounceMilliseconds | int | 300 | Delay after the last keystroke before SearchFunc runs. |
| ItemText | Func<TItem, string>? | null | The text shown and searched for an item. Defaults to ToString(). |
| ItemDisabled | Func<TItem, bool>? | null | Items for which this returns true are shown but cannot be picked; the keyboard skips them. |
| ItemGroup | Func<TItem, string?>? | null | Groups the suggestions under headers. |
| Value | TItem? | default | The picked item. Bind with @bind-Value. For an item type without null (int, an enum, a struct) the default - 0, the first enum member - means 'nothing selected', unless Items contains it; use a nullable TItem (int?) when 0 is a choice a provider (SearchFunc) can return. |
| ValueExpression | Expression<Func<TItem?>>? | null | Identifies the bound field for EditContext validation. Set by @bind-Value. |
| Values | IReadOnlyList<TItem>? | null | The picked items in multiple mode. Bind with @bind-Values. |
| ValuesExpression | Expression<Func<IReadOnlyList<TItem>?>>? | null | Identifies the bound field for EditContext validation. Set by @bind-Values. |
| SelectionMode | DwsSelectionMode | Single | Single binds Value; Multiple binds Values and shows the picks as chips. |
| Text | string? | null | The text in the field. Bind with @bind-Text to read or set what the user typed. |
| AllowCustomValues | bool | false | Accepts text matching no item (Enter, the Add row, or leaving the field). |
| CustomValueFactory | Func<string, TItem?>? | null | Turns typed text into an item for AllowCustomValues. Not needed for string. |
| CustomValueExists | Func<string, CancellationToken, Task<bool>>? | null | Asked before text is added as a custom value when the suggestions come from SearchFunc / SearchPageFunc: return true when an item of that text exists, and nothing is added (OnCustomValueRefused is raised). With Items the whole list is checked without it. Without it a provider-backed field cannot know what its cut or paged result left out and may add a duplicate. |
| SearchMode | DwsAutoCompleteSearchMode | Contains | None, Contains, StartsWith, Exact or Custom (CustomFilter). |
| CaseSensitive | bool | false | Matches the typed text case-sensitively. |
| CustomFilter | Func<TItem, string, bool>? | null | The filter used with SearchMode Custom. Without it that mode filters by 'contains' and logs a warning once. |
| MinCharacters | int | 0 | Characters to type before suggestions are searched. |
| MaxItems | int? | null | The most suggestions shown at once. null, 0 or a negative number: no limit. |
| HighlightMatch | bool | true | Marks the typed text inside each suggestion (not inside ItemTemplate). |
| OpenOn | DwsAutoCompleteOpenMode | Input | What opens the list: Input (typing), Focus, or Manual (toggle button, ArrowDown, OpenAsync). |
| Label | string? | null | The caption above the field. |
| Placeholder | string? | null | Shown in the empty field. |
| HelperText | string? | null | Shown under the field while there is no validation message. |
| Required | bool | false | Asks for a value; the message joins an enclosing EditContext. |
| RequiredErrorText | string? | null | Replaces the localized Required message. |
| Appearance | DwsAppearance | Outline | Outline, Filled or Text. |
| Color | DwsColor | Primary | The accent color (focus border, highlight, chips) through the theme's CSS variables. |
| Dense | bool | false | A compact field. |
| Readonly | bool | false | Shows the value without letting it be changed. |
| FloatingLabel | bool | true | True: the label sits small inside the top of the field, like the floating labels of DwsInput, DwsSelect and the pickers, so forms mixing them line up. False puts it above the field. |
| Haptic | DwsHapticFeedback | DwsHapticFeedback.Selection | Tactile feedback of a tap on a suggestion while the app has haptics switched on; played inside the tap, so also on iOS. Not played on a disabled suggestion or while read-only. The field's own buttons (clear, open the list, cancel loading) are DwsButtons and a DwsCloseButton with 44px touch targets that keep the focus in the field. |
| Clearable | bool | false | Shows a button that clears text and selection. |
| ShowToggleButton | bool | true | Shows the button that opens and closes the list. |
| ToggleIcon | string? | null | The toggle button's icon. Defaults to a search icon with SearchFunc, a chevron with Items. |
| Lines | int | 1 | Rows of text; above 1 the field is a text area. |
| Width | int? | null | Width in pixels. |
| MinWidth | int? | null | Minimum width in pixels. |
| MaxWidth | int? | null | Maximum width in pixels. |
| ListMaxHeight | int? | null | Maximum height of the suggestion list in pixels (default 18rem). |
| Margin | int? | null | Outer spacing in pixels. |
| Padding | int? | null | Inner spacing of the field in pixels. |
| BorderWidth | int? | null | Border width of the field in pixels. |
| BorderRadius | int? | null | Corner radius of the field in pixels. |
| ItemTemplate | RenderFragment<TItem>? | null | Renders each suggestion. |
| SelectedItemTemplate | RenderFragment<TItem>? | null | Renders selected suggestions instead of ItemTemplate. |
| DisabledItemTemplate | RenderFragment<TItem>? | null | Renders disabled suggestions instead of ItemTemplate. |
| GroupTemplate | RenderFragment<string>? | null | Renders group headers. |
| NoResultsTemplate | RenderFragment<string>? | null | Shown when nothing matches; the context is the searched text. |
| LoadingTemplate | RenderFragment? | null | Shown in the list while SearchFunc runs. |
| Virtualize | bool | false | Renders only the visible suggestions once there are more than VirtualizationThreshold. |
| VirtualizationItemHeight | float | 36 | The row height in pixels used for virtualization. |
| VirtualizationThreshold | int | 50 | Virtualization starts above this many suggestions. |
| SearchPageFunc | Func<DwsAutoCompleteSearchRequest, CancellationToken, Task<DwsAutoCompleteSearchPage<TItem>>>? | null | Loads suggestions page by page: asked for Take items after the first Skip of the typed Text, it returns the items and HasMore. The next page is loaded when the list is scrolled to its end, is too short to scroll, or ArrowDown is pressed on its last item; a 'loading more' row shows meanwhile. Takes the place of SearchFunc and Items. |
| PageSize | int | 50 | How many suggestions SearchPageFunc is asked for at a time. |
| ShowRefineHint | bool | true | Ends a list that MaxItems has cut with a row saying that only the first matches are shown and that typing more narrows the search. |
| Name | Typ | Description |
|---|---|---|
| ValueChanged | EventCallback<TItem?> | Fires when the picked item changes. |
| ValuesChanged | EventCallback<IReadOnlyList<TItem>> | Fires when the picked items change (multiple mode). |
| TextChanged | EventCallback<string> | Fires when the text in the field changes. |
| OnOpen | EventCallback | Fires when the list opens. |
| OnClose | EventCallback | Fires when the list closes. |
| OnLoadStarted | EventCallback<string> | Fires when SearchFunc starts, with the searched text. |
| OnLoadCompleted | EventCallback<IReadOnlyList<TItem>> | Fires with the suggestions SearchFunc returned. |
| OnLoadCanceled | EventCallback<string> | Fires when the user or CancelLoadingAsync cancels a running search. |
| OnItemSelected | EventCallback<TItem> | Fires when an item is picked (toggled in multiple mode). |
| OnItemRemoved | EventCallback<TItem> | Fires when a chip is removed in multiple mode. |
| OnCustomValue | EventCallback<string> | Fires when text matching no item is committed through AllowCustomValues. |
| OnCustomValueRefused | EventCallback<string> | Fires with the text when CustomValueExists said that it exists already and nothing was added. |
| OnCleared | EventCallback | Fires when the clear button (or ClearAsync) clears the field. |
| OnFocus | EventCallback<FocusEventArgs> | Fires when the field gets focus. |
| OnBlur | EventCallback<FocusEventArgs> | Fires when the field loses focus. |
| OnKeyDown | EventCallback<KeyboardEventArgs> | Fires for every key pressed in the field. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Element | ElementReference | The associated web component. May be null if accessed before the component is rendered. | |
| ComponentGuid | Guid | Unique identifier for the component. | |
| TypeString | string | The name of the component type, obtained from reflection. | |
| Authorized_RegisterEnabled | bool | false | Indicates whether this component is enabled through a permission registry. |
| Authorized_RegisterSelected | bool | false | Indicates whether this component is selected in permission selection mode. |
| GetInitializedState | bool | false | Returns whether the component is initialized. |
| GetRenderModeState | DwsRenderMode | Returns the current render mode. | |
| GetComponentState | DwsComponentStateType | Returns the current component state. | |
| GetIsInteractiveState | bool | false | Returns whether the component is interactive. |
| GetIsPrerenderingState | bool | false | Returns whether the component is prerendering. |
| GetComponentLoadingState | bool | false | Returns whether the component is loading. |
| IsFormBusy | bool | false | Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState). |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| DebugInfo | void | Logs debug information about the component. | |
| UpdateState | void | Updates the component's state and triggers StateHasChanged. | |
| UpdateStateAsync | Task | Asynchronously updates the component's state and triggers StateHasChanged. | |
| Dispose | void | Disposes the component and releases resources. | |
| FocusAsync | ValueTask | bool containedInScrollableList | Sets focus to the component's element. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Name | string | Required unique identifier for the component within its container. | |
| Visible | bool | true | Determines if the component is visible in the UI. |
| Disabled | bool | false | Disables the form control, ensuring it doesn't participate in form submission. |
| ComponentNameSpace | string | The namespace for permission-based access control. | |
| PermissionKey | string? | null | A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins. |
| Class | string | Optional CSS class names. | |
| Style | string | Optional in-line styles. | |
| Tag | object | An optional user-defined object to store any additional data with the component. | |
| BackReference | DwsReference | A reference to the enclosing component. | |
| ID | string | The HTML ID attribute for the component. | |
| AdditionalAttributes | Dictionary<string, object> | Additional HTML attributes for the component's root element. | |
| IgnorePermissionChecks | bool | false | Bypasses all permission checks for this component. |
| IgnorePermissionSelection | bool | false | Ignore permission selection mode for this component. |
| IgnoreFormBusy | bool | false | Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it. |
| Name | Typ | Description |
|---|---|---|
| OnInitializedCallbackStarted | EventCallback | Callback when OnInitialized starts. |
| OnInitializedCallback | EventCallback | Callback during OnInitialized. |
| OnInitializedCallbackCompleted | EventCallback | Callback when OnInitialized completes. |
| OnInitializedAsyncCallbackStarted | EventCallback | Callback when OnInitializedAsync starts. |
| OnInitializedAsyncCallback | EventCallback | Callback during OnInitializedAsync. |
| OnInitializedAsyncCallbackCompleted | EventCallback | Callback when OnInitializedAsync completes. |
| OnIsComponentLoadingChanged | EventCallback<bool> | Callback when IsComponentLoading changes. |
| OnCurrentRenderModeChanged | EventCallback<DwsRenderMode> | Callback when CurrentRenderMode changes. |
| OnComponentStateChanged | EventCallback<DwsComponentStateType> | Callback when ComponentState changes. |
