react-native-hilal 0.0.0-stage → 0.2.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 +300 -2
- package/lib/module/calendar/Appear.js +75 -0
- package/lib/module/calendar/Appear.js.map +1 -0
- package/lib/module/calendar/Chevron.js +60 -0
- package/lib/module/calendar/Chevron.js.map +1 -0
- package/lib/module/calendar/DayCell.js +51 -0
- package/lib/module/calendar/DayCell.js.map +1 -0
- package/lib/module/calendar/HilalCalendar.js +565 -0
- package/lib/module/calendar/HilalCalendar.js.map +1 -0
- package/lib/module/calendar/PickerGrid.js +66 -0
- package/lib/module/calendar/PickerGrid.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 +291 -0
- package/lib/module/calendar/month.js.map +1 -0
- package/lib/module/calendar/styles.js +338 -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 +191 -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 +62 -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/Appear.d.ts +22 -0
- package/lib/typescript/src/calendar/Appear.d.ts.map +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 +24 -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/PickerGrid.d.ts +28 -0
- package/lib/typescript/src/calendar/PickerGrid.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 +86 -0
- package/lib/typescript/src/calendar/month.d.ts.map +1 -0
- package/lib/typescript/src/calendar/styles.d.ts +206 -0
- package/lib/typescript/src/calendar/styles.d.ts.map +1 -0
- package/lib/typescript/src/calendar/theme.d.ts +51 -0
- package/lib/typescript/src/calendar/theme.d.ts.map +1 -0
- package/lib/typescript/src/calendar/types.d.ts +162 -0
- package/lib/typescript/src/calendar/types.d.ts.map +1 -0
- package/lib/typescript/src/core/convert.d.ts +77 -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/Appear.tsx +94 -0
- package/src/calendar/Chevron.tsx +55 -0
- package/src/calendar/DayCell.tsx +89 -0
- package/src/calendar/HilalCalendar.tsx +728 -0
- package/src/calendar/PickerGrid.tsx +99 -0
- package/src/calendar/index.ts +13 -0
- package/src/calendar/month.ts +364 -0
- package/src/calendar/styles.ts +357 -0
- package/src/calendar/theme.ts +89 -0
- package/src/calendar/types.ts +168 -0
- package/src/core/convert.ts +256 -0
- package/src/core/days.ts +64 -0
- package/src/core/format.ts +65 -0
- package/src/core/index.ts +19 -0
- package/src/core/locale.ts +141 -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,728 @@
|
|
|
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 { checkAdjustment, 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
|
+
getMonthNames,
|
|
22
|
+
getWeekdayNames,
|
|
23
|
+
type CalendarType,
|
|
24
|
+
type Digits,
|
|
25
|
+
type Locale,
|
|
26
|
+
} from '../core/locale';
|
|
27
|
+
import { Appear, useReduceMotion } from './Appear';
|
|
28
|
+
import { Chevron } from './Chevron';
|
|
29
|
+
import { DayCell } from './DayCell';
|
|
30
|
+
import {
|
|
31
|
+
addMonths,
|
|
32
|
+
buildMonthGrid,
|
|
33
|
+
clampDay,
|
|
34
|
+
dateInCalendar,
|
|
35
|
+
monthContaining,
|
|
36
|
+
monthOverlaps,
|
|
37
|
+
monthsOfYear,
|
|
38
|
+
monthStart,
|
|
39
|
+
monthTitle,
|
|
40
|
+
pageOverlaps,
|
|
41
|
+
secondaryTitle,
|
|
42
|
+
selectableRange,
|
|
43
|
+
yearOverlaps,
|
|
44
|
+
yearPageStart,
|
|
45
|
+
yearsOnPage,
|
|
46
|
+
YEARS_PER_PAGE,
|
|
47
|
+
yearsSecondaryTitle,
|
|
48
|
+
type CalendarMonth,
|
|
49
|
+
type DayRange,
|
|
50
|
+
} from './month';
|
|
51
|
+
import { PickerGrid, type PickerGridItem } from './PickerGrid';
|
|
52
|
+
import {
|
|
53
|
+
createStyles,
|
|
54
|
+
MAX_CELL_FONT_SCALE,
|
|
55
|
+
MAX_HEADER_FONT_SCALE,
|
|
56
|
+
withSlots,
|
|
57
|
+
} from './styles';
|
|
58
|
+
import { resolveTheme } from './theme';
|
|
59
|
+
import type { HilalCalendarProps, HilalLabels, SelectedDate } from './types';
|
|
60
|
+
|
|
61
|
+
const LABELS: Record<Locale, Required<HilalLabels> & { separator: string }> = {
|
|
62
|
+
ar: {
|
|
63
|
+
hijri: 'هجري',
|
|
64
|
+
gregorian: 'ميلادي',
|
|
65
|
+
today: 'اليوم',
|
|
66
|
+
thisMonth: 'هذا الشهر',
|
|
67
|
+
thisYear: 'هذه السنة',
|
|
68
|
+
previousMonth: 'الشهر السابق',
|
|
69
|
+
nextMonth: 'الشهر التالي',
|
|
70
|
+
previousYear: 'السنة السابقة',
|
|
71
|
+
nextYear: 'السنة التالية',
|
|
72
|
+
previousYears: 'السنوات السابقة',
|
|
73
|
+
nextYears: 'السنوات التالية',
|
|
74
|
+
chooseMonth: 'اختر الشهر والسنة',
|
|
75
|
+
chooseYear: 'اختر السنة',
|
|
76
|
+
separator: '، ',
|
|
77
|
+
},
|
|
78
|
+
en: {
|
|
79
|
+
hijri: 'Hijri',
|
|
80
|
+
gregorian: 'Gregorian',
|
|
81
|
+
today: 'Today',
|
|
82
|
+
thisMonth: 'This month',
|
|
83
|
+
thisYear: 'This year',
|
|
84
|
+
previousMonth: 'Previous month',
|
|
85
|
+
nextMonth: 'Next month',
|
|
86
|
+
previousYear: 'Previous year',
|
|
87
|
+
nextYear: 'Next year',
|
|
88
|
+
previousYears: 'Previous years',
|
|
89
|
+
nextYears: 'Next years',
|
|
90
|
+
chooseMonth: 'Choose month and year',
|
|
91
|
+
chooseYear: 'Choose year',
|
|
92
|
+
separator: ', ',
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// More below than above, so it never meets the toggle's.
|
|
97
|
+
const TITLE_HIT_SLOP = { top: 8, bottom: 10, left: 6, right: 6 };
|
|
98
|
+
const TOGGLE_HIT_SLOP = { top: 8, bottom: 8 };
|
|
99
|
+
|
|
100
|
+
/** What the grid shows: the days of a month, or the month or year picker. */
|
|
101
|
+
type View_ = 'days' | 'months' | 'years';
|
|
102
|
+
|
|
103
|
+
function epochDayOf(date: Date | undefined) {
|
|
104
|
+
return date instanceof Date && !Number.isNaN(date.getTime())
|
|
105
|
+
? epochDayOfLocalDate(date)
|
|
106
|
+
: undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function selectedDateOf(
|
|
110
|
+
epochDay: number,
|
|
111
|
+
locale: Locale,
|
|
112
|
+
digits: Digits,
|
|
113
|
+
adjustment: number
|
|
114
|
+
): SelectedDate {
|
|
115
|
+
const hijri = hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment);
|
|
116
|
+
return {
|
|
117
|
+
gregorian: localDateOfEpochDay(epochDay),
|
|
118
|
+
hijri,
|
|
119
|
+
formatted: {
|
|
120
|
+
gregorian: formatGregorian(civilFromDays(epochDay), { locale, digits }),
|
|
121
|
+
hijri: formatHijri(hijri, { locale, digits }),
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** A function with a stable identity that always calls the latest `fn`. */
|
|
127
|
+
function useStableCallback<Args extends unknown[]>(
|
|
128
|
+
fn: (...args: Args) => void
|
|
129
|
+
) {
|
|
130
|
+
const ref = useRef(fn);
|
|
131
|
+
useLayoutEffect(() => {
|
|
132
|
+
ref.current = fn;
|
|
133
|
+
});
|
|
134
|
+
return useCallback((...args: Args) => ref.current(...args), []);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The month to show: `preferred` if it can be shown, else a sensible one. */
|
|
138
|
+
function visibleMonth(
|
|
139
|
+
preferred: CalendarMonth,
|
|
140
|
+
calendar: CalendarType,
|
|
141
|
+
range: DayRange,
|
|
142
|
+
fallbackDay: number,
|
|
143
|
+
adjustment: number
|
|
144
|
+
) {
|
|
145
|
+
return preferred.calendar === calendar &&
|
|
146
|
+
monthOverlaps(preferred, range, adjustment)
|
|
147
|
+
? preferred
|
|
148
|
+
: monthContaining(clampDay(fallbackDay, range), calendar, adjustment);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* A month-grid date picker for the Umm al-Qura Hijri and the Gregorian
|
|
153
|
+
* calendar.
|
|
154
|
+
*/
|
|
155
|
+
export function HilalCalendar({
|
|
156
|
+
value,
|
|
157
|
+
defaultValue,
|
|
158
|
+
onChange,
|
|
159
|
+
calendar: calendarProp = 'hijri',
|
|
160
|
+
onCalendarChange,
|
|
161
|
+
showCalendarToggle = true,
|
|
162
|
+
showSecondaryDate = true,
|
|
163
|
+
locale = 'ar',
|
|
164
|
+
digits: digitsProp,
|
|
165
|
+
direction = 'auto',
|
|
166
|
+
firstDayOfWeek = 0,
|
|
167
|
+
minDate,
|
|
168
|
+
maxDate,
|
|
169
|
+
isDateDisabled,
|
|
170
|
+
hijriAdjustment = 0,
|
|
171
|
+
showMonthYearPicker = true,
|
|
172
|
+
weekdayFormat = 'short',
|
|
173
|
+
theme: themeProp = 'auto',
|
|
174
|
+
styles: stylesProp,
|
|
175
|
+
labels: labelsProp,
|
|
176
|
+
style,
|
|
177
|
+
dayStyle,
|
|
178
|
+
maxFontSizeMultiplier,
|
|
179
|
+
}: HilalCalendarProps) {
|
|
180
|
+
const adjustment = checkAdjustment(hijriAdjustment, 'HilalCalendar');
|
|
181
|
+
const digits = digitsProp ?? defaultDigits(locale);
|
|
182
|
+
// Compared by value, so an inline object doesn't recompute every day.
|
|
183
|
+
const labelsKey = JSON.stringify(labelsProp);
|
|
184
|
+
const labels = useMemo(
|
|
185
|
+
() => {
|
|
186
|
+
const merged = { ...LABELS[locale] };
|
|
187
|
+
for (const [key, text] of Object.entries(labelsProp ?? {})) {
|
|
188
|
+
// A label left undefined keeps the default.
|
|
189
|
+
if (text !== undefined) {
|
|
190
|
+
merged[key as keyof HilalLabels] = text;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return merged;
|
|
194
|
+
},
|
|
195
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
196
|
+
[locale, labelsKey]
|
|
197
|
+
);
|
|
198
|
+
const cellFontScale = maxFontSizeMultiplier ?? MAX_CELL_FONT_SCALE;
|
|
199
|
+
const headerFontScale = maxFontSizeMultiplier ?? MAX_HEADER_FONT_SCALE;
|
|
200
|
+
// Today, this month and this year are only marked by a ring; screen
|
|
201
|
+
// readers hear it at the end of the label instead.
|
|
202
|
+
const withCurrent = (label: string, current: boolean, text: string) =>
|
|
203
|
+
current ? `${label}${labels.separator}${text}` : label;
|
|
204
|
+
// React Native Web leaves I18nManager.isRTL undefined, not false.
|
|
205
|
+
const appRTL = I18nManager.isRTL === true;
|
|
206
|
+
const rtl =
|
|
207
|
+
direction === 'rtl' ||
|
|
208
|
+
(direction === 'auto' && (appRTL || locale === 'ar'));
|
|
209
|
+
// React Native already mirrors 'row' when the whole app is RTL.
|
|
210
|
+
const row = rtl === appRTL ? 'row' : 'row-reverse';
|
|
211
|
+
|
|
212
|
+
const colorScheme = useColorScheme();
|
|
213
|
+
const themeKey =
|
|
214
|
+
typeof themeProp === 'string' ? themeProp : JSON.stringify(themeProp);
|
|
215
|
+
const theme = useMemo(
|
|
216
|
+
() => resolveTheme(themeProp, colorScheme),
|
|
217
|
+
// A partial theme is usually an inline object; compare it by value.
|
|
218
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
219
|
+
[themeKey, colorScheme]
|
|
220
|
+
);
|
|
221
|
+
// An inline `styles` prop is compared by value too.
|
|
222
|
+
const stylesKey = JSON.stringify(stylesProp);
|
|
223
|
+
const { styles, dayStyles } = useMemo(
|
|
224
|
+
() => withSlots(createStyles(theme), stylesProp),
|
|
225
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
226
|
+
[theme, stylesKey]
|
|
227
|
+
);
|
|
228
|
+
// Same for an inline dayStyle: a new object every render would re-render
|
|
229
|
+
// every day cell.
|
|
230
|
+
const dayStyleKey = JSON.stringify(dayStyle);
|
|
231
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
232
|
+
const stableDayStyle = useMemo(() => dayStyle, [dayStyleKey]);
|
|
233
|
+
|
|
234
|
+
const minDay = epochDayOf(minDate);
|
|
235
|
+
const maxDay = epochDayOf(maxDate);
|
|
236
|
+
const range = useMemo(
|
|
237
|
+
() => selectableRange(minDay, maxDay, adjustment),
|
|
238
|
+
[minDay, maxDay, adjustment]
|
|
239
|
+
);
|
|
240
|
+
const today = epochDayOfLocalDate(new Date());
|
|
241
|
+
|
|
242
|
+
// Selection: controlled through `value`, otherwise internal state.
|
|
243
|
+
const [internalDay, setInternalDay] = useState(() =>
|
|
244
|
+
epochDayOf(defaultValue)
|
|
245
|
+
);
|
|
246
|
+
const selectedDay = value !== undefined ? epochDayOf(value) : internalDay;
|
|
247
|
+
|
|
248
|
+
// Calendar: internal state that follows the prop when the prop changes.
|
|
249
|
+
const [calendar, setCalendar] = useState(calendarProp);
|
|
250
|
+
const [lastCalendarProp, setLastCalendarProp] = useState(calendarProp);
|
|
251
|
+
const [month, setMonth] = useState(() =>
|
|
252
|
+
monthContaining(
|
|
253
|
+
clampDay(selectedDay ?? today, range),
|
|
254
|
+
calendarProp,
|
|
255
|
+
adjustment
|
|
256
|
+
)
|
|
257
|
+
);
|
|
258
|
+
const [lastSelectedDay, setLastSelectedDay] = useState(selectedDay);
|
|
259
|
+
|
|
260
|
+
// The month and year picker: which grid is shown, the year whose months
|
|
261
|
+
// are listed, and the first year of the page of years.
|
|
262
|
+
const [view, setView] = useState<View_>('days');
|
|
263
|
+
const [pickerYear, setPickerYear] = useState(0);
|
|
264
|
+
const [pageStart, setPageStart] = useState(0);
|
|
265
|
+
const mode: View_ = showMonthYearPicker ? view : 'days';
|
|
266
|
+
// The grids fade in when switching views, but not when the calendar
|
|
267
|
+
// first appears: the days only animate once the picker has been open.
|
|
268
|
+
const reduceMotion = useReduceMotion();
|
|
269
|
+
const [pickerOpened, setPickerOpened] = useState(false);
|
|
270
|
+
if (mode !== 'days' && !pickerOpened) {
|
|
271
|
+
setPickerOpened(true);
|
|
272
|
+
}
|
|
273
|
+
// The height of the days, which the picker takes too so that the
|
|
274
|
+
// calendar doesn't jump in size when switching.
|
|
275
|
+
const [daysHeight, setDaysHeight] = useState<number>();
|
|
276
|
+
|
|
277
|
+
const switchTo = (next: CalendarType, current: CalendarMonth) => {
|
|
278
|
+
setCalendar(next);
|
|
279
|
+
setView('days');
|
|
280
|
+
// Keep the selected day in view, or else the month being looked at.
|
|
281
|
+
const anchor =
|
|
282
|
+
selectedDay ?? clampDay(monthStart(current, adjustment), range);
|
|
283
|
+
setMonth(monthContaining(clampDay(anchor, range), next, adjustment));
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const shown = visibleMonth(
|
|
287
|
+
month,
|
|
288
|
+
calendar,
|
|
289
|
+
range,
|
|
290
|
+
selectedDay ?? today,
|
|
291
|
+
adjustment
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
// Adjust state while rendering when props change (React's recommended
|
|
295
|
+
// alternative to effects for derived state).
|
|
296
|
+
if (calendarProp !== lastCalendarProp) {
|
|
297
|
+
setLastCalendarProp(calendarProp);
|
|
298
|
+
switchTo(calendarProp, shown);
|
|
299
|
+
}
|
|
300
|
+
if (selectedDay !== lastSelectedDay) {
|
|
301
|
+
setLastSelectedDay(selectedDay);
|
|
302
|
+
if (
|
|
303
|
+
selectedDay !== undefined &&
|
|
304
|
+
selectedDay >= range.min &&
|
|
305
|
+
selectedDay <= range.max
|
|
306
|
+
) {
|
|
307
|
+
const containing = monthContaining(selectedDay, calendar, adjustment);
|
|
308
|
+
if (containing.year !== shown.year || containing.month !== shown.month) {
|
|
309
|
+
setMonth(containing);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const grid = useMemo(
|
|
315
|
+
() => buildMonthGrid(shown, firstDayOfWeek, adjustment),
|
|
316
|
+
// `shown` is a new object on every render; its fields identify it.
|
|
317
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
318
|
+
[shown.calendar, shown.year, shown.month, firstDayOfWeek, adjustment]
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
// Everything about a day except `selected` and `today`, so that changing
|
|
322
|
+
// the selection doesn't recompute labels or call isDateDisabled.
|
|
323
|
+
const cells = useMemo(
|
|
324
|
+
() =>
|
|
325
|
+
grid.map((week) =>
|
|
326
|
+
week.map((cell) => {
|
|
327
|
+
if (cell === null) {
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
const { epochDay, day, secondaryDay } = cell;
|
|
331
|
+
const inSupportedRange = secondaryDay !== null;
|
|
332
|
+
const disabled =
|
|
333
|
+
!inSupportedRange ||
|
|
334
|
+
epochDay < range.min ||
|
|
335
|
+
epochDay > range.max ||
|
|
336
|
+
(isDateDisabled?.(
|
|
337
|
+
selectedDateOf(epochDay, locale, digits, adjustment)
|
|
338
|
+
) ??
|
|
339
|
+
false);
|
|
340
|
+
const weekday = getWeekdayNames(locale)[weekdayOfEpochDay(epochDay)];
|
|
341
|
+
const gregorianText = formatGregorian(civilFromDays(epochDay), {
|
|
342
|
+
locale,
|
|
343
|
+
digits,
|
|
344
|
+
});
|
|
345
|
+
const spoken = inSupportedRange
|
|
346
|
+
? [
|
|
347
|
+
formatHijri(
|
|
348
|
+
hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment),
|
|
349
|
+
{ locale, digits }
|
|
350
|
+
),
|
|
351
|
+
gregorianText,
|
|
352
|
+
]
|
|
353
|
+
: [gregorianText];
|
|
354
|
+
if (shown.calendar === 'gregorian') {
|
|
355
|
+
spoken.reverse();
|
|
356
|
+
}
|
|
357
|
+
return {
|
|
358
|
+
epochDay,
|
|
359
|
+
label: formatNumber(day, digits),
|
|
360
|
+
secondaryLabel:
|
|
361
|
+
showSecondaryDate && secondaryDay !== null
|
|
362
|
+
? formatNumber(secondaryDay, digits)
|
|
363
|
+
: null,
|
|
364
|
+
accessibilityLabel: [weekday, ...spoken].join(labels.separator),
|
|
365
|
+
disabled,
|
|
366
|
+
};
|
|
367
|
+
})
|
|
368
|
+
),
|
|
369
|
+
[
|
|
370
|
+
grid,
|
|
371
|
+
shown.calendar,
|
|
372
|
+
range,
|
|
373
|
+
isDateDisabled,
|
|
374
|
+
locale,
|
|
375
|
+
digits,
|
|
376
|
+
showSecondaryDate,
|
|
377
|
+
labels,
|
|
378
|
+
adjustment,
|
|
379
|
+
]
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
const onSelect = useStableCallback((epochDay: number) => {
|
|
383
|
+
if (value === undefined) {
|
|
384
|
+
setInternalDay(epochDay);
|
|
385
|
+
}
|
|
386
|
+
onChange?.(selectedDateOf(epochDay, locale, digits, adjustment));
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
const toggleCalendar = (next: CalendarType) => {
|
|
390
|
+
if (next === calendar) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
switchTo(next, shown);
|
|
394
|
+
onCalendarChange?.(next);
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
// Today's month and year in the shown calendar, for the picker's ring.
|
|
398
|
+
const todayInCalendar = safeDateInCalendar(today, calendar, adjustment);
|
|
399
|
+
|
|
400
|
+
// What the arrows do depends on the grid: months, years, or pages.
|
|
401
|
+
let arrows: Record<
|
|
402
|
+
'previous' | 'next',
|
|
403
|
+
{ enabled: boolean; label: string; onPress: () => void }
|
|
404
|
+
>;
|
|
405
|
+
if (mode === 'months') {
|
|
406
|
+
arrows = {
|
|
407
|
+
previous: {
|
|
408
|
+
enabled: yearOverlaps(calendar, pickerYear - 1, range, adjustment),
|
|
409
|
+
label: labels.previousYear,
|
|
410
|
+
onPress: () => setPickerYear(pickerYear - 1),
|
|
411
|
+
},
|
|
412
|
+
next: {
|
|
413
|
+
enabled: yearOverlaps(calendar, pickerYear + 1, range, adjustment),
|
|
414
|
+
label: labels.nextYear,
|
|
415
|
+
onPress: () => setPickerYear(pickerYear + 1),
|
|
416
|
+
},
|
|
417
|
+
};
|
|
418
|
+
} else if (mode === 'years') {
|
|
419
|
+
const before = pageStart - YEARS_PER_PAGE;
|
|
420
|
+
const after = pageStart + YEARS_PER_PAGE;
|
|
421
|
+
arrows = {
|
|
422
|
+
previous: {
|
|
423
|
+
enabled: pageOverlaps(calendar, before, range, adjustment),
|
|
424
|
+
label: labels.previousYears,
|
|
425
|
+
onPress: () => setPageStart(before),
|
|
426
|
+
},
|
|
427
|
+
next: {
|
|
428
|
+
enabled: pageOverlaps(calendar, after, range, adjustment),
|
|
429
|
+
label: labels.nextYears,
|
|
430
|
+
onPress: () => setPageStart(after),
|
|
431
|
+
},
|
|
432
|
+
};
|
|
433
|
+
} else {
|
|
434
|
+
const previous = addMonths(shown, -1);
|
|
435
|
+
const next = addMonths(shown, 1);
|
|
436
|
+
arrows = {
|
|
437
|
+
previous: {
|
|
438
|
+
enabled: monthOverlaps(previous, range, adjustment),
|
|
439
|
+
label: labels.previousMonth,
|
|
440
|
+
onPress: () => setMonth(previous),
|
|
441
|
+
},
|
|
442
|
+
next: {
|
|
443
|
+
enabled: monthOverlaps(next, range, adjustment),
|
|
444
|
+
label: labels.nextMonth,
|
|
445
|
+
onPress: () => setMonth(next),
|
|
446
|
+
},
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const weekdayNames = getWeekdayNames(locale, weekdayFormat);
|
|
451
|
+
const weekdays = Array.from(
|
|
452
|
+
{ length: 7 },
|
|
453
|
+
(_, i) => weekdayNames[(firstDayOfWeek + i) % 7]!
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
const renderArrow = (kind: 'previous' | 'next') => {
|
|
457
|
+
const { enabled, label, onPress } = arrows[kind];
|
|
458
|
+
// Arrows point the way the months move: outward in reading order.
|
|
459
|
+
const pointsLeft = (kind === 'previous') !== rtl;
|
|
460
|
+
return (
|
|
461
|
+
<Pressable
|
|
462
|
+
accessibilityRole="button"
|
|
463
|
+
accessibilityLabel={label}
|
|
464
|
+
accessibilityState={{ disabled: !enabled }}
|
|
465
|
+
disabled={!enabled}
|
|
466
|
+
hitSlop={8}
|
|
467
|
+
onPress={onPress}
|
|
468
|
+
style={({ pressed }) => [styles.arrow, pressed && styles.arrowPressed]}
|
|
469
|
+
>
|
|
470
|
+
<Chevron
|
|
471
|
+
direction={pointsLeft ? 'left' : 'right'}
|
|
472
|
+
color={enabled ? (theme.arrow ?? theme.text) : theme.disabled}
|
|
473
|
+
/>
|
|
474
|
+
</Pressable>
|
|
475
|
+
);
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
const year = (y: number) => formatNumber(y, digits);
|
|
479
|
+
|
|
480
|
+
// The header title, and what pressing it opens.
|
|
481
|
+
let title: string;
|
|
482
|
+
let subtitle: string;
|
|
483
|
+
let titleAction: { label: string; onPress: () => void } | undefined;
|
|
484
|
+
if (mode === 'months') {
|
|
485
|
+
title = year(pickerYear);
|
|
486
|
+
subtitle = yearsSecondaryTitle(
|
|
487
|
+
calendar,
|
|
488
|
+
pickerYear,
|
|
489
|
+
pickerYear,
|
|
490
|
+
digits,
|
|
491
|
+
adjustment
|
|
492
|
+
);
|
|
493
|
+
titleAction = {
|
|
494
|
+
label: labels.chooseYear,
|
|
495
|
+
onPress: () => {
|
|
496
|
+
setPageStart(yearPageStart(pickerYear));
|
|
497
|
+
setView('years');
|
|
498
|
+
},
|
|
499
|
+
};
|
|
500
|
+
} else if (mode === 'years') {
|
|
501
|
+
const last = pageStart + YEARS_PER_PAGE - 1;
|
|
502
|
+
title = `${year(pageStart)} – ${year(last)}`;
|
|
503
|
+
subtitle = yearsSecondaryTitle(
|
|
504
|
+
calendar,
|
|
505
|
+
pageStart,
|
|
506
|
+
last,
|
|
507
|
+
digits,
|
|
508
|
+
adjustment
|
|
509
|
+
);
|
|
510
|
+
} else {
|
|
511
|
+
title = monthTitle(shown, locale, digits);
|
|
512
|
+
subtitle = secondaryTitle(shown, locale, digits, adjustment);
|
|
513
|
+
if (showMonthYearPicker) {
|
|
514
|
+
titleAction = {
|
|
515
|
+
label: labels.chooseMonth,
|
|
516
|
+
onPress: () => {
|
|
517
|
+
setPickerYear(shown.year);
|
|
518
|
+
setView('months');
|
|
519
|
+
},
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const titleText = (
|
|
525
|
+
<Text
|
|
526
|
+
accessibilityRole="header"
|
|
527
|
+
maxFontSizeMultiplier={headerFontScale}
|
|
528
|
+
style={titleAction ? [styles.title, styles.titleInButton] : styles.title}
|
|
529
|
+
>
|
|
530
|
+
{title}
|
|
531
|
+
</Text>
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
let pickerItems: PickerGridItem[] | undefined;
|
|
535
|
+
if (mode === 'months') {
|
|
536
|
+
const names = getMonthNames(calendar, locale);
|
|
537
|
+
pickerItems = monthsOfYear(calendar, pickerYear, range, adjustment).map(
|
|
538
|
+
({ value: m, disabled }) => {
|
|
539
|
+
const current =
|
|
540
|
+
todayInCalendar?.year === pickerYear && todayInCalendar.month === m;
|
|
541
|
+
return {
|
|
542
|
+
key: m,
|
|
543
|
+
label: names[m - 1]!,
|
|
544
|
+
accessibilityLabel: withCurrent(
|
|
545
|
+
`${names[m - 1]} ${year(pickerYear)}`,
|
|
546
|
+
current,
|
|
547
|
+
labels.thisMonth
|
|
548
|
+
),
|
|
549
|
+
selected: pickerYear === shown.year && m === shown.month,
|
|
550
|
+
current,
|
|
551
|
+
disabled,
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
);
|
|
555
|
+
} else if (mode === 'years') {
|
|
556
|
+
pickerItems = yearsOnPage(calendar, pageStart, range, adjustment).map(
|
|
557
|
+
({ value: y, disabled }) => {
|
|
558
|
+
const current = todayInCalendar?.year === y;
|
|
559
|
+
return {
|
|
560
|
+
key: y,
|
|
561
|
+
label: year(y),
|
|
562
|
+
accessibilityLabel: withCurrent(year(y), current, labels.thisYear),
|
|
563
|
+
selected: y === pickerYear,
|
|
564
|
+
current,
|
|
565
|
+
disabled,
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const pick = (key: number) => {
|
|
572
|
+
if (mode === 'months') {
|
|
573
|
+
setMonth({ calendar, year: pickerYear, month: key });
|
|
574
|
+
setView('days');
|
|
575
|
+
} else {
|
|
576
|
+
setPickerYear(key);
|
|
577
|
+
setView('months');
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
return (
|
|
582
|
+
<View style={[styles.container, style]}>
|
|
583
|
+
{showCalendarToggle && (
|
|
584
|
+
<View style={[styles.toggle, { flexDirection: row }]}>
|
|
585
|
+
{(['hijri', 'gregorian'] as const).map((option) => {
|
|
586
|
+
const active = option === calendar;
|
|
587
|
+
return (
|
|
588
|
+
<Pressable
|
|
589
|
+
key={option}
|
|
590
|
+
// 45 pt tall to the finger; the toggle itself stays slim.
|
|
591
|
+
hitSlop={TOGGLE_HIT_SLOP}
|
|
592
|
+
accessibilityRole="button"
|
|
593
|
+
accessibilityState={{ selected: active }}
|
|
594
|
+
onPress={() => toggleCalendar(option)}
|
|
595
|
+
style={[
|
|
596
|
+
styles.toggleOption,
|
|
597
|
+
active && styles.toggleOptionActive,
|
|
598
|
+
]}
|
|
599
|
+
>
|
|
600
|
+
<Text
|
|
601
|
+
maxFontSizeMultiplier={headerFontScale}
|
|
602
|
+
style={[styles.toggleText, active && styles.toggleTextActive]}
|
|
603
|
+
>
|
|
604
|
+
{labels[option]}
|
|
605
|
+
</Text>
|
|
606
|
+
</Pressable>
|
|
607
|
+
);
|
|
608
|
+
})}
|
|
609
|
+
</View>
|
|
610
|
+
)}
|
|
611
|
+
|
|
612
|
+
<View
|
|
613
|
+
testID="hilal-header"
|
|
614
|
+
style={[styles.header, { flexDirection: row }]}
|
|
615
|
+
>
|
|
616
|
+
{renderArrow('previous')}
|
|
617
|
+
<View style={styles.titles}>
|
|
618
|
+
{titleAction ? (
|
|
619
|
+
<Pressable
|
|
620
|
+
accessibilityRole="button"
|
|
621
|
+
accessibilityLabel={`${titleAction.label}${labels.separator}${title}`}
|
|
622
|
+
// 44 pt tall to the finger, like the arrows.
|
|
623
|
+
hitSlop={TITLE_HIT_SLOP}
|
|
624
|
+
onPress={titleAction.onPress}
|
|
625
|
+
style={({ pressed }) => [
|
|
626
|
+
styles.titleButton,
|
|
627
|
+
{ flexDirection: row },
|
|
628
|
+
pressed && styles.titleButtonPressed,
|
|
629
|
+
]}
|
|
630
|
+
>
|
|
631
|
+
<View style={styles.titleTint} />
|
|
632
|
+
{titleText}
|
|
633
|
+
</Pressable>
|
|
634
|
+
) : mode === 'years' ? (
|
|
635
|
+
// As tall as the title button of the other views, so the
|
|
636
|
+
// header doesn't jump when the years open.
|
|
637
|
+
<View style={styles.titleStatic}>{titleText}</View>
|
|
638
|
+
) : (
|
|
639
|
+
titleText
|
|
640
|
+
)}
|
|
641
|
+
<Text maxFontSizeMultiplier={headerFontScale} style={styles.subtitle}>
|
|
642
|
+
{subtitle}
|
|
643
|
+
</Text>
|
|
644
|
+
</View>
|
|
645
|
+
{renderArrow('next')}
|
|
646
|
+
</View>
|
|
647
|
+
|
|
648
|
+
{pickerItems ? (
|
|
649
|
+
<Appear
|
|
650
|
+
key={mode}
|
|
651
|
+
animate={!reduceMotion}
|
|
652
|
+
style={daysHeight !== undefined && { minHeight: daysHeight }}
|
|
653
|
+
>
|
|
654
|
+
<PickerGrid
|
|
655
|
+
items={pickerItems}
|
|
656
|
+
styles={styles}
|
|
657
|
+
row={row}
|
|
658
|
+
fill={daysHeight !== undefined}
|
|
659
|
+
maxFontScale={cellFontScale}
|
|
660
|
+
onSelect={pick}
|
|
661
|
+
/>
|
|
662
|
+
</Appear>
|
|
663
|
+
) : (
|
|
664
|
+
<Appear
|
|
665
|
+
key="days"
|
|
666
|
+
animate={pickerOpened && !reduceMotion}
|
|
667
|
+
onLayout={(event) => setDaysHeight(event.nativeEvent.layout.height)}
|
|
668
|
+
>
|
|
669
|
+
<View style={[styles.weekdays, { flexDirection: row }]}>
|
|
670
|
+
{/* Keyed by position: narrow names repeat (S, T). */}
|
|
671
|
+
{weekdays.map((name, i) => (
|
|
672
|
+
<Text
|
|
673
|
+
key={i}
|
|
674
|
+
// Shrinks back to fit its column when the phone's text is
|
|
675
|
+
// large.
|
|
676
|
+
numberOfLines={1}
|
|
677
|
+
adjustsFontSizeToFit
|
|
678
|
+
maxFontSizeMultiplier={cellFontScale}
|
|
679
|
+
style={styles.weekday}
|
|
680
|
+
>
|
|
681
|
+
{name}
|
|
682
|
+
</Text>
|
|
683
|
+
))}
|
|
684
|
+
</View>
|
|
685
|
+
|
|
686
|
+
{cells.map((week, w) => (
|
|
687
|
+
<View key={w} style={[styles.week, { flexDirection: row }]}>
|
|
688
|
+
{week.map((cell, d) =>
|
|
689
|
+
cell === null ? (
|
|
690
|
+
<View key={`empty-${d}`} style={styles.slot} />
|
|
691
|
+
) : (
|
|
692
|
+
<DayCell
|
|
693
|
+
key={cell.epochDay}
|
|
694
|
+
{...cell}
|
|
695
|
+
accessibilityLabel={withCurrent(
|
|
696
|
+
cell.accessibilityLabel,
|
|
697
|
+
cell.epochDay === today,
|
|
698
|
+
labels.today
|
|
699
|
+
)}
|
|
700
|
+
selected={cell.epochDay === selectedDay}
|
|
701
|
+
today={cell.epochDay === today}
|
|
702
|
+
styles={dayStyles}
|
|
703
|
+
dayStyle={stableDayStyle}
|
|
704
|
+
maxFontScale={cellFontScale}
|
|
705
|
+
onSelect={onSelect}
|
|
706
|
+
/>
|
|
707
|
+
)
|
|
708
|
+
)}
|
|
709
|
+
</View>
|
|
710
|
+
))}
|
|
711
|
+
</Appear>
|
|
712
|
+
)}
|
|
713
|
+
</View>
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/** A day's date in a calendar, or undefined outside the supported range. */
|
|
718
|
+
function safeDateInCalendar(
|
|
719
|
+
epochDay: number,
|
|
720
|
+
calendar: CalendarType,
|
|
721
|
+
adjustment: number
|
|
722
|
+
) {
|
|
723
|
+
try {
|
|
724
|
+
return dateInCalendar(epochDay, calendar, adjustment);
|
|
725
|
+
} catch {
|
|
726
|
+
return undefined;
|
|
727
|
+
}
|
|
728
|
+
}
|