Skip to content

Commit 03e009e

Browse files
committed
Fix min and max date to include provided day value.
1 parent 5221164 commit 03e009e

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/components/Calendar/Days.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ const Days: React.FC<Props> = ({
149149
);
150150

151151
const isDateTooEarly = useCallback(
152-
(day: number, type: string) => {
152+
(day: number, type: "current" | "previous" | "next") => {
153153
if (!minDate) {
154154
return false;
155155
}
@@ -159,18 +159,16 @@ const Days: React.FC<Props> = ({
159159
next: nextMonth(calendarData.date)
160160
};
161161
const newDate = object[type as keyof typeof object];
162-
const formattedDate = `${newDate.year()}-${newDate.month() + 1}-${
163-
day >= 10 ? day : "0" + day
164-
}`;
165-
return dayjs(formattedDate).isSame(dayjs(minDate))
162+
const formattedDate = newDate.set("date", day);
163+
return dayjs(formattedDate).isSame(dayjs(minDate), "day")
166164
? false
167165
: dayjs(formattedDate).isBefore(dayjs(minDate));
168166
},
169167
[calendarData.date, minDate]
170168
);
171169

172170
const isDateTooLate = useCallback(
173-
(day: number, type: string) => {
171+
(day: number, type: "current" | "previous" | "next") => {
174172
if (!maxDate) {
175173
return false;
176174
}
@@ -180,18 +178,16 @@ const Days: React.FC<Props> = ({
180178
next: nextMonth(calendarData.date)
181179
};
182180
const newDate = object[type as keyof typeof object];
183-
const formattedDate = `${newDate.year()}-${newDate.month() + 1}-${
184-
day >= 10 ? day : "0" + day
185-
}`;
186-
return dayjs(formattedDate).isSame(maxDate)
181+
const formattedDate = newDate.set("date", day);
182+
return dayjs(formattedDate).isSame(dayjs(maxDate), "day")
187183
? false
188184
: dayjs(formattedDate).isAfter(dayjs(maxDate));
189185
},
190186
[calendarData.date, maxDate]
191187
);
192188

193189
const isDateDisabled = useCallback(
194-
(day: number, type: string) => {
190+
(day: number, type: "current" | "previous" | "next") => {
195191
if (isDateTooEarly(day, type) || isDateTooLate(day, type)) {
196192
return true;
197193
}
@@ -230,7 +226,7 @@ const Days: React.FC<Props> = ({
230226
);
231227

232228
const buttonClass = useCallback(
233-
(day: number, type: string) => {
229+
(day: number, type: "current" | "previous" | "next") => {
234230
const baseClass = "flex items-center justify-center w-12 h-12 lg:w-10 lg:h-10";
235231
return cn(
236232
baseClass,

0 commit comments

Comments
 (0)