private void OnSelectDateInternal(DateTime selectedDate)
        {
            // stop propagation - needed?

            var dateRange = DateUtilities.GetDateRangeArray(selectedDate, DateRangeType, FirstDayOfWeek, WorkWeekDays);

            if (DateRangeType != DateRangeType.Day)
            {
                dateRange = GetBoundedDateRange(dateRange, MinDate, MaxDate);
            }
            dateRange = dateRange.Where(d => !GetIsRestrictedDate(d)).ToList();

            OnSelectDate.InvokeAsync(new SelectedDateResult()
            {
                Date = selectedDate, SelectedDateRange = dateRange
            });

            // Navigate to next or previous month if needed
            if (AutoNavigateOnSelection && selectedDate.Month != NavigatedDate.Month)
            {
                var compareResult = DateTime.Compare(selectedDate.Date, NavigatedDate.Date);
                if (compareResult < 0)
                {
                    OnSelectPrevMonth();
                }
                else if (compareResult > 0)
                {
                    OnSelectNextMonth();
                }
            }
        }
Esempio n. 2
0
        protected async void OnGotoToday(MouseEventArgs args)
        {
            if (SelectDateOnClick)
            {
                // When using Defaultprops, TypeScript doesn't know that React is going to inject defaults
                // so we use exclamation mark as a hint to the type checker (see link below)
                // https://decembersoft.com/posts/error-ts2532-optional-react-component-props-in-typescript/
                var dates = DateUtilities.GetDateRangeArray(Today, DateRangeType, FirstDayOfWeek, WorkWeekDays);

                await OnSelectDateInternal(new SelectedDateResult()
                {
                    Date = Today, SelectedDateRange = dates
                });
            }

            NavigateDayPickerDay(Today);
            GoTodayEnabled = false;
            focusOnUpdate  = true;
        }
        protected override Task OnParametersSetAsync()
        {
            GenerateWeeks();

            if (ShowWeekNumbers)
            {
                WeekNumbers = DateUtilities.GetWeekNumbersInMonth(Weeks.Count, FirstDayOfWeek, FirstWeekOfYear, NavigatedDate);
            }
            else
            {
                WeekNumbers = null;
            }

            WeekCornersStyled = CreateWeekCornerStyles();

            // determine if previous/next months are in bounds

            var firstDayOfMonth = new DateTime(NavigatedDate.Year, NavigatedDate.Month, 1);

            PrevMonthInBounds = DateTime.Compare(MinDate, firstDayOfMonth) < 0;
            NextMonthInBounds = DateTime.Compare(firstDayOfMonth.AddMonths(1).AddDays(-1), MaxDate) < 0;

            return(base.OnParametersSetAsync());
        }
        private void GenerateWeeks()
        {
            var date       = new DateTime(NavigatedDate.Year, NavigatedDate.Month, 1);
            var todaysDate = DateTime.Now;

            Weeks = new List <List <DayInfo> >();

            // cycle backwards to get first day of week
            while (date.DayOfWeek != FirstDayOfWeek)
            {
                date = date - TimeSpan.FromDays(1);
            }

            // a flag to indicate whether all days of the week are in the month
            var isAllDaysOfWeekOutOfMonth = false;

            // in work week view we want to select the whole week
            var selecteDateRangeType = DateRangeType == DateRangeType.WorkWeek ? DateRangeType.Week : DateRangeType;
            var selectedDates        = DateUtilities.GetDateRangeArray(SelectedDate, selecteDateRangeType, FirstDayOfWeek, WorkWeekDays);

            if (DateRangeType != DateRangeType.Day)
            {
                selectedDates = GetBoundedDateRange(selectedDates, MinDate, MaxDate);
            }

            var shouldGetWeeks = true;

            for (var weekIndex = 0; shouldGetWeeks; weekIndex++)
            {
                List <DayInfo> week = new List <DayInfo>();
                isAllDaysOfWeekOutOfMonth = true;

                for (var dayIndex = 0; dayIndex < 7; dayIndex++)
                {
                    var originalDate = new DateTime(date.Year, date.Month, date.Day);
                    var dayInfo      = new DayInfo()
                    {
                        Key          = date.ToString(),
                        Date         = date.Date.ToString("D"),
                        OriginalDate = originalDate,
                        WeekIndex    = weekIndex,
                        IsInMonth    = date.Month == NavigatedDate.Month,
                        IsToday      = DateTime.Compare(DateTime.Now.Date, originalDate) == 0,
                        IsSelected   = IsInDateRangeArray(date, selectedDates),
                        OnSelected   = () => OnSelectDateInternal(originalDate),
                        IsInBounds   =
                            (DateTime.Compare(MinDate, date) < 1) &&
                            (DateTime.Compare(date, MaxDate) < 1) &&
                            !GetIsRestrictedDate(date)
                    };

                    week.Add(dayInfo);
                    if (dayInfo.IsInMonth)
                    {
                        isAllDaysOfWeekOutOfMonth = false;
                    }

                    date = date.AddDays(1);
                }

                // We append the condition of the loop depending upon the ShowSixWeeksByDefault parameter.
                shouldGetWeeks = ShowSixWeeksByDefault ? !isAllDaysOfWeekOutOfMonth || weekIndex <= 5 : !isAllDaysOfWeekOutOfMonth;
                if (shouldGetWeeks)
                {
                    Weeks.Add(week);
                }
            }
        }