Skip to content

Commit f7c49ff

Browse files
committed
feat: 增加禁用只读功能
1 parent d6b62af commit f7c49ff

File tree

4 files changed

+64
-32
lines changed

4 files changed

+64
-32
lines changed

src/BootstrapBlazor/Components/Input/OtpInput.razor

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString">
55
@for (var index = 0; index < Digits; index++)
66
{
7-
if (index < _values.Length)
7+
if (IsReadonly || IsDisabled)
88
{
9-
@if (IsReadonly || IsDisabled)
10-
{
11-
<span class="@ItemClassString">@_values[index]</span>
12-
}
13-
else
14-
{
15-
<span class="@ItemClassString">
16-
@{
17-
var charIndex = index;
18-
}
19-
<input type="@TypeString" @bind="@_values[charIndex]" />
20-
</span>
21-
}
9+
<span class="@ItemClassString">@GetValueString(index)</span>
10+
}
11+
else
12+
{
13+
var charIndex = index;
14+
<input type="@TypeString" maxlength="1" inputmode="@TypeModeString" class="@ItemClassString" value="@GetValueString(index)" @onchange="e => OnChanged(e, charIndex)" />
2215
}
2316
}
2417
</div>

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public partial class OtpInput
3333
.Build();
3434

3535
private string? ItemClassString => CssBuilder.Default("bb-opt-item")
36+
.AddClass("input-number-fix", Type == OtpInputType.Number)
3637
.AddClass("disabled", IsDisabled)
3738
.AddClass(ValidCss)
3839
.Build();
@@ -44,6 +45,12 @@ public partial class OtpInput
4445
_ => "text"
4546
};
4647

48+
private string? TypeModeString => Type switch
49+
{
50+
OtpInputType.Number => "numeric",
51+
_ => "text"
52+
};
53+
4754
private char[] _values = [];
4855

4956
/// <summary>
@@ -55,4 +62,26 @@ protected override void OnParametersSet()
5562

5663
_values = Value?.ToCharArray() ?? [];
5764
}
65+
66+
private char? GetValueString(int index)
67+
{
68+
char? c = _values.ElementAtOrDefault(index);
69+
if (c == 0)
70+
{
71+
c = null;
72+
}
73+
return c;
74+
}
75+
76+
private void OnChanged(ChangeEventArgs e, int index)
77+
{
78+
if (index < _values.Length)
79+
{
80+
var v = e.Value?.ToString();
81+
if (!string.IsNullOrEmpty(v))
82+
{
83+
_values[index] = v[0];
84+
}
85+
}
86+
}
5887
}
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
.bb-opt-item {
2-
--bb-opt-item-padding: #{$bb-opt-item-padding};
3-
--bb-opt-item-margin: #{$bb-opt-item-margin};
4-
--bb-opt-font-size: #{$bb-opt-font-size};
5-
--bb-opt-border-width: #{$bb-opt-border-width};
6-
display: inline-block;
7-
border: var(--bb-opt-border-width) solid var(--bs-border-color);
8-
font-size: var(--bb-opt-font-size);
9-
padding: var(--bb-opt-item-padding);
10-
border-radius: var(--bs-border-radius);
1+
.bb-opt-input {
2+
white-space: nowrap;
3+
overflow: hidden;
114

12-
&:not(:last-child) {
13-
margin-right: var(--bb-opt-item-margin);
14-
}
5+
.bb-opt-item {
6+
--bb-opt-item-width: 66px;
7+
--bb-opt-item-padding: #{$bb-opt-item-padding};
8+
--bb-opt-item-margin: #{$bb-opt-item-margin};
9+
--bb-opt-font-size: #{$bb-opt-font-size};
10+
--bb-opt-border-width: #{$bb-opt-border-width};
11+
display: inline-block;
12+
border: var(--bb-opt-border-width) solid var(--bs-border-color);
13+
font-size: var(--bb-opt-font-size);
14+
padding: var(--bb-opt-item-padding);
15+
border-radius: var(--bs-border-radius);
16+
width: var(--bb-opt-item-width);
17+
height: var(--bb-opt-item-width);
1518

16-
&.disabled {
17-
cursor: not-allowed;
18-
}
19+
&:not(:last-child) {
20+
margin-right: var(--bb-opt-item-margin);
21+
}
22+
23+
&.disabled {
24+
cursor: not-allowed;
25+
}
1926

20-
> input {
27+
&[type] {
28+
padding: 0;
29+
text-align: center;
30+
}
2131
}
2232
}

src/BootstrapBlazor/wwwroot/scss/theme/bootstrapblazor.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ $bb-viewer-border-radius: 50%;
315315
$bb-ip-cell-max-width: 30px;
316316

317317
// OptInput
318-
$bb-opt-item-padding: 0 .5rem;
318+
$bb-opt-item-padding: 0 .5em;
319319
$bb-opt-item-margin: .5rem;
320320
$bb-opt-font-size: 3em;
321321
$bb-opt-border-width: 1px;

0 commit comments

Comments
 (0)