Skip to content

Commit db00fd6

Browse files
committed
添加SelectToday参数,用于当组件有设定最小值时,让其可以默认当天
添加SelectToday参数,用于当组件有设定最小值时,让其可以默认当天
1 parent a5b1537 commit db00fd6

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/BootstrapBlazor/Components/DateTimePicker/DateTimePicker.razor.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ public string? Format
287287
[Parameter]
288288
public bool ShowHolidays { get; set; }
289289

290+
/// <summary>
291+
/// <para lang="zh">获得/设置 是否选择今天 默认 false</para>
292+
/// <para lang="en">Gets or sets Whether to Select Today. Default is false</para>
293+
/// </summary>
294+
/// <remarks>当 Value 值为 null 且组件设置了 <see cref="DateTimePicker{TValue}.MinValue"/> 的值是,当前选中时间为当天, <see cref="DateTimePicker{TValue}.MinValue"/> 不为空类型时此参数生效</remarks>
295+
[Parameter]
296+
public bool SelectToday { get; set; }
297+
290298
/// <summary>
291299
/// <para lang="zh">获取/设置 获得自定义禁用日期回调方法,默认 null 内部默认启用数据缓存 可通过 <see cref="EnableDisabledDaysCache"/> 参数关闭</para>
292300
/// <para lang="en">Gets or sets Callback Method to Get Custom Disabled Days. Default is null. Internal Default Enable Data Cache. Can be Closed via <see cref="EnableDisabledDaysCache"/> Parameter</para>
@@ -373,7 +381,7 @@ protected override void OnParametersSet()
373381
}
374382
else
375383
{
376-
SelectedValue = Value == null ? (MinValue ?? DateTime.MinValue) : (DateTime)(object)Value;
384+
SelectedValue = Value == null ? ((SelectToday ? DateTime.Today : MinValue) ?? DateTime.MinValue) : (DateTime)(object)Value;
377385
}
378386

379387
if (MinValue > SelectedValue)

test/UnitTest/Components/DateTimePickerTest.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,40 @@ public void MinValue_Ok()
296296
Assert.Equal(DateTime.Today, cut.Instance.Value.Date);
297297
}
298298

299+
[Fact]
300+
public void SelectToday_Ok()
301+
{
302+
var cut = Context.Render<DateTimePicker<DateTime?>>(builder =>
303+
{
304+
builder.Add(a => a.Value, null);
305+
builder.Add(a => a.SelectToday, true);
306+
});
307+
Assert.Null(cut.Instance.Value);
308+
309+
// 点击确定
310+
var buttons = cut.FindAll(".picker-panel-footer button");
311+
cut.InvokeAsync(() => buttons[1].Click());
312+
313+
Assert.Equal(DateTime.Today, cut.Instance.Value);
314+
315+
316+
cut.Render(pb =>
317+
{
318+
pb.Add(a => a.MinValue, DateTime.Today.AddDays(-1));
319+
pb.Add(a => a.SelectToday, true);
320+
});
321+
Assert.Equal(DateTime.Today, cut.Instance.Value);
322+
323+
cut.Render(pb =>
324+
{
325+
pb.Add(a => a.Value, DateTime.MinValue);
326+
pb.Add(a => a.MinValue, DateTime.Today.AddDays(-1));
327+
pb.Add(a => a.SelectToday, false);
328+
});
329+
Assert.Equal(DateTime.Today.AddDays(-1).Date, cut.Instance.Value?.Date);
330+
331+
}
332+
299333
[Fact]
300334
public void OnTimeChanged_Ok()
301335
{

0 commit comments

Comments
 (0)