Skip to content

Commit 2ef44b3

Browse files
committed
[Fix 🐛] startWeekOn confuses dates in the calendar
1 parent a288c5e commit 2ef44b3

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

src/components/Datepicker.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ const Datepicker = (props: DatepickerType) => {
327327
required,
328328
separator,
329329
showFooter,
330-
startWeekOn,
330+
startWeekOn: startWeekOn || START_WEEK,
331331
toggleClassName,
332332
toggleIcon,
333333
updateFirstDate: (newDate: Date) => firstGotoDate(newDate),

src/index.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Datepicker from "./components/Datepicker";
22

3-
export type {
3+
/*export type {
44
ClassNamesTypeProp,
55
Configs,
66
ClassNameType,
@@ -11,7 +11,8 @@ export type {
1111
PopoverDirectionType,
1212
ColorKeys,
1313
WeekStringType
14-
} from "./types";
14+
} from "./types";*/
1515

16-
// export * from "./types";
16+
// eslint-disable-next-line react-refresh/only-export-components
17+
export * from "./types";
1718
export default Datepicker;

src/libs/date.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -577,32 +577,33 @@ export function dayIndexInWeek(date?: Date) {
577577
export function previousDaysInWeek(date: Date, weekStartDayIndex: WeekDaysIndexType = 0) {
578578
if (!dateIsValid(date)) return [];
579579

580-
const dayIndex = dayIndexInWeek(date);
581-
582580
const previousDays: Date[] = [];
583581

584-
for (let i = weekStartDayIndex; i < dayIndex; i++) {
585-
previousDays.push(dayjs(date).day(i).toDate());
582+
let i = 1;
583+
let previousDay = dateAdd(date, -i, "day");
584+
while (dayIndexInWeek(previousDay) !== weekStartDayIndex) {
585+
previousDays.push(previousDay);
586+
i++;
587+
previousDay = dateAdd(date, -i, "day");
586588
}
587589

588-
return previousDays;
590+
return previousDays.sort((a, b) => {
591+
if (dateIsAfter(a, b, "date")) return 1;
592+
return -1;
593+
});
589594
}
590595

591596
export function nextDaysInWeek(date: Date, weekStartDayIndex: WeekDaysIndexType = 0) {
592597
if (!dateIsValid(date)) return [];
593598

594-
const dayIndex = dayIndexInWeek(date);
595-
596599
const nextDays: Date[] = [];
597600

598-
let i = dayIndex + 1;
599-
600-
if (weekStartDayIndex > i) {
601-
i = weekStartDayIndex;
602-
}
603-
604-
for (i; i <= 6; i++) {
605-
nextDays.push(dayjs(date).day(i).toDate());
601+
let i = 1;
602+
let nextDay = dateAdd(date, i, "day");
603+
while (dayIndexInWeek(nextDay) !== weekStartDayIndex) {
604+
nextDays.push(nextDay);
605+
i++;
606+
nextDay = dateAdd(date, i, "day");
606607
}
607608

608609
return nextDays;

0 commit comments

Comments
 (0)