Skip to content

Range containing non-selectable date(s) #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Robelind opened this issue Mar 5, 2025 · 1 comment
Open

Range containing non-selectable date(s) #110

Robelind opened this issue Mar 5, 2025 · 1 comment

Comments

@Robelind
Copy link

Robelind commented Mar 5, 2025

It is possible to select a range containing non-selectable date(s) (as determined by DaysEnabledFunction).
This should not be possible, or at least a setting should be available to turn off this possibility.

@jdtcn
Copy link
Owner

jdtcn commented Mar 8, 2025

Hi, there is no such setting, but the desired behavior is quite easy to achieve with this code:

<DateRangePicker
    @ref="Picker"
    MaxDate="MaxDate"
    DaysEnabledFunction="DaysEnabled"
    OnSelectionStart="OnSelectionStart"
    OnSelectionEnd="OnSelectionEnd" />

@code {
    DateRangePicker Picker { get; set; }
    DateTimeOffset? MaxDate { get; set; }

    bool DaysEnabled(DateTimeOffset day)
    {
        return day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday;
    }

    void OnSelectionStart(DateTimeOffset startDate)
    {
        // Just for demonstration, use your own logic
        var maxDate = startDate;
        for (int i = 0; i < 60; i++)
        {
            if (!DaysEnabled(maxDate.AddDays(1))) break;
            maxDate = maxDate.AddDays(1);
        }
        MaxDate = maxDate;
    }

    void OnSelectionEnd(DateTimeOffset endDate)
    {
        MaxDate = null;
    }
}

There are also MaxSpan and MinSpan parameters, you can use them as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants