Files
aliasvault/apps/server/AliasVault.Client/Auth/Components/PasswordInputField.razor
2025-09-25 06:28:27 +02:00

109 lines
4.3 KiB
Plaintext

@using System.Linq.Expressions
@inject IJSRuntime JSRuntime
<div class="relative">
<InputText @attributes="AdditionalAttributes"
@ref="inputComponent"
id="@Id"
Value="@Value"
ValueChanged="ValueChanged"
ValueExpression="ValueExpression"
type="@(_showPassword ? "text" : "password")"
placeholder="@Placeholder"
autocomplete="off"
class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 pr-10 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" />
<button type="button"
@onclick="TogglePasswordVisibility"
class="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
aria-label="@(_showPassword ? "Hide password" : "Show password")">
@if (_showPassword)
{
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"></path>
</svg>
}
else
{
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
</svg>
}
</button>
</div>
@code {
private bool _showPassword = false;
private InputText? inputComponent;
/// <summary>
/// Gets or sets the ID of the input field.
/// </summary>
[Parameter]
public required string Id { get; set; }
/// <summary>
/// Gets or sets the value of the input field.
/// </summary>
[Parameter]
public required string Value { get; set; }
/// <summary>
/// Gets or sets the event callback that is triggered when the value changes.
/// </summary>
[Parameter]
public required EventCallback<string?> ValueChanged { get; set; }
/// <summary>
/// Gets or sets the expression that identifies the value property.
/// </summary>
[Parameter]
public required Expression<Func<string>> ValueExpression { get; set; }
/// <summary>
/// Gets or sets the placeholder text for the input field.
/// </summary>
[Parameter]
public required string Placeholder { get; set; }
/// <summary>
/// Gets or sets additional attributes for the input field.
/// </summary>
[Parameter(CaptureUnmatchedValues = true)]
public Dictionary<string, object?>? AdditionalAttributes { get; set; } = new();
/// <summary>
/// Toggles the password visibility.
/// </summary>
private void TogglePasswordVisibility()
{
_showPassword = !_showPassword;
}
/// <summary>
/// Focuses the input field.
/// </summary>
/// <returns>A task that represents the asynchronous focus operation.</returns>
public async Task FocusAsync()
{
try
{
if (inputComponent?.Element != null)
{
await inputComponent.Element.Value.FocusAsync();
}
else
{
// Fallback to JS focus if component element is not available
await JSRuntime.InvokeVoidAsync("eval", $"document.getElementById('{Id}')?.focus()");
}
}
catch (Exception)
{
// Final fallback to JS focus if ElementReference focus fails
await JSRuntime.InvokeVoidAsync("eval", $"document.getElementById('{Id}')?.focus()");
}
}
}