diff --git a/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml b/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml index b6ef74ab41..f18d684e08 100644 --- a/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml +++ b/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml @@ -2731,6 +2731,14 @@ + + + Check if all days between two dates are disabled. + + + + + Gets or sets the culture of the component. @@ -2742,6 +2750,12 @@ Function to know if a specific day must be disabled. + + + By default, the check only the first day of the month and the first day of the year for the Month and Year views. + Set this property to `true` to check if all days of the month and year are disabled (more time consuming). + + Apply the disabled style to the days. @@ -14600,6 +14614,14 @@ + + + Returns the last day of the year. + + + + + Returns the last day of the month. diff --git a/src/Core/Components/DateTime/FluentCalendar.razor b/src/Core/Components/DateTime/FluentCalendar.razor index b257289241..65b259455c 100644 --- a/src/Core/Components/DateTime/FluentCalendar.razor +++ b/src/Core/Components/DateTime/FluentCalendar.razor @@ -163,6 +163,7 @@ Culture="@Culture" DisabledSelectable="@DisabledSelectable" AnimatePeriodChanges="@AnimatePeriodChanges" + DisabledCheckAllDaysOfMonthYear="@DisabledCheckAllDaysOfMonthYear" DisabledDateFunc="@(e => DisabledDateFunc != null ? DisabledDateFunc(e) : false)" /> } @@ -177,6 +178,7 @@ Culture="@Culture" DisabledSelectable="@DisabledSelectable" AnimatePeriodChanges="@AnimatePeriodChanges" + DisabledCheckAllDaysOfMonthYear="@DisabledCheckAllDaysOfMonthYear" DisabledDateFunc="@(e => DisabledDateFunc != null ? DisabledDateFunc(e) : false)" /> } diff --git a/src/Core/Components/DateTime/FluentCalendar.razor.cs b/src/Core/Components/DateTime/FluentCalendar.razor.cs index 66be1cae0f..fa477e0076 100644 --- a/src/Core/Components/DateTime/FluentCalendar.razor.cs +++ b/src/Core/Components/DateTime/FluentCalendar.razor.cs @@ -445,4 +445,28 @@ private Task OnSelectDayMouseOverAsync(DateTime value, bool dayDisabled) return Task.CompletedTask; } + + /// + /// Check if all days between two dates are disabled. + /// + /// + /// + /// + internal bool AllDaysAreDisabled(DateTime start, DateTime end) + { + if (DisabledDateFunc is null) + { + return false; + } + + for (var day = start; day <= end; day = day.AddDays(1)) + { + if (!DisabledDateFunc.Invoke(day)) + { + return false; + } + } + + return true; + } } diff --git a/src/Core/Components/DateTime/FluentCalendarBase.cs b/src/Core/Components/DateTime/FluentCalendarBase.cs index 7e40c40f99..7086caa75b 100644 --- a/src/Core/Components/DateTime/FluentCalendarBase.cs +++ b/src/Core/Components/DateTime/FluentCalendarBase.cs @@ -18,6 +18,13 @@ public abstract class FluentCalendarBase : FluentInputBase [Parameter] public virtual Func? DisabledDateFunc { get; set; } + /// + /// By default, the check only the first day of the month and the first day of the year for the Month and Year views. + /// Set this property to `true` to check if all days of the month and year are disabled (more time consuming). + /// + [Parameter] + public virtual bool DisabledCheckAllDaysOfMonthYear { get; set; } + /// /// Apply the disabled style to the days. /// If this is not the case, the days are displayed like the others, but cannot be selected. diff --git a/src/Core/Components/DateTime/FluentCalendarMonth.cs b/src/Core/Components/DateTime/FluentCalendarMonth.cs index 3e31fd075c..43c6898889 100644 --- a/src/Core/Components/DateTime/FluentCalendarMonth.cs +++ b/src/Core/Components/DateTime/FluentCalendarMonth.cs @@ -20,7 +20,14 @@ internal FluentCalendarMonth(FluentCalendar calendar, DateTime month) _calendar = calendar; Month = month.GetDay(_calendar.Culture) == 1 ? month : month.StartOfMonth(_calendar.Culture); - _isInDisabledList = calendar.DisabledDateFunc?.Invoke(Month) ?? false; + if (calendar.DisabledCheckAllDaysOfMonthYear) + { + _isInDisabledList = calendar.AllDaysAreDisabled(month.StartOfMonth(_calendar.Culture), month.EndOfMonth(_calendar.Culture)); + } + else + { + _isInDisabledList = calendar.DisabledDateFunc?.Invoke(Month) ?? false; + } } /// diff --git a/src/Core/Components/DateTime/FluentCalendarYear.cs b/src/Core/Components/DateTime/FluentCalendarYear.cs index 59fde39d30..7438fe186e 100644 --- a/src/Core/Components/DateTime/FluentCalendarYear.cs +++ b/src/Core/Components/DateTime/FluentCalendarYear.cs @@ -20,7 +20,14 @@ internal FluentCalendarYear(FluentCalendar calendar, DateTime year) _calendar = calendar; Year = year.GetDay(_calendar.Culture) == 1 && year.GetMonth(_calendar.Culture) == 1 ? year : year.StartOfYear(_calendar.Culture); - _isInDisabledList = calendar.DisabledDateFunc?.Invoke(Year) ?? false; + if (calendar.DisabledCheckAllDaysOfMonthYear) + { + _isInDisabledList = calendar.AllDaysAreDisabled(year.StartOfYear(_calendar.Culture), year.EndOfYear(_calendar.Culture)); + } + else + { + _isInDisabledList = calendar.DisabledDateFunc?.Invoke(Year) ?? false; + } } /// diff --git a/src/Core/Components/DateTime/FluentDatePicker.razor b/src/Core/Components/DateTime/FluentDatePicker.razor index 154bd7c36c..d5629f8564 100644 --- a/src/Core/Components/DateTime/FluentDatePicker.razor +++ b/src/Core/Components/DateTime/FluentDatePicker.razor @@ -37,6 +37,7 @@ View="@View" DayFormat="@DayFormat" DisabledDateFunc="@DisabledDateFunc" + DisabledCheckAllDaysOfMonthYear="@DisabledCheckAllDaysOfMonthYear" DisabledSelectable="@DisabledSelectable" Value="@Value" ValueChanged="@OnSelectedDateAsync" diff --git a/src/Core/Extensions/DateTimeExtensions.cs b/src/Core/Extensions/DateTimeExtensions.cs index 8404868ab8..f6f572db94 100644 --- a/src/Core/Extensions/DateTimeExtensions.cs +++ b/src/Core/Extensions/DateTimeExtensions.cs @@ -47,6 +47,17 @@ public static DateTime StartOfYear(this DateTime self, CultureInfo culture) return culture.Calendar.ToDateTime(year, 1, 1, 0, 0, 0, 0); } + /// + /// Returns the last day of the year. + /// + /// + /// + /// + public static DateTime EndOfYear(this DateTime self, CultureInfo culture) + { + return self.StartOfYear(culture).AddYears(1, culture).AddDays(-1, culture); + } + /// /// Returns the last day of the month. ///