Skip to content

Commit 66d5898

Browse files
committed
feat: 完善搜索功能
1 parent d028e02 commit 66d5898

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public partial class SelectCity
2929
[Parameter]
3030
public string? SearchIcon { get; set; }
3131

32+
/// <summary>
33+
/// 获得/设置 单选时选择后是否自动关闭 默认 true
34+
/// </summary>
35+
[Parameter]
36+
public bool AutoClose { get; set; } = true;
37+
3238
private string? ClassString => CssBuilder.Default("select bb-city")
3339
.AddClass("disabled", IsDisabled)
3440
.AddClassFromAttributes(AdditionalAttributes)
@@ -50,7 +56,6 @@ public partial class SelectCity
5056
.AddClass("prev", !string.IsNullOrEmpty(_searchText) && PinYinService.GetFirstLetters(item).StartsWith(_searchText))
5157
.Build();
5258

53-
5459
/// <summary>
5560
/// <inheritdoc/>
5661
/// </summary>
@@ -71,7 +76,7 @@ protected override void OnParametersSet()
7176
});
7277

7378
/// <summary>
74-
/// 触发过滤方法 由 Javascript 触发
79+
/// 触发过滤方法 由 JavaScript 触发
7580
/// </summary>
7681
/// <param name="v"></param>
7782
/// <returns></returns>
@@ -124,7 +129,7 @@ private void OnSelectProvince(string provinceName)
124129
CurrentValue = string.Join(",", _values);
125130
}
126131

127-
private void OnSelectCity(string item)
132+
private async Task OnSelectCity(string item)
128133
{
129134
if (IsMultiple)
130135
{
@@ -138,6 +143,11 @@ private void OnSelectCity(string item)
138143
{
139144
CurrentValue = item;
140145
}
146+
147+
if (AutoClose)
148+
{
149+
await InvokeVoidAsync("hide", Id);
150+
}
141151
}
142152

143153
private HashSet<string> GetProvinces()
@@ -147,6 +157,11 @@ private HashSet<string> GetProvinces()
147157
return Provinces;
148158
}
149159

160+
if (IsChinese(_searchText))
161+
{
162+
return [.. Provinces.Where(i => i.Contains(_searchText) || GetCities(i).Any(city => city.Contains(_searchText)))];
163+
}
164+
150165
return [.. GenerateProvincePinYin().Where(i => FilterProvince(i, _searchText)).Select(i => i.Name)];
151166
}
152167

@@ -161,11 +176,7 @@ private HashSet<ProvinceItem> GenerateProvincePinYin()
161176
{
162177
PinYin = PinYinService.GetFirstLetters(i),
163178
Name = i,
164-
Cities = [.. GetCities(i).Select(i => new CityItem()
165-
{
166-
PinYin = PinYinService.GetFirstLetters(i),
167-
Name = i
168-
})]
179+
Cities = GenerateCityPinYin(i)
169180
})];
170181
return _provinceItems;
171182
}
@@ -177,12 +188,14 @@ private HashSet<ProvinceItem> GenerateProvincePinYin()
177188
_ => RegionService.GetCities(provinceName)
178189
};
179190

180-
private static HashSet<CityItem> GenerateCityPinYin(HashSet<string> cities) => [.. cities.Select(i => new CityItem()
191+
private HashSet<CityItem> GenerateCityPinYin(string provinceName) => [.. GetCities(provinceName).Select(i => new CityItem()
181192
{
182193
PinYin = PinYinService.GetFirstLetters(i),
183194
Name = i
184195
})];
185196

197+
private bool IsChinese(string text) => text.Any(i => i >= 0x4E00 && i <= 0x9FFF);
198+
186199
private static readonly HashSet<string> Provinces = [
187200
"直辖市",
188201
"河北省",

0 commit comments

Comments
 (0)