using Microsoft.AspNetCore.Components;
using System;
using System.Threading.Tasks;
namespace Radzen.Blazor
{
///
/// A single-line text input component that supports data binding, validation, and various input behaviors.
/// RadzenTextBox provides a styled text input with support for placeholders, autocomplete, immediate updates, and string trimming.
/// Supports two-way data binding via @bind-Value and form validation when used within Radzen forms.
/// Can be configured for immediate value updates as the user types or deferred updates on blur/change.
/// Use to automatically remove whitespace, and to limit input length.
///
///
/// Basic usage with two-way binding:
///
/// <RadzenTextBox @bind-Value=@username Placeholder="Enter username" />
///
/// With immediate updates and trimming:
///
/// <RadzenTextBox @bind-Value=@value Immediate="true" Trim="true" MaxLength="50" />
///
/// Read-only text box:
///
/// <RadzenTextBox Value=@displayValue ReadOnly="true" />
///
///
public partial class RadzenTextBox : FormComponentWithAutoComplete
{
///
/// Gets or sets whether the text box is read-only and cannot be edited by the user.
/// When true, the text box displays the value but prevents user input while still allowing selection and copying.
///
/// true if the text box is read-only; otherwise, false. Default is false.
[Parameter]
public bool ReadOnly { get; set; }
///
/// Gets or sets the maximum number of characters that can be entered in the text box.
/// When set, the browser will prevent users from typing beyond this limit.
///
/// The maximum character length, or null for no limit. Default is null.
[Parameter]
public long? MaxLength { get; set; }
///
/// Gets or sets whether to automatically remove leading and trailing whitespace from the value.
/// When enabled, whitespace is trimmed when the value changes.
///
/// true to trim whitespace; otherwise, false. Default is false.
[Parameter]
public bool Trim { get; set; }
///
/// Gets or sets whether the component should update the bound value immediately as the user types (oninput event),
/// rather than waiting for the input to lose focus (onchange event).
/// This enables real-time value updates but may trigger more frequent change events.
///
/// true for immediate updates; false for deferred updates. Default is false.
[Parameter]
public bool Immediate { get; set; }
///
/// Handles the change event of the underlying HTML input element.
/// Applies trimming if enabled and notifies the edit context and change listeners.
///
/// The change event arguments containing the new value.
/// A task representing the asynchronous operation.
protected async Task OnChange(ChangeEventArgs args)
{
ArgumentNullException.ThrowIfNull(args);
await SetValue($"{args.Value}");
}
///
/// Handles the set binding of the underlying HTML input element.
/// Applies trimming if enabled and notifies the edit context and change listeners.
///
/// string containing the new value.
/// A task representing the asynchronous operation.
protected async Task SetValue(string? value)
{
Value = $"{value}";
if (Trim)
{
Value = Value.Trim();
}
await ValueChanged.InvokeAsync(Value);
if (FieldIdentifier.FieldName != null)
{
EditContext?.NotifyFieldChanged(FieldIdentifier);
}
await Change.InvokeAsync(Value);
}
///
protected override string GetComponentCssClass()
{
return GetClassList("rz-textbox").ToString();
}
///
protected override string? GetId()
{
return Name ?? base.GetId();
}
}
}