Class TwMaskedInput
- Namespace
- TwBlazor.Components
- Assembly
- TwBlazor.dll
Represents a text field that enforces a structured format as the user types, such as a phone number, a social security number or a custom code.
public class TwMaskedInput : TwBlazorTextInputComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, ITwInputComponent, IAsyncDisposable
- Inheritance
-
TwMaskedInput
- Implements
- Inherited Members
Remarks
The field is a TwTextfield<T>, so it shares its variants, sizes, label, validation and error display. The format is described by Mask, a string of tokens and literals. The raw, unformatted characters are held in Value; the literals and PlaceholderChars are only shown in the field.
Masking itself runs in the browser, so pasting, IME input, Backspace and Delete all re-format to the same shape and the field never holds a value that breaks the mask. While ShowMask is set, an empty field shows the full mask so its structure is visible before any input.
Constructors
TwMaskedInput()
public TwMaskedInput()
Properties
BindEvent
Gets or sets the name of the DOM event that commits typed text. Defaults to onchange, which commits when
the user leaves the field. Set it to oninput to update the value on every keystroke.
[Parameter]
public string BindEvent { get; set; }
Property Value
Clearable
Gets or sets whether the text field's clear button is shown at the end of the field.
[Parameter]
public bool Clearable { get; set; }
Property Value
InputRef
Gets the reference to the rendered <input> element.
public ElementReference InputRef { get; }
Property Value
Mask
Gets or sets the mask the field enforces.
[Parameter]
[EditorRequired]
public string Mask { get; set; }
Property Value
Remarks
Each character describes one position: 9 accepts a digit 0-9, A a letter A-Z or a-z and *
either. Any other character is a literal shown between the typed characters, such as (, ),
- or a space. For example (999) 999-9999 for a phone number.
OnBlur
Gets or sets the callback invoked when the field 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 field is clicked.
[Parameter]
public EventCallback<MouseEventArgs> OnClick { get; set; }
Property Value
OnFocus
Gets or sets the callback invoked when the field 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 field has focus.
[Parameter]
public EventCallback<KeyboardEventArgs> OnKeyDown { get; set; }
Property Value
Placeholder
[Parameter]
public string Placeholder { get; set; }
Property Value
PlaceholderChar
Gets or sets the character shown for each unfilled mask position. Defaults to _.
[Parameter]
public char PlaceholderChar { 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
ShowMask
Gets or sets whether an empty field shows the full mask as a placeholder. Defaults to true.
[Parameter]
public bool ShowMask { get; set; }
Property Value
Remarks
When false, an empty field is left empty, and a partially filled field shows only the typed characters and the literals that lead up to them.
Validator
Gets or sets an optional asynchronous validator invoked with the raw value whenever it changes.
[Parameter]
public Func<string, Task<string?>>? Validator { get; set; }
Property Value
Remarks
Return null or an empty string to mark the value valid, or a message to mark the field invalid and show the message below it. The validator runs after ValueChanged is invoked.
Value
Gets or sets the current value as the raw, unformatted characters, without the mask's literals or placeholders.
[Parameter]
public string Value { get; set; }
Property Value
Remarks
Works with ValueChanged for @bind-Value. For the mask (999) 999-9999, the value
1234567890 is shown as (123) 456-7890.
ValueChanged
Gets or sets the callback that is invoked when the raw value changes.
[Parameter]
public EventCallback<string> ValueChanged { get; set; }
Property Value
Remarks
Invoked when typed text is committed (see BindEvent) and when the field is cleared.
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<string>>? ValueExpression { get; set; }
Property Value
ownErrorMessage
Gets the field's own error message while it is invalid for a reason the form does not know about, such as a failed validator or text that could not be read, or null. Listed in the form's error summary.
protected override string? ownErrorMessage { get; }
Property Value
Methods
BuildRenderTree(RenderTreeBuilder)
Renders the component to the supplied RenderTreeBuilder.
protected override void BuildRenderTree(RenderTreeBuilder __builder)
Parameters
__builderRenderTreeBuilder
ClearAsync()
Empties the field: clears the raw value, restores the mask placeholder and notifies the consumer.
public Task ClearAsync()
Returns
- Task
A task that completes once the value has been cleared and the consumer notified.
Remarks
Does nothing while the field is disabled or read-only. The mask is redrawn even when Value was already empty, so the field never stays blank.
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()
Removes the input listener from the field.
public override ValueTask DisposeAsync()
Returns
- ValueTask
A task that completes once the listener is removed.
FocusAsync()
Moves focus to the field.
public Task FocusAsync()
Returns
- Task
A task that completes once focus has been requested.
OnAfterRenderAsync(bool)
Method invoked after each time the component has been rendered interactively and the UI has finished updating (for example, after elements have been added to the browser DOM). Any ElementReference fields will be populated by the time this runs.
This method is not invoked during prerendering or server-side rendering, because those processes are not attached to any live browser DOM and are already complete before the DOM is updated.
Note that the component does not automatically re-render after the completion of any returned Task, because that would cause an infinite render loop.
protected override Task OnAfterRenderAsync(bool firstRender)
Parameters
firstRenderboolSet to
trueif this is the first time OnAfterRender(bool) has been invoked on this component instance; otherwisefalse.
Returns
Remarks
The OnAfterRender(bool) and OnAfterRenderAsync(bool) lifecycle methods
are useful for performing interop, or interacting with values received from @ref.
Use the firstRender parameter to ensure that initialization work is only performed
once.
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()