Skip to content

Commit 5b0cc8c

Browse files
committed
refactor: 增加搜索功能
1 parent de9a9f8 commit 5b0cc8c

4 files changed

Lines changed: 135 additions & 49 deletions

File tree

src/components/BootstrapBlazor.Region/Components/SelectCity.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@namespace BootstrapBlazor.Components
22
@inherits SelectRegionBase
3-
@attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.Region/Components/SelectRegion.razor.js")]
3+
@attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.Region/Components/SelectCity.razor.js", JSObjectReference = true)]
44

55
@if (IsShowLabel)
66
{
@@ -21,9 +21,9 @@
2121
<div class="dropdown-menu-search">
2222
<input type="text" class="search-text form-control" autocomplete="off" value="@_searchText" aria-label="search" />
2323
<i class="@SearchIconString"></i>
24-
<i class="@ClearIconString" @onclick="OnClearSearch"></i>
24+
<i class="@ClearIconString"></i>
2525
</div>
26-
<div class="dropdown-menu-city-body">
26+
<div class="dropdown-menu-search-body">
2727
@RenderCities()
2828
</div>
2929
}

src/components/BootstrapBlazor.Region/Components/SelectCity.razor.cs

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ protected override void OnParametersSet()
5555
SearchIcon ??= IconTheme.GetIconByKey(ComponentIcons.SelectSearchIcon);
5656
}
5757

58+
/// <summary>
59+
/// <inheritdoc/>
60+
/// </summary>
61+
/// <returns></returns>
62+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
63+
{
64+
TriggerSearch = nameof(TriggerSearch)
65+
});
66+
67+
/// <summary>
68+
/// 触发过滤方法 由 Javascript 触发
69+
/// </summary>
70+
/// <param name="v"></param>
71+
/// <returns></returns>
72+
[JSInvokable]
73+
public void TriggerSearch(string v)
74+
{
75+
_searchText = v.ToUpperInvariant();
76+
StateHasChanged();
77+
}
78+
5879
private async Task OnClearValue()
5980
{
6081
if (IsMultiple)
@@ -69,12 +90,6 @@ private async Task OnClearValue()
6990
}
7091
}
7192

72-
private void OnClearSearch()
73-
{
74-
_searchText = null;
75-
StateHasChanged();
76-
}
77-
7893
private RenderFragment RenderCities() => builder =>
7994
{
8095
foreach (var item in GetProvinces())
@@ -119,43 +134,57 @@ private void OnSelectCity(string item)
119134
}
120135
}
121136

122-
private static HashSet<string> GetProvinces()
137+
private HashSet<string> GetProvinces()
123138
{
124-
return
125-
[
126-
"直辖市",
127-
"河北省",
128-
"山西省",
129-
"辽宁省",
130-
"吉林省",
131-
"黑龙江省",
132-
"江苏省",
133-
"浙江省",
134-
"安徽省",
135-
"福建省",
136-
"江西省",
137-
"山东省",
138-
"河南省",
139-
"湖北省",
140-
"湖南省",
141-
"广东省",
142-
"海南省",
143-
"四川省",
144-
"贵州省",
145-
"云南省",
146-
"陕西省",
147-
"甘肃省",
148-
"青海省",
149-
"内蒙古自治区",
150-
"广西壮族自治区",
151-
"西藏自治区",
152-
"宁夏回族自治区",
153-
"新疆维吾尔自治区",
154-
"台湾省",
155-
"特别行政区"
156-
];
139+
if (string.IsNullOrEmpty(_searchText))
140+
{
141+
return Provinces;
142+
}
143+
144+
// 处理直辖市
145+
if (PinYinZXS.Contains(_searchText))
146+
{
147+
return ["直辖市"];
148+
}
149+
150+
return Provinces;
157151
}
158152

153+
private static readonly HashSet<string> PinYinZXS = ["BJ", "TJ", "SH", "CQ"];
154+
155+
private static readonly HashSet<string> Provinces = [
156+
"直辖市",
157+
"河北省",
158+
"山西省",
159+
"辽宁省",
160+
"吉林省",
161+
"黑龙江省",
162+
"江苏省",
163+
"浙江省",
164+
"安徽省",
165+
"福建省",
166+
"江西省",
167+
"山东省",
168+
"河南省",
169+
"湖北省",
170+
"湖南省",
171+
"广东省",
172+
"海南省",
173+
"四川省",
174+
"贵州省",
175+
"云南省",
176+
"陕西省",
177+
"甘肃省",
178+
"青海省",
179+
"内蒙古自治区",
180+
"广西壮族自治区",
181+
"西藏自治区",
182+
"宁夏回族自治区",
183+
"新疆维吾尔自治区",
184+
"台湾省",
185+
"特别行政区"
186+
];
187+
159188
private static readonly HashSet<string> Municipalities = ["北京市", "天津市", "上海市", "重庆市"];
160189

161190
private static readonly HashSet<string> SpecialAdministrativeRegions = ["香港特别行政区", "澳门特别行政区"];

src/components/BootstrapBlazor.Region/Components/SelectCity.razor.css

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@
6767
display: flex;
6868
}
6969

70-
.dropdown-menu-city-body {
71-
flex: 1;
72-
height: 1%;
73-
overflow-y: auto;
74-
}
75-
7670
.dropdown-menu-search .clear-icon {
7771
cursor: pointer;
7872
}
@@ -85,6 +79,16 @@
8579
display: none;
8680
}
8781

82+
.dropdown-menu-search .search-text {
83+
text-transform: uppercase;
84+
}
85+
86+
.dropdown-menu-search-body {
87+
flex: 1;
88+
height: 1%;
89+
overflow-y: auto;
90+
}
91+
8892
.bb-region-city-item:not(:last-child) {
8993
margin-bottom: 3px;
9094
padding-bottom: 3px;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import Data from "../../BootstrapBlazor/modules/data.js"
2+
import EventHandler from "../../BootstrapBlazor/modules/event-handler.js"
3+
import Input from "../../BootstrapBlazor/modules/input.js"
4+
import Popover from "../../BootstrapBlazor/modules/base-popover.js"
5+
6+
export function init(id, invoke, options) {
7+
const el = document.getElementById(id)
8+
if (el === null) {
9+
return
10+
}
11+
const popover = Popover.init(el);
12+
13+
const searchInput = el.querySelector(".search-text");
14+
if (searchInput) {
15+
Input.composition(searchInput, async v => {
16+
await invoke.invokeMethodAsync(options.triggerSearch, v);
17+
});
18+
}
19+
20+
const search = el.querySelector(".dropdown-menu-search .clear-icon");
21+
if (search) {
22+
EventHandler.on(search, 'click', async e => {
23+
searchInput.value = '';
24+
await invoke.invokeMethodAsync(options.triggerSearch, '');
25+
});
26+
}
27+
28+
Data.set(id, { el, popover, searchInput, search });
29+
}
30+
31+
export function hide(id) {
32+
const region = Data.get(id)
33+
const { popover } = region;
34+
if (popover) {
35+
popover.hide();
36+
}
37+
}
38+
39+
export function dispose(id) {
40+
const region = Data.get(id)
41+
Data.remove(id)
42+
43+
const { popover, searchInput, search } = region;
44+
if (popover) {
45+
Popover.dispose(popover);
46+
}
47+
if (searchInput) {
48+
Input.dispose(searchInput);
49+
}
50+
if (search) {
51+
EventHandler.off(search, 'click');
52+
}
53+
}

0 commit comments

Comments
 (0)