@@ -35,8 +35,16 @@ const Days: React.FC<Props> = ({
3535 onClickNextDays
3636} ) => {
3737 // 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 ) ;
4048
4149 // Functions
4250 const currentDateClass = useCallback (
@@ -159,8 +167,7 @@ const Days: React.FC<Props> = ({
159167 const newHover = `${ newDate . year ( ) } -${ newDate . month ( ) + 1 } -${
160168 day >= 10 ? day : "0" + day
161169 } `;
162-
163- return dayjs ( newHover ) . isBefore ( dayjs ( minDate ) . subtract ( 1 , "day" ) ) ;
170+ return dayjs ( newHover ) . isBefore ( dayjs ( minDate ) ) ;
164171 } ,
165172 [ calendarData . date , minDate ]
166173 ) ;
@@ -180,20 +187,59 @@ const Days: React.FC<Props> = ({
180187 day >= 10 ? day : "0" + day
181188 } `;
182189
183- return dayjs ( newHover ) . isAfter ( dayjs ( maxDate ) ) ;
190+ return dayjs ( newHover ) . isAfter ( dayjs ( maxDate ) . subtract ( 1 , "day" ) ) ;
184191 } ,
185192 [ calendarData . date , maxDate ]
186193 ) ;
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+
187233 const buttonClass = useCallback (
188234 ( day : number , type : string ) => {
189235 const baseClass = "flex items-center justify-center w-12 h-12 lg:w-10 lg:h-10" ;
190236 return cn (
191237 baseClass ,
192238 ! activeDateData ( day ) . active ? hoverClassByDay ( day ) : activeDateData ( day ) . className ,
193- ( isDateTooEarly ( day , type ) || isDateTooLate ( day , type ) ) && "text-red-500 "
239+ isDateDisabled ( day , type ) && "line-through "
194240 ) ;
195241 } ,
196- [ activeDateData , hoverClassByDay , isDateTooEarly , isDateTooLate ]
242+ [ activeDateData , hoverClassByDay , isDateDisabled ]
197243 ) ;
198244
199245 const hoverDay = useCallback (
@@ -208,36 +254,27 @@ const Days: React.FC<Props> = ({
208254 day >= 10 ? day : "0" + day
209255 } `;
210256
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+ } ) ;
219263 }
264+ changeDayHover ( newHover ) ;
265+ }
220266
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+ } ) ;
228273 }
229274 changeDayHover ( newHover ) ;
230275 }
231276 } ,
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 ]
241278 ) ;
242279
243280 return (
@@ -246,7 +283,7 @@ const Days: React.FC<Props> = ({
246283 < button
247284 type = "button"
248285 key = { index }
249- disabled = { isDateTooEarly ( item , "previous" ) || isDateTooLate ( item , "previous" ) }
286+ disabled = { isDateDisabled ( index , "previous" ) }
250287 className = "flex items-center justify-center text-gray-400 h-12 w-12 lg:w-10 lg:h-10"
251288 onClick = { ( ) => onClickPreviousDays ( item ) }
252289 onMouseOver = { ( ) => {
@@ -261,7 +298,7 @@ const Days: React.FC<Props> = ({
261298 < button
262299 type = "button"
263300 key = { index }
264- disabled = { isDateTooEarly ( item , "current" ) || isDateTooLate ( item , "current" ) }
301+ disabled = { isDateDisabled ( index , "current" ) }
265302 className = { `${ buttonClass ( item , "current" ) } ` }
266303 onClick = { ( ) => {
267304 onClickDay ( item ) ;
@@ -278,7 +315,7 @@ const Days: React.FC<Props> = ({
278315 < button
279316 type = "button"
280317 key = { index }
281- disabled = { isDateTooEarly ( item , "next" ) || isDateTooLate ( item , "next ") }
318+ disabled = { isDateDisabled ( index , "previous " ) }
282319 className = "flex items-center justify-center text-gray-400 h-12 w-12 lg:w-10 lg:h-10"
283320 onClick = { ( ) => {
284321 onClickNextDays ( item ) ;
0 commit comments