Skip to content

Commit 66c5176

Browse files
committed
feat: 增加 MaxLength 属性
1 parent 720d2a4 commit 66c5176

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/BootstrapBlazor/Components/Input/OtpInput.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
else
1212
{
1313
var charIndex = index;
14-
<input type="@TypeString" maxlength="1" inputmode="@TypeModeString" class="@ItemClassString" value="@GetValueString(index)" @onchange="e => OnChanged(e, charIndex)" />
14+
<input type="@TypeString" maxlength="@MaxLengthString" inputmode="@TypeModeString" class="@ItemClassString" value="@GetValueString(index)" @onchange="e => OnChanged(e.Value!.ToString(), charIndex)" />
1515
}
1616
}
1717
</div>

src/BootstrapBlazor/Components/Input/OtpInput.razor.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public partial class OtpInput
4545
_ => "text"
4646
};
4747

48+
private string? MaxLengthString => Type switch
49+
{
50+
OtpInputType.Text => "1",
51+
_ => null
52+
};
53+
4854
private string? TypeModeString => Type switch
4955
{
5056
OtpInputType.Number => "numeric",
@@ -60,7 +66,14 @@ protected override void OnParametersSet()
6066
{
6167
base.OnParametersSet();
6268

63-
_values = Value?.ToCharArray() ?? [];
69+
_values = new char[Digits];
70+
for (var index = 0; index < Digits; index++)
71+
{
72+
if (index < Value.Length)
73+
{
74+
_values[index] = Value[index];
75+
}
76+
}
6477
}
6578

6679
private char? GetValueString(int index)
@@ -73,15 +86,11 @@ protected override void OnParametersSet()
7386
return c;
7487
}
7588

76-
private void OnChanged(ChangeEventArgs e, int index)
89+
private void OnChanged(string? v, int index)
7790
{
78-
if (index < _values.Length)
91+
if (index < Digits && !string.IsNullOrEmpty(v))
7992
{
80-
var v = e.Value?.ToString();
81-
if (!string.IsNullOrEmpty(v))
82-
{
83-
_values[index] = v[0];
84-
}
93+
_values[index] = v[0];
8594
}
8695
}
8796
}

0 commit comments

Comments
 (0)