Table of Contents

Class TwTagInput

Namespace
TwBlazor.Components
Assembly
TwBlazor.dll

Represents a field for entering a list of free-form tags or keywords, each shown as a removable TwChip.

public class TwTagInput : TwPopoverPickerComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, ITwInputComponent, IAsyncDisposable
Inheritance
TwTagInput
Implements
Inherited Members

Remarks

Typed text becomes a tag when the user presses Enter or types one of the Separators (a comma by default). Pasted text is split on the separators and on line breaks, so a whole list can be pasted at once. Backspace in the empty field removes the last tag, and the arrow keys move between the tags, where Delete or Backspace removes the focused one.

Bind the list with @bind-Value. Inside an EditForm or a TwForm the field reports every change, so validation attributes on the bound property, such as [MinLength], run for it. A tag that cannot be added (see TagRejectionReason) stays in the text input, and the reason is shown under the field.

Constructors

TwTagInput()

public TwTagInput()

Properties

AddOnBlur

Gets or sets whether the typed text becomes a tag when focus leaves the field.

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

Property Value

bool

AllowDuplicates

Gets or sets whether the same tag can be added more than once. Tags are compared ignoring case.

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

Property Value

bool

ChipColor

Gets or sets the color of the tags. Defaults to Primary.

[Parameter]
public Color ChipColor { get; set; }

Property Value

Color

ChipVariant

Gets or sets the variant of the tags. Defaults to Outlined, the same as the chips of a multiple TwSelect<T>.

[Parameter]
public ButtonVariant ChipVariant { get; set; }

Property Value

ButtonVariant

Clearable

Gets or sets whether a button that removes every tag is shown at the end of the field. It is disabled while the field is empty.

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

Property Value

bool

Remarks

The button is not a Tab stop, since keyboard users can remove tags with Backspace, and it is hidden while the field is disabled or read-only.

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

HelperText

Gets or sets the text shown under the field, such as a hint on how to add tags. It is read out with the field.

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

Property Value

string

MaxSuggestions

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

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

Property Value

int?

MaxTagLength

Gets or sets the most characters a tag can have, or null (the default) for no limit.

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

Property Value

int?

MaxTags

Gets or sets the most tags the field holds, or null (the default) for no limit.

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

Property Value

int?

MinCharacters

Gets or sets how many characters must be typed before suggestions are shown. Defaults to 1. Arrow Down opens the suggestions whatever has been typed.

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

Property Value

int

OnClear

Gets or sets the callback invoked after the clear button (see Clearable) has removed every tag.

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

Property Value

EventCallback

OnTagAdded

Gets or sets the callback invoked with each tag the user adds.

[Parameter]
public EventCallback<string> OnTagAdded { get; set; }

Property Value

EventCallback<string>

OnTagRejected

Gets or sets the callback invoked when a tag cannot be added: a duplicate, one too many, one too long, or one TagValidator rejected.

[Parameter]
public EventCallback<TagRejectedEventArgs> OnTagRejected { get; set; }

Property Value

EventCallback<TagRejectedEventArgs>

OnTagRemoved

Gets or sets the callback invoked with each tag the user removes.

[Parameter]
public EventCallback<string> OnTagRemoved { get; set; }

Property Value

EventCallback<string>

Placeholder

Gets or sets the placeholder text shown while the field has no tags.

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

Property Value

string

Required

Gets or sets whether at least one tag is required. The field is announced as required by assistive technology.

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

Property Value

bool

Remarks

Add a validation attribute such as [MinLength(1)] to the bound property for the form to enforce it.

SearchFunc

Gets or sets a function that returns the suggestions for the typed text, for searching on a server or a large data set. When set, Suggestions is not used.

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

Property Value

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

Remarks

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

Separators

Gets or sets the characters that turn the typed text into a tag when typed, in addition to Enter. Pasted text is split on them as well as on line breaks. Defaults to a comma.

[Parameter]
public IReadOnlyCollection<char> Separators { get; set; }

Property Value

IReadOnlyCollection<char>

Remarks

Use an empty list to add tags with Enter only, so a tag can contain commas. Adding a space lets the user add a tag per word.

Suggestions

Gets or sets the tags to suggest as the user types. Ignored when SearchFunc is set.

[Parameter]
public IEnumerable<string> Suggestions { get; set; }

Property Value

IEnumerable<string>

Remarks

Suggestions are matched ignoring case and accents, and tags already in the field are left out unless AllowDuplicates is set. The user can still add any other text.

TagValidator

Gets or sets a function that checks each tag before it is added. Return null or an empty string to accept the tag, or a message to reject it. The message is shown under the field.

[Parameter]
public Func<string, string?>? TagValidator { get; set; }

Property Value

Func<string, string>

Value

Gets or sets the tags in the field.

[Parameter]
public IReadOnlyList<string> Value { get; set; }

Property Value

IReadOnlyList<string>

ValueChanged

Gets or sets the callback that is invoked with the new list whenever a tag is added or removed.

[Parameter]
public EventCallback<IReadOnlyList<string>> ValueChanged { get; set; }

Property Value

EventCallback<IReadOnlyList<string>>

ValueExpression

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

[Parameter]
public Expression<Func<IReadOnlyList<string>>>? ValueExpression { get; set; }

Property Value

Expression<Func<IReadOnlyList<string>>>

Methods

BuildRenderTree(RenderTreeBuilder)

Renders the component to the supplied RenderTreeBuilder.

protected override void BuildRenderTree(RenderTreeBuilder __builder)

Parameters

__builder RenderTreeBuilder

ClearAsync()

Removes every tag and the typed text, 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.

CommitTextAsync(string?)

Turns the text in the field into tags, called by twTagInput.attach when the user presses Enter or types one of the Separators. Text that cannot be added stays in the field.

[JSInvokable("CommitText")]
public Task CommitTextAsync(string? value)

Parameters

value string

The text in the field.

Returns

Task

A task that completes once the tags have been added.

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.

FocusAsync()

Moves focus to the text input.

public Task FocusAsync()

Returns

Task

A task that completes once focus has moved.

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 suggestions from the keyboard (Arrow Down or Arrow Up in the field, called by twAutocomplete.attach) and highlights the first or last one.

[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 suggestions are shown.

PasteTextAsync(string?)

Adds a tag for each part of pasted text, split on the Separators and line breaks. Called by twTagInput.attach when the pasted text holds more than one tag.

[JSInvokable("PasteText")]
public Task PasteTextAsync(string? value)

Parameters

value string

The field's text with the pasted text in place.

Returns

Task

A task that completes once the tags have been added.

SelectOptionAsync(int)

Adds a suggestion as a tag, by click or by Enter on the highlighted one (see twAutocomplete.attach), and empties the text. 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 tag is added.