11using Microsoft . AspNetCore . Components ;
22using Microsoft . AspNetCore . Components . Web ;
33using MudBlazor ;
4+ using MudBlazor . State ;
45using MudBlazor . Utilities ;
56
67namespace 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