Class TwTextfield<T>
- Namespace
- TwBlazor.Components
- Assembly
- TwBlazor.dll
Represents a generic text input field component that supports two-way data binding.
public class TwTextfield<T> : TwBlazorTextInputComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, ITwInputComponent, IAsyncDisposable
Type Parameters
TThe type of value bound to the text field. Common types include string, int, decimal, and DateTime.
- Inheritance
-
TwTextfield<T>
- Implements
- Inherited Members
Remarks
The TwTextfield component is a flexible input control that supports various input types (text, password, email, number, etc.) and provides customizable styling for both the input field and its label. Set Multiline to render a <textarea> for longer, multi-line text instead. It implements two-way binding through the Value and ValueChanged parameters, allowing seamless integration with Blazor's @bind directive. The component can be configured to trigger data binding on different DOM events using the BindEvent parameter.
Constructors
TwTextfield()
public TwTextfield()
Properties
AutoGrow
Gets or sets whether a Multiline field grows taller as the user types instead of scrolling.
[Parameter]
public bool AutoGrow { get; set; }
Property Value
Remarks
Uses the CSS field-sizing property, so in browsers without support the field keeps its
Rows height and scrolls. Manual resizing is turned off while this is set.
BindEvent
Gets or sets the name of the DOM event that triggers data binding for the associated element.
[Parameter]
public string BindEvent { get; set; }
Property Value
Remarks
The default value is "onchange", which binds on input changes. Set this property to a different event name (such as "oninput") to change when data binding occurs. The value should correspond to a valid DOM event for the element.
Clearable
Gets or sets whether a button that empties the field is shown at its end. It is disabled while the field is empty.
[Parameter]
public bool Clearable { get; set; }
Property Value
Remarks
Clearing sets Value to its default (an empty string becomes null), invokes ValueChanged and Validator, then OnClear, and moves focus back to the field. The button is not a Tab stop, since keyboard users can clear the text themselves, and it is hidden while the field is disabled or read-only. Not shown on a Multiline field.
EndContent
Gets or sets content drawn over the end of a single-line field, before the clear button, such as a unit or extra buttons.
[Parameter]
public RenderFragment? EndContent { get; set; }
Property Value
Remarks
Its items are laid out in a row 0.25rem apart. The field's padding is not widened for it: add end padding through
Class or a style attribute so the text does not run underneath it.
HasValue
Gets or sets whether the field currently holds a value, overriding the emptiness check the clear button uses.
[Parameter]
public bool? HasValue { get; set; }
Property Value
- bool?
Remarks
null (the default) keeps the usual check: a non-empty bound value. A wrapper component whose displayed text is never empty - such as a masked or numeric field that keeps its placeholders in the field - sets this from its own raw value, so the clear button is disabled while the real value is empty.
InputRef
Reference to the rendered <input> (or <textarea> when Multiline is set) element itself. Lets consumers (e.g. picker components with a decorative trigger icon that sits ahead of the input in DOM order) focus this specific element directly, rather than searching a wider container where the first focusable descendant found might not be this input.
public ElementReference InputRef { get; }
Property Value
InputType
Gets or sets the type of input expected by the control.
[Parameter]
public string InputType { get; set; }
Property Value
Remarks
Common values include "text", "password", "email", and "number". The value determines how the input is rendered and what kind of data is accepted.
MaxRows
Gets or sets the most lines an AutoGrow field grows to before it scrolls. null (the default) lets it grow without limit.
[Parameter]
public int? MaxRows { get; set; }
Property Value
- int?
Multiline
Gets or sets whether the field accepts multiple lines of text, rendering a <textarea> instead of an <input>.
[Parameter]
public bool Multiline { get; set; }
Property Value
Remarks
InputType is ignored while this is set. Enter inserts a new line rather than submitting the form, and Tab still moves focus out of the field.
OnBlur
Gets or sets the callback that is invoked when the textfield loses focus.
[Parameter]
public EventCallback<FocusEventArgs> OnBlur { get; set; }
Property Value
OnClear
Gets or sets the callback invoked after the clear button (see Clearable) has emptied the field.
[Parameter]
public EventCallback OnClear { get; set; }
Property Value
OnClick
Gets or sets the callback invoked when the input is clicked.
[Parameter]
public EventCallback<MouseEventArgs> OnClick { get; set; }
Property Value
OnFocus
Gets or sets the callback that is invoked when the textfield is focused.
[Parameter]
public EventCallback<FocusEventArgs> OnFocus { get; set; }
Property Value
OnKeyDown
Gets or sets the callback invoked when a key is pressed while the input has focus.
[Parameter]
public EventCallback<KeyboardEventArgs> OnKeyDown { get; set; }
Property Value
Placeholder
Gets or sets the placeholder text displayed when the input field is empty.
[Parameter]
public string Placeholder { get; set; }
Property Value
Required
Gets or sets whether a value is required. The field is marked required for the browser's own validation and announced as required by assistive technology.
[Parameter]
public bool Required { get; set; }
Property Value
Rows
Gets or sets the number of visible lines of text in a Multiline field. Defaults to 3.
[Parameter]
public int Rows { get; set; }
Property Value
Remarks
With AutoGrow set, this is the minimum height the field starts at before growing.
StartContent
Gets or sets content drawn over the start of a single-line field, such as an icon or a currency symbol.
[Parameter]
public RenderFragment? StartContent { get; set; }
Property Value
Remarks
The content is not part of the value, and the field's padding is not widened for it: add start padding through
Class or a style attribute so the text does not run underneath it.
Hide decorative content from screen readers with aria-hidden="true".
Validator
Gets or sets an optional asynchronous validator invoked with the new value whenever it changes.
[Parameter]
public Func<T, Task<string?>>? Validator { get; set; }
Property Value
Remarks
Return null or an empty string to mark the value valid. Return a non-empty string to mark the field invalid (see Invalid) and display it as the error message below the input (see ErrorMessage). The validator runs after ValueChanged is invoked, so consumers of this callback see the raw value before validation has been applied. Because the validator returns a Task<TResult>, it can perform asynchronous work such as an API call (e.g. checking whether a username is already taken) without blocking the UI thread.
Value
Gets or sets the current value of the text field.
[Parameter]
public T Value { get; set; }
Property Value
- T
Remarks
This property works in conjunction with ValueChanged to support two-way binding. The type parameter T determines what type of value the text field accepts and emits.
ValueChanged
Gets or sets the callback that is invoked when the value of the text field changes.
[Parameter]
public EventCallback<T> ValueChanged { get; set; }
Property Value
Remarks
This callback is invoked based on the event specified by BindEvent (default is "onchange"). It enables two-way data binding when used with the @bind-Value directive in Blazor.
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<T>>? ValueExpression { get; set; }
Property Value
- Expression<Func<T>>
Methods
BuildRenderTree(RenderTreeBuilder)
Renders the component to the supplied RenderTreeBuilder.
protected override void BuildRenderTree(RenderTreeBuilder __builder)
Parameters
__builderRenderTreeBuilder
ClearAsync()
Empties the field, notifies the consumer, and moves focus back to the field.
public Task ClearAsync()
Returns
- Task
A task that completes once the field has been cleared.
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.
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.
ValidateAsync()
Runs Validator on the current value, then reports whether the field is valid. Called by an enclosing TwForm when it is submitted.
public override Task<bool> ValidateAsync()