jdtcn / BlazorDateRangePicker

A Blazor component for choosing date ranges and dates
MIT License
186 stars 34 forks source link

Disable start date picker #30

Closed phazlett closed 4 years ago

phazlett commented 4 years ago

Is there a way to make the start date read-only while allowing the end date to be modified?

jdtcn commented 4 years ago

Hi, yes it's possible. For example by using DaysEnabledFunction propery:

@page "/startDate"

<DateRangePicker @ref="Picker"
                 DaysEnabledFunction="IsDayEnabled"
                 @bind-StartDate="StartDate"
                 @bind-EndDate="EndDate">
</DateRangePicker>

@code {
    DateRangePicker Picker;

    DateTimeOffset? StartDate { get; set; } = DateTime.Now;
    DateTimeOffset? EndDate { get; set; }

    private bool IsDayEnabled(DateTimeOffset day)
    {
        return (Picker?.TStartDate, Picker?.TEndDate) switch
        {
            (null, null) => day.Date == DateTime.Today,
            (_, null) => day.Date >= DateTime.Today,
            (_, _) => day.Date == DateTime.Today,
        };
    }
}