Skip to content

Commit 86cf839

Browse files
authored
[Calendar] Add DisabledCheckAllDaysOfMonthYear (#3351)
* Add DisabledCheckAllDaysOfMonthYear * Update doc
1 parent f60a549 commit 86cf839

File tree

8 files changed

+83
-2
lines changed

8 files changed

+83
-2
lines changed

examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,6 +2731,14 @@
27312731
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentCalendar.OnSelectDayMouseOverAsync(System.DateTime,System.Boolean)">
27322732
<summary />
27332733
</member>
2734+
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentCalendar.AllDaysAreDisabled(System.DateTime,System.DateTime)">
2735+
<summary>
2736+
Check if all days between two dates are disabled.
2737+
</summary>
2738+
<param name="start"></param>
2739+
<param name="end"></param>
2740+
<returns></returns>
2741+
</member>
27342742
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentCalendarBase.Culture">
27352743
<summary>
27362744
Gets or sets the culture of the component.
@@ -2742,6 +2750,12 @@
27422750
Function to know if a specific day must be disabled.
27432751
</summary>
27442752
</member>
2753+
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentCalendarBase.DisabledCheckAllDaysOfMonthYear">
2754+
<summary>
2755+
By default, the <see cref="P:Microsoft.FluentUI.AspNetCore.Components.FluentCalendarBase.DisabledDateFunc" /> check only the first day of the month and the first day of the year for the Month and Year views.
2756+
Set this property to `true` to check if all days of the month and year are disabled (more time consuming).
2757+
</summary>
2758+
</member>
27452759
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentCalendarBase.DisabledSelectable">
27462760
<summary>
27472761
Apply the disabled style to the <see cref="P:Microsoft.FluentUI.AspNetCore.Components.FluentCalendarBase.DisabledDateFunc"/> days.
@@ -14600,6 +14614,14 @@
1460014614
<param name="culture"></param>
1460114615
<returns></returns>
1460214616
</member>
14617+
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Extensions.DateTimeExtensions.EndOfYear(System.DateTime,System.Globalization.CultureInfo)">
14618+
<summary>
14619+
Returns the last day of the year.
14620+
</summary>
14621+
<param name="self"></param>
14622+
<param name="culture"></param>
14623+
<returns></returns>
14624+
</member>
1460314625
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Extensions.DateTimeExtensions.EndOfMonth(System.DateTime,System.Globalization.CultureInfo)">
1460414626
<summary>
1460514627
Returns the last day of the month.

src/Core/Components/DateTime/FluentCalendar.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
Culture="@Culture"
164164
DisabledSelectable="@DisabledSelectable"
165165
AnimatePeriodChanges="@AnimatePeriodChanges"
166+
DisabledCheckAllDaysOfMonthYear="@DisabledCheckAllDaysOfMonthYear"
166167
DisabledDateFunc="@(e => DisabledDateFunc != null ? DisabledDateFunc(e) : false)" />
167168
}
168169

@@ -177,6 +178,7 @@
177178
Culture="@Culture"
178179
DisabledSelectable="@DisabledSelectable"
179180
AnimatePeriodChanges="@AnimatePeriodChanges"
181+
DisabledCheckAllDaysOfMonthYear="@DisabledCheckAllDaysOfMonthYear"
180182
DisabledDateFunc="@(e => DisabledDateFunc != null ? DisabledDateFunc(e) : false)" />
181183
}
182184

src/Core/Components/DateTime/FluentCalendar.razor.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,28 @@ private Task OnSelectDayMouseOverAsync(DateTime value, bool dayDisabled)
445445

446446
return Task.CompletedTask;
447447
}
448+
449+
/// <summary>
450+
/// Check if all days between two dates are disabled.
451+
/// </summary>
452+
/// <param name="start"></param>
453+
/// <param name="end"></param>
454+
/// <returns></returns>
455+
internal bool AllDaysAreDisabled(DateTime start, DateTime end)
456+
{
457+
if (DisabledDateFunc is null)
458+
{
459+
return false;
460+
}
461+
462+
for (var day = start; day <= end; day = day.AddDays(1))
463+
{
464+
if (!DisabledDateFunc.Invoke(day))
465+
{
466+
return false;
467+
}
468+
}
469+
470+
return true;
471+
}
448472
}

src/Core/Components/DateTime/FluentCalendarBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public abstract class FluentCalendarBase : FluentInputBase<DateTime?>
1818
[Parameter]
1919
public virtual Func<DateTime, bool>? DisabledDateFunc { get; set; }
2020

21+
/// <summary>
22+
/// By default, the <see cref="DisabledDateFunc" /> check only the first day of the month and the first day of the year for the Month and Year views.
23+
/// Set this property to `true` to check if all days of the month and year are disabled (more time consuming).
24+
/// </summary>
25+
[Parameter]
26+
public virtual bool DisabledCheckAllDaysOfMonthYear { get; set; }
27+
2128
/// <summary>
2229
/// Apply the disabled style to the <see cref="DisabledDateFunc"/> days.
2330
/// If this is not the case, the days are displayed like the others, but cannot be selected.

src/Core/Components/DateTime/FluentCalendarMonth.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ internal FluentCalendarMonth(FluentCalendar calendar, DateTime month)
2020
_calendar = calendar;
2121
Month = month.GetDay(_calendar.Culture) == 1 ? month : month.StartOfMonth(_calendar.Culture);
2222

23-
_isInDisabledList = calendar.DisabledDateFunc?.Invoke(Month) ?? false;
23+
if (calendar.DisabledCheckAllDaysOfMonthYear)
24+
{
25+
_isInDisabledList = calendar.AllDaysAreDisabled(month.StartOfMonth(_calendar.Culture), month.EndOfMonth(_calendar.Culture));
26+
}
27+
else
28+
{
29+
_isInDisabledList = calendar.DisabledDateFunc?.Invoke(Month) ?? false;
30+
}
2431
}
2532

2633
/// <summary>

src/Core/Components/DateTime/FluentCalendarYear.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ internal FluentCalendarYear(FluentCalendar calendar, DateTime year)
2020
_calendar = calendar;
2121
Year = year.GetDay(_calendar.Culture) == 1 && year.GetMonth(_calendar.Culture) == 1 ? year : year.StartOfYear(_calendar.Culture);
2222

23-
_isInDisabledList = calendar.DisabledDateFunc?.Invoke(Year) ?? false;
23+
if (calendar.DisabledCheckAllDaysOfMonthYear)
24+
{
25+
_isInDisabledList = calendar.AllDaysAreDisabled(year.StartOfYear(_calendar.Culture), year.EndOfYear(_calendar.Culture));
26+
}
27+
else
28+
{
29+
_isInDisabledList = calendar.DisabledDateFunc?.Invoke(Year) ?? false;
30+
}
2431
}
2532

2633
/// <summary>

src/Core/Components/DateTime/FluentDatePicker.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
View="@View"
3838
DayFormat="@DayFormat"
3939
DisabledDateFunc="@DisabledDateFunc"
40+
DisabledCheckAllDaysOfMonthYear="@DisabledCheckAllDaysOfMonthYear"
4041
DisabledSelectable="@DisabledSelectable"
4142
Value="@Value"
4243
ValueChanged="@OnSelectedDateAsync"

src/Core/Extensions/DateTimeExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public static DateTime StartOfYear(this DateTime self, CultureInfo culture)
4747
return culture.Calendar.ToDateTime(year, 1, 1, 0, 0, 0, 0);
4848
}
4949

50+
/// <summary>
51+
/// Returns the last day of the year.
52+
/// </summary>
53+
/// <param name="self"></param>
54+
/// <param name="culture"></param>
55+
/// <returns></returns>
56+
public static DateTime EndOfYear(this DateTime self, CultureInfo culture)
57+
{
58+
return self.StartOfYear(culture).AddYears(1, culture).AddDays(-1, culture);
59+
}
60+
5061
/// <summary>
5162
/// Returns the last day of the month.
5263
/// </summary>

0 commit comments

Comments
 (0)