Skip to content

Commit 6bea102

Browse files
authored
feat(DateTimeRange): add ShowSelectedValue parameter (#5800)
* refactor: 使用模式语法 * refactor: 精简代码 * refactor: 代码规范 * doc: 增加24节气变量开关多语言 * feat: 增加 OnDateClick 回调方法 * doc: 增加节气开关 * doc: 增加 OnDateClick 回调方法文档 * feat: 增加 ShowSelectedValue 参数 * doc: 更新示例 * doc: 增加 ShowSelectedValue 说明 * test: 更新单元测试
1 parent 50bc529 commit 6bea102

6 files changed

Lines changed: 101 additions & 10 deletions

File tree

src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,33 @@
1313
</DemoBlock>
1414

1515
<DemoBlock Title="@Localizer["BindValueTitle"]" Introduction="@Localizer["BindValueIntro"]" Name="BindValue">
16+
<section ignore>
17+
<GroupBox Title="@Localizer["Feature"]">
18+
<div class="row g-3 form-inline text-end">
19+
<div class="col-12 col-sm-3">
20+
<Switch DisplayText="@Localizer["FeatureShowLunar"]" ShowLabel="true" @bind-Value="_showLunar" />
21+
</div>
22+
<div class="col-12 col-sm-3">
23+
<Switch DisplayText="@Localizer["FeatureShowSolarTerm"]" ShowLabel="true" @bind-Value="_showSolarTerm" />
24+
</div>
25+
<div class="col-12 col-sm-3">
26+
<Switch DisplayText="@Localizer["FeatureShowFestivals"]" ShowLabel="true" @bind-Value="_showFestivals" />
27+
</div>
28+
<div class="col-12 col-sm-3">
29+
<Switch DisplayText="@Localizer["FeatureShowHolidays"]" ShowLabel="true" @bind-Value="_showHolidays" />
30+
</div>
31+
</div>
32+
</GroupBox>
33+
<div class="mt-3">
34+
<Pre>builder.Services.AddBootstrapHolidayService();</Pre>
35+
</div>
36+
</section>
1637
<div class="row g-3">
1738
<div class="col-12 col-sm-6">
18-
<DateTimeRange @bind-Value="@BindValueDemoDateTimeRangeValue" OnValueChanged="v => BindValueDemoOnValueChanged(v, 1)" ShowLunar="_showLunar" ShowSolarTerm="_showSolarTerm" ShowFestivals="_showFestivals" ShowHolidays="_showHolidays"></DateTimeRange>
39+
<DateTimeRange @bind-Value="@BindValueDemoDateTimeRangeValue" OnValueChanged="v => BindValueDemoOnValueChanged(v, 1)"
40+
ShowLunar="_showLunar" ShowSolarTerm="_showSolarTerm"
41+
ShowFestivals="_showFestivals" ShowHolidays="_showHolidays"
42+
ShowSelectedValue="true"></DateTimeRange>
1943
</div>
2044
<div class="col-12 col-sm-6">
2145
<BootstrapInputGroup>
@@ -56,7 +80,7 @@
5680

5781
<DemoBlock Title="@Localizer["SidebarTitle"]" Introduction="@Localizer["SidebarIntro"]" Name="Sidebar">
5882
<section ignore class="mb-3">@((MarkupString)Localizer["SidebarTip"].Value)</section>
59-
<DateTimeRange Value="@SidebarDateTimeRangeValue" ShowSidebar="true" />
83+
<DateTimeRange Value="@SidebarDateTimeRangeValue" ShowSidebar="true"></DateTimeRange>
6084
</DemoBlock>
6185

6286
<DemoBlock Title="@Localizer["TodayTitle"]" Introduction="@Localizer["TodayIntro"]" Name="Today">
@@ -81,7 +105,7 @@
81105
<DateTimeRange @bind-Value="@ValidateFormModel.Range" />
82106
</div>
83107
<div class="col-12">
84-
<Button ButtonType="ButtonType.Submit" Icon="fa-fw fa-solid fa-floppy-disk" Text="@Localizer["Submit"]" />
108+
<Button ButtonType="ButtonType.Submit" Icon="fa-fw fa-solid fa-floppy-disk" Text="@Localizer["Submit"]"></Button>
85109
</div>
86110
</div>
87111
</ValidateForm>

src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ private Task MaxMinOnValueChanged(DateTimeRangeValue val, int index)
100100
/// </summary>
101101
protected override void OnInitialized()
102102
{
103+
base.OnInitialized();
104+
103105
ValidateFormModel = new ValidateFormRangeFoo()
104106
{
105107
DateTime = DateTime.Now,
@@ -148,6 +150,12 @@ private static List<EventItem> GetEvents() =>
148150
Name = "OnValueChanged",
149151
Description="ValueChanged callback delegate",
150152
Type ="Func<DateTimeRangeValue,Task>"
153+
},
154+
new()
155+
{
156+
Name = "OnDateClick",
157+
Description="Date cell click event callback",
158+
Type ="Func<DateTime,Task>"
151159
}
152160
];
153161

@@ -174,6 +182,14 @@ private static List<AttributeItem> GetAttributes() =>
174182
DefaultValue = "false"
175183
},
176184
new()
185+
{
186+
Name = nameof(DateTimeRange.ShowSelectedValue),
187+
Description = "Whether to show the selected value",
188+
Type = "bool",
189+
ValueList = "true|false",
190+
DefaultValue = "false"
191+
},
192+
new()
177193
{
178194
Name = "ShowToday",
179195
Description = "Whether to show today shortcut button",

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4007,7 +4007,12 @@
40074007
"Submit": "Submit",
40084008
"Limit": "time limit",
40094009
"ViewModeTitle": "ViewMode",
4010-
"ViewModeIntro": "By setting <code>ViewMode=\"DatePickerViewMode.Year\"</code>, the component view is the year view, and by setting <code>ViewMode=\"DatePickerViewMode.Month\"</code>, the component view is the year view."
4010+
"ViewModeIntro": "By setting <code>ViewMode=\"DatePickerViewMode.Year\"</code>, the component view is the year view, and by setting <code>ViewMode=\"DatePickerViewMode.Month\"</code>, the component view is the year view.",
4011+
"Feature": "Feature",
4012+
"FeatureShowLunar": "Lunar",
4013+
"FeatureShowSolarTerm": "Solar Term",
4014+
"FeatureShowFestivals": "Festivals",
4015+
"FeatureShowHolidays": "Holidays"
40114016
},
40124017
"BootstrapBlazor.Server.Components.Samples.Ips": {
40134018
"IpNormalTitle": "Basic usage",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4007,7 +4007,12 @@
40074007
"Submit": "提交",
40084008
"Limit": "时间范围",
40094009
"ViewModeTitle": "显示模式",
4010-
"ViewModeIntro": "通过设置 <code>ViewMode=\"DatePickerViewMode.Year\"</code> 使组件视图为年视图,设置 <code>ViewMode=\"DatePickerViewMode.Month\"</code> 使组件视图为年视图"
4010+
"ViewModeIntro": "通过设置 <code>ViewMode=\"DatePickerViewMode.Year\"</code> 使组件视图为年视图,设置 <code>ViewMode=\"DatePickerViewMode.Month\"</code> 使组件视图为年视图",
4011+
"Feature": "功能体验区",
4012+
"FeatureShowLunar": "显示农历",
4013+
"FeatureShowSolarTerm": "24 节气",
4014+
"FeatureShowFestivals": "节日",
4015+
"FeatureShowHolidays": "法定假日"
40114016
},
40124017
"BootstrapBlazor.Server.Components.Samples.Ips": {
40134018
"IpNormalTitle": "基础用法",

src/BootstrapBlazor/Components/DateTimeRange/DateTimeRange.razor.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using Microsoft.Extensions.Localization;
77
using System.Globalization;
8-
using System.Reflection;
98

109
namespace BootstrapBlazor.Components;
1110

@@ -102,6 +101,12 @@ private string? EndValueString
102101
[Parameter]
103102
public bool AutoCloseClickSideBar { get; set; }
104103

104+
/// <summary>
105+
/// Gets or sets whether show the selected value. Default is false.
106+
/// </summary>
107+
[Parameter]
108+
public bool ShowSelectedValue { get; set; }
109+
105110
/// <summary>
106111
/// 获得/设置 清空按钮文字
107112
/// </summary>
@@ -252,6 +257,12 @@ public bool AllowNull
252257
[Parameter]
253258
public bool ShowHolidays { get; set; }
254259

260+
/// <summary>
261+
/// Gets or sets the date value changed event callback.
262+
/// </summary>
263+
[Parameter]
264+
public Func<DateTime, Task>? OnDateClick { get; set; }
265+
255266
[Inject]
256267
[NotNull]
257268
private IStringLocalizer<DateTimeRange>? Localizer { get; set; }
@@ -344,7 +355,6 @@ private async Task OnClickSidebarItem(DateTimeRangeSidebarItem item)
344355

345356
if (AutoCloseClickSideBar)
346357
{
347-
await InvokeVoidAsync("hide", Id);
348358
await ClickConfirmButton();
349359
}
350360
}
@@ -450,7 +460,7 @@ private async Task ClickConfirmButton()
450460
/// 更新值方法
451461
/// </summary>
452462
/// <param name="d"></param>
453-
private void UpdateValue(DateTime d)
463+
private async Task UpdateValue(DateTime d)
454464
{
455465
if (SelectedValue.Start == DateTime.MinValue)
456466
{
@@ -477,7 +487,7 @@ private void UpdateValue(DateTime d)
477487
SelectedValue.End = DateTime.MinValue;
478488
}
479489

480-
if (ViewMode == DatePickerViewMode.Year || ViewMode == DatePickerViewMode.Month)
490+
if (ViewMode is DatePickerViewMode.Year or DatePickerViewMode.Month)
481491
{
482492
if (SelectedValue.Start != DateTime.MinValue)
483493
{
@@ -488,6 +498,17 @@ private void UpdateValue(DateTime d)
488498
EndValue = SelectedValue.End;
489499
}
490500
}
501+
502+
if (ShowSelectedValue)
503+
{
504+
Value.Start = SelectedValue.Start;
505+
Value.End = SelectedValue.End;
506+
}
507+
508+
if (OnDateClick != null)
509+
{
510+
await OnDateClick(d);
511+
}
491512
StateHasChanged();
492513
}
493514

test/UnitTest/Components/DateTimeRangeTest.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,40 @@ public void StarEqualEnd_Ok()
7878
[Fact]
7979
public async Task RangeValue_Ok()
8080
{
81-
var cut = Context.RenderComponent<DateTimeRange>();
81+
var val = DateTime.Today;
82+
var cut = Context.RenderComponent<DateTimeRange>(pb =>
83+
{
84+
pb.Add(a => a.ShowSelectedValue, true);
85+
pb.Add(a => a.OnDateClick, d =>
86+
{
87+
val = d;
88+
return Task.CompletedTask;
89+
});
90+
});
8291
var cells = cut.FindAll(".date-table tbody span");
8392
var end = cells.First(i => i.TextContent == "7");
8493
await cut.InvokeAsync(() =>
8594
{
8695
end.Click();
8796
});
97+
Assert.Equal(7, val.Day);
98+
var inputs = cut.FindAll(".datetime-range-input");
99+
var input = inputs[0];
100+
var start = val;
101+
Assert.Equal(start.ToString("yyyy-MM-dd"), input.GetAttribute("value"));
88102

89103
cells = cut.FindAll(".date-table tbody span");
90104
var first = cells.First(i => i.TextContent == "1");
91105
await cut.InvokeAsync(() =>
92106
{
93107
first.Click();
94108
});
109+
inputs = cut.FindAll(".datetime-range-input");
110+
input = inputs[0];
111+
Assert.Equal(val.ToString("yyyy-MM-dd"), input.GetAttribute("value"));
112+
113+
input = inputs[1];
114+
Assert.Equal(start.ToString("yyyy-MM-dd"), input.GetAttribute("value"));
95115

96116
// confirm
97117
var confirm = cut.FindAll(".is-confirm")[cut.FindAll(".is-confirm").Count - 1];

0 commit comments

Comments
 (0)