Skip to content

Commit 2476741

Browse files
authored
PasswordField: ParameterState (#411)
* PasswordField ParameterState * Remove Unnecessary Code
1 parent c8be30a commit 2476741

File tree

3 files changed

+26
-34
lines changed

3 files changed

+26
-34
lines changed

Diff for: CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/PasswordField/Examples/PasswordFieldExample1.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</MudGrid>
2828

2929
@code{
30-
string _password = "asdf";
30+
string? _password = "asdf";
3131
bool _passwordMode;
3232
bool _adornmentStart;
3333
bool _disablePaste;

Diff for: CodeBeam.MudBlazor.Extensions/Components/PasswordField/MudPasswordField.razor

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<MudInputExtended T="string"
2525
@ref="InputReference"
2626
@attributes="UserAttributes"
27-
InputType="@_passwordInput"
27+
InputType="@GetPasswordInputType()"
2828
Lines="@Lines"
2929
Label="@Label"
3030
Style="@Style"
@@ -64,7 +64,7 @@
6464
}
6565
else
6666
{
67-
<MudIconButton Icon="@_passwordIcon" Color="@AdornmentColor" Size="@IconSize" OnClick="AdornmentClick" tabindex="@(AdornmentTabStop ? 0 : -1)"></MudIconButton>
67+
<MudIconButton Icon="@GetPasswordIcon()" Color="@AdornmentColor" Size="@IconSize" OnClick="AdornmentClick" tabindex="@(AdornmentTabStop ? 0 : -1)"></MudIconButton>
6868
}
6969
</AdornmentEnd>
7070
</MudInputExtended>
@@ -74,7 +74,7 @@
7474
<MudInputExtended T="string"
7575
@ref="InputReference"
7676
@attributes="UserAttributes"
77-
InputType="@_passwordInput"
77+
InputType="@GetPasswordInputType()"
7878
Lines="@Lines"
7979
Label="@Label"
8080
Style="@Style"
@@ -111,7 +111,7 @@
111111
}
112112
else
113113
{
114-
<MudIconButton Icon="@_passwordIcon" Color="@AdornmentColor" Size="@IconSize" OnClick="AdornmentClick" tabindex="@(AdornmentTabStop ? 0 : -1)"></MudIconButton>
114+
<MudIconButton Icon="@GetPasswordIcon()" Color="@AdornmentColor" Size="@IconSize" OnClick="AdornmentClick" tabindex="@(AdornmentTabStop ? 0 : -1)"></MudIconButton>
115115
}
116116
</AdornmentEnd>
117117
</MudInputExtended>

Diff for: CodeBeam.MudBlazor.Extensions/Components/PasswordField/MudPasswordField.razor.cs

+21-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Components;
22
using Microsoft.AspNetCore.Components.Web;
33
using MudBlazor;
4+
using MudBlazor.State;
45
using MudBlazor.Utilities;
56

67
namespace MudExtensions
@@ -11,6 +12,19 @@ namespace MudExtensions
1112
/// <typeparam name="T"></typeparam>
1213
public partial class MudPasswordField<T> : MudDebouncedInput<T>
1314
{
15+
/// <summary>
16+
/// MudPasswordField constructor.
17+
/// </summary>
18+
public MudPasswordField()
19+
{
20+
using var registerScope = CreateRegisterScope();
21+
_passwordMode = registerScope.RegisterParameter<bool>(nameof(PasswordMode))
22+
.WithParameter(() => PasswordMode)
23+
.WithEventCallback(() => PasswordModeChanged);
24+
}
25+
26+
private readonly ParameterState<bool> _passwordMode;
27+
1428
/// <summary>
1529
///
1630
/// </summary>
@@ -22,10 +36,11 @@ public partial class MudPasswordField<T> : MudDebouncedInput<T>
2236
/// <summary>
2337
///
2438
/// </summary>
25-
public MudInputExtended<string?> InputReference { get; private set; } = new();
26-
InputType _passwordInput = InputType.Password;
27-
string? _passwordIcon = Icons.Material.Filled.VisibilityOff;
28-
bool _passwordMode = true;
39+
public MudInputExtended<string?> InputReference { get; private set; } = null!;
40+
private InputType GetPasswordInputType() => _passwordMode.Value ? InputType.Password : InputType.Text;
41+
private string? GetPasswordIcon() => _passwordMode.Value ? Icons.Material.Filled.VisibilityOff : Icons.Material.Filled.Visibility;
42+
//InputType _passwordInput = InputType.Password;
43+
//string? _passwordIcon = Icons.Material.Filled.VisibilityOff;
2944

3045
[CascadingParameter(Name = "Standalone")]
3146
internal bool StandaloneEx { get; set; } = true;
@@ -152,30 +167,7 @@ private async Task OnMaskedValueChanged(string s)
152167
/// If true, masks text with password mode.
153168
/// </summary>
154169
[Parameter]
155-
public bool PasswordMode
156-
{
157-
get => _passwordMode;
158-
set
159-
{
160-
if (_passwordMode == value)
161-
{
162-
return;
163-
}
164-
_passwordMode = value;
165-
if (_passwordMode)
166-
{
167-
_passwordInput = InputType.Password;
168-
_passwordIcon = Icons.Material.Filled.VisibilityOff;
169-
}
170-
else
171-
{
172-
_passwordInput = InputType.Text;
173-
_passwordIcon = Icons.Material.Filled.Visibility;
174-
}
175-
176-
PasswordModeChanged.InvokeAsync(value).CatchAndLog();
177-
}
178-
}
170+
public bool PasswordMode { get; set; } = true;
179171

180172
/// <summary>
181173
/// Fires when password mode changed.
@@ -189,7 +181,7 @@ public bool PasswordMode
189181
/// <returns></returns>
190182
protected async Task AdornmentClick()
191183
{
192-
PasswordMode = !PasswordMode;
184+
await _passwordMode.SetValueAsync(!_passwordMode.Value);
193185
await OnAdornmentClick.InvokeAsync();
194186
}
195187

0 commit comments

Comments
 (0)