Skip to content

Commit edb667c

Browse files
izanhzhArgoZhang
andauthored
feat(DateTimePicker): support customize disabled date (#4304)
* feat(DateTimePicker): Support custom disable dates * 调整单元测试 * 撤销格式化调整 * 撤销格式化调整 * 调整单元测试 * 调整ViewMode的赋值逻辑 * 调整单元测试 * 调整SelectedValue赋值 * 提高覆盖率 * 调整MinValueToToday逻辑 * 撤销MinValueToToday时对MinValue/MaxValue的校验 * refactor: 重命名参数名称 * 调整支持不可为空时间 * 补充单元测试 * test: 更新单元测试 * doc: 移除不使用的命名空间 * revert: 撤销更改 * refactor: 更新代码提高代码覆盖率 * refactor: 精简代码提高可读性 * doc: 更新功能示例 * refactor: 更改禁用日期回调方法名称 * test: 更新单元测试 * refactor: 重新设计禁用日期获得逻辑 * doc: 更新示例 * test: 更新单元测试 * refactor: 重构代码 * refactor: 重构代码 * refactor: 撤销代码更改 * test: 更新单元测试 * refactor: 重构清除缓存逻辑 * doc: 更新示例 * refactor: 更新缓存清除逻辑 * test: 更新单元测试 * refactor: 代码重构 * doc: 更新注释文档 * doc: 更新示例 * refactor: 代码重构 * perf: 提高性能 * refactor: 重构代码 * feat: 视图切换时更新禁用日期缓存 * refactor: 格式化代码 * wip: 临时提交 * refactor: 增加 UI 显示逻辑 * revert: 撤销 UI 更改 * refactor: 重命名回调方法 * refactor: 优化代码 * refactor: 更新示例 * feat: 增加缓存清除实例方法 * doc: 增加动态切换回调方法示例 * refactor: 重构 FormatValueAsString 提高可读性 * doc: 更新文档说明 * doc: 更新注意事项 * test: 增加清除方法单元测试 * doc: 更新介绍文字 * doc: 更正单词拼写错误 * refactor: 调整示例 * doc: 更新文档 * doc: 更新示例 * refactor: 更新文档 * refactor: 防止多线程报错 * feat: 增加 DisplayDisabledDayAsEmpty 参数 * doc: 更新示例 * refactor: 更新 FormatValueAsString 逻辑 * doc: 更新 DisplayDisabledDayAsEmpty 参数说明 --------- Co-authored-by: Argo-AscioTech <[email protected]>
1 parent 9b879c4 commit edb667c

File tree

11 files changed

+571
-134
lines changed

11 files changed

+571
-134
lines changed

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
<section ignore>
4040
<Tips class="mt-3">
4141
<p>@((MarkupString)Localizer["FeatureIntro"].Value)</p>
42+
<ul class="ul-demo mt-3">
43+
<li>@((MarkupString)Localizer["FeatureShowLunarIntro"].Value)</li>
44+
<li>@((MarkupString)Localizer["FeatureShowSolarTermIntro"].Value)</li>
45+
<li>@((MarkupString)Localizer["FeatureShowFestivalsIntro"].Value)</li>
46+
<li>@((MarkupString)Localizer["FeatureShowHolidaysIntro"].Value)</li>
47+
</ul>
4248
<div>@((MarkupString)Localizer["FeatureFestivalIntro"].Value)</div>
4349
</Tips>
4450
</section>
@@ -51,7 +57,7 @@
5157
<DateTimePicker @bind-Value="@ValidateFormValue" />
5258
</div>
5359
<div class="col-12 col-sm-auto align-self-end">
54-
<Button ButtonType="ButtonType.Submit" Text="@Localizer["SubmitText"]" Icon="fa-solid fa-floppy-disk" />
60+
<Button ButtonType="ButtonType.Submit" Text="@Localizer["SubmitText"]" Icon="fa-solid fa-floppy-disk"></Button>
5561
</div>
5662
</div>
5763
</ValidateForm>
@@ -173,10 +179,6 @@
173179
</div>
174180
</DemoBlock>
175181

176-
<DemoBlock Title="@Localizer["ShowLunarTitle"]" Introduction="@Localizer["ShowLunarIntro"]" Name="ShowLunar">
177-
<DateTimePicker TValue="DateTimeOffset" ShowLunar="true" ShowSolarTerm="true" ShowFestivals="true" ShowHolidays="true"></DateTimePicker>
178-
</DemoBlock>
179-
180182
<DemoBlock Title="@Localizer["DayTemplateTitle"]" Introduction="@Localizer["DayTemplateIntro"]" Name="DayTemplate">
181183
<DateTimePicker TValue="DateTimeOffset?" CustomClass="custom-picker">
182184
<DayTemplate>
@@ -188,6 +190,38 @@
188190
</DateTimePicker>
189191
</DemoBlock>
190192

193+
<DemoBlock Title="@Localizer["DisableDayCallbackTitle"]" Introduction="@Localizer["DisableDayCallbackIntro"]" Name="OnDisabledDayCallback">
194+
<section ignore>
195+
<Tips>
196+
@((MarkupString)Localizer["DisableDayCallbackTip"].Value)
197+
</Tips>
198+
<GroupBox Title="@Localizer["DisableOptions"]">
199+
<div class="row g-3 form-inline text-end">
200+
<div class="col-12 col-sm-6 col-md-auto">
201+
<Switch DisplayText="@Localizer["DisableWeekend"]" ShowLabel="true" @bind-Value="_disableWeekend" OnValueChanged="OnDisabledDaysChanged" />
202+
</div>
203+
<div class="col-12 col-sm-6 col-md-auto">
204+
<Switch DisplayText="@Localizer["DisableToday"]" ShowLabel="true" @bind-Value="_disableToday" OnValueChanged="OnDisabledDaysChanged" />
205+
</div>
206+
</div>
207+
</GroupBox>
208+
</section>
209+
<div class="row form-inline g-3">
210+
<div class="col-12 col-sm-6">
211+
<DateTimePicker ViewMode="DatePickerViewMode.DateTime" DisplayText="@Localizer["DisableDayCallbackAllowNullDisplayText"]"
212+
ShowLabel="true" @bind-Value="@_disabledNullValue" @ref="_picker1"
213+
OnGetDisabledDaysCallback="OnGetDisabledDaysCallback" DisplayDisabledDayAsEmpty="true">
214+
</DateTimePicker>
215+
</div>
216+
<div class="col-12 col-sm-6">
217+
<DateTimePicker ViewMode="DatePickerViewMode.DateTime" DisplayText="@Localizer["DisableDayCallbackNotAllowNullDisplayText"]"
218+
ShowLabel="true" @bind-Value="@_disabledValue" @ref="_picker2"
219+
OnGetDisabledDaysCallback="OnGetDisabledDaysCallback">
220+
</DateTimePicker>
221+
</div>
222+
</div>
223+
</DemoBlock>
224+
191225
<AttributeTable Items="@GetAttributes()" />
192226

193227
<EventTable Items="@GetEvents()" />

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,54 @@ private string BindValueString
9090
private bool _showSolarTerm = true;
9191
private bool _showFestivals = true;
9292
private bool _showHolidays = true;
93+
private bool _disableWeekend = true;
94+
private bool _disableToday = true;
95+
private DateTime? _disabledNullValue = DateTime.Today;
96+
private DateTime _disabledValue = DateTime.Today;
97+
98+
private async Task<List<DateTime>> OnGetDisabledDaysCallback(DateTime start, DateTime end)
99+
{
100+
var ret = new List<DateTime>();
101+
if (_disableWeekend)
102+
{
103+
var day = start;
104+
while (day <= end)
105+
{
106+
if (day.DayOfWeek is DayOfWeek.Sunday or DayOfWeek.Saturday)
107+
{
108+
ret.Add(day);
109+
}
110+
day = day.AddDays(1);
111+
}
112+
113+
if (DateTime.Today.DayOfWeek is DayOfWeek.Sunday or DayOfWeek.Saturday)
114+
{
115+
// 处理今天是否禁用
116+
ret.Add(DateTime.Today);
117+
}
118+
}
119+
120+
if (_disableToday)
121+
{
122+
// 处理今天是否禁用
123+
ret.Add(DateTime.Today);
124+
}
125+
126+
// 模拟异步延迟
127+
await Task.Delay(100);
128+
return ret;
129+
}
130+
131+
private DateTimePicker<DateTime?> _picker1 = default!;
132+
133+
private DateTimePicker<DateTime> _picker2 = default!;
134+
135+
private Task OnDisabledDaysChanged(bool v)
136+
{
137+
_picker1.ClearDisabledDays();
138+
_picker2.ClearDisabledDays();
139+
return Task.CompletedTask;
140+
}
93141

94142
/// <summary>
95143
/// 获得事件方法
@@ -108,6 +156,12 @@ private EventItem[] GetEvents() =>
108156
Name = "ValueChanged",
109157
Description = Localizer["Event2"],
110158
Type ="EventCallback<DateTime?>"
159+
},
160+
new()
161+
{
162+
Name = "OnGetDisabledDaysCallback",
163+
Description = Localizer["OnGetDisabledDaysCallbackEvent"],
164+
Type ="Func<DateTime, DateTime, Task<List<DateTime>>>"
111165
}
112166
];
113167

@@ -214,6 +268,20 @@ private AttributeItem[] GetAttributes() =>
214268
Type = "bool",
215269
ValueList = "true/false",
216270
DefaultValue = "false"
271+
},
272+
new() {
273+
Name = "EnableDisabledDaysCache",
274+
Description = Localizer["AttrEnableDisabledDaysCache"],
275+
Type = "bool",
276+
ValueList = "true/false",
277+
DefaultValue = "true"
278+
},
279+
new() {
280+
Name = "DisplayDisabledDayAsEmpty",
281+
Description = Localizer["AttrDisplayDisabledDayAsEmpty"],
282+
Type = "bool",
283+
ValueList = "true/false",
284+
DefaultValue = "false"
217285
}
218286
];
219287
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ private AttributeItem[] GetAttributes() =>
7171
Description = Localizer["OffTextAttr"],
7272
Type = "string",
7373
ValueList = "—",
74-
DefaultValue = Localizer["OffTextDefautValue"]!
74+
DefaultValue = Localizer["OffTextDefaultValue"]!
7575
},
7676
new()
7777
{
7878
Name = "OnText",
7979
Description = Localizer["OnTextAttr"],
8080
Type = "string",
8181
ValueList = "—",
82-
DefaultValue = Localizer["OnTextDefautValue"]!
82+
DefaultValue = Localizer["OnTextDefaultValue"]!
8383
},
8484
new()
8585
{

0 commit comments

Comments
 (0)