1- import { ICalendarDate , ICalendarDay , IDateFilter , IDefaultSelect , ITextInputOptions } from '../interfaces' ;
1+ import {
2+ ICalendarDate ,
3+ ICalendarDay ,
4+ IDateFilter ,
5+ IDefaultSelect ,
6+ ITextInputOptions ,
7+ WeekStartNum ,
8+ } from '../interfaces' ;
9+ import { getAddedDays , isDateEqual , resetDateTime } from './date-utils' ;
210
311/**
412 * Depending on a week start get starting date of the current calendar
@@ -13,12 +21,6 @@ const getFirstDayOfTheFirstWeek = (firstDate: Date, start: number): Date => {
1321 return startDate ;
1422} ;
1523
16- // From the giving date get the date after 7 days
17- const getSevenDaysOffset = ( date : Date ) : Date => {
18- const startDate = new Date ( JSON . parse ( JSON . stringify ( date ) ) ) ;
19- return new Date ( startDate . setDate ( startDate . getDate ( ) + 7 ) ) ;
20- } ;
21-
2224// Get 7 days from the provided start date, month is used to check whether the date is from the specified month or in the offset
2325const getWeekDays = ( startDay : Date , month : number , hideOffsetDates : boolean ) : ICalendarDay [ ] => {
2426 const startDate = new Date ( JSON . parse ( JSON . stringify ( startDay ) ) ) ;
@@ -36,33 +38,30 @@ const getWeekDays = (startDay: Date, month: number, hideOffsetDates: boolean): I
3638 return dates ;
3739} ;
3840
39- /**
40- * Returns the number of weeks in a month, each week must have current month date in
41- */
42- const getNumberOfWeeksInAMonth = ( firstDate : Date , lastDate : Date ) : number => {
43- return Math . ceil ( ( firstDate . getDay ( ) + lastDate . getDate ( ) - 1 ) / 7 ) ;
44- } ;
45-
4641// Get days for the calendar to be displayed in a table grouped by weeks
4742export const getCalendarDays = (
4843 month : number ,
4944 year : number ,
50- start : number ,
45+ start : WeekStartNum ,
5146 hideOffsetDates : boolean ,
5247) : ICalendarDate [ ] => {
5348 const weeks : ICalendarDate [ ] = [ ] ;
5449 const firstDate = new Date ( year , month , 1 ) ;
5550 const lastDate = new Date ( year , month + 1 , 0 ) ;
5651
57- let firstDateInCalendar = getFirstDayOfTheFirstWeek ( firstDate , start ) ;
58- const weeksInMonth = getNumberOfWeeksInAMonth ( firstDate , lastDate ) ;
59- for ( let week = 0 ; week < weeksInMonth ; week ++ ) {
60- if ( week !== 0 ) {
61- firstDateInCalendar = getSevenDaysOffset ( firstDateInCalendar ) ;
62- }
63- const days = getWeekDays ( firstDateInCalendar , month , hideOffsetDates ) ;
52+ const firstDateInCalendar = getFirstDayOfTheFirstWeek ( firstDate , start ) ;
53+
54+ const addDaysToWeek = ( date : Date ) => {
55+ const days = getWeekDays ( date , month , hideOffsetDates ) ;
6456 weeks . push ( { days } ) ;
65- }
57+ if (
58+ ! weeks [ weeks . length - 1 ] . days . some ( ( day ) => isDateEqual ( resetDateTime ( day . value ) , resetDateTime ( lastDate ) ) )
59+ ) {
60+ const nextDate = getAddedDays ( date , 7 ) ;
61+ addDaysToWeek ( nextDate ) ;
62+ }
63+ } ;
64+ addDaysToWeek ( firstDateInCalendar ) ;
6665
6766 return weeks ;
6867} ;
0 commit comments