Skip to content

Commit cfa0597

Browse files
authored
feat(Search): add ShowSearchButton parameter (#5243)
* doc: 增加异常捕获 * feat: add ShowSearchButton parameter * style: 增加样式 * feat: 增加 ShowPrefixIcon 参数 * style: 更新样式 * feat: 增加 PrefixIcon 参数 * doc: 更新示例 * style: 更新样式 * doc: 更新示例 * doc: 更新示例 * doc: 增加文档说明 * test: 增加单元测试
1 parent 78f2789 commit cfa0597

File tree

11 files changed

+166
-18
lines changed

11 files changed

+166
-18
lines changed

src/BootstrapBlazor.Server/Components/Components/CultureChooser.razor.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,22 @@ private async Task SetCulture(SelectedItem item)
5656
var uri = new Uri(NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
5757
var query = $"?culture={Uri.EscapeDataString(culture)}&redirectUri={Uri.EscapeDataString(uri)}";
5858

59-
// use a path that matches your culture redirect controller from the previous steps
60-
NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
59+
try
60+
{
61+
// use a path that matches your culture redirect controller from the previous steps
62+
NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
63+
}
64+
catch { }
6165
}
62-
}
63-
else
64-
{
65-
if (SelectedCulture != item.Value)
66+
else
6667
{
67-
var culture = item.Value;
68-
await JSRuntime.InvokeVoidAsync("bbCulture.set", culture);
68+
if (SelectedCulture != item.Value)
69+
{
70+
var culture = item.Value;
71+
await JSRuntime.InvokeVoidAsync("bbCulture.set", culture);
6972

70-
NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
73+
NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
74+
}
7175
}
7276
}
7377
}

src/BootstrapBlazor.Server/Components/Samples/Searches.razor

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<Search IsAutoFocus="true"
1515
PlaceHolder="@Localizer["SearchesPlaceHolder"]"
1616
OnSearch="@OnSearch"
17-
IsSelectAllTextOnFocus="true" />
18-
<ConsoleLogger @ref="Logger" />
17+
IsSelectAllTextOnFocus="true"></Search>
18+
<ConsoleLogger @ref="Logger"></ConsoleLogger>
1919
</DemoBlock>
2020

2121
<DemoBlock Title="@Localizer["SearchesDisplayButtonTitle"]"
@@ -25,7 +25,7 @@
2525
ShowClearButton="true"
2626
OnSearch="@OnDisplaySearch"
2727
OnClear="@OnClear"></Search>
28-
<ConsoleLogger @ref="DisplayLogger" />
28+
<ConsoleLogger @ref="DisplayLogger"></ConsoleLogger>
2929
</DemoBlock>
3030

3131
<DemoBlock Title="@Localizer["SearchesItemTemplateTitle"]"
@@ -56,16 +56,43 @@
5656
Name="keyboards">
5757
<Search PlaceHolder="@Localizer["SearchesPlaceHolder"]"
5858
IsTriggerSearchByInput="false"
59-
OnSearch="@OnKeyboardSearch" />
60-
<ConsoleLogger @ref="KeyboardLogger" />
59+
OnSearch="@OnKeyboardSearch"></Search>
60+
<ConsoleLogger @ref="KeyboardLogger"></ConsoleLogger>
6161
</DemoBlock>
6262

6363
<DemoBlock Title="@Localizer["SearchesValidateFormTitle"]"
6464
Introduction="@Localizer["SearchesValidateFormIntro"]"
6565
Name="ValidateForm">
6666
<ValidateForm Model="@Model">
67-
<Search @bind-Value="Model.Name" OnSearch="@OnModelSearch" />
67+
<Search @bind-Value="Model.Name" OnSearch="@OnModelSearch"></Search>
6868
</ValidateForm>
6969
</DemoBlock>
7070

71+
<DemoBlock Title="@Localizer["SearchesShowPrefixIconTitle"]"
72+
Introduction="@Localizer["SearchesShowPrefixIconIntro"]"
73+
Name="ShowPrefixIcon">
74+
<section ignore>@((MarkupString)Localizer["SearchesShowPrefixIconDescription"].Value)</section>
75+
<div class="row g-3">
76+
<div class="col-12 col-sm-6">
77+
<Search PlaceHolder="@Localizer["SearchesPlaceHolder"]"
78+
OnSearch="@OnModelSearch"
79+
ShowClearButton="false" ShowSearchButton="false"
80+
ShowPrefixIcon="true" PrefixIcon="fa fa-flag"></Search>
81+
</div>
82+
<div class="col-12 col-sm-6">
83+
<Search PlaceHolder="@Localizer["SearchesPlaceHolder"]"
84+
OnSearch="@OnModelSearch"
85+
ShowClearButton="false" ShowSearchButton="false"
86+
ShowPrefixIcon="true">
87+
<PrefixIconTemplate>
88+
<div class="search-custom-prefix">
89+
<svg width="16" height="16" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M11 16V42" stroke="#d0021b" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/><path d="M24 29V42" stroke="#d0021b" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/><path d="M24 19V6" stroke="#d0021b" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/><path d="M37 6V32" stroke="#d0021b" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/><path d="M11 16C13.7614 16 16 13.7614 16 11C16 8.23858 13.7614 6 11 6C8.23858 6 6 8.23858 6 11C6 13.7614 8.23858 16 11 16Z" fill="none" stroke="#d0021b" stroke-width="3" stroke-linejoin="round"/><path d="M24 29C26.7614 29 29 26.7614 29 24C29 21.2386 26.7614 19 24 19C21.2386 19 19 21.2386 19 24C19 26.7614 21.2386 29 24 29Z" fill="none" stroke="#d0021b" stroke-width="3" stroke-linejoin="round"/><path d="M37 42C39.7614 42 42 39.7614 42 37C42 34.2386 39.7614 32 37 32C34.2386 32 32 34.2386 32 37C32 39.7614 34.2386 42 37 42Z" fill="none" stroke="#d0021b" stroke-width="3" stroke-linejoin="round"/></svg>
90+
</div>
91+
</PrefixIconTemplate>
92+
</Search>
93+
</div>
94+
</div>
95+
96+
</DemoBlock>
97+
7198
<AttributeTable Items="@GetAttributes()" />

src/BootstrapBlazor.Server/Components/Samples/Searches.razor.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@
2929
font-size: 86%;
3030
color: #c0c4cc;
3131
}
32+
33+
.search-custom-prefix {
34+
display: flex;
35+
margin-left: -2px;
36+
}

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4220,7 +4220,10 @@
42204220
"SearchesOnSearch": "Call back this delegate when you click Search",
42214221
"SearchesOnClear": "Click Recall this order when emptying",
42224222
"SearchesItemTemplateTitle": "Item Template",
4223-
"SearchesItemTemplateIntro": "By setting <code>ItemTemplate</code> and matching generic data, you can achieve any desired effect. In this example, by searching for any keyword, the backend calls any third-party search results and displays them. After selecting the search item, you can handle it yourself through the <code>OnSelectedItemChanged</code> callback method"
4223+
"SearchesItemTemplateIntro": "By setting <code>ItemTemplate</code> and matching generic data, you can achieve any desired effect. In this example, by searching for any keyword, the backend calls any third-party search results and displays them. After selecting the search item, you can handle it yourself through the <code>OnSelectedItemChanged</code> callback method",
4224+
"SearchesShowPrefixIconTitle": "ShowPrefixIcon",
4225+
"SearchesShowPrefixIconIntro": "Control whether to show the prefix icon by setting the <code>ShowPrefixIcon</code> parameter",
4226+
"SearchesShowPrefixIconDescription": "You can customize the prefix through <code>PrefixIconTemplate</code>. In this example, the <code>Svg</code> icon is used through the prefix template."
42244227
},
42254228
"BootstrapBlazor.Server.Components.Samples.Titles": {
42264229
"Title": "Title",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4220,7 +4220,10 @@
42204220
"SearchesOnSearch": "点击搜索时回调此委托",
42214221
"SearchesOnClear": "点击清空时回调此委托",
42224222
"SearchesItemTemplateTitle": "模板",
4223-
"SearchesItemTemplateIntro": "通过设置 <code>ItemTemplate</code> 配合泛型数据可以做出自己想要的任何效果,本例中通过搜索任意关键字,后台调用任意第三方搜索结果并且进行展示,选中搜索项后通过 <code>OnSelectedItemChanged</code> 回调方法可以自行处理"
4223+
"SearchesItemTemplateIntro": "通过设置 <code>ItemTemplate</code> 配合泛型数据可以做出自己想要的任何效果,本例中通过搜索任意关键字,后台调用任意第三方搜索结果并且进行展示,选中搜索项后通过 <code>OnSelectedItemChanged</code> 回调方法可以自行处理",
4224+
"SearchesShowPrefixIconTitle": "显示前缀图标",
4225+
"SearchesShowPrefixIconIntro": "通过设置 <code>ShowPrefixIcon</code> 参数控制是否显示前缀图标",
4226+
"SearchesShowPrefixIconDescription": "可以通过 <code>PrefixIconTemplate</code> 自定义前缀,本例中通过前缀模板使用 <code>Svg</code> 图标"
42244227
},
42254228
"BootstrapBlazor.Server.Components.Samples.Titles": {
42264229
"Title": "Title 网站标题",

src/BootstrapBlazor/Components/Search/Search.razor

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,24 @@
1717
{
1818
<Button Color="ClearButtonColor" Text="@ClearButtonText" Icon="@ClearButtonIcon" OnClick="OnClearClick" aria-label="Clear"></Button>
1919
}
20-
<Button Color="SearchButtonColor" Text="@SearchButtonText" Icon="@ButtonIcon" OnClick="OnSearchClick" aria-label="Search"></Button>
20+
@if (ShowSearchButton)
21+
{
22+
<Button Color="SearchButtonColor" Text="@SearchButtonText" Icon="@ButtonIcon" OnClick="OnSearchClick" aria-label="Search"></Button>
23+
}
2124
</div>
25+
@if (ShowPrefixIcon)
26+
{
27+
<div class="search-prefix-icon">
28+
@if (PrefixIconTemplate != null)
29+
{
30+
@PrefixIconTemplate
31+
}
32+
else
33+
{
34+
<i class="@PrefixIcon"></i>
35+
}
36+
</div>
37+
}
2238
<ul class="dropdown-menu">
2339
@foreach (var item in _filterItems)
2440
{

src/BootstrapBlazor/Components/Search/Search.razor.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public partial class Search<TValue>
3636
[Parameter]
3737
public Color ClearButtonColor { get; set; } = Color.Primary;
3838

39+
/// <summary>
40+
/// 获得/设置 是否显示搜索按钮 默认为 true 显示
41+
/// </summary>
42+
[Parameter]
43+
public bool ShowSearchButton { get; set; } = true;
44+
3945
/// <summary>
4046
/// 获得/设置 搜索按钮颜色
4147
/// </summary>
@@ -61,6 +67,24 @@ public partial class Search<TValue>
6167
[NotNull]
6268
public string? SearchButtonText { get; set; }
6369

70+
/// <summary>
71+
/// 获得/设置 是否显示前缀图标 默认为 false 不显示
72+
/// </summary>
73+
[Parameter]
74+
public bool ShowPrefixIcon { get; set; }
75+
76+
/// <summary>
77+
/// 获得/设置 前缀图标 默认为 null
78+
/// </summary>
79+
[Parameter]
80+
public string? PrefixIcon { get; set; }
81+
82+
/// <summary>
83+
/// 获得/设置 前缀图标模板 默认为 null
84+
/// </summary>
85+
[Parameter]
86+
public RenderFragment? PrefixIconTemplate { get; set; }
87+
6488
/// <summary>
6589
/// 获得/设置 点击搜索后是否自动清空搜索框
6690
/// </summary>
@@ -99,6 +123,7 @@ public partial class Search<TValue>
99123
private IStringLocalizer<Search<TValue>>? Localizer { get; set; }
100124

101125
private string? ClassString => CssBuilder.Default("search auto-complete")
126+
.AddClass("search-prefix", ShowPrefixIcon)
102127
.AddClassFromAttributes(AdditionalAttributes)
103128
.Build();
104129

@@ -125,6 +150,8 @@ protected override void OnParametersSet()
125150
ClearButtonIcon ??= IconTheme.GetIconByKey(ComponentIcons.SearchClearButtonIcon);
126151
SearchButtonIcon ??= IconTheme.GetIconByKey(ComponentIcons.SearchButtonIcon);
127152
SearchButtonLoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.SearchButtonLoadingIcon);
153+
PrefixIcon ??= IconTheme.GetIconByKey(ComponentIcons.SearchButtonIcon);
154+
128155
SearchButtonText ??= Localizer[nameof(SearchButtonText)];
129156
ButtonIcon ??= SearchButtonIcon;
130157
NoDataTip ??= Localizer[nameof(NoDataTip)];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.search.auto-complete {
2+
--bb-ac-padding-right: #{$bb-search-padding-right};
3+
--bb-search-prefix-input-padding-left: #{$bb-search-prefix-input-padding-left};
4+
--bb-search-prefix-icon-color: #{$bb-search-prefix-icon-color};
5+
6+
&.search-prefix .form-control {
7+
padding-left: var(--bb-search-prefix-input-padding-left);
8+
}
9+
10+
.search-prefix-icon {
11+
position: absolute;
12+
left: 0;
13+
top: 0;
14+
bottom: 0;
15+
z-index: 6;
16+
padding-left: 1rem;
17+
display: flex;
18+
align-items: center;
19+
color: var(--bb-search-prefix-icon-color);
20+
}
21+
}

src/BootstrapBlazor/wwwroot/scss/components.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
@import "../../Components/RibbonTab/RibbonTab.razor.scss";
7878
@import "../../Components/Row/Row.razor.scss";
7979
@import "../../Components/Scroll/Scroll.razor.scss";
80+
@import "../../Components/Search/Search.razor.scss";
8081
@import "../../Components/Select/MultiSelect.razor.scss";
8182
@import "../../Components/Select/Select.razor.scss";
8283
@import "../../Components/Select/SelectObject.razor.scss";

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ $bb-scroll-thumb-hover-bg: rgba(var(--bs-emphasis-color-rgb),0.3);
482482
$bb-scroll-thumb-active-bg: rgba(var(--bs-emphasis-color-rgb),0.5);
483483
$bb-scroll-track-bg: rgba(var(--bs-emphasis-color-rgb),0.08);
484484

485+
// Search
486+
$bb-search-padding-right: 0.75rem;
487+
$bb-search-prefix-icon-padding-left: 2rem;
488+
$bb-search-prefix-input-padding-left: 36px;
489+
$bb-search-prefix-icon-color: #59636e;
490+
485491
// Segmented
486492
$bb-segmented-padding: 2px;
487493
$bb-segmented-bg: rgba(var(--bs-body-color-rgb),.06);

0 commit comments

Comments
 (0)