-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSelectRegion.razor.cs
More file actions
216 lines (173 loc) · 6.04 KB
/
SelectRegion.razor.cs
File metadata and controls
216 lines (173 loc) · 6.04 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// 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>
/// <para lang="zh">SelectRegion 组件</para>
/// <para lang="en">SelectRegion component</para>
/// </summary>
public partial class SelectRegion
{
private string? ClassString => CssBuilder.Default("select bb-region")
.AddClass("disabled", IsDisabled)
.AddClassFromAttributes(AdditionalAttributes)
.Build();
private string? GetHeaderActiveClass(RegionViewMode type) => _currentViewMode == type ? "active" : null;
private string? GetBodyActiveClass(RegionViewMode type) => CssBuilder.Default("bb-region-body-item")
.AddClass("active", _currentViewMode == type)
.Build();
private string? GetProvinceActiveClass(string item) => _provinceValue == item ? "active" : null;
private string? GetCityActiveClass(string item) => _cityValue == item ? "active" : null;
private string? GetCountyActiveClass(CountyItem item) => _countyValue == item ? "active" : null;
private string? GetDetailActiveClass(string item) => _detailValue == item ? "active" : null;
private RegionViewMode _currentViewMode = RegionViewMode.Province;
private string? _provinceValue;
private string? _cityValue;
private CountyItem _countyValue;
private string? _detailValue;
/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnParametersSet()
{
base.OnParametersSet();
ResetValue();
}
private void ResetValue()
{
if (!string.IsNullOrEmpty(Value) && Value != GetCurrentValue())
{
var segments = Value.Split('-', StringSplitOptions.RemoveEmptyEntries);
if (segments.Length > 0 && _provinceValue != segments[0] && GetProvinces().Contains(segments[0]))
{
_provinceValue = segments[0];
Value = _provinceValue;
}
if (segments.Length > 1 && _cityValue != segments[1] && GetCities().Contains(segments[1]))
{
_cityValue = segments[1];
Value = $"{_provinceValue}-{_cityValue}";
}
if (segments.Length > 2 && _countyValue.Name != segments[2])
{
var county = GetCounties().First(i => i.Name == segments[2]);
if (!string.IsNullOrEmpty(county.Name))
{
_countyValue = county;
}
Value = $"{_provinceValue}-{_cityValue}-{_countyValue.Name}";
}
if (segments.Length > 3 && _cityValue != segments[3] && GetDetails().Contains(segments[3]))
{
_detailValue = segments[3];
Value = $"{_provinceValue}-{_cityValue}-{_countyValue.Name}-{_detailValue}";
}
}
}
private string? GetCurrentValue()
{
string? value = null;
if (!string.IsNullOrEmpty(_provinceValue))
{
value = _provinceValue;
}
if (!string.IsNullOrEmpty(_cityValue))
{
value = $"{_provinceValue}-{_cityValue}";
}
if (!string.IsNullOrEmpty(_countyValue.Name))
{
value = $"{_provinceValue}-{_cityValue}-{_countyValue.Name}";
}
if (!string.IsNullOrEmpty(_detailValue))
{
value = $"{_provinceValue}-{_cityValue}-{_countyValue.Name}-{_detailValue}";
}
return value;
}
private async Task OnClearValue()
{
_provinceValue = null;
_cityValue = null;
_countyValue = new();
_detailValue = null;
CurrentValue = null;
_currentViewMode = RegionViewMode.Province;
if (OnClearAsync != null)
{
await OnClearAsync();
}
}
private HashSet<string> GetProvinces() => RegionService.GetProvinces();
private HashSet<string> GetCities()
{
if (string.IsNullOrEmpty(_provinceValue))
{
return [];
}
return RegionService.GetCities(_provinceValue);
}
private HashSet<CountyItem> GetCounties()
{
if (string.IsNullOrEmpty(_cityValue))
{
return [];
}
return RegionService.GetCounties(_cityValue);
}
private HashSet<string> GetDetails()
{
if (string.IsNullOrEmpty(_countyValue.Code))
{
return [];
}
return RegionService.GetDetails(_countyValue.Code);
}
private void OnClickProvince(string value)
{
_provinceValue = value;
_cityValue = null;
_countyValue = new();
_detailValue = null;
_currentViewMode = RegionViewMode.City;
CurrentValue = _provinceValue;
}
private void OnClickCity(string value)
{
_cityValue = value;
_countyValue = new();
_detailValue = null;
_currentViewMode = RegionViewMode.County;
CurrentValue = $"{_provinceValue}-{_cityValue}";
}
private void OnClickCounty(CountyItem item)
{
_countyValue = item;
_detailValue = null;
_currentViewMode = RegionViewMode.Detail;
CurrentValue = $"{_provinceValue}-{_cityValue}-{_countyValue.Name}";
}
private async Task OnClickDetail(string value)
{
_detailValue = value;
_currentViewMode = RegionViewMode.Province;
CurrentValue = $"{_provinceValue}-{_cityValue}-{_countyValue.Name}-{_detailValue}";
await InvokeVoidAsync("hide", Id);
}
private void OnSwitchProvinceView()
{
_currentViewMode = RegionViewMode.Province;
}
private void OnSwitchCityView()
{
_currentViewMode = RegionViewMode.City;
}
private void OnSwitchCountyView()
{
_currentViewMode = RegionViewMode.County;
}
private void OnSwitchDetailView()
{
_currentViewMode = RegionViewMode.Detail;
}
}