Skip to content

Commit 343e080

Browse files
authored
CodeInput Try (#400)
1 parent c043a9f commit 343e080

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

CodeBeam.MudBlazor.Extensions/Base/MudBaseInputExtended.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ protected MudBaseInputExtended() : base(new DefaultConverter<T>()) { }
4949
/// <returns></returns>
5050
protected bool GetReadOnlyState() => ReadOnly || ParentReadOnly;
5151

52+
/// <summary>
53+
/// Fires on input.
54+
/// </summary>
55+
[Parameter] public EventCallback OnInput { get; set; }
56+
57+
/// <summary>
58+
/// Fires on change.
59+
/// </summary>
60+
[Parameter] public EventCallback OnChange { get; set; }
61+
5262
/// <summary>
5363
/// If true, the input will take up the full width of its container.
5464
/// </summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
@for (int i = 0; i < Count; i++)
77
{
88
int a = i;
9-
<MudTextField @ref="_elementReferences[a]" T="T" Class="@InputClassname" Style="@(Margin == Margin.Dense ? "width: 32px" : "width: 42px")" MaxLength="1"
10-
@onkeydown="HandleKeyDown" @onfocus="@(() => CheckFocus(a))" @onblur="SetValue"
9+
<MudTextFieldExtended @ref="_elementReferences[a]" T="T" Class="@InputClassname" Style="@(Margin == Margin.Dense ? "width: 32px" : "width: 42px")" MaxLength="1"
10+
@onkeydown="HandleKeyDown" @onfocus="@(() => CheckFocus(a))" @onblur="SetValue" OnInput="OnInputHandler"
1111
Variant="@Variant" Margin="@Margin" Disabled="Disabled" ReadOnly="ReadOnly" Immediate="true" InputType="InputType" />
1212
}
1313
</div>

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ public MudCodeInput() : base(new DefaultConverter<T>())
3232

3333
private async Task OnValueChanged()
3434
{
35-
await SetValueFromOutside(Value);
35+
await SetValueFromOutside(_theValue.Value);
3636
}
3737

38-
private void OnCountChanged()
38+
private async Task OnCountChanged()
3939
{
40-
if (Count < 0)
40+
if (_count.Value < 0)
4141
{
42-
Count = 0;
42+
await _count.SetValueAsync(0);
4343
}
4444

45-
if (12 < Count)
45+
if (12 < _count.Value)
4646
{
47-
Count = 12;
47+
await _count.SetValueAsync(12);
4848
}
4949
}
5050

@@ -65,7 +65,7 @@ private void OnCountChanged()
6565
.AddClass(InputClass)
6666
.Build();
6767

68-
private List<MudTextField<T>> _elementReferences = new();
68+
private List<MudTextFieldExtended<T>> _elementReferences = new();
6969

7070
/// <summary>
7171
/// The CSS classes for each input, seperated by space.
@@ -137,6 +137,11 @@ private void OnCountChanged()
137137
[Category(CategoryTypes.FormComponent.Behavior)]
138138
public Margin Margin { get; set; }
139139

140+
private async Task OnInputHandler()
141+
{
142+
await FocusNext();
143+
}
144+
140145
/// <summary>
141146
/// Protected keydown event.
142147
/// </summary>
@@ -160,7 +165,7 @@ protected async Task HandleKeyDown(KeyboardEventArgs arg)
160165
return;
161166
}
162167

163-
if (arg.Key.Length == 1 || arg.Key == "ArrowRight")
168+
if (arg.Key == "ArrowRight")
164169
{
165170
await FocusNext();
166171
}
@@ -183,7 +188,7 @@ protected void CheckFocus(int count)
183188
/// <returns></returns>
184189
public async Task FocusNext()
185190
{
186-
if (_lastFocusedIndex >= Count - 1)
191+
if (_lastFocusedIndex >= _count.Value - 1)
187192
{
188193
await _elementReferences[_lastFocusedIndex].BlurAsync();
189194
await _elementReferences[_lastFocusedIndex].FocusAsync();
@@ -221,7 +226,7 @@ private void SyncReferences()
221226
_elementReferences.Clear();
222227
for (int i = 0; i < 12; i++)
223228
{
224-
_elementReferences.Add(new MudTextField<T>());
229+
_elementReferences.Add(new MudTextFieldExtended<T>());
225230
}
226231
}
227232

@@ -232,7 +237,7 @@ private void SyncReferences()
232237
public async Task SetValue()
233238
{
234239
string result = "";
235-
for (int i = 0; i < Count; i++)
240+
for (int i = 0; i < _count.Value; i++)
236241
{
237242
var val = _elementReferences[i].Value?.ToString();
238243
if (val == null)
@@ -254,12 +259,12 @@ public async Task SetValue()
254259
public async Task SetValueFromOutside(T? value)
255260
{
256261
string? val = Converter.Set(value);
257-
if (Count < val?.Length)
262+
if (_count.Value < val?.Length)
258263
{
259-
val = val.Substring(0, Count);
264+
val = val.Substring(0, _count.Value);
260265
}
261266
await _theValue.SetValueAsync(Converter.Get(val));
262-
for (int i = 0; i < Count; i++)
267+
for (int i = 0; i < _count.Value; i++)
263268
{
264269
if (i < val?.Length)
265270
{

CodeBeam.MudBlazor.Extensions/Components/InputExtended/MudInputExtended.razor.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,6 @@ public virtual async Task ForceAutoSize()
176176
/// </summary>
177177
[Parameter] public bool AutoSize { get; set; }
178178

179-
/// <summary>
180-
/// Fires on input.
181-
/// </summary>
182-
[Parameter] public EventCallback OnInput { get; set; }
183-
184-
/// <summary>
185-
/// Fires on change.
186-
/// </summary>
187-
[Parameter] public EventCallback OnChange { get; set; }
188-
189179
/// <summary>
190180
/// Paste hook for descendants.
191181
/// </summary>

CodeBeam.MudBlazor.Extensions/Components/TextFieldExtended/MudTextFieldExtended.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
ErrorId="@ErrorId"
4343
Immediate="@Immediate"
4444
Margin="@Margin"
45+
OnInput="@OnInput"
4546
OnBlur="@OnBlurredAsync"
4647
OnKeyDown="@InvokeKeyDownAsync"
4748
OnInternalInputChanged="OnChange"

0 commit comments

Comments
 (0)