react-native-hilal 0.1.0 → 0.3.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/README.md +165 -49
- package/lib/module/calendar/Appear.js +75 -0
- package/lib/module/calendar/Appear.js.map +1 -0
- package/lib/module/calendar/DayCell.js +32 -5
- package/lib/module/calendar/DayCell.js.map +1 -1
- package/lib/module/calendar/HilalCalendar.js +434 -76
- package/lib/module/calendar/HilalCalendar.js.map +1 -1
- package/lib/module/calendar/PickerGrid.js +66 -0
- package/lib/module/calendar/PickerGrid.js.map +1 -0
- package/lib/module/calendar/month.js +135 -22
- package/lib/module/calendar/month.js.map +1 -1
- package/lib/module/calendar/range.js +103 -0
- package/lib/module/calendar/range.js.map +1 -0
- package/lib/module/calendar/styles.js +233 -7
- package/lib/module/calendar/styles.js.map +1 -1
- package/lib/module/calendar/theme.js +3 -3
- package/lib/module/calendar/theme.js.map +1 -1
- package/lib/module/core/convert.js +52 -21
- package/lib/module/core/convert.js.map +1 -1
- package/lib/module/core/index.js.map +1 -1
- package/lib/module/core/locale.js +5 -2
- package/lib/module/core/locale.js.map +1 -1
- 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/DayCell.d.ts +8 -1
- package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -1
- package/lib/typescript/src/calendar/HilalCalendar.d.ts +1 -1
- package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -1
- 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 +1 -1
- package/lib/typescript/src/calendar/index.d.ts.map +1 -1
- package/lib/typescript/src/calendar/month.d.ts +30 -7
- package/lib/typescript/src/calendar/month.d.ts.map +1 -1
- package/lib/typescript/src/calendar/range.d.ts +39 -0
- package/lib/typescript/src/calendar/range.d.ts.map +1 -0
- package/lib/typescript/src/calendar/styles.d.ts +207 -113
- package/lib/typescript/src/calendar/styles.d.ts.map +1 -1
- package/lib/typescript/src/calendar/theme.d.ts +21 -1
- package/lib/typescript/src/calendar/theme.d.ts.map +1 -1
- package/lib/typescript/src/calendar/types.d.ts +153 -2
- package/lib/typescript/src/calendar/types.d.ts.map +1 -1
- package/lib/typescript/src/core/convert.d.ts +38 -15
- package/lib/typescript/src/core/convert.d.ts.map +1 -1
- package/lib/typescript/src/core/index.d.ts +1 -1
- package/lib/typescript/src/core/index.d.ts.map +1 -1
- package/lib/typescript/src/core/locale.d.ts +1 -1
- package/lib/typescript/src/core/locale.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/calendar/Appear.tsx +94 -0
- package/src/calendar/DayCell.tsx +41 -1
- package/src/calendar/HilalCalendar.tsx +541 -85
- package/src/calendar/PickerGrid.tsx +99 -0
- package/src/calendar/index.ts +8 -1
- package/src/calendar/month.ts +174 -23
- package/src/calendar/range.ts +114 -0
- package/src/calendar/styles.ts +249 -6
- package/src/calendar/theme.ts +23 -3
- package/src/calendar/types.ts +156 -2
- package/src/core/convert.ts +87 -23
- package/src/core/index.ts +1 -0
- package/src/core/locale.ts +4 -1
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
View,
|
|
8
8
|
} from 'react-native';
|
|
9
9
|
|
|
10
|
-
import { hijriOfEpochDay } from '../core/convert';
|
|
10
|
+
import { checkAdjustment, hijriOfEpochDay } from '../core/convert';
|
|
11
11
|
import {
|
|
12
12
|
civilFromDays,
|
|
13
13
|
epochDayOfLocalDate,
|
|
@@ -18,74 +18,160 @@ import { formatGregorian, formatHijri } from '../core/format';
|
|
|
18
18
|
import {
|
|
19
19
|
defaultDigits,
|
|
20
20
|
formatNumber,
|
|
21
|
+
getMonthNames,
|
|
21
22
|
getWeekdayNames,
|
|
22
23
|
type CalendarType,
|
|
23
24
|
type Digits,
|
|
24
25
|
type Locale,
|
|
25
26
|
} from '../core/locale';
|
|
27
|
+
import { Appear, useReduceMotion } from './Appear';
|
|
26
28
|
import { Chevron } from './Chevron';
|
|
27
29
|
import { DayCell } from './DayCell';
|
|
28
30
|
import {
|
|
29
31
|
addMonths,
|
|
30
32
|
buildMonthGrid,
|
|
31
33
|
clampDay,
|
|
34
|
+
dateInCalendar,
|
|
32
35
|
monthContaining,
|
|
33
36
|
monthOverlaps,
|
|
37
|
+
monthsOfYear,
|
|
34
38
|
monthStart,
|
|
35
39
|
monthTitle,
|
|
40
|
+
pageOverlaps,
|
|
36
41
|
secondaryTitle,
|
|
37
42
|
selectableRange,
|
|
43
|
+
yearOverlaps,
|
|
44
|
+
yearPageStart,
|
|
45
|
+
yearsOnPage,
|
|
46
|
+
YEARS_PER_PAGE,
|
|
47
|
+
yearsSecondaryTitle,
|
|
38
48
|
type CalendarMonth,
|
|
39
49
|
type DayRange,
|
|
40
50
|
} from './month';
|
|
41
|
-
import {
|
|
51
|
+
import { PickerGrid, type PickerGridItem } from './PickerGrid';
|
|
52
|
+
import {
|
|
53
|
+
checkRangeLimits,
|
|
54
|
+
nextRange,
|
|
55
|
+
outsideRangeLimits,
|
|
56
|
+
rangeLength,
|
|
57
|
+
rangePosition,
|
|
58
|
+
type DayRangeSelection,
|
|
59
|
+
type RangePosition,
|
|
60
|
+
} from './range';
|
|
61
|
+
import {
|
|
62
|
+
createStyles,
|
|
63
|
+
MAX_CELL_FONT_SCALE,
|
|
64
|
+
MAX_HEADER_FONT_SCALE,
|
|
65
|
+
withSlots,
|
|
66
|
+
} from './styles';
|
|
42
67
|
import { resolveTheme } from './theme';
|
|
43
|
-
import type {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
next: string;
|
|
52
|
-
separator: string;
|
|
53
|
-
}
|
|
54
|
-
> = {
|
|
68
|
+
import type {
|
|
69
|
+
DateRange,
|
|
70
|
+
HilalCalendarProps,
|
|
71
|
+
HilalLabels,
|
|
72
|
+
SelectedDate,
|
|
73
|
+
} from './types';
|
|
74
|
+
|
|
75
|
+
const LABELS: Record<Locale, Required<HilalLabels> & { separator: string }> = {
|
|
55
76
|
ar: {
|
|
56
77
|
hijri: 'هجري',
|
|
57
78
|
gregorian: 'ميلادي',
|
|
58
|
-
|
|
59
|
-
|
|
79
|
+
today: 'اليوم',
|
|
80
|
+
rangeStart: 'بداية الفترة',
|
|
81
|
+
rangeEnd: 'نهاية الفترة',
|
|
82
|
+
thisMonth: 'هذا الشهر',
|
|
83
|
+
thisYear: 'هذه السنة',
|
|
84
|
+
previousMonth: 'الشهر السابق',
|
|
85
|
+
nextMonth: 'الشهر التالي',
|
|
86
|
+
previousYear: 'السنة السابقة',
|
|
87
|
+
nextYear: 'السنة التالية',
|
|
88
|
+
previousYears: 'السنوات السابقة',
|
|
89
|
+
nextYears: 'السنوات التالية',
|
|
90
|
+
chooseMonth: 'اختر الشهر والسنة',
|
|
91
|
+
chooseYear: 'اختر السنة',
|
|
60
92
|
separator: '، ',
|
|
61
93
|
},
|
|
62
94
|
en: {
|
|
63
95
|
hijri: 'Hijri',
|
|
64
96
|
gregorian: 'Gregorian',
|
|
65
|
-
|
|
66
|
-
|
|
97
|
+
today: 'Today',
|
|
98
|
+
rangeStart: 'Start of range',
|
|
99
|
+
rangeEnd: 'End of range',
|
|
100
|
+
thisMonth: 'This month',
|
|
101
|
+
thisYear: 'This year',
|
|
102
|
+
previousMonth: 'Previous month',
|
|
103
|
+
nextMonth: 'Next month',
|
|
104
|
+
previousYear: 'Previous year',
|
|
105
|
+
nextYear: 'Next year',
|
|
106
|
+
previousYears: 'Previous years',
|
|
107
|
+
nextYears: 'Next years',
|
|
108
|
+
chooseMonth: 'Choose month and year',
|
|
109
|
+
chooseYear: 'Choose year',
|
|
67
110
|
separator: ', ',
|
|
68
111
|
},
|
|
69
112
|
};
|
|
70
113
|
|
|
114
|
+
// More below than above, so it never meets the toggle's.
|
|
115
|
+
const TITLE_HIT_SLOP = { top: 8, bottom: 10, left: 6, right: 6 };
|
|
116
|
+
const TOGGLE_HIT_SLOP = { top: 8, bottom: 8 };
|
|
117
|
+
|
|
118
|
+
/** What the grid shows: the days of a month, or the month or year picker. */
|
|
119
|
+
type View_ = 'days' | 'months' | 'years';
|
|
120
|
+
|
|
71
121
|
function epochDayOf(date: Date | undefined) {
|
|
72
122
|
return date instanceof Date && !Number.isNaN(date.getTime())
|
|
73
123
|
? epochDayOfLocalDate(date)
|
|
74
124
|
: undefined;
|
|
75
125
|
}
|
|
76
126
|
|
|
127
|
+
/** A DateRange as epoch days, its ends in order; undefined without a start. */
|
|
128
|
+
function rangeOfDates(
|
|
129
|
+
dates: DateRange | undefined
|
|
130
|
+
): DayRangeSelection | undefined {
|
|
131
|
+
const start = epochDayOf(dates?.start);
|
|
132
|
+
if (start === undefined) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
const end = epochDayOf(dates?.end ?? undefined);
|
|
136
|
+
if (end === undefined) {
|
|
137
|
+
return { start };
|
|
138
|
+
}
|
|
139
|
+
return { start: Math.min(start, end), end: Math.max(start, end) };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The days of a range drawn as selected: its two ends. */
|
|
143
|
+
function isRangeEnd(position: RangePosition | null) {
|
|
144
|
+
return position === 'start' || position === 'end' || position === 'single';
|
|
145
|
+
}
|
|
146
|
+
|
|
77
147
|
function selectedDateOf(
|
|
78
148
|
epochDay: number,
|
|
79
149
|
locale: Locale,
|
|
80
|
-
digits: Digits
|
|
150
|
+
digits: Digits,
|
|
151
|
+
adjustment: number
|
|
81
152
|
): SelectedDate {
|
|
82
|
-
const hijri = hijriOfEpochDay(epochDay);
|
|
153
|
+
const hijri = hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment);
|
|
154
|
+
// Formatted on first read: isDateDisabled runs for every day of a
|
|
155
|
+
// range and rarely reads the texts. Enumerable getters, so the object
|
|
156
|
+
// still spreads and serializes like plain data.
|
|
157
|
+
let gregorianText: string | undefined;
|
|
158
|
+
let hijriText: string | undefined;
|
|
83
159
|
return {
|
|
84
160
|
gregorian: localDateOfEpochDay(epochDay),
|
|
85
161
|
hijri,
|
|
86
162
|
formatted: {
|
|
87
|
-
gregorian
|
|
88
|
-
|
|
163
|
+
get gregorian() {
|
|
164
|
+
return (gregorianText ??= formatGregorian(civilFromDays(epochDay), {
|
|
165
|
+
locale,
|
|
166
|
+
digits,
|
|
167
|
+
}));
|
|
168
|
+
},
|
|
169
|
+
get hijri() {
|
|
170
|
+
return (hijriText ??= formatHijri(
|
|
171
|
+
hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment),
|
|
172
|
+
{ locale, digits }
|
|
173
|
+
));
|
|
174
|
+
},
|
|
89
175
|
},
|
|
90
176
|
};
|
|
91
177
|
}
|
|
@@ -106,11 +192,13 @@ function visibleMonth(
|
|
|
106
192
|
preferred: CalendarMonth,
|
|
107
193
|
calendar: CalendarType,
|
|
108
194
|
range: DayRange,
|
|
109
|
-
fallbackDay: number
|
|
195
|
+
fallbackDay: number,
|
|
196
|
+
adjustment: number
|
|
110
197
|
) {
|
|
111
|
-
return preferred.calendar === calendar &&
|
|
198
|
+
return preferred.calendar === calendar &&
|
|
199
|
+
monthOverlaps(preferred, range, adjustment)
|
|
112
200
|
? preferred
|
|
113
|
-
: monthContaining(clampDay(fallbackDay, range), calendar);
|
|
201
|
+
: monthContaining(clampDay(fallbackDay, range), calendar, adjustment);
|
|
114
202
|
}
|
|
115
203
|
|
|
116
204
|
/**
|
|
@@ -121,6 +209,12 @@ export function HilalCalendar({
|
|
|
121
209
|
value,
|
|
122
210
|
defaultValue,
|
|
123
211
|
onChange,
|
|
212
|
+
selectionMode = 'single',
|
|
213
|
+
rangeValue,
|
|
214
|
+
defaultRangeValue,
|
|
215
|
+
onRangeChange,
|
|
216
|
+
minRangeDays,
|
|
217
|
+
maxRangeDays,
|
|
124
218
|
calendar: calendarProp = 'hijri',
|
|
125
219
|
onCalendarChange,
|
|
126
220
|
showCalendarToggle = true,
|
|
@@ -132,12 +226,40 @@ export function HilalCalendar({
|
|
|
132
226
|
minDate,
|
|
133
227
|
maxDate,
|
|
134
228
|
isDateDisabled,
|
|
229
|
+
hijriAdjustment = 0,
|
|
230
|
+
showMonthYearPicker = true,
|
|
231
|
+
weekdayFormat = 'short',
|
|
135
232
|
theme: themeProp = 'auto',
|
|
233
|
+
styles: stylesProp,
|
|
234
|
+
labels: labelsProp,
|
|
136
235
|
style,
|
|
137
236
|
dayStyle,
|
|
237
|
+
maxFontSizeMultiplier,
|
|
138
238
|
}: HilalCalendarProps) {
|
|
239
|
+
const adjustment = checkAdjustment(hijriAdjustment, 'HilalCalendar');
|
|
139
240
|
const digits = digitsProp ?? defaultDigits(locale);
|
|
140
|
-
|
|
241
|
+
// Compared by value, so an inline object doesn't recompute every day.
|
|
242
|
+
const labelsKey = JSON.stringify(labelsProp);
|
|
243
|
+
const labels = useMemo(
|
|
244
|
+
() => {
|
|
245
|
+
const merged = { ...LABELS[locale] };
|
|
246
|
+
for (const [key, text] of Object.entries(labelsProp ?? {})) {
|
|
247
|
+
// A label left undefined keeps the default.
|
|
248
|
+
if (text !== undefined) {
|
|
249
|
+
merged[key as keyof HilalLabels] = text;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return merged;
|
|
253
|
+
},
|
|
254
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
255
|
+
[locale, labelsKey]
|
|
256
|
+
);
|
|
257
|
+
const cellFontScale = maxFontSizeMultiplier ?? MAX_CELL_FONT_SCALE;
|
|
258
|
+
const headerFontScale = maxFontSizeMultiplier ?? MAX_HEADER_FONT_SCALE;
|
|
259
|
+
// Today, this month and this year are only marked by a ring; screen
|
|
260
|
+
// readers hear it at the end of the label instead.
|
|
261
|
+
const withCurrent = (label: string, current: boolean, text: string) =>
|
|
262
|
+
current ? `${label}${labels.separator}${text}` : label;
|
|
141
263
|
// React Native Web leaves I18nManager.isRTL undefined, not false.
|
|
142
264
|
const appRTL = I18nManager.isRTL === true;
|
|
143
265
|
const rtl =
|
|
@@ -155,7 +277,13 @@ export function HilalCalendar({
|
|
|
155
277
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
156
278
|
[themeKey, colorScheme]
|
|
157
279
|
);
|
|
158
|
-
|
|
280
|
+
// An inline `styles` prop is compared by value too.
|
|
281
|
+
const stylesKey = JSON.stringify(stylesProp);
|
|
282
|
+
const { styles, dayStyles } = useMemo(
|
|
283
|
+
() => withSlots(createStyles(theme), stylesProp),
|
|
284
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
285
|
+
[theme, stylesKey]
|
|
286
|
+
);
|
|
159
287
|
// Same for an inline dayStyle: a new object every render would re-render
|
|
160
288
|
// every day cell.
|
|
161
289
|
const dayStyleKey = JSON.stringify(dayStyle);
|
|
@@ -165,8 +293,8 @@ export function HilalCalendar({
|
|
|
165
293
|
const minDay = epochDayOf(minDate);
|
|
166
294
|
const maxDay = epochDayOf(maxDate);
|
|
167
295
|
const range = useMemo(
|
|
168
|
-
() => selectableRange(minDay, maxDay),
|
|
169
|
-
[minDay, maxDay]
|
|
296
|
+
() => selectableRange(minDay, maxDay, adjustment),
|
|
297
|
+
[minDay, maxDay, adjustment]
|
|
170
298
|
);
|
|
171
299
|
const today = epochDayOfLocalDate(new Date());
|
|
172
300
|
|
|
@@ -176,22 +304,65 @@ export function HilalCalendar({
|
|
|
176
304
|
);
|
|
177
305
|
const selectedDay = value !== undefined ? epochDayOf(value) : internalDay;
|
|
178
306
|
|
|
307
|
+
// Range selection: controlled through `rangeValue`, otherwise internal.
|
|
308
|
+
const rangeMode = selectionMode === 'range';
|
|
309
|
+
const rangeLimits = checkRangeLimits(minRangeDays, maxRangeDays);
|
|
310
|
+
const [internalRange, setInternalRange] = useState(() =>
|
|
311
|
+
rangeOfDates(defaultRangeValue)
|
|
312
|
+
);
|
|
313
|
+
const pickedRange =
|
|
314
|
+
rangeValue !== undefined
|
|
315
|
+
? rangeOfDates(rangeValue ?? undefined)
|
|
316
|
+
: internalRange;
|
|
317
|
+
// The day the calendar opens on and follows: the selected day, or the
|
|
318
|
+
// start of the range.
|
|
319
|
+
const anchorDay = rangeMode ? pickedRange?.start : selectedDay;
|
|
320
|
+
|
|
179
321
|
// Calendar: internal state that follows the prop when the prop changes.
|
|
180
322
|
const [calendar, setCalendar] = useState(calendarProp);
|
|
181
323
|
const [lastCalendarProp, setLastCalendarProp] = useState(calendarProp);
|
|
182
324
|
const [month, setMonth] = useState(() =>
|
|
183
|
-
monthContaining(
|
|
325
|
+
monthContaining(
|
|
326
|
+
clampDay(anchorDay ?? today, range),
|
|
327
|
+
calendarProp,
|
|
328
|
+
adjustment
|
|
329
|
+
)
|
|
184
330
|
);
|
|
185
|
-
const [
|
|
331
|
+
const [lastAnchorDay, setLastAnchorDay] = useState(anchorDay);
|
|
332
|
+
|
|
333
|
+
// The month and year picker: which grid is shown, the year whose months
|
|
334
|
+
// are listed, and the first year of the page of years.
|
|
335
|
+
const [view, setView] = useState<View_>('days');
|
|
336
|
+
const [pickerYear, setPickerYear] = useState(0);
|
|
337
|
+
const [pageStart, setPageStart] = useState(0);
|
|
338
|
+
const mode: View_ = showMonthYearPicker ? view : 'days';
|
|
339
|
+
// The grids fade in when switching views, but not when the calendar
|
|
340
|
+
// first appears: the days only animate once the picker has been open.
|
|
341
|
+
const reduceMotion = useReduceMotion();
|
|
342
|
+
const [pickerOpened, setPickerOpened] = useState(false);
|
|
343
|
+
if (mode !== 'days' && !pickerOpened) {
|
|
344
|
+
setPickerOpened(true);
|
|
345
|
+
}
|
|
346
|
+
// The height of the days, which the picker takes too so that the
|
|
347
|
+
// calendar doesn't jump in size when switching.
|
|
348
|
+
const [daysHeight, setDaysHeight] = useState<number>();
|
|
186
349
|
|
|
187
350
|
const switchTo = (next: CalendarType, current: CalendarMonth) => {
|
|
188
351
|
setCalendar(next);
|
|
352
|
+
setView('days');
|
|
189
353
|
// Keep the selected day in view, or else the month being looked at.
|
|
190
|
-
const anchor =
|
|
191
|
-
|
|
354
|
+
const anchor =
|
|
355
|
+
anchorDay ?? clampDay(monthStart(current, adjustment), range);
|
|
356
|
+
setMonth(monthContaining(clampDay(anchor, range), next, adjustment));
|
|
192
357
|
};
|
|
193
358
|
|
|
194
|
-
const shown = visibleMonth(
|
|
359
|
+
const shown = visibleMonth(
|
|
360
|
+
month,
|
|
361
|
+
calendar,
|
|
362
|
+
range,
|
|
363
|
+
anchorDay ?? today,
|
|
364
|
+
adjustment
|
|
365
|
+
);
|
|
195
366
|
|
|
196
367
|
// Adjust state while rendering when props change (React's recommended
|
|
197
368
|
// alternative to effects for derived state).
|
|
@@ -199,14 +370,14 @@ export function HilalCalendar({
|
|
|
199
370
|
setLastCalendarProp(calendarProp);
|
|
200
371
|
switchTo(calendarProp, shown);
|
|
201
372
|
}
|
|
202
|
-
if (
|
|
203
|
-
|
|
373
|
+
if (anchorDay !== lastAnchorDay) {
|
|
374
|
+
setLastAnchorDay(anchorDay);
|
|
204
375
|
if (
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
376
|
+
anchorDay !== undefined &&
|
|
377
|
+
anchorDay >= range.min &&
|
|
378
|
+
anchorDay <= range.max
|
|
208
379
|
) {
|
|
209
|
-
const containing = monthContaining(
|
|
380
|
+
const containing = monthContaining(anchorDay, calendar, adjustment);
|
|
210
381
|
if (containing.year !== shown.year || containing.month !== shown.month) {
|
|
211
382
|
setMonth(containing);
|
|
212
383
|
}
|
|
@@ -214,10 +385,10 @@ export function HilalCalendar({
|
|
|
214
385
|
}
|
|
215
386
|
|
|
216
387
|
const grid = useMemo(
|
|
217
|
-
() => buildMonthGrid(shown, firstDayOfWeek),
|
|
388
|
+
() => buildMonthGrid(shown, firstDayOfWeek, adjustment),
|
|
218
389
|
// `shown` is a new object on every render; its fields identify it.
|
|
219
390
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
220
|
-
[shown.calendar, shown.year, shown.month, firstDayOfWeek]
|
|
391
|
+
[shown.calendar, shown.year, shown.month, firstDayOfWeek, adjustment]
|
|
221
392
|
);
|
|
222
393
|
|
|
223
394
|
// Everything about a day except `selected` and `today`, so that changing
|
|
@@ -235,7 +406,9 @@ export function HilalCalendar({
|
|
|
235
406
|
!inSupportedRange ||
|
|
236
407
|
epochDay < range.min ||
|
|
237
408
|
epochDay > range.max ||
|
|
238
|
-
(isDateDisabled?.(
|
|
409
|
+
(isDateDisabled?.(
|
|
410
|
+
selectedDateOf(epochDay, locale, digits, adjustment)
|
|
411
|
+
) ??
|
|
239
412
|
false);
|
|
240
413
|
const weekday = getWeekdayNames(locale)[weekdayOfEpochDay(epochDay)];
|
|
241
414
|
const gregorianText = formatGregorian(civilFromDays(epochDay), {
|
|
@@ -244,7 +417,10 @@ export function HilalCalendar({
|
|
|
244
417
|
});
|
|
245
418
|
const spoken = inSupportedRange
|
|
246
419
|
? [
|
|
247
|
-
formatHijri(
|
|
420
|
+
formatHijri(
|
|
421
|
+
hijriOfEpochDay(epochDay, 'HilalCalendar', adjustment),
|
|
422
|
+
{ locale, digits }
|
|
423
|
+
),
|
|
248
424
|
gregorianText,
|
|
249
425
|
]
|
|
250
426
|
: [gregorianText];
|
|
@@ -272,14 +448,34 @@ export function HilalCalendar({
|
|
|
272
448
|
digits,
|
|
273
449
|
showSecondaryDate,
|
|
274
450
|
labels,
|
|
451
|
+
adjustment,
|
|
275
452
|
]
|
|
276
453
|
);
|
|
277
454
|
|
|
278
455
|
const onSelect = useStableCallback((epochDay: number) => {
|
|
456
|
+
if (rangeMode) {
|
|
457
|
+
const dateOf = (day: number) =>
|
|
458
|
+
selectedDateOf(day, locale, digits, adjustment);
|
|
459
|
+
const next = nextRange(
|
|
460
|
+
pickedRange,
|
|
461
|
+
epochDay,
|
|
462
|
+
isDateDisabled && ((day) => isDateDisabled(dateOf(day))),
|
|
463
|
+
rangeLimits
|
|
464
|
+
);
|
|
465
|
+
if (rangeValue === undefined) {
|
|
466
|
+
setInternalRange(next);
|
|
467
|
+
}
|
|
468
|
+
onRangeChange?.({
|
|
469
|
+
start: dateOf(next.start),
|
|
470
|
+
end: next.end === undefined ? null : dateOf(next.end),
|
|
471
|
+
days: rangeLength(next),
|
|
472
|
+
});
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
279
475
|
if (value === undefined) {
|
|
280
476
|
setInternalDay(epochDay);
|
|
281
477
|
}
|
|
282
|
-
onChange?.(selectedDateOf(epochDay, locale, digits));
|
|
478
|
+
onChange?.(selectedDateOf(epochDay, locale, digits, adjustment));
|
|
283
479
|
});
|
|
284
480
|
|
|
285
481
|
const toggleCalendar = (next: CalendarType) => {
|
|
@@ -290,39 +486,208 @@ export function HilalCalendar({
|
|
|
290
486
|
onCalendarChange?.(next);
|
|
291
487
|
};
|
|
292
488
|
|
|
293
|
-
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
489
|
+
// A day's spoken label: its date, then today and the ends of a range.
|
|
490
|
+
const dayLabel = (cell: { epochDay: number; accessibilityLabel: string }) => {
|
|
491
|
+
const position = rangeMode
|
|
492
|
+
? rangePosition(pickedRange, cell.epochDay)
|
|
493
|
+
: null;
|
|
494
|
+
const label = withCurrent(
|
|
495
|
+
cell.accessibilityLabel,
|
|
496
|
+
cell.epochDay === today,
|
|
497
|
+
labels.today
|
|
498
|
+
);
|
|
499
|
+
if (position === 'start' || position === 'single') {
|
|
500
|
+
return `${label}${labels.separator}${labels.rangeStart}`;
|
|
501
|
+
}
|
|
502
|
+
return position === 'end'
|
|
503
|
+
? `${label}${labels.separator}${labels.rangeEnd}`
|
|
504
|
+
: label;
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
// Today's month and year in the shown calendar, for the picker's ring.
|
|
508
|
+
const todayInCalendar = safeDateInCalendar(today, calendar, adjustment);
|
|
509
|
+
|
|
510
|
+
// What the arrows do depends on the grid: months, years, or pages.
|
|
511
|
+
let arrows: Record<
|
|
512
|
+
'previous' | 'next',
|
|
513
|
+
{ enabled: boolean; label: string; onPress: () => void }
|
|
514
|
+
>;
|
|
515
|
+
if (mode === 'months') {
|
|
516
|
+
arrows = {
|
|
517
|
+
previous: {
|
|
518
|
+
enabled: yearOverlaps(calendar, pickerYear - 1, range, adjustment),
|
|
519
|
+
label: labels.previousYear,
|
|
520
|
+
onPress: () => setPickerYear(pickerYear - 1),
|
|
521
|
+
},
|
|
522
|
+
next: {
|
|
523
|
+
enabled: yearOverlaps(calendar, pickerYear + 1, range, adjustment),
|
|
524
|
+
label: labels.nextYear,
|
|
525
|
+
onPress: () => setPickerYear(pickerYear + 1),
|
|
526
|
+
},
|
|
527
|
+
};
|
|
528
|
+
} else if (mode === 'years') {
|
|
529
|
+
const before = pageStart - YEARS_PER_PAGE;
|
|
530
|
+
const after = pageStart + YEARS_PER_PAGE;
|
|
531
|
+
arrows = {
|
|
532
|
+
previous: {
|
|
533
|
+
enabled: pageOverlaps(calendar, before, range, adjustment),
|
|
534
|
+
label: labels.previousYears,
|
|
535
|
+
onPress: () => setPageStart(before),
|
|
536
|
+
},
|
|
537
|
+
next: {
|
|
538
|
+
enabled: pageOverlaps(calendar, after, range, adjustment),
|
|
539
|
+
label: labels.nextYears,
|
|
540
|
+
onPress: () => setPageStart(after),
|
|
541
|
+
},
|
|
542
|
+
};
|
|
543
|
+
} else {
|
|
544
|
+
const previous = addMonths(shown, -1);
|
|
545
|
+
const next = addMonths(shown, 1);
|
|
546
|
+
arrows = {
|
|
547
|
+
previous: {
|
|
548
|
+
enabled: monthOverlaps(previous, range, adjustment),
|
|
549
|
+
label: labels.previousMonth,
|
|
550
|
+
onPress: () => setMonth(previous),
|
|
551
|
+
},
|
|
552
|
+
next: {
|
|
553
|
+
enabled: monthOverlaps(next, range, adjustment),
|
|
554
|
+
label: labels.nextMonth,
|
|
555
|
+
onPress: () => setMonth(next),
|
|
556
|
+
},
|
|
557
|
+
};
|
|
558
|
+
}
|
|
297
559
|
|
|
298
|
-
const weekdayNames = getWeekdayNames(locale,
|
|
560
|
+
const weekdayNames = getWeekdayNames(locale, weekdayFormat);
|
|
299
561
|
const weekdays = Array.from(
|
|
300
562
|
{ length: 7 },
|
|
301
563
|
(_, i) => weekdayNames[(firstDayOfWeek + i) % 7]!
|
|
302
564
|
);
|
|
303
565
|
|
|
304
566
|
const renderArrow = (kind: 'previous' | 'next') => {
|
|
305
|
-
const enabled
|
|
567
|
+
const { enabled, label, onPress } = arrows[kind];
|
|
306
568
|
// Arrows point the way the months move: outward in reading order.
|
|
307
569
|
const pointsLeft = (kind === 'previous') !== rtl;
|
|
308
570
|
return (
|
|
309
571
|
<Pressable
|
|
310
572
|
accessibilityRole="button"
|
|
311
|
-
accessibilityLabel={
|
|
573
|
+
accessibilityLabel={label}
|
|
312
574
|
accessibilityState={{ disabled: !enabled }}
|
|
313
575
|
disabled={!enabled}
|
|
314
576
|
hitSlop={8}
|
|
315
|
-
onPress={
|
|
577
|
+
onPress={onPress}
|
|
316
578
|
style={({ pressed }) => [styles.arrow, pressed && styles.arrowPressed]}
|
|
317
579
|
>
|
|
318
580
|
<Chevron
|
|
319
581
|
direction={pointsLeft ? 'left' : 'right'}
|
|
320
|
-
color={enabled ? theme.text : theme.disabled}
|
|
582
|
+
color={enabled ? (theme.arrow ?? theme.text) : theme.disabled}
|
|
321
583
|
/>
|
|
322
584
|
</Pressable>
|
|
323
585
|
);
|
|
324
586
|
};
|
|
325
587
|
|
|
588
|
+
const year = (y: number) => formatNumber(y, digits);
|
|
589
|
+
|
|
590
|
+
// The header title, and what pressing it opens.
|
|
591
|
+
let title: string;
|
|
592
|
+
let subtitle: string;
|
|
593
|
+
let titleAction: { label: string; onPress: () => void } | undefined;
|
|
594
|
+
if (mode === 'months') {
|
|
595
|
+
title = year(pickerYear);
|
|
596
|
+
subtitle = yearsSecondaryTitle(
|
|
597
|
+
calendar,
|
|
598
|
+
pickerYear,
|
|
599
|
+
pickerYear,
|
|
600
|
+
digits,
|
|
601
|
+
adjustment
|
|
602
|
+
);
|
|
603
|
+
titleAction = {
|
|
604
|
+
label: labels.chooseYear,
|
|
605
|
+
onPress: () => {
|
|
606
|
+
setPageStart(yearPageStart(pickerYear));
|
|
607
|
+
setView('years');
|
|
608
|
+
},
|
|
609
|
+
};
|
|
610
|
+
} else if (mode === 'years') {
|
|
611
|
+
const last = pageStart + YEARS_PER_PAGE - 1;
|
|
612
|
+
title = `${year(pageStart)} – ${year(last)}`;
|
|
613
|
+
subtitle = yearsSecondaryTitle(
|
|
614
|
+
calendar,
|
|
615
|
+
pageStart,
|
|
616
|
+
last,
|
|
617
|
+
digits,
|
|
618
|
+
adjustment
|
|
619
|
+
);
|
|
620
|
+
} else {
|
|
621
|
+
title = monthTitle(shown, locale, digits);
|
|
622
|
+
subtitle = secondaryTitle(shown, locale, digits, adjustment);
|
|
623
|
+
if (showMonthYearPicker) {
|
|
624
|
+
titleAction = {
|
|
625
|
+
label: labels.chooseMonth,
|
|
626
|
+
onPress: () => {
|
|
627
|
+
setPickerYear(shown.year);
|
|
628
|
+
setView('months');
|
|
629
|
+
},
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
const titleText = (
|
|
635
|
+
<Text
|
|
636
|
+
accessibilityRole="header"
|
|
637
|
+
maxFontSizeMultiplier={headerFontScale}
|
|
638
|
+
style={titleAction ? [styles.title, styles.titleInButton] : styles.title}
|
|
639
|
+
>
|
|
640
|
+
{title}
|
|
641
|
+
</Text>
|
|
642
|
+
);
|
|
643
|
+
|
|
644
|
+
let pickerItems: PickerGridItem[] | undefined;
|
|
645
|
+
if (mode === 'months') {
|
|
646
|
+
const names = getMonthNames(calendar, locale);
|
|
647
|
+
pickerItems = monthsOfYear(calendar, pickerYear, range, adjustment).map(
|
|
648
|
+
({ value: m, disabled }) => {
|
|
649
|
+
const current =
|
|
650
|
+
todayInCalendar?.year === pickerYear && todayInCalendar.month === m;
|
|
651
|
+
return {
|
|
652
|
+
key: m,
|
|
653
|
+
label: names[m - 1]!,
|
|
654
|
+
accessibilityLabel: withCurrent(
|
|
655
|
+
`${names[m - 1]} ${year(pickerYear)}`,
|
|
656
|
+
current,
|
|
657
|
+
labels.thisMonth
|
|
658
|
+
),
|
|
659
|
+
selected: pickerYear === shown.year && m === shown.month,
|
|
660
|
+
current,
|
|
661
|
+
disabled,
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
);
|
|
665
|
+
} else if (mode === 'years') {
|
|
666
|
+
pickerItems = yearsOnPage(calendar, pageStart, range, adjustment).map(
|
|
667
|
+
({ value: y, disabled }) => {
|
|
668
|
+
const current = todayInCalendar?.year === y;
|
|
669
|
+
return {
|
|
670
|
+
key: y,
|
|
671
|
+
label: year(y),
|
|
672
|
+
accessibilityLabel: withCurrent(year(y), current, labels.thisYear),
|
|
673
|
+
selected: y === pickerYear,
|
|
674
|
+
current,
|
|
675
|
+
disabled,
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
const pick = (key: number) => {
|
|
682
|
+
if (mode === 'months') {
|
|
683
|
+
setMonth({ calendar, year: pickerYear, month: key });
|
|
684
|
+
setView('days');
|
|
685
|
+
} else {
|
|
686
|
+
setPickerYear(key);
|
|
687
|
+
setView('months');
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
|
|
326
691
|
return (
|
|
327
692
|
<View style={[styles.container, style]}>
|
|
328
693
|
{showCalendarToggle && (
|
|
@@ -332,6 +697,8 @@ export function HilalCalendar({
|
|
|
332
697
|
return (
|
|
333
698
|
<Pressable
|
|
334
699
|
key={option}
|
|
700
|
+
// 45 pt tall to the finger; the toggle itself stays slim.
|
|
701
|
+
hitSlop={TOGGLE_HIT_SLOP}
|
|
335
702
|
accessibilityRole="button"
|
|
336
703
|
accessibilityState={{ selected: active }}
|
|
337
704
|
onPress={() => toggleCalendar(option)}
|
|
@@ -341,6 +708,7 @@ export function HilalCalendar({
|
|
|
341
708
|
]}
|
|
342
709
|
>
|
|
343
710
|
<Text
|
|
711
|
+
maxFontSizeMultiplier={headerFontScale}
|
|
344
712
|
style={[styles.toggleText, active && styles.toggleTextActive]}
|
|
345
713
|
>
|
|
346
714
|
{labels[option]}
|
|
@@ -357,43 +725,131 @@ export function HilalCalendar({
|
|
|
357
725
|
>
|
|
358
726
|
{renderArrow('previous')}
|
|
359
727
|
<View style={styles.titles}>
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
728
|
+
{titleAction ? (
|
|
729
|
+
<Pressable
|
|
730
|
+
accessibilityRole="button"
|
|
731
|
+
accessibilityLabel={`${titleAction.label}${labels.separator}${title}`}
|
|
732
|
+
// 44 pt tall to the finger, like the arrows.
|
|
733
|
+
hitSlop={TITLE_HIT_SLOP}
|
|
734
|
+
onPress={titleAction.onPress}
|
|
735
|
+
style={({ pressed }) => [
|
|
736
|
+
styles.titleButton,
|
|
737
|
+
{ flexDirection: row },
|
|
738
|
+
pressed && styles.titleButtonPressed,
|
|
739
|
+
]}
|
|
740
|
+
>
|
|
741
|
+
<View style={styles.titleTint} />
|
|
742
|
+
{titleText}
|
|
743
|
+
</Pressable>
|
|
744
|
+
) : mode === 'years' ? (
|
|
745
|
+
// As tall as the title button of the other views, so the
|
|
746
|
+
// header doesn't jump when the years open.
|
|
747
|
+
<View style={styles.titleStatic}>{titleText}</View>
|
|
748
|
+
) : (
|
|
749
|
+
titleText
|
|
750
|
+
)}
|
|
751
|
+
<Text maxFontSizeMultiplier={headerFontScale} style={styles.subtitle}>
|
|
752
|
+
{subtitle}
|
|
365
753
|
</Text>
|
|
366
754
|
</View>
|
|
367
755
|
{renderArrow('next')}
|
|
368
756
|
</View>
|
|
369
757
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
758
|
+
{pickerItems ? (
|
|
759
|
+
<Appear
|
|
760
|
+
key={mode}
|
|
761
|
+
animate={!reduceMotion}
|
|
762
|
+
style={daysHeight !== undefined && { minHeight: daysHeight }}
|
|
763
|
+
>
|
|
764
|
+
<PickerGrid
|
|
765
|
+
items={pickerItems}
|
|
766
|
+
styles={styles}
|
|
767
|
+
row={row}
|
|
768
|
+
fill={daysHeight !== undefined}
|
|
769
|
+
maxFontScale={cellFontScale}
|
|
770
|
+
onSelect={pick}
|
|
771
|
+
/>
|
|
772
|
+
</Appear>
|
|
773
|
+
) : (
|
|
774
|
+
<Appear
|
|
775
|
+
key="days"
|
|
776
|
+
animate={pickerOpened && !reduceMotion}
|
|
777
|
+
onLayout={(event) => setDaysHeight(event.nativeEvent.layout.height)}
|
|
778
|
+
>
|
|
779
|
+
<View style={[styles.weekdays, { flexDirection: row }]}>
|
|
780
|
+
{/* Keyed by position: narrow names repeat (S, T). */}
|
|
781
|
+
{weekdays.map((name, i) => (
|
|
782
|
+
<Text
|
|
783
|
+
key={i}
|
|
784
|
+
// Shrinks back to fit its column when the phone's text is
|
|
785
|
+
// large.
|
|
786
|
+
numberOfLines={1}
|
|
787
|
+
adjustsFontSizeToFit
|
|
788
|
+
maxFontSizeMultiplier={cellFontScale}
|
|
789
|
+
style={styles.weekday}
|
|
790
|
+
>
|
|
791
|
+
{name}
|
|
792
|
+
</Text>
|
|
793
|
+
))}
|
|
794
|
+
</View>
|
|
377
795
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
796
|
+
{cells.map((week, w) => (
|
|
797
|
+
<View key={w} style={[styles.week, { flexDirection: row }]}>
|
|
798
|
+
{week.map((cell, d) =>
|
|
799
|
+
cell === null ? (
|
|
800
|
+
<View key={`empty-${d}`} style={styles.slot} />
|
|
801
|
+
) : (
|
|
802
|
+
<DayCell
|
|
803
|
+
key={cell.epochDay}
|
|
804
|
+
{...cell}
|
|
805
|
+
accessibilityLabel={dayLabel(cell)}
|
|
806
|
+
// After the start, days that would make the range too
|
|
807
|
+
// short or too long can't be pressed.
|
|
808
|
+
disabled={
|
|
809
|
+
cell.disabled ||
|
|
810
|
+
(rangeMode &&
|
|
811
|
+
outsideRangeLimits(
|
|
812
|
+
pickedRange,
|
|
813
|
+
cell.epochDay,
|
|
814
|
+
rangeLimits
|
|
815
|
+
))
|
|
816
|
+
}
|
|
817
|
+
selected={
|
|
818
|
+
rangeMode
|
|
819
|
+
? isRangeEnd(rangePosition(pickedRange, cell.epochDay))
|
|
820
|
+
: cell.epochDay === selectedDay
|
|
821
|
+
}
|
|
822
|
+
rangePosition={
|
|
823
|
+
rangeMode
|
|
824
|
+
? rangePosition(pickedRange, cell.epochDay)
|
|
825
|
+
: null
|
|
826
|
+
}
|
|
827
|
+
nextSide={row === 'row' ? 'end' : 'start'}
|
|
828
|
+
today={cell.epochDay === today}
|
|
829
|
+
styles={dayStyles}
|
|
830
|
+
dayStyle={stableDayStyle}
|
|
831
|
+
maxFontScale={cellFontScale}
|
|
832
|
+
onSelect={onSelect}
|
|
833
|
+
/>
|
|
834
|
+
)
|
|
835
|
+
)}
|
|
836
|
+
</View>
|
|
837
|
+
))}
|
|
838
|
+
</Appear>
|
|
839
|
+
)}
|
|
397
840
|
</View>
|
|
398
841
|
);
|
|
399
842
|
}
|
|
843
|
+
|
|
844
|
+
/** A day's date in a calendar, or undefined outside the supported range. */
|
|
845
|
+
function safeDateInCalendar(
|
|
846
|
+
epochDay: number,
|
|
847
|
+
calendar: CalendarType,
|
|
848
|
+
adjustment: number
|
|
849
|
+
) {
|
|
850
|
+
try {
|
|
851
|
+
return dateInCalendar(epochDay, calendar, adjustment);
|
|
852
|
+
} catch {
|
|
853
|
+
return undefined;
|
|
854
|
+
}
|
|
855
|
+
}
|