@using System.Linq.Expressions
@inject IJSRuntime JSRuntime
@code {
private bool _showPassword = false;
private ElementReference inputElement;
///
/// Gets or sets the ID of the input field.
///
[Parameter]
public required string Id { get; set; }
///
/// Gets or sets the value of the input field.
///
[Parameter]
public required string Value { get; set; }
///
/// Gets or sets the event callback that is triggered when the value changes.
///
[Parameter]
public required EventCallback ValueChanged { get; set; }
///
/// Gets or sets the expression that identifies the value property.
///
[Parameter]
public Expression>? ValueExpression { get; set; }
///
/// Gets or sets the placeholder text for the input field.
///
[Parameter]
public required string Placeholder { get; set; }
///
/// Gets or sets additional attributes for the input field.
///
[Parameter(CaptureUnmatchedValues = true)]
public Dictionary? AdditionalAttributes { get; set; } = new();
///
/// Handles input changes and triggers the ValueChanged callback immediately.
///
private async Task OnInputChanged(ChangeEventArgs e)
{
var newValue = e.Value?.ToString();
await ValueChanged.InvokeAsync(newValue);
}
///
/// Toggles the password visibility.
///
private void TogglePasswordVisibility()
{
_showPassword = !_showPassword;
}
///
/// Focuses the input field.
///
/// A task that represents the asynchronous focus operation.
public async Task FocusAsync()
{
try
{
await inputElement.FocusAsync();
}
catch (Exception)
{
// Fallback to JS focus if ElementReference focus fails
await JSRuntime.InvokeVoidAsync("eval", $"document.getElementById('{Id}')?.focus()");
}
}
}