react-native-dot-calendar 0.0.0-stage → 0.1.1

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/src/dates.ts ADDED
@@ -0,0 +1,102 @@
1
+ export interface CalendarDay {
2
+ /** `YYYY-MM-DD` */
3
+ date: string;
4
+ /** Day of the month, 1-31. */
5
+ day: number;
6
+ /** False for the leading and trailing days that belong to the neighbouring months. */
7
+ inMonth: boolean;
8
+ }
9
+
10
+ export type WeekDay = 0 | 1 | 2 | 3 | 4 | 5 | 6;
11
+
12
+ interface DateParts {
13
+ year: number;
14
+ /** 0-11 */
15
+ month: number;
16
+ day: number;
17
+ }
18
+
19
+ const DAYS_IN_WEEK = 7;
20
+ const DATE_PATTERN = /^(\d{4})-(\d{2})(?:-(\d{2}))?$/;
21
+
22
+ const pad = (value: number, length: number) => String(value).padStart(length, '0');
23
+
24
+ // Built with setFullYear because `new Date(year, ...)` reads years 0-99 as 1900-1999.
25
+ const localDate = (year: number, month: number, day: number) => {
26
+ const date = new Date(2000, 0, 1);
27
+ date.setFullYear(year, month, day);
28
+ return date;
29
+ };
30
+
31
+ const daysInMonth = (year: number, month: number) => localDate(year, month + 1, 0).getDate();
32
+
33
+ /** Formats a `Date` as `YYYY-MM-DD` in the device's time zone. */
34
+ export const toDateString = (date: Date): string =>
35
+ `${pad(date.getFullYear(), 4)}-${pad(date.getMonth() + 1, 2)}-${pad(date.getDate(), 2)}`;
36
+
37
+ /** Today as `YYYY-MM-DD` in the device's time zone. */
38
+ export const todayString = (): string => toDateString(new Date());
39
+
40
+ /** Reads `YYYY-MM-DD` or `YYYY-MM` (day defaults to 1). Returns null when it is not a real date. */
41
+ export const parseDate = (value: string | null | undefined): DateParts | null => {
42
+ const match = DATE_PATTERN.exec(value ?? '');
43
+ if (!match) {
44
+ return null;
45
+ }
46
+ const year = Number(match[1]);
47
+ const month = Number(match[2]) - 1;
48
+ const day = match[3] === undefined ? 1 : Number(match[3]);
49
+ if (month < 0 || month > 11 || day < 1 || day > daysInMonth(year, month)) {
50
+ return null;
51
+ }
52
+ return { year, month, day };
53
+ };
54
+
55
+ export const isValidDate = (value: string | null | undefined): boolean => parseDate(value) !== null;
56
+
57
+ const requireDate = (value: string): DateParts => {
58
+ const parts = parseDate(value);
59
+ if (!parts) {
60
+ throw new RangeError(`Expected a date as YYYY-MM-DD or YYYY-MM, got "${value}"`);
61
+ }
62
+ return parts;
63
+ };
64
+
65
+ /** First day of the month that holds `value`, as `YYYY-MM-01`. */
66
+ export const startOfMonth = (value: string): string => {
67
+ const { year, month } = requireDate(value);
68
+ return toDateString(localDate(year, month, 1));
69
+ };
70
+
71
+ /** Last day of the month that holds `value`. */
72
+ export const endOfMonth = (value: string): string => {
73
+ const { year, month } = requireDate(value);
74
+ return toDateString(localDate(year, month + 1, 0));
75
+ };
76
+
77
+ /** First day of the month `count` months away from the month that holds `value`. */
78
+ export const addMonths = (value: string, count: number): string => {
79
+ const { year, month } = requireDate(value);
80
+ return toDateString(localDate(year, month + count, 1));
81
+ };
82
+
83
+ /**
84
+ * The weeks to draw for a month. Every week has seven days, so the first and
85
+ * last weeks are filled with days from the neighbouring months.
86
+ */
87
+ export const buildMonthGrid = (value: string, firstDayOfWeek: WeekDay = 0): CalendarDay[][] => {
88
+ const { year, month } = requireDate(value);
89
+ const leading = (localDate(year, month, 1).getDay() - firstDayOfWeek + DAYS_IN_WEEK) % DAYS_IN_WEEK;
90
+ const weekCount = Math.ceil((leading + daysInMonth(year, month)) / DAYS_IN_WEEK);
91
+
92
+ return Array.from({ length: weekCount }, (_week, weekIndex) =>
93
+ Array.from({ length: DAYS_IN_WEEK }, (_day, dayIndex) => {
94
+ const date = localDate(year, month, weekIndex * DAYS_IN_WEEK + dayIndex + 1 - leading);
95
+ return {
96
+ date: toDateString(date),
97
+ day: date.getDate(),
98
+ inMonth: date.getMonth() === month,
99
+ };
100
+ }),
101
+ );
102
+ };
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ export { MonthCalendar } from './MonthCalendar';
2
+ export type { CalendarDot, DayMarking, MarkedDates, MonthCalendarProps } from './MonthCalendar';
3
+ export { defaultTheme } from './theme';
4
+ export type { CalendarTheme } from './theme';
5
+ export {
6
+ addMonths,
7
+ buildMonthGrid,
8
+ endOfMonth,
9
+ isValidDate,
10
+ startOfMonth,
11
+ toDateString,
12
+ todayString,
13
+ } from './dates';
14
+ export type { CalendarDay, WeekDay } from './dates';
package/src/theme.ts ADDED
@@ -0,0 +1,41 @@
1
+ export interface CalendarTheme {
2
+ backgroundColor: string;
3
+ borderRadius: number;
4
+ headerTextColor: string;
5
+ headerBorderColor: string;
6
+ arrowColor: string;
7
+ arrowBackgroundColor: string;
8
+ weekdayTextColor: string;
9
+ dayTextColor: string;
10
+ /** Days of the previous and next month shown in the first and last weeks. */
11
+ adjacentDayTextColor: string;
12
+ /** Days outside `minDate` / `maxDate`. */
13
+ disabledDayTextColor: string;
14
+ todayTextColor: string;
15
+ todayBackgroundColor: string;
16
+ selectedDayTextColor: string;
17
+ selectedDayBackgroundColor: string;
18
+ /** Dots take this colour on the selected day so they stay visible. */
19
+ selectedDotColor: string;
20
+ headerFontFamily?: string;
21
+ weekdayFontFamily?: string;
22
+ dayFontFamily?: string;
23
+ }
24
+
25
+ export const defaultTheme: CalendarTheme = {
26
+ backgroundColor: '#FFFFFF',
27
+ borderRadius: 12,
28
+ headerTextColor: '#1A1C1E',
29
+ headerBorderColor: '#F0F2F5',
30
+ arrowColor: '#3B82F6',
31
+ arrowBackgroundColor: '#F5F7F9',
32
+ weekdayTextColor: '#6B7280',
33
+ dayTextColor: '#1A1C1E',
34
+ adjacentDayTextColor: '#D1D5DB',
35
+ disabledDayTextColor: '#D1D5DB',
36
+ todayTextColor: '#3B82F6',
37
+ todayBackgroundColor: 'rgba(59, 130, 246, 0.1)',
38
+ selectedDayTextColor: '#FFFFFF',
39
+ selectedDayBackgroundColor: '#3B82F6',
40
+ selectedDotColor: '#FFFFFF',
41
+ };