Skip to content

Commit 941c1d1

Browse files
authored
CodeInput Add ParameterState and PasswordField Label Fix (#398)
1 parent 57729c8 commit 941c1d1

File tree

4 files changed

+50
-54
lines changed

4 files changed

+50
-54
lines changed

CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/CodeInput/Examples/CodeInputExample1.razor

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</MudItem>
88

99
<MudItem xs="12" sm="4">
10-
<MudStack Spacing="4">
10+
<MudStack Spacing="2">
1111
<MudText>Value: @_value</MudText>
12-
<MudTextField @bind-Value="@_value" Immediate="true" Label="Two Way Bind Controller" />
12+
<MudTextField @bind-Value="@_value" Immediate="true" Label="Two Way Bind Controller" Variant="Variant.Outlined" />
1313
<MudNumericField @bind-Value="_count" Label="Count" Variant="Variant.Outlined" />
1414
<MudNumericField @bind-Value="_spacing" Label="Spacing" Variant="Variant.Outlined" />
1515
<MudSelect @bind-Value="_variant" Variant="Variant.Outlined" Label="Variant">
@@ -24,8 +24,8 @@
2424
<MudSelectItem Value="item">@item.ToDescriptionString()</MudSelectItem>
2525
}
2626
</MudSelect>
27-
<MudSwitchM3 @bind-Value="@_disabled" Label="Disabled" Color="Color.Primary" />
28-
<MudSwitchM3 @bind-Value="@_readonly" Label="ReadOnly" Color="Color.Primary" />
27+
<MudSwitchM3 @bind-Value="@_disabled" Label="Disabled" Color="Color.Secondary" />
28+
<MudSwitchM3 @bind-Value="@_readonly" Label="ReadOnly" Color="Color.Secondary" />
2929
</MudStack>
3030
</MudItem>
3131
</MudGrid>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<MudItem xs="12" sm="8" Class="d-flex align-center">
55
@if (_adornmentStart)
66
{
7-
<MudPasswordField @bind-Value="@_password" @bind-PasswordMode="@_passwordMode" Variant="Variant.Outlined" IconSize="_size" DisablePaste="_disablePaste">
7+
<MudPasswordField @bind-Value="@_password" @bind-PasswordMode="@_passwordMode" Variant="Variant.Outlined" Label="Password" IconSize="_size" DisablePaste="_disablePaste">
88
<AdornmentStart>
99
<MudIcon Icon="@Icons.Material.Filled.Key"/>
1010
</AdornmentStart>
1111
</MudPasswordField>
1212
}
1313
else
1414
{
15-
<MudPasswordField @bind-Value="@_password" @bind-PasswordMode="@_passwordMode" Variant="Variant.Outlined" IconSize="_size" DisablePaste="_disablePaste" />
15+
<MudPasswordField @bind-Value="@_password" @bind-PasswordMode="@_passwordMode" Variant="Variant.Outlined" Label="Password" IconSize="_size" DisablePaste="_disablePaste" />
1616
}
1717
</MudItem>
1818

CodeBeam.MudBlazor.Extensions/Components/CodeInput/MudCodeInput.razor.cs

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
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
78
{
89
/// <summary>
9-
///
10+
/// Inputs which each input box can contain only one character.
1011
/// </summary>
1112
/// <typeparam name="T"></typeparam>
1213
public partial class MudCodeInput<T> : MudFormComponent<T, string>
1314
{
1415
/// <summary>
15-
///
16+
/// MudCodeInput constructor.
1617
/// </summary>
17-
public MudCodeInput() : base(new DefaultConverter<T>()) { }
18+
public MudCodeInput() : base(new DefaultConverter<T>())
19+
{
20+
using var registerScope = CreateRegisterScope();
21+
_theValue = registerScope.RegisterParameter<T?>(nameof(Value))
22+
.WithParameter(() => Value)
23+
.WithEventCallback(() => ValueChanged)
24+
.WithChangeHandler(OnValueChanged);
25+
_count = registerScope.RegisterParameter<int>(nameof(Count))
26+
.WithParameter(() => Count)
27+
.WithChangeHandler(OnCountChanged);
28+
}
29+
30+
private readonly ParameterState<T?> _theValue;
31+
private readonly ParameterState<int> _count;
32+
33+
private async Task OnValueChanged()
34+
{
35+
await SetValueFromOutside(Value);
36+
}
37+
38+
private void OnCountChanged()
39+
{
40+
if (Count < 0)
41+
{
42+
Count = 0;
43+
}
44+
45+
if (12 < Count)
46+
{
47+
Count = 12;
48+
}
49+
}
1850

1951
/// <summary>
2052
/// Protected classes.
@@ -30,7 +62,7 @@ public MudCodeInput() : base(new DefaultConverter<T>()) { }
3062
protected string? InputClassname =>
3163
new CssBuilder("justify-text-center")
3264
.AddClass("mud-code", Variant != Variant.Text)
33-
.AddClass(ClassInput)
65+
.AddClass(InputClass)
3466
.Build();
3567

3668
private List<MudTextField<T>> _elementReferences = new();
@@ -40,35 +72,21 @@ public MudCodeInput() : base(new DefaultConverter<T>()) { }
4072
/// </summary>
4173
[Parameter]
4274
[Category(CategoryTypes.FormComponent.Behavior)]
43-
public string? ClassInput { get; set; }
75+
public string? InputClass { get; set; }
4476

4577
/// <summary>
4678
/// Type of the input element. It should be a valid HTML5 input type.
4779
/// </summary>
4880
[Parameter]
4981
[Category(CategoryTypes.FormComponent.Behavior)]
5082
public InputType InputType { get; set; } = InputType.Text;
51-
52-
private T? _theValue;
5383

5484
/// <summary>
5585
/// The value of the input.
5686
/// </summary>
5787
[Parameter]
5888
[Category(CategoryTypes.FormComponent.Behavior)]
59-
public T? Value
60-
{
61-
get => _theValue;
62-
set
63-
{
64-
if (Converter.Set(_theValue) == Converter.Set(value))
65-
{
66-
return;
67-
}
68-
_theValue = value;
69-
SetValueFromOutside(_theValue).CatchAndLog();
70-
}
71-
}
89+
public T? Value { get; set; }
7290

7391
/// <summary>
7492
/// The event fires when value changed.
@@ -77,32 +95,12 @@ public T? Value
7795
[Category(CategoryTypes.FormComponent.Behavior)]
7896
public EventCallback<T?> ValueChanged { get; set; }
7997

80-
private int _count;
8198
/// <summary>
8299
/// The number of text fields.
83100
/// </summary>
84101
[Parameter]
85102
[Category(CategoryTypes.FormComponent.Behavior)]
86-
public int Count
87-
{
88-
get => _count;
89-
set
90-
{
91-
if (value == _count || value < 0)
92-
{
93-
return;
94-
}
95-
96-
if (12 < value)
97-
{
98-
_count = 12;
99-
}
100-
else
101-
{
102-
_count = value;
103-
}
104-
}
105-
}
103+
public int Count { get; set; }
106104

107105
/// <summary>
108106
/// Determines the spacing between each input.
@@ -245,12 +243,11 @@ public async Task SetValue()
245243
result += val;
246244
}
247245

248-
Value = Converter.Get(result);
249-
await ValueChanged.InvokeAsync(Value);
246+
await _theValue.SetValueAsync(Converter.Get(result));
250247
}
251248

252249
/// <summary>
253-
///
250+
/// Call this method to set value programmatically.
254251
/// </summary>
255252
/// <param name="value"></param>
256253
/// <returns></returns>
@@ -261,7 +258,7 @@ public async Task SetValueFromOutside(T? value)
261258
{
262259
val = val.Substring(0, Count);
263260
}
264-
Value = Converter.Get(val);
261+
await _theValue.SetValueAsync(Converter.Get(val));
265262
for (int i = 0; i < Count; i++)
266263
{
267264
if (i < val?.Length)
@@ -273,9 +270,6 @@ public async Task SetValueFromOutside(T? value)
273270
await _elementReferences[i].SetText(null);
274271
}
275272
}
276-
277-
await ValueChanged.InvokeAsync(Value);
278-
StateHasChanged();
279273
}
280274

281275
}

CodeBeam.MudBlazor.Extensions/Components/PasswordField/MudPasswordField.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
@attributes="UserAttributes"
2727
InputType="@_passwordInput"
2828
Lines="@Lines"
29+
Label="@Label"
2930
Style="@Style"
3031
Variant="@Variant"
3132
TextUpdateSuppression="@TextUpdateSuppression"
@@ -76,6 +77,7 @@
7677
@attributes="UserAttributes"
7778
InputType="@_passwordInput"
7879
Lines="@Lines"
80+
Label="@Label"
7981
Style="@Style"
8082
Variant="@Variant"
8183
TextUpdateSuppression="@TextUpdateSuppression"

0 commit comments

Comments
 (0)