@@ -3,7 +3,6 @@ import { useState } from 'react';
33import type {
44 UseCalendarOptions ,
55 DateCellInfo ,
6- SelectDate ,
76 SelectDateOptions ,
87 SelectType ,
98} from '../types' ;
@@ -12,11 +11,11 @@ import useDisplayedDate from './useDisplayedDate';
1211
1312const CELLS_OF_PICKER = 42 ;
1413
15- export default function useCalendarComponent < S extends SelectType > ( {
14+ export default function useCalendarComponent < S extends SelectType = 'single' > ( {
1615 displayedDate = new Date ( ) ,
1716 selectType,
17+ defaultValue,
1818} : UseCalendarOptions < S > = { } ) {
19- console . log ( selectType ) ;
2019 const {
2120 displayedYear,
2221 displayedMonth,
@@ -25,7 +24,8 @@ export default function useCalendarComponent<S extends SelectType>({
2524 changeDisplayedYear,
2625 changeDisplayedMonth,
2726 } = useDisplayedDate ( displayedDate ) ;
28- const [ selectedDate , setSelectedDate ] = useState < Date [ ] > ( [ ] ) ;
27+ const [ selectedDates , setSelectedDates ] = useState < Date [ ] > ( [ ] ) ;
28+ const [ value , setValue ] = useState ( defaultValue ) ;
2929
3030 const getDateCellInfo = ( cellIndex : number ) : DateCellInfo => {
3131 const prevMonth = displayedMonth === 0 ? 11 : displayedMonth - 1 ;
@@ -58,8 +58,28 @@ export default function useCalendarComponent<S extends SelectType>({
5858 } ;
5959 } ) ( ) ;
6060 const weekDay = cellIndex % 7 ;
61- const selectThisDate = ( options ?: SelectDateOptions ) => {
62- selectDate ( { year, month, dayOfMonth } , options ) ;
61+ const selectThisDate = ( options : SelectDateOptions = { } ) => {
62+ const { changeDisplayedValues = true } = options ;
63+ if ( changeDisplayedValues ) {
64+ setDisplayedYear ( year ) ;
65+ setDisplayedMonth ( month ) ;
66+ }
67+ const selectedDate = new Date ( year , month , dayOfMonth ) ;
68+ if ( selectType === 'multiple' ) {
69+ const newDates = selectedDates . filter (
70+ date => date . toString ( ) !== selectedDate . toString ( )
71+ ) ;
72+ if ( newDates . length === selectedDates . length )
73+ newDates . push ( selectedDate ) ;
74+ newDates . sort ( ( a , b ) => a . getTime ( ) - b . getTime ( ) ) ;
75+ setSelectedDates ( newDates ) ;
76+ setValue ( newDates as any ) ;
77+
78+ return ;
79+ }
80+
81+ setSelectedDates ( [ selectedDate ] ) ;
82+ setValue ( selectedDate as any ) ;
6383 } ;
6484
6585 return {
@@ -69,10 +89,12 @@ export default function useCalendarComponent<S extends SelectType>({
6989 dayOfMonth : dayOfMonth ,
7090 dayOfWeek : weekDay ,
7191 isToday : isToday ( year , month , dayOfMonth ) ,
72- isSelected :
73- year === selectedDate [ 0 ] ?. getFullYear ( ) &&
74- month === selectedDate [ 0 ] ?. getMonth ( ) &&
75- dayOfMonth === selectedDate [ 0 ] ?. getDate ( ) ,
92+ isSelected : ! ! selectedDates . find (
93+ date =>
94+ date . getFullYear ( ) === year &&
95+ date . getMonth ( ) === month &&
96+ date . getDate ( ) === dayOfMonth
97+ ) ,
7698 monthStatus :
7799 monthOffset < 0 ? 'previous' : monthOffset > 0 ? 'next' : 'current' ,
78100 selectThisDate,
@@ -88,21 +110,11 @@ export default function useCalendarComponent<S extends SelectType>({
88110 return arr ;
89111 } ;
90112
91- const selectDate : SelectDate = (
92- { year, month, dayOfMonth } ,
93- options = { }
94- ) => {
95- const { changeDisplayedValues = false } = options ;
96- setSelectedDate ( [ new Date ( year , month , dayOfMonth ) ] ) ;
97- if ( changeDisplayedValues ) {
98- setDisplayedYear ( year ) ;
99- setDisplayedMonth ( month ) ;
100- }
101- } ;
102-
103113 return {
114+ selectedDates,
104115 displayedYear,
105116 displayedMonth,
117+ value,
106118 changeDisplayedYear,
107119 changeDisplayedMonth,
108120 getDateCellInfos,
0 commit comments