@@ -35,8 +35,16 @@ const Days: React.FC<Props> = ({
35
35
onClickNextDays
36
36
} ) => {
37
37
// Contexts
38
- const { primaryColor, period, changePeriod, dayHover, changeDayHover, minDate, maxDate } =
39
- useContext ( DatepickerContext ) ;
38
+ const {
39
+ primaryColor,
40
+ period,
41
+ changePeriod,
42
+ dayHover,
43
+ changeDayHover,
44
+ minDate,
45
+ maxDate,
46
+ disabledDates
47
+ } = useContext ( DatepickerContext ) ;
40
48
41
49
// Functions
42
50
const currentDateClass = useCallback (
@@ -159,8 +167,7 @@ const Days: React.FC<Props> = ({
159
167
const newHover = `${ newDate . year ( ) } -${ newDate . month ( ) + 1 } -${
160
168
day >= 10 ? day : "0" + day
161
169
} `;
162
-
163
- return dayjs ( newHover ) . isBefore ( dayjs ( minDate ) . subtract ( 1 , "day" ) ) ;
170
+ return dayjs ( newHover ) . isBefore ( dayjs ( minDate ) ) ;
164
171
} ,
165
172
[ calendarData . date , minDate ]
166
173
) ;
@@ -180,20 +187,59 @@ const Days: React.FC<Props> = ({
180
187
day >= 10 ? day : "0" + day
181
188
} `;
182
189
183
- return dayjs ( newHover ) . isAfter ( dayjs ( maxDate ) ) ;
190
+ return dayjs ( newHover ) . isAfter ( dayjs ( maxDate ) . subtract ( 1 , "day" ) ) ;
184
191
} ,
185
192
[ calendarData . date , maxDate ]
186
193
) ;
194
+
195
+ const isDateDisabled = useCallback (
196
+ ( day : number , type : string ) => {
197
+ if ( isDateTooEarly ( day , type ) || isDateTooLate ( day , type ) ) {
198
+ return true ;
199
+ }
200
+ const object = {
201
+ previous : previousMonth ( calendarData . date ) ,
202
+ current : calendarData . date ,
203
+ next : nextMonth ( calendarData . date )
204
+ } ;
205
+ const newDate = object [ type as keyof typeof object ] ;
206
+ const newHover = `${ newDate . year ( ) } -${ newDate . month ( ) + 1 } -${
207
+ day >= 10 ? day : "0" + day
208
+ } `;
209
+
210
+ // disabledDates?.forEach(dateRange => {
211
+ // // If no endDate is provided, disable a single date
212
+ // if (!dateRange.endDate) {
213
+ // console.log(dayjs(newHover));
214
+ // console.log(dayjs(dateRange.startDate));
215
+ // console.log(dayjs(newHover).isSame(dayjs(dateRange.startDate)));
216
+ // if (dayjs(newHover).isSame(dayjs(dateRange.startDate))) {
217
+ // dateShouldBeDisabled = true;
218
+ // }
219
+ // } else {
220
+ // if (
221
+ // dayjs(newHover).isAfter(dayjs(dateRange.endDate)) ||
222
+ // dayjs(newHover).isBefore(dayjs(dateRange.startDate))
223
+ // ) {
224
+ // dateShouldBeDisabled = true;
225
+ // }
226
+ // }
227
+ // });
228
+ return false ;
229
+ } ,
230
+ [ calendarData . date , isDateTooEarly , isDateTooLate ]
231
+ ) ;
232
+
187
233
const buttonClass = useCallback (
188
234
( day : number , type : string ) => {
189
235
const baseClass = "flex items-center justify-center w-12 h-12 lg:w-10 lg:h-10" ;
190
236
return cn (
191
237
baseClass ,
192
238
! activeDateData ( day ) . active ? hoverClassByDay ( day ) : activeDateData ( day ) . className ,
193
- ( isDateTooEarly ( day , type ) || isDateTooLate ( day , type ) ) && "text-red-500 "
239
+ isDateDisabled ( day , type ) && "line-through "
194
240
) ;
195
241
} ,
196
- [ activeDateData , hoverClassByDay , isDateTooEarly , isDateTooLate ]
242
+ [ activeDateData , hoverClassByDay , isDateDisabled ]
197
243
) ;
198
244
199
245
const hoverDay = useCallback (
@@ -208,36 +254,27 @@ const Days: React.FC<Props> = ({
208
254
day >= 10 ? day : "0" + day
209
255
} `;
210
256
211
- if ( ! isDateTooEarly ( day , type ) && ! isDateTooLate ( day , type ) ) {
212
- if ( period . start && ! period . end ) {
213
- if ( dayjs ( newHover ) . isBefore ( dayjs ( period . start ) ) ) {
214
- changePeriod ( {
215
- start : null ,
216
- end : period . start
217
- } ) ;
218
- }
257
+ if ( period . start && ! period . end ) {
258
+ if ( dayjs ( newHover ) . isBefore ( dayjs ( period . start ) ) ) {
259
+ changePeriod ( {
260
+ start : null ,
261
+ end : period . start
262
+ } ) ;
219
263
}
264
+ changeDayHover ( newHover ) ;
265
+ }
220
266
221
- if ( ! period . start && period . end ) {
222
- if ( dayjs ( newHover ) . isAfter ( dayjs ( period . end ) ) ) {
223
- changePeriod ( {
224
- start : period . end ,
225
- end : null
226
- } ) ;
227
- }
267
+ if ( ! period . start && period . end ) {
268
+ if ( dayjs ( newHover ) . isAfter ( dayjs ( period . end ) ) ) {
269
+ changePeriod ( {
270
+ start : period . end ,
271
+ end : null
272
+ } ) ;
228
273
}
229
274
changeDayHover ( newHover ) ;
230
275
}
231
276
} ,
232
- [
233
- calendarData . date ,
234
- changeDayHover ,
235
- changePeriod ,
236
- isDateTooEarly ,
237
- isDateTooLate ,
238
- period . end ,
239
- period . start
240
- ]
277
+ [ calendarData . date , changeDayHover , changePeriod , period . end , period . start ]
241
278
) ;
242
279
243
280
return (
@@ -246,7 +283,7 @@ const Days: React.FC<Props> = ({
246
283
< button
247
284
type = "button"
248
285
key = { index }
249
- disabled = { isDateTooEarly ( item , "previous" ) || isDateTooLate ( item , "previous" ) }
286
+ disabled = { isDateDisabled ( index , "previous" ) }
250
287
className = "flex items-center justify-center text-gray-400 h-12 w-12 lg:w-10 lg:h-10"
251
288
onClick = { ( ) => onClickPreviousDays ( item ) }
252
289
onMouseOver = { ( ) => {
@@ -261,7 +298,7 @@ const Days: React.FC<Props> = ({
261
298
< button
262
299
type = "button"
263
300
key = { index }
264
- disabled = { isDateTooEarly ( item , "current" ) || isDateTooLate ( item , "current" ) }
301
+ disabled = { isDateDisabled ( index , "current" ) }
265
302
className = { `${ buttonClass ( item , "current" ) } ` }
266
303
onClick = { ( ) => {
267
304
onClickDay ( item ) ;
@@ -278,7 +315,7 @@ const Days: React.FC<Props> = ({
278
315
< button
279
316
type = "button"
280
317
key = { index }
281
- disabled = { isDateTooEarly ( item , "next" ) || isDateTooLate ( item , "next ") }
318
+ disabled = { isDateDisabled ( index , "previous " ) }
282
319
className = "flex items-center justify-center text-gray-400 h-12 w-12 lg:w-10 lg:h-10"
283
320
onClick = { ( ) => {
284
321
onClickNextDays ( item ) ;
0 commit comments