Skip to content

Commit 12d10e8

Browse files
committed
refactor: 重构组件提高性能
1 parent 2941062 commit 12d10e8

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

src/BootstrapBlazor/Components/Input/OtpInput.razor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
@inherits ValidateBase<string>
33

44
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString">
5+
<input hidden value="@CurrentValueAsString" class="" />
56
@for (var index = 0; index < Digits; index++)
67
{
7-
<span class="@ItemClassString">
88
@if(IsReadonly || IsDisabled)
99
{
10-
@GetValueString(index)
10+
<span class="@ItemClassString">
11+
@GetValueString(index)
12+
</span>
1113
}
1214
else
1315
{
14-
var charIndex = index;
1516
<input type="@TypeString" class="@InputClassString"
1617
maxlength="@MaxLengthString" inputmode="@TypeModeString" placeholder="@PlaceHolder"
17-
value="@GetValueString(index)" @onchange="e => OnChanged(e.Value!.ToString(), charIndex)" />
18+
value="@GetValueString(index)" />
1819
}
19-
</span>
2020
}
2121
</div>

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ public partial class OtpInput
4444
.AddClass(ValidCss)
4545
.Build();
4646

47-
private string? InputClassString => CssBuilder.Default()
47+
private string? InputClassString => CssBuilder.Default("bb-opt-item")
4848
.AddClass("input-number-fix", Type == OtpInputType.Number)
49-
.AddClass("disabled", IsDisabled)
5049
.Build();
5150

5251
private string TypeString => Type switch
@@ -58,8 +57,8 @@ public partial class OtpInput
5857

5958
private string? MaxLengthString => Type switch
6059
{
61-
OtpInputType.Text => "1",
62-
_ => null
60+
OtpInputType.Number => null,
61+
_ => "1"
6362
};
6463

6564
private string? TypeModeString => Type switch
@@ -77,34 +76,19 @@ protected override void OnParametersSet()
7776
{
7877
base.OnParametersSet();
7978

79+
Value ??= "";
8080
_values = new char[Digits];
81-
if (Value != null)
81+
for (var index = 0; index < Digits; index++)
8282
{
83-
for (var index = 0; index < Digits; index++)
83+
if (index < Value.Length)
8484
{
85-
if (index < Value.Length)
86-
{
87-
_values[index] = Value[index];
88-
}
85+
_values[index] = Value[index];
8986
}
9087
}
9188
}
9289

9390
private char? GetValueString(int index)
9491
{
95-
char? c = _values.ElementAtOrDefault(index);
96-
if (c == 0)
97-
{
98-
c = null;
99-
}
100-
return c;
101-
}
102-
103-
private void OnChanged(string? v, int index)
104-
{
105-
if (index < Digits && !string.IsNullOrEmpty(v))
106-
{
107-
_values[index] = v[0];
108-
}
92+
return _values[index] != 0 ? _values[index] : null;
10993
}
11094
}

0 commit comments

Comments
 (0)