1
1
import { computed , onMounted , ref } from 'vue' ;
2
- import { addYears , endOfYear , getMonth , getYear , set , startOfYear , subYears } from 'date-fns' ;
2
+ import { addYears , differenceInYears , endOfYear , getMonth , getYear , set , startOfYear , subYears } from 'date-fns' ;
3
3
4
4
import { checkHighlightYear , getDate , getMinMaxYear , resetDate , validateMonthYear } from '@/utils/date-utils' ;
5
5
import { checkMinMaxValue , getYears , groupListAndMap } from '@/utils/util' ;
@@ -16,6 +16,7 @@ import type {
16
16
HighlightFn ,
17
17
PropDates ,
18
18
DateFilter ,
19
+ RangeConfig ,
19
20
} from '@/interfaces' ;
20
21
import type { PickerBasePropsType } from '@/props' ;
21
22
import { FlowStep } from '@/constants' ;
@@ -30,6 +31,7 @@ interface Opts {
30
31
highlight : ComputedRef < Highlight | HighlightFn > ;
31
32
propDates : ComputedRef < PropDates > ;
32
33
filters : ComputedRef < DateFilter > ;
34
+ range : ComputedRef < RangeConfig > ;
33
35
emit : VueEmit ;
34
36
}
35
37
@@ -38,6 +40,7 @@ interface Opts {
38
40
*/
39
41
export const useMonthOrQuarterPicker = ( {
40
42
multiCalendars,
43
+ range,
41
44
highlight,
42
45
propDates,
43
46
calendars,
@@ -66,10 +69,19 @@ export const useMonthOrQuarterPicker = ({
66
69
) ;
67
70
} ) ;
68
71
72
+ const isSoloMultiInRange = ( ) => {
73
+ return Array . isArray ( modelValue . value ) && multiCalendars . value . solo && modelValue . value [ 1 ] ;
74
+ } ;
75
+
69
76
const assignMultiCalendars = ( ) => {
70
77
for ( let i = 0 ; i < multiCalendars . value . count ; i ++ ) {
71
78
if ( i === 0 ) {
72
79
calendars . value [ i ] = calendars . value [ 0 ] ;
80
+ } else if ( i === multiCalendars . value . count - 1 && isSoloMultiInRange ( ) ) {
81
+ calendars . value [ i ] = {
82
+ month : getMonth ( ( modelValue . value as Date [ ] ) [ 1 ] ) ,
83
+ year : getYear ( ( modelValue . value as Date [ ] ) [ 1 ] ) ,
84
+ } ;
73
85
} else {
74
86
const prevDate = set ( getDate ( ) , calendars . value [ i - 1 ] ) ;
75
87
calendars . value [ i ] = { month : getMonth ( prevDate ) , year : getYear ( addYears ( prevDate , 1 ) ) } ;
@@ -84,9 +96,15 @@ export const useMonthOrQuarterPicker = ({
84
96
return assignMultiCalendars ( ) ;
85
97
} ;
86
98
99
+ const getDateToFocus = ( dateOne : Date , dateTwo : Date ) => {
100
+ const diff = differenceInYears ( dateTwo , dateOne ) ;
101
+ return range . value . showLastInRange && diff > 1 ? dateTwo : dateOne ;
102
+ } ;
103
+
87
104
const getRangedValueDate = ( dates : Date [ ] ) => {
88
105
if ( props . focusStartDate ) return dates [ 0 ] ;
89
- return dates [ 1 ] ? dates [ 1 ] : dates [ 0 ] ;
106
+ if ( multiCalendars . value . solo ) return dates [ 0 ] ;
107
+ return dates [ 1 ] ? getDateToFocus ( dates [ 0 ] , dates [ 1 ] ) : dates [ 0 ] ;
90
108
} ;
91
109
92
110
const checkModelValue = ( ) => {
0 commit comments