Skip to content

Commit 16469d7

Browse files
dvoituronvnbaaij
andauthored
[dev-v5] Convert the Calendar and DatePicker components to a generic type (#4153)
* Refactor FluentCalendar components to support generic TValue type for enhanced flexibility * Refactoring CalendarTValue convert methods * Refacroring to extension methods * Refactored `FluentCalendar.razor.cs` to use generic type `TValue` for selected dates, improving type safety and flexibility. * Converted `PickerDay`, `PickerMonth`, and `PickerYear` to nullable types in `CalendarDefault.razor`. * Refactor calendar components for nullable date handling Updated calendar components to support nullable DateTime values and generic date representations. Key changes include: - Modified `DisabledDate` methods to accept `DateTime?`. - Changed `SelectedDay` in `CalendarSelection.razor` to a non-nullable `DateTime`. - Updated `DisabledDateFunc` in `FluentCalendarBase.cs` to use a generic type `TValue`. - Refactored `OnSelectedDateHandlerAsync` to accept `TValue`. - Implemented conversions to `TValue` in various methods for consistency. - Enhanced `PickerMonthChanged` to handle nullable month values in `FluentDatePicker.razor`. - Streamlined `OnSelectedDateAsync` to directly use `TValue`. These changes improve the flexibility and robustness of the calendar components. * Fix Unit Tests * Refactoring * Refactoring * Remove "where TValue : struct, IComparable" * Update example * Refactoring * Refactoring * Refactoring * Refactoring * Refactoring * Refactoring * Refactoring * refactoring and add CalendarTypes example * Refactoring * Refactoring * Refactoring * Fix Unit Tests * add comments * Rename IsNull to IsNullOrDefault and update usage * Add Unit Tests * Refactoring * Fix some Unit Tests * Fix Unit Tests * Update Unit Tests * Update parsing logic to handle invalid date inputs and adjust validation error messaging * Update Demo * Update doc * Add TValue Unit Tests * Add Unit Tests --------- Co-authored-by: Vincent Baaij <[email protected]>
1 parent 2342430 commit 16469d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1815
-746
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ dotnet_diagnostic.IDE0029.severity = warning
307307
dotnet_diagnostic.IDE0030.severity = warning
308308

309309
# IDE0031: Use null propagation
310-
dotnet_diagnostic.IDE0031.severity = warning
310+
dotnet_diagnostic.IDE0031.severity = none
311311

312312
# IDE0035: Remove unreachable code
313313
dotnet_diagnostic.IDE0035.severity = warning

examples/Demo/FluentUI.Demo.Client/Documentation/Components/DateTime/Calendar/Examples/CalendarCulture.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@using System.Globalization
1+
@using System.Globalization
22

33
<FluentStack Wrap="true" HorizontalGap="64px">
44

@@ -27,3 +27,4 @@
2727
.ToArray();
2828
private CultureInfo SelectedCulture = new CultureInfo("fa");
2929
}
30+

examples/Demo/FluentUI.Demo.Client/Documentation/Components/DateTime/Calendar/Examples/CalendarDayCustomized.razor

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<FluentCalendar DisabledDateFunc="@DisabledDate" @bind-Value="@SelectedValue">
1+
<FluentCalendar DisabledDateFunc="@DisabledDate" @bind-Value="@SelectedValue">
22
<DaysTemplate>
33
@if (!context.IsInactive &&
44
(context.Date.Day == 5 || context.Date.Day == 15))
@@ -14,16 +14,17 @@
1414
</DaysTemplate>
1515
</FluentCalendar>
1616

17-
<p>Selected date @(SelectedValue?.ToString("yyyy-MM-dd"))</p>
17+
<p>Selected date @(SelectedValue.ToString("yyyy-MM-dd"))</p>
1818

1919
@code
2020
{
21-
private DateTime? SelectedValue = null;
21+
private DateOnly SelectedValue = DateOnly.FromDateTime(DateTime.Today);
2222

23-
private bool DisabledDate(DateTime date)
23+
private bool DisabledDate(DateOnly date)
2424
{
2525
return (date.Day == 3 ||
2626
date.Day == 8 ||
2727
date.Day == 20);
2828
}
2929
}
30+

examples/Demo/FluentUI.Demo.Client/Documentation/Components/DateTime/Calendar/Examples/CalendarDefault.razor

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<FluentStack Wrap="true" HorizontalGap="24px">
1+
<FluentStack Wrap="true" HorizontalGap="24px">
22

33
<div>
44
<FluentCalendar DisabledDateFunc="@DisabledDay"
55
@bind-Value="@SelectedDay"
66
@bind-PickerMonth="@PickerDay"
77
Style="height: 250px; align-content: start;" />
8-
<p>Selected @(SelectedDay?.ToString("yyyy-MM-dd"))</p>
8+
<p>Selected @(SelectedDay.ToString("yyyy-MM-dd"))</p>
99
<p>Panel @(PickerDay.ToString("yyyy-MM-dd"))</p>
1010
</div>
1111

@@ -15,7 +15,7 @@
1515
@bind-Value="@SelectedMonth"
1616
@bind-PickerMonth="@PickerMonth"
1717
Style="height: 250px; align-content: start;" />
18-
<p>Selected @(SelectedMonth?.ToString("yyyy-MM-dd"))</p>
18+
<p>Selected @(SelectedMonth.ToString("yyyy-MM-dd"))</p>
1919
<p>Panel @(PickerMonth.ToString("yyyy-MM-dd"))</p>
2020
</div>
2121

@@ -25,19 +25,22 @@
2525
@bind-Value="@SelectedYear"
2626
@bind-PickerMonth="@PickerYear"
2727
Style="height: 250px; align-content: start;" />
28-
<p>Selected @(SelectedYear?.ToString("yyyy-MM-dd"))</p>
28+
<p>Selected @(SelectedYear.ToString("yyyy-MM-dd"))</p>
2929
<p>Panel @(PickerYear.ToString("yyyy-MM-dd"))</p>
3030
</div>
3131
</FluentStack>
3232

3333
@code
3434
{
35-
private DateTime? SelectedDay = null;
36-
private DateTime PickerDay = DateTime.Today;
37-
private DateTime? SelectedMonth = null;
38-
private DateTime PickerMonth = DateTime.Today;
39-
private DateTime? SelectedYear = null;
40-
private DateTime PickerYear = DateTime.Today;
35+
private static readonly DateTime Today = DateTime.Today;
36+
private static readonly DateTime StartOfMonth = new DateTime(Today.Year, Today.Month, 1);
37+
38+
private DateTime SelectedDay = Today;
39+
private DateTime PickerDay = StartOfMonth;
40+
private DateTime SelectedMonth = Today;
41+
private DateTime PickerMonth = StartOfMonth;
42+
private DateTime SelectedYear = Today;
43+
private DateTime PickerYear = StartOfMonth;
4144

4245
private bool DisabledDay(DateTime date) => date.Day == 3 || date.Day == 8 || date.Day == 20;
4346
private bool DisableMonth(DateTime date) => date.Month == 3 || date.Month == 8;

examples/Demo/FluentUI.Demo.Client/Documentation/Components/DateTime/Calendar/Examples/CalendarSelection.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@* SelectMode: Single *@
55
<div>
66
<FluentCalendar Label="Single"
7-
DisabledDateFunc="@DisabledDay"
7+
DisabledDateFunc="@DisabledNullableDay"
88
DisplayToday="false"
99
@bind-Value="@SelectedDay"
1010
SelectMode="CalendarSelectMode.Single" />
@@ -69,6 +69,7 @@
6969

7070
// Disable all days with the day number 3, 8, and 20.
7171
private bool DisabledDay(DateTime date) => date.Day == 3 || date.Day == 8 || date.Day == 20;
72+
private bool DisabledNullableDay(DateTime? date) => date.HasValue && DisabledDay(date.Value);
7273

7374
// Always select the full week, containing the "clicked" day.
7475
private IEnumerable<DateTime> SelectOneWeek(DateTime date)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<FluentStack Wrap="true" HorizontalGap="24px">
2+
<FluentDatePicker Label="DateTime?" @bind-Value="@NullableDateTime" Width="140px" />
3+
<FluentDatePicker Label="DateTime" @bind-Value="@DateTime" Width="140px" />
4+
<FluentDatePicker Label="DateOnly?" @bind-Value="@NullableDateOnly" Width="140px" />
5+
<FluentDatePicker Label="DateOnly" @bind-Value="@DateOnly" Width="140px" />
6+
</FluentStack>
7+
8+
@code
9+
{
10+
private DateTime? NullableDateTime;
11+
private DateTime DateTime = DateTime.Today;
12+
private DateOnly? NullableDateOnly;
13+
private DateOnly DateOnly = DateOnly.FromDateTime(DateTime.Today);
14+
}

examples/Demo/FluentUI.Demo.Client/Documentation/Components/DateTime/Calendar/FluentCalendar.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ Example: `Culture="@(new CultureInfo("fr"))"` will display the calendar in Frenc
4747

4848
{{ CalendarCulture }}
4949

50+
## Value type
51+
52+
The **FluentCalendar** and **FluentDatePicker** components are a generic components, so you can use it with date types such as `DateTime?`, `DateTime`, `DateOnly?` or `DateOnly`.
53+
Blazor will automatically infer the type based on the value you provide to the `Value` or `SelectedDates` parameters.
54+
You can also explicitly set the type using the generic type parameter: `TValue` (i.e. `TValue="DateOnly?"`).
55+
56+
{{ CalendarTypes }}
5057

5158
## API FluentCalendar
5259

53-
{{ API Type=FluentCalendar }}
60+
{{ API Type=FluentCalendar<DateTime> }}

examples/Demo/FluentUI.Demo.Client/Documentation/Components/DateTime/DatePicker/Examples/DatePickerCulture.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@using System.Globalization
1+
@using System.Globalization
22

33
<FluentStack Wrap="true" HorizontalGap="64px">
44

@@ -26,3 +26,4 @@
2626
.ToArray();
2727
private CultureInfo SelectedCulture = new CultureInfo("fa");
2828
}
29+

examples/Demo/FluentUI.Demo.Client/Documentation/Components/DateTime/DatePicker/Examples/DatePickerDefault.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div>
1+
<div>
22
<b>Selected Date:</b> @(SelectedValue?.ToString("yyyy-MM-dd"))
33
</div>
44

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
12
<FluentDatePicker @bind-Value="@SelectedValue"
23
DoubleClickToDate="@DateTime.Today" />
34
@code
45
{
56
DateTime? SelectedValue = DateTime.Today.AddDays(-2);
67
}
8+

0 commit comments

Comments
 (0)