Skip to content

Commit aadcb49

Browse files
committed
refactor: 增加 ResetValue 逻辑
1 parent 8d2f049 commit aadcb49

1 file changed

Lines changed: 51 additions & 8 deletions

File tree

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

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,47 @@ protected override void OnParametersSet()
113113

114114
DropdownIcon ??= IconTheme.GetIconByKey(ComponentIcons.SelectDropdownIcon);
115115
ClearIcon ??= IconTheme.GetIconByKey(ComponentIcons.SelectClearIcon);
116+
117+
ResetValue();
116118
}
117119

118-
private void OnClearValue()
120+
private void ResetValue()
121+
{
122+
if (!string.IsNullOrEmpty(Value))
123+
{
124+
var segments = Value.Split('-', StringSplitOptions.RemoveEmptyEntries);
125+
if (segments.Length > 0 && _provinceValue != segments[0] && GetProvinces().Contains(segments[0]))
126+
{
127+
_provinceValue = segments[0];
128+
129+
Value = _provinceValue;
130+
}
131+
if (segments.Length > 1 && _cityValue != segments[1] && GetCities().Contains(segments[1]))
132+
{
133+
_cityValue = segments[1];
134+
135+
Value = $"{_provinceValue}-{_cityValue}";
136+
}
137+
if (segments.Length > 2 && _countyValue.Name != segments[2])
138+
{
139+
var county = GetCounties().First(i => i.Name == segments[2]);
140+
if (!string.IsNullOrEmpty(county.Name))
141+
{
142+
_countyValue = county;
143+
}
144+
145+
Value = $"{_provinceValue}-{_cityValue}-{_countyValue.Name}";
146+
}
147+
if (segments.Length > 3 && _cityValue != segments[3] && GetDetails().Contains(segments[3]))
148+
{
149+
_detailValue = segments[3];
150+
151+
Value = $"{_provinceValue}-{_cityValue}-{_countyValue.Name}-{_detailValue}";
152+
}
153+
}
154+
}
155+
156+
private async Task OnClearValue()
119157
{
120158
_provinceValue = "";
121159
_cityValue = "";
@@ -124,35 +162,40 @@ private void OnClearValue()
124162
CurrentValue = "";
125163

126164
_currentViewMode = RegionViewMode.Province;
165+
166+
if (OnClearAsync != null)
167+
{
168+
await OnClearAsync();
169+
}
127170
}
128171

129-
private IReadOnlySet<string> GetProvinces() => RegionService.GetProvinces();
172+
private HashSet<string> GetProvinces() => RegionService.GetProvinces();
130173

131-
private IReadOnlySet<string> GetCities()
174+
private HashSet<string> GetCities()
132175
{
133176
if (string.IsNullOrEmpty(_provinceValue))
134177
{
135-
return new HashSet<string>();
178+
return [];
136179
}
137180

138181
return RegionService.GetCities(_provinceValue);
139182
}
140183

141-
private IReadOnlySet<CountyItem> GetCounties()
184+
private HashSet<CountyItem> GetCounties()
142185
{
143186
if (string.IsNullOrEmpty(_cityValue))
144187
{
145-
return new HashSet<CountyItem>();
188+
return [];
146189
}
147190

148191
return RegionService.GetCounties(_cityValue);
149192
}
150193

151-
private IReadOnlySet<string> GetDetails()
194+
private HashSet<string> GetDetails()
152195
{
153196
if (string.IsNullOrEmpty(_countyValue.Code))
154197
{
155-
return new HashSet<string>();
198+
return [];
156199
}
157200

158201
return RegionService.GetDetails(_countyValue.Code);

0 commit comments

Comments
 (0)