Skip to content

Commit 4ab9425

Browse files
committed
refactor: 重构组件结构
1 parent f9a2f00 commit 4ab9425

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/BootstrapBlazor/Components/Input/OtpInput.razor

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString">
55
@for (var index = 0; index < Digits; index++)
66
{
7-
if (IsReadonly || IsDisabled)
8-
{
9-
<span class="@ItemClassString">@GetValueString(index)</span>
10-
}
11-
else
12-
{
13-
var charIndex = index;
14-
<input type="@TypeString" maxlength="@MaxLengthString" inputmode="@TypeModeString" placeholder="@PlaceHolder"
15-
class="@ItemClassString" value="@GetValueString(index)" @onchange="e => OnChanged(e.Value!.ToString(), charIndex)" />
16-
}
7+
<span class="@ItemClassString">
8+
@if(IsReadonly || IsDisabled)
9+
{
10+
@GetValueString(index)
11+
}
12+
else
13+
{
14+
var charIndex = index;
15+
<input type="@TypeString" class="@InputClassString"
16+
maxlength="@MaxLengthString" inputmode="@TypeModeString" placeholder="@PlaceHolder"
17+
value="@GetValueString(index)" @onchange="e => OnChanged(e.Value!.ToString(), charIndex)" />
18+
}
19+
</span>
1720
}
1821
</div>

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ public partial class OtpInput
4040
.Build();
4141

4242
private string? ItemClassString => CssBuilder.Default("bb-opt-item")
43-
.AddClass("input-number-fix", Type == OtpInputType.Number)
4443
.AddClass("disabled", IsDisabled)
4544
.AddClass(ValidCss)
4645
.Build();
4746

47+
private string? InputClassString => CssBuilder.Default()
48+
.AddClass("input-number-fix", Type == OtpInputType.Number)
49+
.AddClass("disabled", IsDisabled)
50+
.Build();
51+
4852
private string TypeString => Type switch
4953
{
5054
OtpInputType.Number => "number",
@@ -74,11 +78,14 @@ protected override void OnParametersSet()
7478
base.OnParametersSet();
7579

7680
_values = new char[Digits];
77-
for (var index = 0; index < Digits; index++)
81+
if (Value != null)
7882
{
79-
if (index < Value.Length)
83+
for (var index = 0; index < Digits; index++)
8084
{
81-
_values[index] = Value[index];
85+
if (index < Value.Length)
86+
{
87+
_values[index] = Value[index];
88+
}
8289
}
8390
}
8491
}

0 commit comments

Comments
 (0)