Skip to content

Commit 77ee4dd

Browse files
committed
feat: 增加省选择组件
1 parent a11152e commit 77ee4dd

3 files changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@namespace BootstrapBlazor.Components
2+
@inherits SelectRegionBase
3+
@attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.Region/Components/SelectRegion.razor.js")]
4+
5+
@if (IsShowLabel)
6+
{
7+
<BootstrapLabel required="@Required" for="@InputId" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
8+
}
9+
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString">
10+
<div class="dropdown-toggle" data-bs-toggle="bb.dropdown" data-bs-placement="@PlacementString" data-bs-offset="@OffsetString" data-bs-custom-class="@CustomClassString">
11+
<input type="text" id="@InputId" disabled="@Disabled" readonly placeholder="@PlaceHolder" class="@InputClassString" value="@CurrentValueAsString" />
12+
<span class="@AppendClassString"><i class="@DropdownIcon"></i></span>
13+
</div>
14+
@if (!IsDisabled)
15+
{
16+
<span class="@ClearClassString" @onclick="OnClearValue"><i class="@ClearIcon"></i></span>
17+
}
18+
<div class="dropdown-menu bb-region-body">
19+
<ul>
20+
@foreach (var item in GetProvinces())
21+
{
22+
<li class="@GetActiveClass(item)" @onclick="() => OnSelectProvince(item)">@item</li>
23+
}
24+
</ul>
25+
</div>
26+
</div>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components;
6+
7+
/// <summary>
8+
/// SelectProvince 组件
9+
/// </summary>
10+
public partial class SelectProvince
11+
{
12+
/// <summary>
13+
/// 获得/设置 是否可多选 默认 false 单选
14+
/// </summary>
15+
[Parameter]
16+
public bool IsMultiple { get; set; }
17+
18+
private string InputId => $"{Id}_input";
19+
20+
private string? ClassString => CssBuilder.Default("select bb-province")
21+
.AddClass("disabled", IsDisabled)
22+
.AddClassFromAttributes(AdditionalAttributes)
23+
.Build();
24+
25+
private readonly HashSet<string> _values = [];
26+
27+
private string? GetActiveClass(string item) => _values.Contains(item) || CurrentValue == item ? "active" : null;
28+
29+
private async Task OnClearValue()
30+
{
31+
if (IsMultiple)
32+
{
33+
_values.Clear();
34+
}
35+
CurrentValue = "";
36+
37+
if (OnClearAsync != null)
38+
{
39+
await OnClearAsync();
40+
}
41+
}
42+
43+
private void OnSelectProvince(string item)
44+
{
45+
if (IsMultiple)
46+
{
47+
if (!_values.Remove(item))
48+
{
49+
_values.Add(item);
50+
}
51+
CurrentValue = string.Join(",", _values);
52+
}
53+
else
54+
{
55+
CurrentValue = item;
56+
}
57+
}
58+
59+
private HashSet<string> GetProvinces() => RegionService.GetProvinces();
60+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
.bb-province {
2+
position: relative;
3+
}
4+
5+
.bb-province:not(.disabled):hover .form-select-append {
6+
display: none;
7+
}
8+
9+
.dropdown-menu {
10+
--bs-dropdown-padding-y: 0;
11+
--bb-region-body-color: #495057;
12+
--bb-region-body-hover-bg-color: #e9ecef;
13+
--bb-region-body-active-bg-color: #dee2e6;
14+
--bb-region-body-hover-color: #000;
15+
--bb-region-body-active-color: #000;
16+
--bb-region-body-width: 400px;
17+
--bb-region-body-padding: .5rem;
18+
--bb-region-body-item-padding: 3px 12px;
19+
--bb-region-body-gap: 5px;
20+
}
21+
22+
.dropdown-menu ul {
23+
margin: 0;
24+
padding: 0;
25+
}
26+
27+
.dropdown-menu li {
28+
list-style: none;
29+
transition: background-color .3s linear, color .3s linear;
30+
}
31+
32+
[data-bs-theme="dark"] .dropdown-menu {
33+
--bb-region-body-color: #c0c4cc;
34+
--bb-region-body-hover-color: #fff;
35+
--bb-region-body-active-color: #fff;
36+
--bb-region-body-hover-bg-color: #495057;
37+
--bb-region-body-active-bg-color: #6c757d;
38+
}
39+
40+
.bb-region-body {
41+
padding: var(--bb-region-body-padding);
42+
width: var(--bb-region-body-width);
43+
}
44+
45+
.bb-region-body ul {
46+
display: flex;
47+
flex-wrap: wrap;
48+
gap: var(--bb-region-body-gap);
49+
}
50+
51+
.bb-region-body ul li {
52+
padding: var(--bb-region-body-item-padding);
53+
border-radius: var(--bs-border-radius);
54+
color: var(--bb-region-body-color);
55+
cursor: pointer;
56+
}
57+
58+
.bb-region-body ul li:hover {
59+
background-color: var(--bb-region-body-hover-bg-color);
60+
color: var(--bb-region-body-hover-color);
61+
}
62+
63+
.bb-region-body ul li.active {
64+
background-color: var(--bb-region-body-active-bg-color);
65+
color: var(--bb-region-body-active-color);
66+
}

0 commit comments

Comments
 (0)