From 9389978152ae4d8939cc2fda7a1cb0c772fcb7b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Can=20Karag=C3=B6z?= Date: Wed, 29 Oct 2025 00:33:37 +0300 Subject: [PATCH] DateWheelPicker: Add MinDate and MaxDate --- .../wwwroot/CodeBeam.MudBlazor.Extensions.xml | 10 +++ .../DateWheelPicker/DateWheelPickerPage.razor | 3 + .../Examples/DateWheelPickerExample2.razor | 23 +++++ .../DateWheelPicker/MudDateWheelPicker.razor | 8 +- .../MudDateWheelPicker.razor.cs | 84 ++++++++++++++++--- 5 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/DateWheelPicker/Examples/DateWheelPickerExample2.razor diff --git a/CodeBeam.MudBlazor.Extensions.Docs.Wasm/wwwroot/CodeBeam.MudBlazor.Extensions.xml b/CodeBeam.MudBlazor.Extensions.Docs.Wasm/wwwroot/CodeBeam.MudBlazor.Extensions.xml index 2a4b9d13..d0f56c68 100644 --- a/CodeBeam.MudBlazor.Extensions.Docs.Wasm/wwwroot/CodeBeam.MudBlazor.Extensions.xml +++ b/CodeBeam.MudBlazor.Extensions.Docs.Wasm/wwwroot/CodeBeam.MudBlazor.Extensions.xml @@ -1915,6 +1915,16 @@ The minimum swipe delta to change wheel value on touch devices. Default is 30. + + + The minimum selectable date. + + + + + The maximum selectable date. + + If true, the year wheel is disabled. diff --git a/CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/DateWheelPicker/DateWheelPickerPage.razor b/CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/DateWheelPicker/DateWheelPickerPage.razor index ad170317..7cebca63 100644 --- a/CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/DateWheelPicker/DateWheelPickerPage.razor +++ b/CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/DateWheelPicker/DateWheelPickerPage.razor @@ -5,4 +5,7 @@ + + + diff --git a/CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/DateWheelPicker/Examples/DateWheelPickerExample2.razor b/CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/DateWheelPicker/Examples/DateWheelPickerExample2.razor new file mode 100644 index 00000000..71c8d73e --- /dev/null +++ b/CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/DateWheelPicker/Examples/DateWheelPickerExample2.razor @@ -0,0 +1,23 @@ +@namespace MudExtensions.Docs.Examples +@using MudBlazor.Extensions + + + + + + + + + + + Set Today + + + + +@code { + DateTime? _date = DateTime.Now; + DateTime? _minDate = DateTime.Now; + DateTime? _maxDate = DateTime.Now.AddYears(3); +} diff --git a/CodeBeam.MudBlazor.Extensions/Components/DateWheelPicker/MudDateWheelPicker.razor b/CodeBeam.MudBlazor.Extensions/Components/DateWheelPicker/MudDateWheelPicker.razor index bf79ab99..4f33f99d 100644 --- a/CodeBeam.MudBlazor.Extensions/Components/DateWheelPicker/MudDateWheelPicker.razor +++ b/CodeBeam.MudBlazor.Extensions/Components/DateWheelPicker/MudDateWheelPicker.razor @@ -37,9 +37,9 @@ { string dateFormat = new string(DateFormat); - while (dateFormat.Length > 0) + foreach (var ch in dateFormat.Distinct()) { - switch (dateFormat[0]) + switch (ch) { case 'd': @@ -48,11 +48,9 @@ OnMonthChanged(m)) ItemCollection="Months" Sensitivity="@Sensitivity" Label="@(ShowHeader ? LocalizedStrings.Month : null)" ToStringFunc="new Func(NumberToString)" Dense="@Dense" Color="@Color" Disabled="FixMonth" /> break; case 'y': - + break; } - - dateFormat = dateFormat.Replace(dateFormat[0].ToString(), string.Empty); } } @if (DateView == DateView.Time || DateView == DateView.Both) diff --git a/CodeBeam.MudBlazor.Extensions/Components/DateWheelPicker/MudDateWheelPicker.razor.cs b/CodeBeam.MudBlazor.Extensions/Components/DateWheelPicker/MudDateWheelPicker.razor.cs index 8b52d852..063bfd78 100644 --- a/CodeBeam.MudBlazor.Extensions/Components/DateWheelPicker/MudDateWheelPicker.razor.cs +++ b/CodeBeam.MudBlazor.Extensions/Components/DateWheelPicker/MudDateWheelPicker.razor.cs @@ -73,6 +73,20 @@ protected override void OnInitialized() [Parameter] public int Sensitivity { get; set; } = 30; + /// + /// The minimum selectable date. + /// + [Parameter] + [Category(CategoryTypes.FormComponent.Behavior)] + public DateTime? MinDate { get; set; } + + /// + /// The maximum selectable date. + /// + [Parameter] + [Category(CategoryTypes.FormComponent.Behavior)] + public DateTime? MaxDate { get; set; } + /// /// If true, the year wheel is disabled. /// @@ -392,23 +406,65 @@ protected void ToggleDateView() private void RefreshDays() { - var month = _month; - var year = _year; - var day = _day; + var years = Enumerable.Range(1, 9999).ToList(); + + if (MinDate.HasValue) + years = years.Where(y => y >= MinDate.Value.Year).ToList(); + + if (MaxDate.HasValue) + years = years.Where(y => y <= MaxDate.Value.Year).ToList(); + + Years = years; + + if (_year < Years.First()) + _year = Years.First(); + else if (_year > Years.Last()) + _year = Years.Last(); + + var months = Enumerable.Range(1, 12).ToList(); + + if (MinDate.HasValue && _year == MinDate.Value.Year) + months = months.Where(m => m >= MinDate.Value.Month).ToList(); + + if (MaxDate.HasValue && _year == MaxDate.Value.Year) + months = months.Where(m => m <= MaxDate.Value.Month).ToList(); - ++month; + Months = months; - if (month == 13) + if (_month < Months.First()) + _month = Months.First(); + else if (_month > Months.Last()) + _month = Months.Last(); + + int maxDay; + try { - month = 1; - ++year; + var nextMonth = _month == 12 ? 1 : _month + 1; + var nextYear = _month == 12 ? _year + 1 : _year; + maxDay = new DateTime(nextYear, nextMonth, 1).AddDays(-1).Day; } - - Days = Enumerable.Range(1, new DateTime(year, month, 1).AddDays(-1).Day).ToList(); - if (Days.Last() < _day) + catch { - _day = Days.Last(); + Days = new(); + return; } + + var days = Enumerable.Range(1, maxDay).ToList(); + + if (MinDate.HasValue && _year == MinDate.Value.Year && _month == MinDate.Value.Month) + days = days.Where(d => d >= MinDate.Value.Day).ToList(); + + if (MaxDate.HasValue && _year == MaxDate.Value.Year && _month == MaxDate.Value.Month) + days = days.Where(d => d <= MaxDate.Value.Day).ToList(); + + Days = days; + + if (Days.Count == 0) + _day = 1; + else if (_day < Days.First()) + _day = Days.First(); + else if (_day > Days.Last()) + _day = Days.Last(); } private void OnMonthChanged(int month) @@ -418,6 +474,12 @@ private void OnMonthChanged(int month) RefreshDays(); } + private void OnYearChanged(int year) + { + _year = year; + RefreshDays(); + } + /// /// ///