diff --git a/src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor b/src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor index 14cf2aa879a..874adac8e2f 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor +++ b/src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor @@ -13,9 +13,33 @@ +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
builder.Services.AddBootstrapHolidayService();
+
+
- +
@@ -56,7 +80,7 @@
@((MarkupString)Localizer["SidebarTip"].Value)
- +
@@ -81,7 +105,7 @@
-
diff --git a/src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor.cs b/src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor.cs index 06a5fe1a2d5..b70f84da724 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Samples/DateTimeRanges.razor.cs @@ -100,6 +100,8 @@ private Task MaxMinOnValueChanged(DateTimeRangeValue val, int index) /// protected override void OnInitialized() { + base.OnInitialized(); + ValidateFormModel = new ValidateFormRangeFoo() { DateTime = DateTime.Now, @@ -148,6 +150,12 @@ private static List GetEvents() => Name = "OnValueChanged", Description="ValueChanged callback delegate", Type ="Func" + }, + new() + { + Name = "OnDateClick", + Description="Date cell click event callback", + Type ="Func" } ]; @@ -174,6 +182,14 @@ private static List GetAttributes() => DefaultValue = "false" }, new() + { + Name = nameof(DateTimeRange.ShowSelectedValue), + Description = "Whether to show the selected value", + Type = "bool", + ValueList = "true|false", + DefaultValue = "false" + }, + new() { Name = "ShowToday", Description = "Whether to show today shortcut button", diff --git a/src/BootstrapBlazor.Server/Locales/en-US.json b/src/BootstrapBlazor.Server/Locales/en-US.json index bc17c834253..654363a9b31 100644 --- a/src/BootstrapBlazor.Server/Locales/en-US.json +++ b/src/BootstrapBlazor.Server/Locales/en-US.json @@ -4007,7 +4007,12 @@ "Submit": "Submit", "Limit": "time limit", "ViewModeTitle": "ViewMode", - "ViewModeIntro": "By setting ViewMode=\"DatePickerViewMode.Year\", the component view is the year view, and by setting ViewMode=\"DatePickerViewMode.Month\", the component view is the year view." + "ViewModeIntro": "By setting ViewMode=\"DatePickerViewMode.Year\", the component view is the year view, and by setting ViewMode=\"DatePickerViewMode.Month\", the component view is the year view.", + "Feature": "Feature", + "FeatureShowLunar": "Lunar", + "FeatureShowSolarTerm": "Solar Term", + "FeatureShowFestivals": "Festivals", + "FeatureShowHolidays": "Holidays" }, "BootstrapBlazor.Server.Components.Samples.Ips": { "IpNormalTitle": "Basic usage", diff --git a/src/BootstrapBlazor.Server/Locales/zh-CN.json b/src/BootstrapBlazor.Server/Locales/zh-CN.json index aa2ff96ebde..5d8b77edbdb 100644 --- a/src/BootstrapBlazor.Server/Locales/zh-CN.json +++ b/src/BootstrapBlazor.Server/Locales/zh-CN.json @@ -4007,7 +4007,12 @@ "Submit": "提交", "Limit": "时间范围", "ViewModeTitle": "显示模式", - "ViewModeIntro": "通过设置 ViewMode=\"DatePickerViewMode.Year\" 使组件视图为年视图,设置 ViewMode=\"DatePickerViewMode.Month\" 使组件视图为年视图" + "ViewModeIntro": "通过设置 ViewMode=\"DatePickerViewMode.Year\" 使组件视图为年视图,设置 ViewMode=\"DatePickerViewMode.Month\" 使组件视图为年视图", + "Feature": "功能体验区", + "FeatureShowLunar": "显示农历", + "FeatureShowSolarTerm": "24 节气", + "FeatureShowFestivals": "节日", + "FeatureShowHolidays": "法定假日" }, "BootstrapBlazor.Server.Components.Samples.Ips": { "IpNormalTitle": "基础用法", diff --git a/src/BootstrapBlazor/Components/DateTimeRange/DateTimeRange.razor.cs b/src/BootstrapBlazor/Components/DateTimeRange/DateTimeRange.razor.cs index 0ede2ac4bea..60bc1b681c1 100644 --- a/src/BootstrapBlazor/Components/DateTimeRange/DateTimeRange.razor.cs +++ b/src/BootstrapBlazor/Components/DateTimeRange/DateTimeRange.razor.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.Localization; using System.Globalization; -using System.Reflection; namespace BootstrapBlazor.Components; @@ -102,6 +101,12 @@ private string? EndValueString [Parameter] public bool AutoCloseClickSideBar { get; set; } + /// + /// Gets or sets whether show the selected value. Default is false. + /// + [Parameter] + public bool ShowSelectedValue { get; set; } + /// /// 获得/设置 清空按钮文字 /// @@ -252,6 +257,12 @@ public bool AllowNull [Parameter] public bool ShowHolidays { get; set; } + /// + /// Gets or sets the date value changed event callback. + /// + [Parameter] + public Func? OnDateClick { get; set; } + [Inject] [NotNull] private IStringLocalizer? Localizer { get; set; } @@ -344,7 +355,6 @@ private async Task OnClickSidebarItem(DateTimeRangeSidebarItem item) if (AutoCloseClickSideBar) { - await InvokeVoidAsync("hide", Id); await ClickConfirmButton(); } } @@ -450,7 +460,7 @@ private async Task ClickConfirmButton() /// 更新值方法 /// /// - private void UpdateValue(DateTime d) + private async Task UpdateValue(DateTime d) { if (SelectedValue.Start == DateTime.MinValue) { @@ -477,7 +487,7 @@ private void UpdateValue(DateTime d) SelectedValue.End = DateTime.MinValue; } - if (ViewMode == DatePickerViewMode.Year || ViewMode == DatePickerViewMode.Month) + if (ViewMode is DatePickerViewMode.Year or DatePickerViewMode.Month) { if (SelectedValue.Start != DateTime.MinValue) { @@ -488,6 +498,17 @@ private void UpdateValue(DateTime d) EndValue = SelectedValue.End; } } + + if (ShowSelectedValue) + { + Value.Start = SelectedValue.Start; + Value.End = SelectedValue.End; + } + + if (OnDateClick != null) + { + await OnDateClick(d); + } StateHasChanged(); } diff --git a/test/UnitTest/Components/DateTimeRangeTest.cs b/test/UnitTest/Components/DateTimeRangeTest.cs index e02bfa8988c..4c25d8250e7 100644 --- a/test/UnitTest/Components/DateTimeRangeTest.cs +++ b/test/UnitTest/Components/DateTimeRangeTest.cs @@ -78,13 +78,27 @@ public void StarEqualEnd_Ok() [Fact] public async Task RangeValue_Ok() { - var cut = Context.RenderComponent(); + var val = DateTime.Today; + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.ShowSelectedValue, true); + pb.Add(a => a.OnDateClick, d => + { + val = d; + return Task.CompletedTask; + }); + }); var cells = cut.FindAll(".date-table tbody span"); var end = cells.First(i => i.TextContent == "7"); await cut.InvokeAsync(() => { end.Click(); }); + Assert.Equal(7, val.Day); + var inputs = cut.FindAll(".datetime-range-input"); + var input = inputs[0]; + var start = val; + Assert.Equal(start.ToString("yyyy-MM-dd"), input.GetAttribute("value")); cells = cut.FindAll(".date-table tbody span"); var first = cells.First(i => i.TextContent == "1"); @@ -92,6 +106,12 @@ await cut.InvokeAsync(() => { first.Click(); }); + inputs = cut.FindAll(".datetime-range-input"); + input = inputs[0]; + Assert.Equal(val.ToString("yyyy-MM-dd"), input.GetAttribute("value")); + + input = inputs[1]; + Assert.Equal(start.ToString("yyyy-MM-dd"), input.GetAttribute("value")); // confirm var confirm = cut.FindAll(".is-confirm")[cut.FindAll(".is-confirm").Count - 1];