@lumx/react 3.5.2 → 3.5.4-alpha-remove-moment.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -2
- package/index.js +237 -91
- package/index.js.map +1 -1
- package/package.json +4 -6
- package/src/components/date-picker/DatePicker.stories.tsx +38 -0
- package/src/components/date-picker/DatePicker.tsx +8 -11
- package/src/components/date-picker/DatePickerControlled.tsx +35 -43
- package/src/components/date-picker/DatePickerField.stories.tsx +0 -1
- package/src/components/date-picker/DatePickerField.tsx +7 -8
- package/src/components/date-picker/types.ts +1 -1
- package/src/utils/date/addMonthResetDay.test.ts +13 -0
- package/src/utils/date/addMonthResetDay.ts +9 -0
- package/src/utils/date/formatDate.ts +9 -0
- package/src/utils/date/formatDayNumber.ts +2 -0
- package/src/utils/date/formatMonthYear.ts +5 -0
- package/src/utils/date/getFirstDayOfWeek.test.ts +27 -0
- package/src/utils/date/getFirstDayOfWeek.ts +59 -0
- package/src/utils/date/getMonthCalendar.test.ts +119 -0
- package/src/utils/date/getMonthCalendar.ts +52 -0
- package/src/utils/date/getWeekDays.test.ts +52 -0
- package/src/utils/date/getWeekDays.ts +32 -0
- package/src/utils/date/isDateValid.test.ts +15 -0
- package/src/utils/date/isDateValid.ts +4 -0
- package/src/utils/date/isSameDay.test.ts +27 -0
- package/src/utils/date/isSameDay.ts +7 -0
- package/src/utils/locale/getCurrentLocale.ts +4 -0
- package/src/utils/locale/getLocaleLang.ts +5 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getCurrentLocale } from '../locale/getCurrentLocale';
|
|
2
|
+
import { getFirstDayOfWeek } from './getFirstDayOfWeek';
|
|
3
|
+
|
|
4
|
+
export type WeekDayInfo = { letter: string; number: number };
|
|
5
|
+
|
|
6
|
+
export const DAYS_PER_WEEK = 7;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* List week days (based on locale) with the week day letter (ex: "M" for "Monday") and week day number
|
|
10
|
+
* (0-based index starting on Sunday).
|
|
11
|
+
*/
|
|
12
|
+
export const getWeekDays = (locale = getCurrentLocale(), referenceDate = new Date()): Array<WeekDayInfo> => {
|
|
13
|
+
const iterDate = new Date(referenceDate.getTime());
|
|
14
|
+
const firstDay = getFirstDayOfWeek(locale) ?? 1;
|
|
15
|
+
|
|
16
|
+
// Go to start of the week
|
|
17
|
+
const offset = firstDay - iterDate.getDay();
|
|
18
|
+
iterDate.setDate(iterDate.getDate() + offset);
|
|
19
|
+
|
|
20
|
+
// Iterate through the days of the week
|
|
21
|
+
const weekDays: Array<WeekDayInfo> = [];
|
|
22
|
+
for (let i = 0; i < DAYS_PER_WEEK; i++) {
|
|
23
|
+
// Single letter week day (ex: "M" for "Monday", "L" for "Lundi", etc.)
|
|
24
|
+
const letter = iterDate.toLocaleDateString(locale, { weekday: 'narrow' });
|
|
25
|
+
// Day number (1-based index starting on Monday)
|
|
26
|
+
const number = iterDate.getDay();
|
|
27
|
+
|
|
28
|
+
weekDays.push({ letter, number });
|
|
29
|
+
iterDate.setDate(iterDate.getDate() + 1);
|
|
30
|
+
}
|
|
31
|
+
return weekDays;
|
|
32
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isDateValid } from '@lumx/react/utils/date/isDateValid';
|
|
2
|
+
|
|
3
|
+
describe(isDateValid.name, () => {
|
|
4
|
+
it('should mark `undefined` as invalid', () => {
|
|
5
|
+
expect(isDateValid(undefined)).toBe(false);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('should mark invalid date as invalid', () => {
|
|
9
|
+
expect(isDateValid(new Date('foo'))).toBe(false);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should mark valid date as valid', () => {
|
|
13
|
+
expect(isDateValid(new Date())).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isSameDay } from '@lumx/react/utils/date/isSameDay';
|
|
2
|
+
|
|
3
|
+
describe(isSameDay, () => {
|
|
4
|
+
it('should return true for same dates', () => {
|
|
5
|
+
const date1 = new Date('2023-08-26T12:00:00');
|
|
6
|
+
const date2 = new Date('2023-08-26T15:30:00');
|
|
7
|
+
expect(isSameDay(date1, date2)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should return false for different dates', () => {
|
|
11
|
+
const date1 = new Date('2023-08-26T10:00:00');
|
|
12
|
+
const date2 = new Date('2023-08-27T10:00:00');
|
|
13
|
+
expect(isSameDay(date1, date2)).toBe(false);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should handle different months', () => {
|
|
17
|
+
const date1 = new Date('2023-08-15T08:00:00');
|
|
18
|
+
const date2 = new Date('2023-09-15T08:00:00');
|
|
19
|
+
expect(isSameDay(date1, date2)).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should handle different years', () => {
|
|
23
|
+
const date1 = new Date('2022-12-25T18:30:00');
|
|
24
|
+
const date2 = new Date('2023-12-25T18:30:00');
|
|
25
|
+
expect(isSameDay(date1, date2)).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
});
|