Skip to content

Commit e167097

Browse files
authored
Add PasswordInput (#955)
1 parent d971eae commit e167097

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@namespace BlazorBootstrap
2+
@inherits BlazorBootstrapComponentBase
3+
@preservewhitespace true
4+
5+
<div class="input-group mb-3">
6+
<input
7+
@ref="@Element"
8+
type="@InputTextType"
9+
id="@Id"
10+
class="@BootstrapClass.FormControl"
11+
disabled="@Disabled"
12+
value="@Value"
13+
@attributes="@AdditionalAttributes"
14+
@onchange="OnChange">
15+
<button type="button" class="btn btn-primary btn-sm" @onclick="OnShowHidePasswordButtonClick"><i class="bi bi-eye-fill" /></button>
16+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using Microsoft.AspNetCore.Components.Forms;
2+
using Microsoft.AspNetCore.Components;
3+
using System.Linq.Expressions;
4+
5+
namespace BlazorBootstrap
6+
{
7+
public partial class PasswordInput
8+
{
9+
#region Fields and Constants
10+
11+
private FieldIdentifier fieldIdentifier;
12+
13+
private string? oldValue;
14+
15+
#endregion
16+
17+
#region Methods
18+
19+
protected override async Task OnInitializedAsync()
20+
{
21+
oldValue = Value;
22+
23+
AdditionalAttributes ??= new Dictionary<string, object>();
24+
25+
fieldIdentifier = FieldIdentifier.Create(ValueExpression);
26+
27+
await base.OnInitializedAsync();
28+
}
29+
30+
protected override async Task OnParametersSetAsync()
31+
{
32+
if (oldValue != Value)
33+
{
34+
await ValueChanged.InvokeAsync(Value);
35+
36+
EditContext?.NotifyFieldChanged(fieldIdentifier);
37+
38+
oldValue = Value;
39+
}
40+
}
41+
42+
public string InputTextType = "password";
43+
44+
void OnShowHidePasswordButtonClick()
45+
{
46+
if (this.InputTextType == "password")
47+
this.InputTextType = "text";
48+
else
49+
this.InputTextType = "password";
50+
}
51+
52+
/// <summary>
53+
/// Disables InputPassword.
54+
/// </summary>
55+
public void Disable() => Disabled = true;
56+
57+
/// <summary>
58+
/// Enables InputPassword.
59+
/// </summary>
60+
public void Enable() => Disabled = false;
61+
62+
/// <summary>
63+
/// This event is triggered only when the user changes the selection from the UI.
64+
/// </summary>
65+
/// <param name="args"></param>
66+
private async Task OnChange(ChangeEventArgs args)
67+
{
68+
Value = args.Value?.ToString();
69+
70+
await ValueChanged.InvokeAsync(Value);
71+
72+
EditContext?.NotifyFieldChanged(fieldIdentifier);
73+
74+
oldValue = Value;
75+
}
76+
77+
#endregion
78+
79+
#region Properties, Indexers
80+
81+
protected override string? ClassNames =>
82+
BuildClassNames(Class,
83+
(BootstrapClass.FormControl, true));
84+
85+
/// <summary>
86+
/// Gets or sets the disabled state.
87+
/// </summary>
88+
/// <remarks>
89+
/// Default value is false.
90+
/// </remarks>
91+
[Parameter]
92+
public bool Disabled { get; set; }
93+
94+
[CascadingParameter] private EditContext EditContext { get; set; } = default!;
95+
96+
private string fieldCssClasses => EditContext?.FieldCssClass(fieldIdentifier) ?? "";
97+
98+
99+
/// <summary>
100+
/// Gets or sets the value.
101+
/// </summary>
102+
/// <remarks>
103+
/// Default value is null.
104+
/// </remarks>
105+
[Parameter]
106+
public string? Value { get; set; } = default!;
107+
108+
/// <summary>
109+
/// This event is fired when the inputpassword value changes.
110+
/// </summary>
111+
[Parameter]
112+
public EventCallback<string?> ValueChanged { get; set; } = default!;
113+
114+
[Parameter] public Expression<Func<string?>> ValueExpression { get; set; } = default!;
115+
116+
#endregion
117+
}
118+
}

0 commit comments

Comments
 (0)