From e167097c96f9abdb99413ae6d87910a6c80b4f31 Mon Sep 17 00:00:00 2001 From: Diego Martinez Date: Wed, 18 Dec 2024 11:52:17 -0300 Subject: [PATCH] Add PasswordInput (#955) --- .../Form/PasswordInput/PasswordInput.razor | 16 +++ .../Form/PasswordInput/PasswordInput.razor.cs | 118 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 blazorbootstrap/Components/Form/PasswordInput/PasswordInput.razor create mode 100644 blazorbootstrap/Components/Form/PasswordInput/PasswordInput.razor.cs diff --git a/blazorbootstrap/Components/Form/PasswordInput/PasswordInput.razor b/blazorbootstrap/Components/Form/PasswordInput/PasswordInput.razor new file mode 100644 index 000000000..4123d7373 --- /dev/null +++ b/blazorbootstrap/Components/Form/PasswordInput/PasswordInput.razor @@ -0,0 +1,16 @@ +@namespace BlazorBootstrap +@inherits BlazorBootstrapComponentBase +@preservewhitespace true + +
+ + +
diff --git a/blazorbootstrap/Components/Form/PasswordInput/PasswordInput.razor.cs b/blazorbootstrap/Components/Form/PasswordInput/PasswordInput.razor.cs new file mode 100644 index 000000000..e69698788 --- /dev/null +++ b/blazorbootstrap/Components/Form/PasswordInput/PasswordInput.razor.cs @@ -0,0 +1,118 @@ +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components; +using System.Linq.Expressions; + +namespace BlazorBootstrap +{ + public partial class PasswordInput + { + #region Fields and Constants + + private FieldIdentifier fieldIdentifier; + + private string? oldValue; + + #endregion + + #region Methods + + protected override async Task OnInitializedAsync() + { + oldValue = Value; + + AdditionalAttributes ??= new Dictionary(); + + fieldIdentifier = FieldIdentifier.Create(ValueExpression); + + await base.OnInitializedAsync(); + } + + protected override async Task OnParametersSetAsync() + { + if (oldValue != Value) + { + await ValueChanged.InvokeAsync(Value); + + EditContext?.NotifyFieldChanged(fieldIdentifier); + + oldValue = Value; + } + } + + public string InputTextType = "password"; + + void OnShowHidePasswordButtonClick() + { + if (this.InputTextType == "password") + this.InputTextType = "text"; + else + this.InputTextType = "password"; + } + + /// + /// Disables InputPassword. + /// + public void Disable() => Disabled = true; + + /// + /// Enables InputPassword. + /// + public void Enable() => Disabled = false; + + /// + /// This event is triggered only when the user changes the selection from the UI. + /// + /// + private async Task OnChange(ChangeEventArgs args) + { + Value = args.Value?.ToString(); + + await ValueChanged.InvokeAsync(Value); + + EditContext?.NotifyFieldChanged(fieldIdentifier); + + oldValue = Value; + } + + #endregion + + #region Properties, Indexers + + protected override string? ClassNames => + BuildClassNames(Class, + (BootstrapClass.FormControl, true)); + + /// + /// Gets or sets the disabled state. + /// + /// + /// Default value is false. + /// + [Parameter] + public bool Disabled { get; set; } + + [CascadingParameter] private EditContext EditContext { get; set; } = default!; + + private string fieldCssClasses => EditContext?.FieldCssClass(fieldIdentifier) ?? ""; + + + /// + /// Gets or sets the value. + /// + /// + /// Default value is null. + /// + [Parameter] + public string? Value { get; set; } = default!; + + /// + /// This event is fired when the inputpassword value changes. + /// + [Parameter] + public EventCallback ValueChanged { get; set; } = default!; + + [Parameter] public Expression> ValueExpression { get; set; } = default!; + + #endregion + } +}