Skip to content

Commit f44f296

Browse files
committed
feat: 增加 Clearable 属性
1 parent 252bc62 commit f44f296

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

src/BootstrapBlazor/Components/Input/BootstrapInput.razor

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,22 @@
77
<BootstrapLabel required="@Required" for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
88
}
99

10-
<input @attributes="@AdditionalAttributes" type="@Type" placeholder="@PlaceHolder" id="@Id" readonly="@ReadonlyString" class="@ClassName" disabled="@Disabled" @bind-value="CurrentValueAsString" @bind-value:event="@EventString" @onblur="@OnBlur" @ref="FocusElement" />
10+
@if (Clearable)
11+
{
12+
<div class="bb-clearable-input">
13+
@RenderInput
14+
<i class="@ClearableIconString" @onclick="OnClickClear"></i>
15+
</div>
16+
}
17+
else
18+
{
19+
@RenderInput
20+
}
21+
22+
@code {
23+
RenderFragment RenderInput =>
24+
@<input @attributes="@AdditionalAttributes" type="@Type" id="@Id" class="@ClassName"
25+
readonly="@ReadonlyString" disabled="@Disabled"
26+
placeholder="@PlaceHolder"
27+
@bind-value="CurrentValueAsString" @bind-value:event="@EventString" @onblur="@OnBlur" @ref="FocusElement" />;
28+
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,47 @@ public partial class BootstrapInput<TValue>
2222
[Parameter]
2323
public bool AutoSetDefaultWhenNull { get; set; }
2424

25+
/// <summary>
26+
/// 获得/设置 是否显示清空小按钮 默认 false
27+
/// </summary>
28+
[Parameter]
29+
public bool Clearable { get; set; }
30+
31+
/// <summary>
32+
/// 获得/设置 清空文本框时回调方法 默认 null
33+
/// </summary>
34+
[Parameter]
35+
public Func<TValue, Task>? OnClear { get; set; }
36+
37+
/// <summary>
38+
/// 获得/设置 清空小按钮图标 默认 null
39+
/// </summary>
40+
[Parameter]
41+
public string? ClearableIcon { get; set; }
42+
43+
/// <summary>
44+
/// 图标主题服务
45+
/// </summary>
46+
[Inject]
47+
[NotNull]
48+
private IIconTheme? IconTheme { get; set; }
49+
2550
private string? ReadonlyString => Readonly ? "true" : null;
2651

52+
private string? ClearableIconString => CssBuilder.Default("form-control-clear-icon")
53+
.AddClass(ClearableIcon)
54+
.Build();
55+
56+
/// <summary>
57+
/// <inheritdoc/>
58+
/// </summary>
59+
protected override void OnParametersSet()
60+
{
61+
base.OnParametersSet();
62+
63+
ClearableIcon ??= IconTheme.GetIconByKey(ComponentIcons.InputClearIcon);
64+
}
65+
2766
/// <summary>
2867
/// <inheritdoc/>
2968
/// </summary>
@@ -47,4 +86,13 @@ protected override bool TryParseValueFromString(string value, [MaybeNullWhen(fal
4786
}
4887
return ret;
4988
}
89+
90+
private async Task OnClickClear()
91+
{
92+
if (OnClear != null)
93+
{
94+
await OnClear(Value);
95+
}
96+
CurrentValueAsString = "";
97+
}
5098
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.bb-clearable-input {
2+
display: inline-flex;
3+
align-items: center;
4+
flex-grow: 1;
5+
width: 100%;
6+
position: relative;
7+
8+
> input {
9+
width: 100%;
10+
flex-grow: 1;
11+
padding-right: 2rem;
12+
13+
+ .form-control-clear-icon {
14+
color: var(--bb-border-hover-color);
15+
cursor: pointer;
16+
position: absolute;
17+
right: 11px;
18+
display: none;
19+
}
20+
21+
&:focus + .form-control-clear-icon {
22+
display: block;
23+
}
24+
}
25+
26+
&:hover .form-control-clear-icon {
27+
display: block;
28+
}
29+
}

src/BootstrapBlazor/wwwroot/scss/components.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
@import "../../Components/IFrame/IFrame.razor.scss";
5252
@import "../../Components/ImagePreviewer/ImagePreviewer.razor.scss";
5353
@import "../../Components/ImageViewer/ImageViewer.razor.scss";
54+
@import "../../Components/Input/BootstrapInput.razor.scss";
5455
@import "../../Components/Input/BootstrapInputGroup.razor.scss";
5556
@import "../../Components/Input/FloatingLabel.razor.scss";
5657
@import "../../Components/IpAddress/IpAddress.razor.scss";

0 commit comments

Comments
 (0)