TextInput
The TextInput component is a simple spacer tool. It allows sibling HTML elements to have a consistent margin between them.
How it works #
The TextInput component provides a simple way to capture user input in a text field, supporting two-way data binding for seamless integration with your data model.
How to use:
This demo shows the basic setup for capturing and displaying user input using the
How to use:
- Add the
TextInputcomponent to your page. - Bind its
Valueproperty to a variable using@bind-Valuefor automatic updates. - Display or process the bound value as needed in your UI or logic.
TextInput component.
Entered text:
<TextInput @bind-Value="@enteredText" />
<div class="mb-3">Entered text: @enteredText</div>
@code {
private string? enteredText = null;
}Bind event #
The TextInput component allows you to control when the bound value updates by specifying the
How to use:
This demo demonstrates how to update the bound value in real-time as the user types.
BindEvent parameter.
How to use:
- Use
@bind-Valueto bind the input value to a variable. - Set
BindEvent="BindEvent.OnInput"to update the value on every keystroke, instead of the defaultonchangeevent. - Use the bound variable in your UI or logic as needed.
Entered text:
<TextInput @bind-Value="@enteredText" BindEvent="BindEvent.OnInput" />
<div class="mb-3">Entered text: @enteredText</div>
@code {
private string? enteredText = null;
}Colors #
The TextInput component supports various color themes to match your application's style.
How to use:
This demo shows how to apply different color styles to the input field.
How to use:
- Add the
TextInputcomponent to your page. - Set the
Colorproperty to one of the available options (e.g.,Primary,Info,Success,Warning,Danger,Link). - Bind the
Valueas needed.
<TextInput Class="mb-2" Color="TextInputColor.Link" Placeholder="Link input text" @bind-Value="@value1" />
<TextInput Class="mb-2" Color="TextInputColor.Primary" Placeholder="Primary input text" @bind-Value="@value2" />
<TextInput Class="mb-2" Color="TextInputColor.Info" Placeholder="Info input text" @bind-Value="@value3" />
<TextInput Class="mb-2" Color="TextInputColor.Success" Placeholder="Success input text" @bind-Value="@value4" />
<TextInput Class="mb-2" Color="TextInputColor.Warning" Placeholder="Warning input text" @bind-Value="@value5" />
<TextInput Class="mb-2" Color="TextInputColor.Danger" Placeholder="Danger input text" @bind-Value="@value6" />
@code {
private string? value1 = null;
private string? value2 = null;
private string? value3 = null;
private string? value4 = null;
private string? value5 = null;
private string? value6 = null;
}Sizes #
The TextInput component can be rendered in multiple sizes to fit different UI requirements.
How to use:
This demo illustrates the available size options for the input field.
How to use:
- Add the
TextInputcomponent to your page. - Set the
Sizeproperty toSmall,Normal,Medium, orLargeas needed. - Bind the
Valueproperty for user input.
<TextInput Class="mb-2" Size="TextInputSize.Small" Placeholder="Small input text" @bind-Value="@value1" />
<TextInput Class="mb-2" Size="TextInputSize.Normal" Placeholder="Normal input text" @bind-Value="@value2" />
<TextInput Class="mb-2" Size="TextInputSize.Medium" Placeholder="Medium input text" @bind-Value="@value3" />
<TextInput Class="mb-2" Size="TextInputSize.Large" Placeholder="Large input text" @bind-Value="@value4" />
@code {
private string? value1 = null;
private string? value2 = null;
private string? value3 = null;
private string? value4 = null;
}Styles #
The TextInput component supports additional style options, such as rounded corners, to enhance the visual appearance.
How to use:
This demo shows how to apply a rounded style to the input field.
How to use:
- Add the
TextInputcomponent to your page. - Set
IsRounded="true"to enable rounded corners. - Bind the
Valueproperty as usual.
<TextInput IsRounded="true" Placeholder="Rounded input text" @bind-Value="@value1" />
@code {
private string? value1 = null;
}States #
The TextInput component can visually represent different states, such as hovered, focused, or loading, to provide user feedback.
How to use:
This demo demonstrates how to display different visual states for the input field.
How to use:
- Add the
TextInputcomponent to your page. - Set the
Stateproperty toHovered,Focused, orLoadingto display the corresponding visual state. - Bind the
Valueproperty as needed.
<TextInput Class="mb-2" Placeholder="Normal input text" @bind-Value="@value1" />
<TextInput Class="mb-2" State="TextInputState.Hovered" Placeholder="Hovered input text" @bind-Value="@value2" />
<TextInput Class="mb-2" State="TextInputState.Focused" Placeholder="Focused input text" @bind-Value="@value3" />
<TextInput Class="mb-2" State="TextInputState.Loading" Placeholder="Loading input text" @bind-Value="@value4" />
@code {
private string? value1 = null;
private string? value2 = null;
private string? value3 = null;
private string? value4 = null;
}Disabled #
The TextInput component can be disabled to prevent user interaction, making it read-only.
How to use:
This demo shows how to render a disabled input field.
How to use:
- Add the
TextInputcomponent to your page. - Set
Disabled="true"to disable the input field. - Optionally, provide a placeholder or value to display in the disabled field.
<TextInput Class="mb-2" Placeholder="Disabled input text" @bind-Value="@value1" Disabled="true" />
@code {
private string? value1 = null;
}Validations #
The TextInput component integrates with Blazor's form validation system, supporting data annotations and real-time feedback.
How to use:
This demo demonstrates how to use
How to use:
- Wrap your
TextInputcomponents in anEditFormand provide a model with validation attributes. - Bind each
TextInputto a property on your model using@bind-Value. - Use
ValidationMessageto display validation errors for each field. - Set
BindEvent="BindEvent.OnInput"for real-time validation as the user types.
TextInput with form validation and display error messages.
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms
<style>
/* validations */
.valid.modified:not([type=checkbox]) {
border-color: hsl(var(--bulma-success-h), var(--bulma-success-s), var(--bulma-success-l)) !important;
outline: 1px solid hsl(var(--bulma-success-h), var(--bulma-success-s), var(--bulma-success-l)) !important;
}
.invalid {
border-color: hsl(var(--bulma-danger-h), var(--bulma-danger-s), var(--bulma-danger-l)) !important;
outline: 1px solid hsl(var(--bulma-danger-h), var(--bulma-danger-s), var(--bulma-danger-l)) !important;
}
.validation-message {
color: hsl(var(--bulma-danger-h), var(--bulma-danger-s), var(--bulma-danger-l)) !important;
}
</style>
<EditForm EditContext="@editContext" OnValidSubmit="HandleOnValidSubmit">
<DataAnnotationsValidator />
<div class="form-group row mb-3">
<label class="col-md-2 col-form-label">First Name: <span class="text-danger">*</span></label>
<div class="col-md-10">
<TextInput @bind-Value="@customer.FirstName" BindEvent="BindEvent.OnInput" Placeholder="Enter first name" />
<ValidationMessage For="@(() => customer.FirstName)" />
</div>
</div>
<div class="form-group row mb-3">
<label class="col-md-2 col-form-label">Last Name: <span class="text-danger">*</span></label>
<div class="col-md-10">
<TextInput @bind-Value="@customer.LastName" BindEvent="BindEvent.OnInput" Placeholder="Enter last name" />
<ValidationMessage For="@(() => customer.LastName)" />
</div>
</div>
<div class="row">
<div class="col-md-12 text-right">
<Button Type="ButtonType.Button" Color="ButtonColor.Light" Size="ButtonSize.Small" Class="float-end" @onclick="ResetForm">Reset</Button>
<Button Type="ButtonType.Submit" Color="ButtonColor.Success" Size="ButtonSize.Small" Class="float-end me-2">Submit</Button>
</div>
</div>
</EditForm>
@code {
private Customer customer = new();
private EditContext editContext = default!;
protected override void OnInitialized()
{
editContext = new EditContext(customer);
base.OnInitialized();
}
public void HandleOnValidSubmit()
{
// additional check
if (editContext.Validate())
{
// do something
// submit the form
Console.WriteLine("Form submitted successfully");
}
}
private void ResetForm()
{
customer = new();
editContext = new EditContext(customer);
}
public class Customer
{
[Required(ErrorMessage = "First name required.")]
public string? FirstName { get; set; }
[Required(ErrorMessage = "Last name required.")]
public string? LastName { get; set; }
}
}
Alignment #
The TextInput component allows you to control the text alignment within the input field.
How to use:
This demo shows how to adjust the text alignment inside the input field.
How to use:
- Add the
TextInputcomponent to your page. - Set the
TextAlignmentproperty toLeft,Center, orRightto align the text as desired. - Bind the
Valueproperty for user input.
<TextInput Class="mb-2" @bind-Value="@enteredText1" />
<TextInput Class="mb-2" @bind-Value="@enteredText2" TextAlignment="TextAlignment.Left" />
<TextInput Class="mb-2" @bind-Value="@enteredText3" TextAlignment="TextAlignment.Center" />
<TextInput Class="mb-2" @bind-Value="@enteredText4" TextAlignment="TextAlignment.Right" />
@code {
private string? enteredText1 = "Sample text";
private string? enteredText2 = "Sample text";
private string? enteredText3 = "Sample text";
private string? enteredText4 = "Sample text";
}Events: ValueChanged #
The TextInput component exposes a
How to use (onchange):
This demo shows how to handle value changes using the default event.
ValueChanged event, allowing you to execute custom logic whenever the input value changes.
How to use (onchange):
- Add the
TextInputcomponent and bind itsValueproperty. - Set the
ValueChangedcallback to handle value changes. - By default, the event fires on
onchange(when the input loses focus or the user presses Enter).
Entered name:
<TextInput Value="@name"
ValueExpression="() => name"
ValueChanged="(value) => NameChanged(value)" />
<div class="mb-3">Entered name: @name</div>
@code {
private string? name = null;
private void NameChanged(string? value)
{
name = value;
// do something
}
}How to use (oninput):
This demo demonstrates handling value changes on every input event.
- Set the
BindEventproperty toBindEvent.OnInputto triggerValueChangedon every keystroke. - Bind the
Valueproperty and provide aValueChangedcallback as before. - Use this approach for real-time processing or validation as the user types.
Entered name:
<TextInput Value="@name"
ValueExpression="() => name"
ValueChanged="(value) => NameChanged(value)"
BindEvent="BindEvent.OnInput" />
<div class="mb-3">Entered name: @name</div>
@code {
private string? name = null;
private void NameChanged(string? value)
{
name = value;
// do something
}
}