Skip to content

Commit 821d30e

Browse files
authored
CodeInput Changes (#402)
1 parent 343e080 commit 821d30e

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<MudGrid>
44
<MudItem xs="12" sm="8" Class="d-flex flex-column gap-8 align-center justify-center" Style="height: 500px">
5-
<MudCodeInput @ref="_textFieldGroup" T="string" @bind-Value="@_value" Count="@_count" Spacing="_spacing" Variant="@_variant" Margin="_margin"
6-
Disabled="@_disabled" ReadOnly="@_readonly" />
5+
<MudCodeInput T="string" @bind-Value="@_value" Count="@_count" Spacing="_spacing" Variant="@_variant" Margin="_margin"
6+
Disabled="@_disabled" ReadOnly="@_readonly" />
77
</MudItem>
88

99
<MudItem xs="12" sm="4">
@@ -30,8 +30,7 @@
3030
</MudItem>
3131
</MudGrid>
3232

33-
@code{
34-
MudCodeInput<string>? _textFieldGroup;
33+
@code {
3534
string? _value;
3635
int _count = 4;
3736
int _spacing = 2;

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
@typeparam T
33
@inherits MudFormComponent<T, string>
44

5-
<div class="@Classname" style="@Style">
6-
@for (int i = 0; i < Count; i++)
7-
{
8-
int a = i;
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"
11-
Variant="@Variant" Margin="@Margin" Disabled="Disabled" ReadOnly="ReadOnly" Immediate="true" InputType="InputType" />
12-
}
13-
</div>
5+
<MudInputControl Class="@Class" Style="@Style" Error="@Error" ErrorText="@ErrorText" Required="false">
6+
<InputContent>
7+
<div class="@Classname" style="@InnerStyle">
8+
@for (int i = 0; i < Count; i++)
9+
{
10+
int a = i;
11+
<MudTextFieldExtended @ref="_elementReferences[a]" T="T" Class="@InputClassname" Style="@(Margin == Margin.Dense ? "width: 32px" : "width: 42px")" MaxLength="1"
12+
@onkeydown="HandleKeyDown" @onfocus="@(() => CheckFocus(a))" @onblur="SetValue" OnInput="OnInputHandler" Error="@Error"
13+
Variant="@Variant" Margin="@Margin" Disabled="Disabled" ReadOnly="ReadOnly" Immediate="true" InputType="InputType" />
14+
}
15+
</div>
16+
</InputContent>
17+
</MudInputControl>
1418

1519
<style>
1620
.justify-text-center input {

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

Lines changed: 50 additions & 10 deletions
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.Interfaces;
45
using MudBlazor.State;
56
using MudBlazor.Utilities;
67

@@ -53,7 +54,8 @@ private async Task OnCountChanged()
5354
/// </summary>
5455
protected string? Classname =>
5556
new CssBuilder($"d-flex gap-{Spacing}")
56-
.AddClass(Class)
57+
.AddClass("mud-code-input-inner")
58+
.AddClass(InnerClass)
5759
.Build();
5860

5961
/// <summary>
@@ -74,6 +76,20 @@ private async Task OnCountChanged()
7476
[Category(CategoryTypes.FormComponent.Behavior)]
7577
public string? InputClass { get; set; }
7678

79+
/// <summary>
80+
/// The CSS classes for input container div, seperated by space.
81+
/// </summary>
82+
[Parameter]
83+
[Category(CategoryTypes.FormComponent.Behavior)]
84+
public string? InnerClass { get; set; }
85+
86+
/// <summary>
87+
/// The CSS styles for input container div.
88+
/// </summary>
89+
[Parameter]
90+
[Category(CategoryTypes.FormComponent.Behavior)]
91+
public string? InnerStyle { get; set; }
92+
7793
/// <summary>
7894
/// Type of the input element. It should be a valid HTML5 input type.
7995
/// </summary>
@@ -137,8 +153,15 @@ private async Task OnCountChanged()
137153
[Category(CategoryTypes.FormComponent.Behavior)]
138154
public Margin Margin { get; set; }
139155

156+
bool _skipInputEvent = false;
157+
bool _skipRefocus = false;
140158
private async Task OnInputHandler()
141159
{
160+
if (_skipInputEvent)
161+
{
162+
_skipInputEvent = false;
163+
return;
164+
}
142165
await FocusNext();
143166
}
144167

@@ -154,32 +177,49 @@ protected async Task HandleKeyDown(KeyboardEventArgs arg)
154177
return;
155178
}
156179

157-
if (RuntimeLocation.IsClientSide)
158-
{
159-
await Task.Delay(10);
160-
}
161-
162-
if (arg.Key == "Backspace" || arg.Key == "ArrowLeft")
180+
if (arg.Key == "Backspace" || arg.Key == "ArrowLeft" || arg.Key == "Delete")
163181
{
182+
_skipInputEvent = true;
183+
_skipRefocus = true;
184+
if (arg.Key == "Delete")
185+
{
186+
await _elementReferences[_lastFocusedIndex].Clear();
187+
_skipInputEvent = false;
188+
}
189+
if (RuntimeLocation.IsClientSide)
190+
{
191+
await Task.Delay(10);
192+
}
164193
await FocusPrevious();
165194
return;
166195
}
167196

168197
if (arg.Key == "ArrowRight")
169198
{
199+
if (RuntimeLocation.IsClientSide)
200+
{
201+
await Task.Delay(10);
202+
}
170203
await FocusNext();
171204
}
172-
205+
173206
}
174207

175208
private int _lastFocusedIndex = 0;
176209
/// <summary>
177210
///
178211
/// </summary>
179212
/// <param name="count"></param>
180-
protected void CheckFocus(int count)
213+
protected async Task CheckFocus(int count)
181214
{
182215
_lastFocusedIndex = count;
216+
if (_skipRefocus == true)
217+
{
218+
_skipRefocus = false;
219+
return;
220+
}
221+
string str = Converter.Set(_theValue.Value) ?? string.Empty;
222+
await _elementReferences[str.Length].FocusAsync();
183223
}
184224

185225
/// <summary>
@@ -236,7 +276,7 @@ private void SyncReferences()
236276
/// <returns></returns>
237277
public async Task SetValue()
238278
{
239-
string result = "";
279+
string result = "";
240280
for (int i = 0; i < _count.Value; i++)
241281
{
242282
var val = _elementReferences[i].Value?.ToString();

0 commit comments

Comments
 (0)