11import { computed , onMounted , Ref , ref , UnwrapRef , watch } from 'vue' ;
2+ import { addDays , addMonths , getDay , getHours , getISOWeek , getMinutes , getMonth , getSeconds , getYear } from 'date-fns' ;
3+
24import { ICalendarDay , IMarker , InternalModuleValue , UseCalendar , VueEmit } from '../../interfaces' ;
35import {
4- addMonthsToDate ,
5- getAddedDays ,
6- getDateHours ,
7- getDateMinutes ,
8- getDateMonth ,
9- getDateSeconds ,
10- getDateYear ,
116 getNextMonthYear ,
127 getNextYearMonth ,
138 getPreviousMonthYear ,
14- getWeekDay ,
15- getWeekNumber ,
169 isDateAfter ,
1710 isDateBefore ,
1811 isDateEqual ,
@@ -56,21 +49,25 @@ interface IUseCalendar {
5649export const useCalendar = ( props : UseCalendar , emit : VueEmit ) : IUseCalendar => {
5750 const today = ref < Date > ( new Date ( ) ) ;
5851 const hoveredDate = ref < Date | null > ( ) ;
59- const month = ref < number > ( getDateMonth ( new Date ( ) ) ) ;
60- const year = ref < number > ( getDateYear ( new Date ( ) ) ) ;
52+ const month = ref < number > ( getMonth ( new Date ( ) ) ) ;
53+ const year = ref < number > ( getYear ( new Date ( ) ) ) ;
6154 const monthNext = ref < number > ( getNextMonthYear ( new Date ( ) ) . month ) ;
6255 const yearNext = ref < number > ( getNextMonthYear ( new Date ( ) ) . year ) ;
63- const hours = ref < number | number [ ] > ( props . range ? [ getDateHours ( ) , getDateHours ( ) ] : getDateHours ( ) ) ;
64- const minutes = ref < number | number [ ] > ( props . range ? [ getDateMinutes ( ) , getDateMinutes ( ) ] : getDateMinutes ( ) ) ;
56+ const hours = ref < number | number [ ] > (
57+ props . range ? [ getHours ( new Date ( ) ) , getHours ( new Date ( ) ) ] : getHours ( new Date ( ) ) ,
58+ ) ;
59+ const minutes = ref < number | number [ ] > (
60+ props . range ? [ getMinutes ( new Date ( ) ) , getMinutes ( new Date ( ) ) ] : getMinutes ( new Date ( ) ) ,
61+ ) ;
6562 const seconds = ref < number | number [ ] > ( props . range ? [ 0 , 0 ] : 0 ) ;
6663
6764 onMounted ( ( ) => {
6865 mapInternalModuleValues ( ) ;
6966
7067 if ( ! modelValue . value ) {
7168 if ( props . startDate ) {
72- month . value = getDateMonth ( new Date ( props . startDate ) ) ;
73- year . value = getDateYear ( new Date ( props . startDate ) ) ;
69+ month . value = getMonth ( new Date ( props . startDate ) ) ;
70+ year . value = getYear ( new Date ( props . startDate ) ) ;
7471 if ( props . twoCalendars ) {
7572 monthNext . value = getNextMonthYear ( new Date ( props . startDate ) ) . month ;
7673 yearNext . value = getNextMonthYear ( new Date ( props . startDate ) ) . year ;
@@ -144,15 +141,15 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
144141 isDateEqual ( sanitizeDate ( new Date ( disabledDate ) ) , sanitizeDate ( date ) ) ,
145142 ) ;
146143 const disabledMonths = props . filters . months . length ? props . filters . months . map ( ( month ) => + month ) : [ ] ;
147- const inDisabledMonths = disabledMonths . includes ( getDateMonth ( date ) ) ;
144+ const inDisabledMonths = disabledMonths . includes ( getMonth ( date ) ) ;
148145 const weekDayDisabled = props . disabledWeekDays . length
149- ? props . disabledWeekDays . some ( ( day ) => + day === getWeekDay ( date ) )
146+ ? props . disabledWeekDays . some ( ( day ) => + day === getDay ( date ) )
150147 : false ;
151148 const notInSpecific = props . allowedDates . length
152149 ? ! props . allowedDates . some ( ( dateVal ) => isDateEqual ( sanitizeDate ( new Date ( dateVal ) ) , sanitizeDate ( date ) ) )
153150 : false ;
154151
155- const dateYear = getDateYear ( date ) ;
152+ const dateYear = getYear ( date ) ;
156153
157154 const outOfYearRange = dateYear < + props . yearRange [ 0 ] || dateYear > + props . yearRange [ 1 ] ;
158155
@@ -204,8 +201,8 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
204201 * Extracted method to map month and year
205202 */
206203 const assignMonthAndYear = ( date : Date ) : void => {
207- month . value = getDateMonth ( date ) ;
208- year . value = getDateYear ( date ) ;
204+ month . value = getMonth ( date ) ;
205+ year . value = getYear ( date ) ;
209206 } ;
210207
211208 /**
@@ -216,18 +213,18 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
216213 if ( isModelValueRange ( modelValue . value ) ) {
217214 if ( modelValue . value . length === 2 ) {
218215 assignMonthAndYear ( modelValue . value [ 0 ] ) ;
219- hours . value = [ getDateHours ( modelValue . value [ 0 ] ) , getDateHours ( modelValue . value [ 1 ] ) ] ;
220- minutes . value = [ getDateMinutes ( modelValue . value [ 0 ] ) , getDateMinutes ( modelValue . value [ 1 ] ) ] ;
221- seconds . value = [ getDateSeconds ( modelValue . value [ 0 ] ) , getDateSeconds ( modelValue . value [ 1 ] ) ] ;
216+ hours . value = [ getHours ( modelValue . value [ 0 ] ) , getHours ( modelValue . value [ 1 ] ) ] ;
217+ minutes . value = [ getMinutes ( modelValue . value [ 0 ] ) , getMinutes ( modelValue . value [ 1 ] ) ] ;
218+ seconds . value = [ getSeconds ( modelValue . value [ 0 ] ) , getSeconds ( modelValue . value [ 1 ] ) ] ;
222219 }
223220 if ( props . twoCalendars ) {
224221 handleNextMonthYear ( ) ;
225222 }
226223 } else {
227224 assignMonthAndYear ( modelValue . value ) ;
228- hours . value = getDateHours ( modelValue . value ) ;
229- minutes . value = getDateMinutes ( modelValue . value ) ;
230- seconds . value = getDateSeconds ( modelValue . value ) ;
225+ hours . value = getHours ( modelValue . value ) ;
226+ minutes . value = getMinutes ( modelValue . value ) ;
227+ seconds . value = getSeconds ( modelValue . value ) ;
231228 }
232229 } else {
233230 if ( props . timePicker ) {
@@ -255,8 +252,8 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
255252 * When using next calendar on auto range mode, adjust month and year for both calendars
256253 */
257254 const handleNextCalendarAutoRange = ( date : string | Date ) => {
258- const monthValue = getDateMonth ( new Date ( date ) ) ;
259- const yearValue = getDateYear ( new Date ( date ) ) ;
255+ const monthValue = getMonth ( new Date ( date ) ) ;
256+ const yearValue = getYear ( new Date ( date ) ) ;
260257 const next = getNextMonthYear ( new Date ( date ) ) ;
261258 month . value = monthValue ;
262259 year . value = yearValue ;
@@ -289,7 +286,7 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
289286 if ( isNext ) {
290287 handleNextCalendarAutoRange ( day . value ) ;
291288 }
292- rangeDate = [ new Date ( day . value ) , getAddedDays ( new Date ( day . value ) , + props . autoRange ) ] ;
289+ rangeDate = [ new Date ( day . value ) , addDays ( new Date ( day . value ) , + props . autoRange ) ] ;
293290 } else {
294291 if ( ! rangeDate [ 0 ] ) {
295292 rangeDate [ 0 ] = new Date ( day . value ) ;
@@ -320,7 +317,7 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
320317 const getWeekNum = ( days : UnwrapRef < ICalendarDay [ ] > ) : string | number => {
321318 const firstCurrentData = days . find ( ( day ) => day . current ) ;
322319 if ( firstCurrentData ) {
323- return getWeekNumber ( firstCurrentData . value ) ;
320+ return getISOWeek ( firstCurrentData . value ) ;
324321 }
325322 return '' ;
326323 } ;
@@ -342,7 +339,7 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
342339 if ( props . autoRange ) {
343340 if ( hoveredDate . value ) {
344341 if ( props . hideOffsetDates && ! day . current ) return false ;
345- const rangeEnd = getAddedDays ( hoveredDate . value , + props . autoRange ) ;
342+ const rangeEnd = addDays ( hoveredDate . value , + props . autoRange ) ;
346343 return isDateEqual ( rangeEnd , new Date ( day . value ) ) ;
347344 }
348345 return false ;
@@ -356,7 +353,7 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
356353 const isAutoRangeInBetween = ( day : UnwrapRef < ICalendarDay > ) : boolean => {
357354 if ( props . autoRange ) {
358355 if ( hoveredDate . value ) {
359- const rangeEnd = getAddedDays ( hoveredDate . value , + props . autoRange ) ;
356+ const rangeEnd = addDays ( hoveredDate . value , + props . autoRange ) ;
360357 if ( props . hideOffsetDates && ! day . current ) return false ;
361358 return isDateAfter ( day . value , hoveredDate . value ) && isDateBefore ( day . value , rangeEnd ) ;
362359 }
@@ -378,15 +375,15 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
378375
379376 const handleNextMonthYear = ( ) : void => {
380377 if ( Array . isArray ( modelValue . value ) && modelValue . value . length === 2 ) {
381- const date = new Date ( modelValue . value [ 1 ] ? modelValue . value [ 1 ] : addMonthsToDate ( modelValue . value [ 0 ] ) ) ;
378+ const date = new Date ( modelValue . value [ 1 ] ? modelValue . value [ 1 ] : addMonths ( modelValue . value [ 0 ] , 1 ) ) ;
382379 if ( ( monthNext . value === month . value && yearNext . value === year . value ) || ! props . twoCalendarsSolo ) {
383380 const date = getNextYearMonth ( month . value , year . value ) ;
384381 monthNext . value = date . month ;
385382 yearNext . value = date . year ;
386383 } else {
387- if ( getDateMonth ( modelValue . value [ 0 ] ) !== getDateMonth ( modelValue . value [ 1 ] ) ) {
388- monthNext . value = getDateMonth ( date ) ;
389- yearNext . value = getDateYear ( date ) ;
384+ if ( getMonth ( modelValue . value [ 0 ] ) !== getMonth ( modelValue . value [ 1 ] ) ) {
385+ monthNext . value = getMonth ( date ) ;
386+ yearNext . value = getYear ( date ) ;
390387 }
391388 }
392389 }
0 commit comments