react-native-hilal 0.0.0-stage → 0.1.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/LICENSE +20 -0
- package/README.md +221 -2
- package/lib/module/calendar/Chevron.js +60 -0
- package/lib/module/calendar/Chevron.js.map +1 -0
- package/lib/module/calendar/DayCell.js +48 -0
- package/lib/module/calendar/DayCell.js.map +1 -0
- package/lib/module/calendar/HilalCalendar.js +288 -0
- package/lib/module/calendar/HilalCalendar.js.map +1 -0
- package/lib/module/calendar/index.js +5 -0
- package/lib/module/calendar/index.js.map +1 -0
- package/lib/module/calendar/month.js +178 -0
- package/lib/module/calendar/month.js.map +1 -0
- package/lib/module/calendar/styles.js +125 -0
- package/lib/module/calendar/styles.js.map +1 -0
- package/lib/module/calendar/theme.js +49 -0
- package/lib/module/calendar/theme.js.map +1 -0
- package/lib/module/calendar/types.js +4 -0
- package/lib/module/calendar/types.js.map +1 -0
- package/lib/module/core/convert.js +160 -0
- package/lib/module/core/convert.js.map +1 -0
- package/lib/module/core/days.js +61 -0
- package/lib/module/core/days.js.map +1 -0
- package/lib/module/core/format.js +37 -0
- package/lib/module/core/format.js.map +1 -0
- package/lib/module/core/index.js +6 -0
- package/lib/module/core/index.js.map +1 -0
- package/lib/module/core/locale.js +59 -0
- package/lib/module/core/locale.js.map +1 -0
- package/lib/module/core/types.js +2 -0
- package/lib/module/core/types.js.map +1 -0
- package/lib/module/core/umalqura-data.js +24 -0
- package/lib/module/core/umalqura-data.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/calendar/Chevron.d.ts +13 -0
- package/lib/typescript/src/calendar/Chevron.d.ts.map +1 -0
- package/lib/typescript/src/calendar/DayCell.d.ts +22 -0
- package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -0
- package/lib/typescript/src/calendar/HilalCalendar.d.ts +7 -0
- package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -0
- package/lib/typescript/src/calendar/index.d.ts +4 -0
- package/lib/typescript/src/calendar/index.d.ts.map +1 -0
- package/lib/typescript/src/calendar/month.d.ts +63 -0
- package/lib/typescript/src/calendar/month.d.ts.map +1 -0
- package/lib/typescript/src/calendar/styles.d.ts +119 -0
- package/lib/typescript/src/calendar/styles.d.ts.map +1 -0
- package/lib/typescript/src/calendar/theme.d.ts +31 -0
- package/lib/typescript/src/calendar/theme.d.ts.map +1 -0
- package/lib/typescript/src/calendar/types.d.ts +58 -0
- package/lib/typescript/src/calendar/types.d.ts.map +1 -0
- package/lib/typescript/src/core/convert.d.ts +54 -0
- package/lib/typescript/src/core/convert.d.ts.map +1 -0
- package/lib/typescript/src/core/days.d.ts +24 -0
- package/lib/typescript/src/core/days.d.ts.map +1 -0
- package/lib/typescript/src/core/format.d.ts +23 -0
- package/lib/typescript/src/core/format.d.ts.map +1 -0
- package/lib/typescript/src/core/index.d.ts +5 -0
- package/lib/typescript/src/core/index.d.ts.map +1 -0
- package/lib/typescript/src/core/locale.d.ts +21 -0
- package/lib/typescript/src/core/locale.d.ts.map +1 -0
- package/lib/typescript/src/core/types.d.ts +7 -0
- package/lib/typescript/src/core/types.d.ts.map +1 -0
- package/lib/typescript/src/core/umalqura-data.d.ts +15 -0
- package/lib/typescript/src/core/umalqura-data.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +180 -4
- package/src/calendar/Chevron.tsx +55 -0
- package/src/calendar/DayCell.tsx +83 -0
- package/src/calendar/HilalCalendar.tsx +399 -0
- package/src/calendar/index.ts +8 -0
- package/src/calendar/month.ts +213 -0
- package/src/calendar/styles.ts +127 -0
- package/src/calendar/theme.ts +69 -0
- package/src/calendar/types.ts +61 -0
- package/src/core/convert.ts +192 -0
- package/src/core/days.ts +64 -0
- package/src/core/format.ts +65 -0
- package/src/core/index.ts +18 -0
- package/src/core/locale.ts +138 -0
- package/src/core/types.ts +6 -0
- package/src/core/umalqura-data.ts +34 -0
- package/src/index.tsx +2 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
I18nManager,
|
|
4
|
+
Pressable,
|
|
5
|
+
Text,
|
|
6
|
+
useColorScheme,
|
|
7
|
+
View,
|
|
8
|
+
} from 'react-native';
|
|
9
|
+
|
|
10
|
+
import { hijriOfEpochDay } from '../core/convert';
|
|
11
|
+
import {
|
|
12
|
+
civilFromDays,
|
|
13
|
+
epochDayOfLocalDate,
|
|
14
|
+
localDateOfEpochDay,
|
|
15
|
+
weekdayOfEpochDay,
|
|
16
|
+
} from '../core/days';
|
|
17
|
+
import { formatGregorian, formatHijri } from '../core/format';
|
|
18
|
+
import {
|
|
19
|
+
defaultDigits,
|
|
20
|
+
formatNumber,
|
|
21
|
+
getWeekdayNames,
|
|
22
|
+
type CalendarType,
|
|
23
|
+
type Digits,
|
|
24
|
+
type Locale,
|
|
25
|
+
} from '../core/locale';
|
|
26
|
+
import { Chevron } from './Chevron';
|
|
27
|
+
import { DayCell } from './DayCell';
|
|
28
|
+
import {
|
|
29
|
+
addMonths,
|
|
30
|
+
buildMonthGrid,
|
|
31
|
+
clampDay,
|
|
32
|
+
monthContaining,
|
|
33
|
+
monthOverlaps,
|
|
34
|
+
monthStart,
|
|
35
|
+
monthTitle,
|
|
36
|
+
secondaryTitle,
|
|
37
|
+
selectableRange,
|
|
38
|
+
type CalendarMonth,
|
|
39
|
+
type DayRange,
|
|
40
|
+
} from './month';
|
|
41
|
+
import { createStyles } from './styles';
|
|
42
|
+
import { resolveTheme } from './theme';
|
|
43
|
+
import type { HilalCalendarProps, SelectedDate } from './types';
|
|
44
|
+
|
|
45
|
+
const LABELS: Record<
|
|
46
|
+
Locale,
|
|
47
|
+
{
|
|
48
|
+
hijri: string;
|
|
49
|
+
gregorian: string;
|
|
50
|
+
previous: string;
|
|
51
|
+
next: string;
|
|
52
|
+
separator: string;
|
|
53
|
+
}
|
|
54
|
+
> = {
|
|
55
|
+
ar: {
|
|
56
|
+
hijri: 'هجري',
|
|
57
|
+
gregorian: 'ميلادي',
|
|
58
|
+
previous: 'الشهر السابق',
|
|
59
|
+
next: 'الشهر التالي',
|
|
60
|
+
separator: '، ',
|
|
61
|
+
},
|
|
62
|
+
en: {
|
|
63
|
+
hijri: 'Hijri',
|
|
64
|
+
gregorian: 'Gregorian',
|
|
65
|
+
previous: 'Previous month',
|
|
66
|
+
next: 'Next month',
|
|
67
|
+
separator: ', ',
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
function epochDayOf(date: Date | undefined) {
|
|
72
|
+
return date instanceof Date && !Number.isNaN(date.getTime())
|
|
73
|
+
? epochDayOfLocalDate(date)
|
|
74
|
+
: undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function selectedDateOf(
|
|
78
|
+
epochDay: number,
|
|
79
|
+
locale: Locale,
|
|
80
|
+
digits: Digits
|
|
81
|
+
): SelectedDate {
|
|
82
|
+
const hijri = hijriOfEpochDay(epochDay);
|
|
83
|
+
return {
|
|
84
|
+
gregorian: localDateOfEpochDay(epochDay),
|
|
85
|
+
hijri,
|
|
86
|
+
formatted: {
|
|
87
|
+
gregorian: formatGregorian(civilFromDays(epochDay), { locale, digits }),
|
|
88
|
+
hijri: formatHijri(hijri, { locale, digits }),
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A function with a stable identity that always calls the latest `fn`. */
|
|
94
|
+
function useStableCallback<Args extends unknown[]>(
|
|
95
|
+
fn: (...args: Args) => void
|
|
96
|
+
) {
|
|
97
|
+
const ref = useRef(fn);
|
|
98
|
+
useLayoutEffect(() => {
|
|
99
|
+
ref.current = fn;
|
|
100
|
+
});
|
|
101
|
+
return useCallback((...args: Args) => ref.current(...args), []);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The month to show: `preferred` if it can be shown, else a sensible one. */
|
|
105
|
+
function visibleMonth(
|
|
106
|
+
preferred: CalendarMonth,
|
|
107
|
+
calendar: CalendarType,
|
|
108
|
+
range: DayRange,
|
|
109
|
+
fallbackDay: number
|
|
110
|
+
) {
|
|
111
|
+
return preferred.calendar === calendar && monthOverlaps(preferred, range)
|
|
112
|
+
? preferred
|
|
113
|
+
: monthContaining(clampDay(fallbackDay, range), calendar);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A month-grid date picker for the Umm al-Qura Hijri and the Gregorian
|
|
118
|
+
* calendar.
|
|
119
|
+
*/
|
|
120
|
+
export function HilalCalendar({
|
|
121
|
+
value,
|
|
122
|
+
defaultValue,
|
|
123
|
+
onChange,
|
|
124
|
+
calendar: calendarProp = 'hijri',
|
|
125
|
+
onCalendarChange,
|
|
126
|
+
showCalendarToggle = true,
|
|
127
|
+
showSecondaryDate = true,
|
|
128
|
+
locale = 'ar',
|
|
129
|
+
digits: digitsProp,
|
|
130
|
+
direction = 'auto',
|
|
131
|
+
firstDayOfWeek = 0,
|
|
132
|
+
minDate,
|
|
133
|
+
maxDate,
|
|
134
|
+
isDateDisabled,
|
|
135
|
+
theme: themeProp = 'auto',
|
|
136
|
+
style,
|
|
137
|
+
dayStyle,
|
|
138
|
+
}: HilalCalendarProps) {
|
|
139
|
+
const digits = digitsProp ?? defaultDigits(locale);
|
|
140
|
+
const labels = LABELS[locale];
|
|
141
|
+
// React Native Web leaves I18nManager.isRTL undefined, not false.
|
|
142
|
+
const appRTL = I18nManager.isRTL === true;
|
|
143
|
+
const rtl =
|
|
144
|
+
direction === 'rtl' ||
|
|
145
|
+
(direction === 'auto' && (appRTL || locale === 'ar'));
|
|
146
|
+
// React Native already mirrors 'row' when the whole app is RTL.
|
|
147
|
+
const row = rtl === appRTL ? 'row' : 'row-reverse';
|
|
148
|
+
|
|
149
|
+
const colorScheme = useColorScheme();
|
|
150
|
+
const themeKey =
|
|
151
|
+
typeof themeProp === 'string' ? themeProp : JSON.stringify(themeProp);
|
|
152
|
+
const theme = useMemo(
|
|
153
|
+
() => resolveTheme(themeProp, colorScheme),
|
|
154
|
+
// A partial theme is usually an inline object; compare it by value.
|
|
155
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
156
|
+
[themeKey, colorScheme]
|
|
157
|
+
);
|
|
158
|
+
const styles = useMemo(() => createStyles(theme), [theme]);
|
|
159
|
+
// Same for an inline dayStyle: a new object every render would re-render
|
|
160
|
+
// every day cell.
|
|
161
|
+
const dayStyleKey = JSON.stringify(dayStyle);
|
|
162
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
163
|
+
const stableDayStyle = useMemo(() => dayStyle, [dayStyleKey]);
|
|
164
|
+
|
|
165
|
+
const minDay = epochDayOf(minDate);
|
|
166
|
+
const maxDay = epochDayOf(maxDate);
|
|
167
|
+
const range = useMemo(
|
|
168
|
+
() => selectableRange(minDay, maxDay),
|
|
169
|
+
[minDay, maxDay]
|
|
170
|
+
);
|
|
171
|
+
const today = epochDayOfLocalDate(new Date());
|
|
172
|
+
|
|
173
|
+
// Selection: controlled through `value`, otherwise internal state.
|
|
174
|
+
const [internalDay, setInternalDay] = useState(() =>
|
|
175
|
+
epochDayOf(defaultValue)
|
|
176
|
+
);
|
|
177
|
+
const selectedDay = value !== undefined ? epochDayOf(value) : internalDay;
|
|
178
|
+
|
|
179
|
+
// Calendar: internal state that follows the prop when the prop changes.
|
|
180
|
+
const [calendar, setCalendar] = useState(calendarProp);
|
|
181
|
+
const [lastCalendarProp, setLastCalendarProp] = useState(calendarProp);
|
|
182
|
+
const [month, setMonth] = useState(() =>
|
|
183
|
+
monthContaining(clampDay(selectedDay ?? today, range), calendarProp)
|
|
184
|
+
);
|
|
185
|
+
const [lastSelectedDay, setLastSelectedDay] = useState(selectedDay);
|
|
186
|
+
|
|
187
|
+
const switchTo = (next: CalendarType, current: CalendarMonth) => {
|
|
188
|
+
setCalendar(next);
|
|
189
|
+
// Keep the selected day in view, or else the month being looked at.
|
|
190
|
+
const anchor = selectedDay ?? clampDay(monthStart(current), range);
|
|
191
|
+
setMonth(monthContaining(clampDay(anchor, range), next));
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const shown = visibleMonth(month, calendar, range, selectedDay ?? today);
|
|
195
|
+
|
|
196
|
+
// Adjust state while rendering when props change (React's recommended
|
|
197
|
+
// alternative to effects for derived state).
|
|
198
|
+
if (calendarProp !== lastCalendarProp) {
|
|
199
|
+
setLastCalendarProp(calendarProp);
|
|
200
|
+
switchTo(calendarProp, shown);
|
|
201
|
+
}
|
|
202
|
+
if (selectedDay !== lastSelectedDay) {
|
|
203
|
+
setLastSelectedDay(selectedDay);
|
|
204
|
+
if (
|
|
205
|
+
selectedDay !== undefined &&
|
|
206
|
+
selectedDay >= range.min &&
|
|
207
|
+
selectedDay <= range.max
|
|
208
|
+
) {
|
|
209
|
+
const containing = monthContaining(selectedDay, calendar);
|
|
210
|
+
if (containing.year !== shown.year || containing.month !== shown.month) {
|
|
211
|
+
setMonth(containing);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const grid = useMemo(
|
|
217
|
+
() => buildMonthGrid(shown, firstDayOfWeek),
|
|
218
|
+
// `shown` is a new object on every render; its fields identify it.
|
|
219
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
220
|
+
[shown.calendar, shown.year, shown.month, firstDayOfWeek]
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
// Everything about a day except `selected` and `today`, so that changing
|
|
224
|
+
// the selection doesn't recompute labels or call isDateDisabled.
|
|
225
|
+
const cells = useMemo(
|
|
226
|
+
() =>
|
|
227
|
+
grid.map((week) =>
|
|
228
|
+
week.map((cell) => {
|
|
229
|
+
if (cell === null) {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
const { epochDay, day, secondaryDay } = cell;
|
|
233
|
+
const inSupportedRange = secondaryDay !== null;
|
|
234
|
+
const disabled =
|
|
235
|
+
!inSupportedRange ||
|
|
236
|
+
epochDay < range.min ||
|
|
237
|
+
epochDay > range.max ||
|
|
238
|
+
(isDateDisabled?.(selectedDateOf(epochDay, locale, digits)) ??
|
|
239
|
+
false);
|
|
240
|
+
const weekday = getWeekdayNames(locale)[weekdayOfEpochDay(epochDay)];
|
|
241
|
+
const gregorianText = formatGregorian(civilFromDays(epochDay), {
|
|
242
|
+
locale,
|
|
243
|
+
digits,
|
|
244
|
+
});
|
|
245
|
+
const spoken = inSupportedRange
|
|
246
|
+
? [
|
|
247
|
+
formatHijri(hijriOfEpochDay(epochDay), { locale, digits }),
|
|
248
|
+
gregorianText,
|
|
249
|
+
]
|
|
250
|
+
: [gregorianText];
|
|
251
|
+
if (shown.calendar === 'gregorian') {
|
|
252
|
+
spoken.reverse();
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
epochDay,
|
|
256
|
+
label: formatNumber(day, digits),
|
|
257
|
+
secondaryLabel:
|
|
258
|
+
showSecondaryDate && secondaryDay !== null
|
|
259
|
+
? formatNumber(secondaryDay, digits)
|
|
260
|
+
: null,
|
|
261
|
+
accessibilityLabel: [weekday, ...spoken].join(labels.separator),
|
|
262
|
+
disabled,
|
|
263
|
+
};
|
|
264
|
+
})
|
|
265
|
+
),
|
|
266
|
+
[
|
|
267
|
+
grid,
|
|
268
|
+
shown.calendar,
|
|
269
|
+
range,
|
|
270
|
+
isDateDisabled,
|
|
271
|
+
locale,
|
|
272
|
+
digits,
|
|
273
|
+
showSecondaryDate,
|
|
274
|
+
labels,
|
|
275
|
+
]
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const onSelect = useStableCallback((epochDay: number) => {
|
|
279
|
+
if (value === undefined) {
|
|
280
|
+
setInternalDay(epochDay);
|
|
281
|
+
}
|
|
282
|
+
onChange?.(selectedDateOf(epochDay, locale, digits));
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
const toggleCalendar = (next: CalendarType) => {
|
|
286
|
+
if (next === calendar) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
switchTo(next, shown);
|
|
290
|
+
onCalendarChange?.(next);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const previous = addMonths(shown, -1);
|
|
294
|
+
const next = addMonths(shown, 1);
|
|
295
|
+
const canGoBack = monthOverlaps(previous, range);
|
|
296
|
+
const canGoForward = monthOverlaps(next, range);
|
|
297
|
+
|
|
298
|
+
const weekdayNames = getWeekdayNames(locale, 'short');
|
|
299
|
+
const weekdays = Array.from(
|
|
300
|
+
{ length: 7 },
|
|
301
|
+
(_, i) => weekdayNames[(firstDayOfWeek + i) % 7]!
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
const renderArrow = (kind: 'previous' | 'next') => {
|
|
305
|
+
const enabled = kind === 'previous' ? canGoBack : canGoForward;
|
|
306
|
+
// Arrows point the way the months move: outward in reading order.
|
|
307
|
+
const pointsLeft = (kind === 'previous') !== rtl;
|
|
308
|
+
return (
|
|
309
|
+
<Pressable
|
|
310
|
+
accessibilityRole="button"
|
|
311
|
+
accessibilityLabel={labels[kind]}
|
|
312
|
+
accessibilityState={{ disabled: !enabled }}
|
|
313
|
+
disabled={!enabled}
|
|
314
|
+
hitSlop={8}
|
|
315
|
+
onPress={() => setMonth(kind === 'previous' ? previous : next)}
|
|
316
|
+
style={({ pressed }) => [styles.arrow, pressed && styles.arrowPressed]}
|
|
317
|
+
>
|
|
318
|
+
<Chevron
|
|
319
|
+
direction={pointsLeft ? 'left' : 'right'}
|
|
320
|
+
color={enabled ? theme.text : theme.disabled}
|
|
321
|
+
/>
|
|
322
|
+
</Pressable>
|
|
323
|
+
);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
<View style={[styles.container, style]}>
|
|
328
|
+
{showCalendarToggle && (
|
|
329
|
+
<View style={[styles.toggle, { flexDirection: row }]}>
|
|
330
|
+
{(['hijri', 'gregorian'] as const).map((option) => {
|
|
331
|
+
const active = option === calendar;
|
|
332
|
+
return (
|
|
333
|
+
<Pressable
|
|
334
|
+
key={option}
|
|
335
|
+
accessibilityRole="button"
|
|
336
|
+
accessibilityState={{ selected: active }}
|
|
337
|
+
onPress={() => toggleCalendar(option)}
|
|
338
|
+
style={[
|
|
339
|
+
styles.toggleOption,
|
|
340
|
+
active && styles.toggleOptionActive,
|
|
341
|
+
]}
|
|
342
|
+
>
|
|
343
|
+
<Text
|
|
344
|
+
style={[styles.toggleText, active && styles.toggleTextActive]}
|
|
345
|
+
>
|
|
346
|
+
{labels[option]}
|
|
347
|
+
</Text>
|
|
348
|
+
</Pressable>
|
|
349
|
+
);
|
|
350
|
+
})}
|
|
351
|
+
</View>
|
|
352
|
+
)}
|
|
353
|
+
|
|
354
|
+
<View
|
|
355
|
+
testID="hilal-header"
|
|
356
|
+
style={[styles.header, { flexDirection: row }]}
|
|
357
|
+
>
|
|
358
|
+
{renderArrow('previous')}
|
|
359
|
+
<View style={styles.titles}>
|
|
360
|
+
<Text accessibilityRole="header" style={styles.title}>
|
|
361
|
+
{monthTitle(shown, locale, digits)}
|
|
362
|
+
</Text>
|
|
363
|
+
<Text style={styles.subtitle}>
|
|
364
|
+
{secondaryTitle(shown, locale, digits)}
|
|
365
|
+
</Text>
|
|
366
|
+
</View>
|
|
367
|
+
{renderArrow('next')}
|
|
368
|
+
</View>
|
|
369
|
+
|
|
370
|
+
<View style={[styles.weekdays, { flexDirection: row }]}>
|
|
371
|
+
{weekdays.map((name) => (
|
|
372
|
+
<Text key={name} style={styles.weekday}>
|
|
373
|
+
{name}
|
|
374
|
+
</Text>
|
|
375
|
+
))}
|
|
376
|
+
</View>
|
|
377
|
+
|
|
378
|
+
{cells.map((week, w) => (
|
|
379
|
+
<View key={w} style={[styles.week, { flexDirection: row }]}>
|
|
380
|
+
{week.map((cell, d) =>
|
|
381
|
+
cell === null ? (
|
|
382
|
+
<View key={`empty-${d}`} style={styles.slot} />
|
|
383
|
+
) : (
|
|
384
|
+
<DayCell
|
|
385
|
+
key={cell.epochDay}
|
|
386
|
+
{...cell}
|
|
387
|
+
selected={cell.epochDay === selectedDay}
|
|
388
|
+
today={cell.epochDay === today}
|
|
389
|
+
styles={styles}
|
|
390
|
+
dayStyle={stableDayStyle}
|
|
391
|
+
onSelect={onSelect}
|
|
392
|
+
/>
|
|
393
|
+
)
|
|
394
|
+
)}
|
|
395
|
+
</View>
|
|
396
|
+
))}
|
|
397
|
+
</View>
|
|
398
|
+
);
|
|
399
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The calendar's model: which days a month shows, how they are laid out
|
|
3
|
+
* in weeks, how the header reads, and how far navigation may go. Pure
|
|
4
|
+
* functions on epoch days, no React.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
epochDayOfHijri,
|
|
8
|
+
FIRST_DAY,
|
|
9
|
+
getHijriMonthLength,
|
|
10
|
+
hijriOfEpochDay,
|
|
11
|
+
LAST_DAY,
|
|
12
|
+
} from '../core/convert';
|
|
13
|
+
import { civilFromDays, daysFromCivil, weekdayOfEpochDay } from '../core/days';
|
|
14
|
+
import {
|
|
15
|
+
formatNumber,
|
|
16
|
+
getMonthNames,
|
|
17
|
+
type CalendarType,
|
|
18
|
+
type Digits,
|
|
19
|
+
type Locale,
|
|
20
|
+
} from '../core/locale';
|
|
21
|
+
|
|
22
|
+
/** A month of either calendar. `month` is 1-12. */
|
|
23
|
+
export type CalendarMonth = {
|
|
24
|
+
calendar: CalendarType;
|
|
25
|
+
year: number;
|
|
26
|
+
month: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type DayCell = {
|
|
30
|
+
epochDay: number;
|
|
31
|
+
/** Day of the month in the primary calendar. */
|
|
32
|
+
day: number;
|
|
33
|
+
/**
|
|
34
|
+
* Day of the month in the other calendar, or null when the day is
|
|
35
|
+
* outside the supported Hijri range (possible in the first and last
|
|
36
|
+
* Gregorian months).
|
|
37
|
+
*/
|
|
38
|
+
secondaryDay: number | null;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** A month as weeks of 7 slots; null slots pad the first and last week. */
|
|
42
|
+
export type MonthGrid = Array<Array<DayCell | null>>;
|
|
43
|
+
|
|
44
|
+
/** Inclusive range of epoch days. */
|
|
45
|
+
export type DayRange = { min: number; max: number };
|
|
46
|
+
|
|
47
|
+
/** The days the calendar can ever show: the supported Hijri range. */
|
|
48
|
+
export const SUPPORTED_DAYS: DayRange = { min: FIRST_DAY, max: LAST_DAY };
|
|
49
|
+
|
|
50
|
+
export function otherCalendar(calendar: CalendarType): CalendarType {
|
|
51
|
+
return calendar === 'hijri' ? 'gregorian' : 'hijri';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Epoch day of the first day of a month. */
|
|
55
|
+
export function monthStart({ calendar, year, month }: CalendarMonth) {
|
|
56
|
+
return calendar === 'hijri'
|
|
57
|
+
? epochDayOfHijri({ year, month, day: 1 }, 'monthStart')
|
|
58
|
+
: daysFromCivil(year, month, 1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Number of days in a month. */
|
|
62
|
+
export function monthLength(m: CalendarMonth) {
|
|
63
|
+
if (m.calendar === 'hijri') {
|
|
64
|
+
return getHijriMonthLength(m.year, m.month);
|
|
65
|
+
}
|
|
66
|
+
const next = addMonths(m, 1);
|
|
67
|
+
return daysFromCivil(next.year, next.month, 1) - monthStart(m);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Year, month and day of an epoch day in either calendar. */
|
|
71
|
+
export function dateInCalendar(epochDay: number, calendar: CalendarType) {
|
|
72
|
+
return calendar === 'hijri'
|
|
73
|
+
? hijriOfEpochDay(epochDay)
|
|
74
|
+
: civilFromDays(epochDay);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The month of `calendar` that contains `epochDay`. Days outside the
|
|
79
|
+
* supported range are clamped to it first.
|
|
80
|
+
*/
|
|
81
|
+
export function monthContaining(
|
|
82
|
+
epochDay: number,
|
|
83
|
+
calendar: CalendarType
|
|
84
|
+
): CalendarMonth {
|
|
85
|
+
const { year, month } = dateInCalendar(
|
|
86
|
+
clampDay(epochDay, SUPPORTED_DAYS),
|
|
87
|
+
calendar
|
|
88
|
+
);
|
|
89
|
+
return { calendar, year, month };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function addMonths(m: CalendarMonth, delta: number): CalendarMonth {
|
|
93
|
+
const index = m.year * 12 + (m.month - 1) + delta;
|
|
94
|
+
return {
|
|
95
|
+
calendar: m.calendar,
|
|
96
|
+
year: Math.floor(index / 12),
|
|
97
|
+
month: (index % 12) + 1,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function clampDay(epochDay: number, range: DayRange) {
|
|
102
|
+
return Math.min(Math.max(epochDay, range.min), range.max);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The days that may be selected: `minDay`/`maxDay` (when given)
|
|
107
|
+
* intersected with the supported range.
|
|
108
|
+
*/
|
|
109
|
+
export function selectableRange(
|
|
110
|
+
minDay: number | undefined,
|
|
111
|
+
maxDay: number | undefined
|
|
112
|
+
): DayRange {
|
|
113
|
+
return {
|
|
114
|
+
min: Math.max(minDay ?? SUPPORTED_DAYS.min, SUPPORTED_DAYS.min),
|
|
115
|
+
max: Math.min(maxDay ?? SUPPORTED_DAYS.max, SUPPORTED_DAYS.max),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Whether any day of month `m` falls inside `range`. */
|
|
120
|
+
export function monthOverlaps(m: CalendarMonth, range: DayRange) {
|
|
121
|
+
if (m.calendar === 'hijri') {
|
|
122
|
+
// Hijri months outside the table have no start day at all.
|
|
123
|
+
const { min, max } = monthRangeOfSupportedHijri();
|
|
124
|
+
const index = m.year * 12 + m.month;
|
|
125
|
+
if (index < min || index > max) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const start = monthStart(m);
|
|
130
|
+
const end = start + monthLength(m) - 1;
|
|
131
|
+
return start <= range.max && end >= range.min;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function monthRangeOfSupportedHijri() {
|
|
135
|
+
const first = hijriOfEpochDay(FIRST_DAY);
|
|
136
|
+
const last = hijriOfEpochDay(LAST_DAY);
|
|
137
|
+
return {
|
|
138
|
+
min: first.year * 12 + first.month,
|
|
139
|
+
max: last.year * 12 + last.month,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Lays out month `m` in weeks starting on `firstDayOfWeek` (0 = Sunday,
|
|
145
|
+
* 1 = Monday, 6 = Saturday).
|
|
146
|
+
*/
|
|
147
|
+
export function buildMonthGrid(
|
|
148
|
+
m: CalendarMonth,
|
|
149
|
+
firstDayOfWeek: number
|
|
150
|
+
): MonthGrid {
|
|
151
|
+
const start = monthStart(m);
|
|
152
|
+
const length = monthLength(m);
|
|
153
|
+
const secondary = otherCalendar(m.calendar);
|
|
154
|
+
const leading = (weekdayOfEpochDay(start) - firstDayOfWeek + 7) % 7;
|
|
155
|
+
|
|
156
|
+
const slots: Array<DayCell | null> = Array(leading).fill(null);
|
|
157
|
+
for (let day = 1; day <= length; day++) {
|
|
158
|
+
const epochDay = start + day - 1;
|
|
159
|
+
const inRange =
|
|
160
|
+
epochDay >= SUPPORTED_DAYS.min && epochDay <= SUPPORTED_DAYS.max;
|
|
161
|
+
slots.push({
|
|
162
|
+
epochDay,
|
|
163
|
+
day,
|
|
164
|
+
secondaryDay: inRange ? dateInCalendar(epochDay, secondary).day : null,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
while (slots.length % 7 !== 0) {
|
|
168
|
+
slots.push(null);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const weeks: MonthGrid = [];
|
|
172
|
+
for (let i = 0; i < slots.length; i += 7) {
|
|
173
|
+
weeks.push(slots.slice(i, i + 7));
|
|
174
|
+
}
|
|
175
|
+
return weeks;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** "رمضان ١٤٤٦" / "Ramadan 1446" / "March 2025". */
|
|
179
|
+
export function monthTitle(m: CalendarMonth, locale: Locale, digits: Digits) {
|
|
180
|
+
const name = getMonthNames(m.calendar, locale)[m.month - 1];
|
|
181
|
+
return `${name} ${formatNumber(m.year, digits)}`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The months of the other calendar that month `m` overlaps, for example
|
|
186
|
+
* "مارس – أبريل ٢٠٢٤" for Ramadan 1445, "ديسمبر ٢٠٢٤ – يناير ٢٠٢٥" across
|
|
187
|
+
* a year boundary, or a single month. Only days in the supported range
|
|
188
|
+
* count.
|
|
189
|
+
*/
|
|
190
|
+
export function secondaryTitle(
|
|
191
|
+
m: CalendarMonth,
|
|
192
|
+
locale: Locale,
|
|
193
|
+
digits: Digits
|
|
194
|
+
) {
|
|
195
|
+
const start = monthStart(m);
|
|
196
|
+
const first = clampDay(start, SUPPORTED_DAYS);
|
|
197
|
+
const last = clampDay(start + monthLength(m) - 1, SUPPORTED_DAYS);
|
|
198
|
+
const calendar = otherCalendar(m.calendar);
|
|
199
|
+
const from = dateInCalendar(first, calendar);
|
|
200
|
+
const to = dateInCalendar(last, calendar);
|
|
201
|
+
const names = getMonthNames(calendar, locale);
|
|
202
|
+
const fromName = names[from.month - 1];
|
|
203
|
+
const toName = names[to.month - 1];
|
|
204
|
+
const year = (y: number) => formatNumber(y, digits);
|
|
205
|
+
|
|
206
|
+
if (from.year === to.year && from.month === to.month) {
|
|
207
|
+
return `${fromName} ${year(from.year)}`;
|
|
208
|
+
}
|
|
209
|
+
if (from.year === to.year) {
|
|
210
|
+
return `${fromName} – ${toName} ${year(to.year)}`;
|
|
211
|
+
}
|
|
212
|
+
return `${fromName} ${year(from.year)} – ${toName} ${year(to.year)}`;
|
|
213
|
+
}
|