Skip to content

Commit 770ab9d

Browse files
authored
fix(datepicker): fix the inconsistent weekday labels (#1173)
Updating the WeekStart enum to be aligned with the values Javascript Date object uses allows to simplify the code and maintain compaitbility. Fixed the off-by-one error BREAKING CHANGE: As the WeekStart enum changed order to be aligned with Javascript Date object, now you have to change your weekStart attribute to be -1, so for Monday you should put 1, instead of 2. But it wasn't working before as the values were incorrectly rendered in the first place. fix #1044
1 parent d1f7529 commit 770ab9d

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

content/docs/components/datepicker.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ The `weekStart` prop can be used to set the first day of the week inside the dat
3939

4040
```json
4141
{
42-
"0": "Saturday",
43-
"1": "Sunday",
44-
"2": "Monday",
45-
"3": "Tuesday",
46-
"4": "Wednesday",
47-
"5": "Thursday",
48-
"6": "Friday"
42+
"0": "Sunday",
43+
"1": "Monday",
44+
"2": "Tuesday",
45+
"3": "Wednesday",
46+
"4": "Thursday",
47+
"5": "Friday",
48+
"6": "Saturday"
4949
}
5050
```
5151

examples/datepicker/datepicker.weekStart.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Datepicker } from 'flowbite-react';
99
function Component() {
1010
return (
1111
<Datepicker
12-
weekStart={2} // Monday
12+
weekStart={1} // Monday
1313
/>
1414
);
1515
}
@@ -21,7 +21,7 @@ import { Datepicker } from 'flowbite-react';
2121
function Component() {
2222
return (
2323
<Datepicker
24-
weekStart={2} // Monday
24+
weekStart={1} // Monday
2525
/>
2626
);
2727
}
@@ -30,7 +30,7 @@ function Component() {
3030
function Component() {
3131
return (
3232
<Datepicker
33-
weekStart={2} // Monday
33+
weekStart={1} // Monday
3434
/>
3535
);
3636
}

src/components/Datepicker/Views/Days.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const DatepickerViewsDays: FC<DatepickerViewsDaysProps> = ({ theme: custo
5252
</div>
5353
<div className={theme.items.base}>
5454
{[...Array(42)].map((_date, index) => {
55-
const currentDate = addDays(startDate, index - 1);
55+
const currentDate = addDays(startDate, index);
5656
const day = getFormattedDate(language, currentDate, { day: 'numeric' });
5757

5858
const isSelected = isDateEqual(selectedDate, currentDate);

src/components/Datepicker/helpers.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ export enum Views {
66
}
77

88
export enum WeekStart {
9-
Saturday,
109
Sunday,
1110
Monday,
1211
Tuesday,
1312
Wednesday,
1413
Thursday,
1514
Friday,
15+
Saturday,
1616
}
1717

1818
export const isDateInRange = (date: Date, minDate?: Date, maxDate?: Date): boolean => {
@@ -59,20 +59,23 @@ export const getFirstDayOfTheMonth = (date: Date, weekStart: WeekStart): Date =>
5959
const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
6060
const dayOfWeek = firstDayOfMonth.getDay();
6161

62-
const diff = (dayOfWeek - weekStart + 7) % 7;
62+
let diff = dayOfWeek - weekStart;
63+
if (diff < 0) {
64+
diff += 7;
65+
}
66+
6367
return addDays(firstDayOfMonth, -diff);
6468
};
6569

6670
export const getWeekDays = (lang: string, weekStart: WeekStart): string[] => {
6771
const weekdays: string[] = [];
6872
const date = new Date(0);
73+
date.setDate(date.getDate() - date.getDay() + weekStart);
6974

7075
const formatter = new Intl.DateTimeFormat(lang, { weekday: 'short' });
7176

7277
for (let i = 0; i < 7; i++) {
73-
const dayIndex = (i + weekStart + 1) % 7;
74-
const formattedWeekday = formatter.format(addDays(date, dayIndex + 1));
75-
weekdays.push(formattedWeekday.slice(0, 2).charAt(0).toUpperCase() + formattedWeekday.slice(1, 3));
78+
weekdays.push(formatter.format(addDays(date, i)));
7679
}
7780

7881
return weekdays;

0 commit comments

Comments
 (0)