-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSelectCity.razor.cs
More file actions
132 lines (116 loc) · 3.4 KB
/
SelectCity.razor.cs
File metadata and controls
132 lines (116 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
namespace BootstrapBlazor.Components;
/// <summary>
/// SelectCity 组件
/// </summary>
public partial class SelectCity
{
/// <summary>
/// 获得/设置 是否可多选 默认 false 单选
/// </summary>
[Parameter]
public bool IsMultiple { get; set; }
private string? ClassString => CssBuilder.Default("select bb-city")
.AddClass("disabled", IsDisabled)
.AddClassFromAttributes(AdditionalAttributes)
.Build();
private readonly HashSet<string> _values = [];
private string? GetActiveClass(string item) => _values.Contains(item) || CurrentValue == item ? "active" : null;
private async Task OnClearValue()
{
if (IsMultiple)
{
_values.Clear();
}
CurrentValue = "";
if (OnClearAsync != null)
{
await OnClearAsync();
}
}
private void OnSelectProvince(string province)
{
if (!IsMultiple)
{
return;
}
HashSet<string> cities = province switch
{
"直辖市" => Municipalities,
"特别行政区" => SpecialAdministrativeRegions,
_ => GetCities(province)
};
foreach (var city in cities.Where(city => !_values.Remove(city)))
{
_values.Add(city);
}
CurrentValue = string.Join(",", _values);
}
private void OnSelectCity(string item)
{
if (IsMultiple)
{
if (!_values.Remove(item))
{
_values.Add(item);
}
CurrentValue = string.Join(",", _values);
}
else
{
CurrentValue = item;
}
}
private static HashSet<string> GetProvinces()
{
return
[
"直辖市",
"河北省",
"山西省",
"辽宁省",
"吉林省",
"黑龙江省",
"江苏省",
"浙江省",
"安徽省",
"福建省",
"江西省",
"山东省",
"河南省",
"湖北省",
"湖南省",
"广东省",
"海南省",
"四川省",
"贵州省",
"云南省",
"陕西省",
"甘肃省",
"青海省",
"内蒙古自治区",
"广西壮族自治区",
"西藏自治区",
"宁夏回族自治区",
"新疆维吾尔自治区",
"台湾省",
"特别行政区"
];
}
private static readonly HashSet<string> Municipalities = ["北京市", "天津市", "上海市", "重庆市"];
private static readonly HashSet<string> SpecialAdministrativeRegions = ["香港特别行政区", "澳门特别行政区"];
private HashSet<string> GetCities(string provinceName)
{
if (provinceName == "直辖市")
{
return Municipalities;
}
if (provinceName == "特别行政区")
{
return SpecialAdministrativeRegions;
}
return RegionService.GetCities(provinceName);
}
}