@super-calendar/native 2.8.1 → 2.9.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 +1 -0
- package/dist/{DefaultMonthEvent-CjwX3Nt1.d.ts → DefaultMonthEvent--9Jg6w-x.d.ts} +98 -10
- package/dist/{DefaultMonthEvent-VgJarTO_.d.mts → DefaultMonthEvent-BlnKHaq2.d.mts} +98 -10
- package/dist/{MonthList-DjAz4xoi.mjs → MonthList-CjKhzOJX.mjs} +356 -102
- package/dist/{MonthList-C-vBotP0.js → MonthList-DNT1LJkk.js} +355 -101
- package/dist/index.d.mts +85 -37
- package/dist/index.d.ts +85 -37
- package/dist/index.js +377 -60
- package/dist/index.mjs +378 -61
- package/dist/picker.d.mts +1 -1
- package/dist/picker.d.ts +1 -1
- package/dist/picker.js +1 -1
- package/dist/picker.mjs +1 -1
- package/package.json +2 -2
- package/src/components/Agenda.tsx +3 -3
- package/src/components/AllDayLane.tsx +2 -2
- package/src/components/Calendar.tsx +79 -16
- package/src/components/MonthList.tsx +3 -2
- package/src/components/MonthPager.tsx +152 -63
- package/src/components/MonthView.tsx +422 -80
- package/src/components/TimeGrid.tsx +247 -35
- package/src/components/YearView.tsx +371 -83
- package/src/types.ts +19 -0
- package/src/utils/slots.ts +33 -9
|
@@ -1,31 +1,47 @@
|
|
|
1
1
|
import { format, startOfDay, type Locale } from "date-fns";
|
|
2
|
-
import { memo, type ReactElement, useMemo, useState } from "react";
|
|
2
|
+
import { memo, type ReactElement, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import {
|
|
4
4
|
type LayoutChangeEvent,
|
|
5
|
+
Platform,
|
|
5
6
|
Pressable,
|
|
6
7
|
ScrollView,
|
|
7
8
|
StyleSheet,
|
|
8
9
|
Text,
|
|
10
|
+
type TextStyle,
|
|
9
11
|
View,
|
|
10
12
|
} from "react-native";
|
|
13
|
+
import { Gesture, GestureDetector, type PanGesture } from "react-native-gesture-handler";
|
|
11
14
|
import { useCalendarTheme } from "../theme";
|
|
12
15
|
import type { CalendarEvent, WeekStartsOn } from "../types";
|
|
13
16
|
import { createSlots, type SlotStyleProps } from "../utils/slots";
|
|
14
17
|
import {
|
|
18
|
+
bandRounding,
|
|
15
19
|
buildMonthWeeks,
|
|
20
|
+
type DateRange,
|
|
21
|
+
dayBadgeKind,
|
|
22
|
+
monthCreateRange,
|
|
23
|
+
daySelectionState,
|
|
16
24
|
getIsToday,
|
|
17
25
|
filterHiddenDays,
|
|
18
26
|
getWeekDays,
|
|
19
27
|
getYearMonths,
|
|
20
28
|
groupEventsByDay,
|
|
21
29
|
isBackgroundEvent,
|
|
30
|
+
isDateSelectable,
|
|
22
31
|
isSameCalendarDay,
|
|
32
|
+
rangeBandKind,
|
|
23
33
|
weekdayFormatToken,
|
|
24
34
|
} from "@super-calendar/core";
|
|
25
35
|
|
|
36
|
+
const isWeb = Platform.OS === "web";
|
|
37
|
+
// Hold before a year-grid drag takes over, so a tap still opens the day.
|
|
38
|
+
const DRAG_HOLD_MS = 400;
|
|
39
|
+
|
|
26
40
|
/**
|
|
27
41
|
* The styleable parts of {@link YearView}. `dayText` and `eventDot` are the
|
|
28
|
-
* text/marker inside a day cell (React Native text colour doesn't inherit)
|
|
42
|
+
* text/marker inside a day cell (React Native text colour doesn't inherit);
|
|
43
|
+
* `dayBadge` is the round highlight behind the number and `rangeBand` the
|
|
44
|
+
* selection strip behind it.
|
|
29
45
|
*/
|
|
30
46
|
export type YearViewSlot =
|
|
31
47
|
| "grid"
|
|
@@ -35,7 +51,9 @@ export type YearViewSlot =
|
|
|
35
51
|
| "weekday"
|
|
36
52
|
| "week"
|
|
37
53
|
| "day"
|
|
54
|
+
| "dayBadge"
|
|
38
55
|
| "dayText"
|
|
56
|
+
| "rangeBand"
|
|
39
57
|
| "eventDot";
|
|
40
58
|
|
|
41
59
|
/** Props for {@link YearView}, the twelve mini-month year grid. */
|
|
@@ -50,6 +68,16 @@ export type YearViewProps<T = unknown> = SlotStyleProps<YearViewSlot> & {
|
|
|
50
68
|
locale?: Locale;
|
|
51
69
|
/** Highlight this date instead of the real "today". */
|
|
52
70
|
activeDate?: Date;
|
|
71
|
+
/** A selected span: endpoints get a filled badge, the days between get the range band. */
|
|
72
|
+
selectedRange?: DateRange;
|
|
73
|
+
/** Discrete selected days, drawn with a filled badge. */
|
|
74
|
+
selectedDates?: Date[];
|
|
75
|
+
/** Earliest selectable day (inclusive); earlier days render disabled. */
|
|
76
|
+
minDate?: Date;
|
|
77
|
+
/** Latest selectable day (inclusive); later days render disabled. */
|
|
78
|
+
maxDate?: Date;
|
|
79
|
+
/** Return true to render a specific day disabled (dimmed, taps ignored). */
|
|
80
|
+
isDateDisabled?: (date: Date) => boolean;
|
|
53
81
|
/**
|
|
54
82
|
* Smallest width a mini month may take before the grid drops a column
|
|
55
83
|
* (default 150). The grid fits 2–4 columns from the measured width.
|
|
@@ -59,6 +87,22 @@ export type YearViewProps<T = unknown> = SlotStyleProps<YearViewSlot> & {
|
|
|
59
87
|
onPressDay?: (date: Date) => void;
|
|
60
88
|
/** Tap a month's title (e.g. to jump to that month's view). */
|
|
61
89
|
onPressMonth?: (month: Date) => void;
|
|
90
|
+
/**
|
|
91
|
+
* Enables drag-to-select and reports the swept span live, as the ordered
|
|
92
|
+
* inclusive `[start, end]` days (both at midnight). Pair it with
|
|
93
|
+
* `useDateRange`'s `selectRange` to drive `selectedRange`. Fires on every day the
|
|
94
|
+
* drag crosses, so the highlight follows it; `onCreateEvent` (when also set)
|
|
95
|
+
* fires once on release. Either handler enables the same sweep: hold a day and
|
|
96
|
+
* drag on a device, press and drag on the web. On a device a sweep stays inside
|
|
97
|
+
* the mini month it started in; on the web it can run across months.
|
|
98
|
+
*/
|
|
99
|
+
onSelectDrag?: (start: Date, end: Date) => void;
|
|
100
|
+
/**
|
|
101
|
+
* Enables drag-to-create: sweep across days and release to fire this with the
|
|
102
|
+
* all-day range (`start` at midnight of the first day, `end` at midnight after
|
|
103
|
+
* the last, exclusive). A plain tap still fires `onPressDay`, not this.
|
|
104
|
+
*/
|
|
105
|
+
onCreateEvent?: (start: Date, end: Date) => void;
|
|
62
106
|
};
|
|
63
107
|
|
|
64
108
|
// Seed before the first layout pass so the grid renders immediately (and in
|
|
@@ -72,9 +116,16 @@ function YearViewInner<T>({
|
|
|
72
116
|
hiddenDays,
|
|
73
117
|
locale,
|
|
74
118
|
activeDate,
|
|
119
|
+
selectedRange,
|
|
120
|
+
selectedDates,
|
|
121
|
+
minDate,
|
|
122
|
+
maxDate,
|
|
123
|
+
isDateDisabled,
|
|
75
124
|
minMonthWidth = 150,
|
|
76
125
|
onPressDay,
|
|
77
126
|
onPressMonth,
|
|
127
|
+
onSelectDrag,
|
|
128
|
+
onCreateEvent,
|
|
78
129
|
classNames,
|
|
79
130
|
styles: styleOverrides,
|
|
80
131
|
}: YearViewProps<T>): ReactElement {
|
|
@@ -89,6 +140,12 @@ function YearViewInner<T>({
|
|
|
89
140
|
};
|
|
90
141
|
|
|
91
142
|
const months = useMemo(() => getYearMonths(date), [date]);
|
|
143
|
+
// Each mini month's week rows, built once per year rather than per render, so
|
|
144
|
+
// the drag hit-test (and the pan it is built into) keeps a stable identity.
|
|
145
|
+
const monthWeeks = useMemo(
|
|
146
|
+
() => months.map((month) => buildMonthWeeks(month, weekStartsOn, { hiddenDays })),
|
|
147
|
+
[months, weekStartsOn, hiddenDays],
|
|
148
|
+
);
|
|
92
149
|
// Weekday initials for one shared header per mini month.
|
|
93
150
|
const weekdayLabels = useMemo(
|
|
94
151
|
() =>
|
|
@@ -108,16 +165,94 @@ function YearViewInner<T>({
|
|
|
108
165
|
[events],
|
|
109
166
|
);
|
|
110
167
|
|
|
168
|
+
// ---- drag-to-select / create ---------------------------------------------
|
|
169
|
+
// One sweep gesture, reported live through `onSelectDrag` and committed to
|
|
170
|
+
// `onCreateEvent`. Native runs a pan over each mini month's grid (so a hold and
|
|
171
|
+
// drag sweeps its days); the web drives it from the cells' own pointer events,
|
|
172
|
+
// which lets a sweep run across mini months.
|
|
173
|
+
const sweepEnabled = onSelectDrag != null || onCreateEvent != null;
|
|
174
|
+
const [sweep, setSweep] = useState<{ anchor: number; hover: number } | null>(null);
|
|
175
|
+
const sweepRef = useRef(sweep);
|
|
176
|
+
sweepRef.current = sweep;
|
|
177
|
+
const movedRef = useRef(false);
|
|
178
|
+
// Web only: a committed sweep has to swallow the click the browser fires next,
|
|
179
|
+
// so it doesn't also open the day it landed on. A pan never leaves a press
|
|
180
|
+
// behind, so the flag would otherwise outlive its interaction and eat the next
|
|
181
|
+
// unrelated tap; every fresh sweep clears it. Mirrors MonthView.
|
|
182
|
+
const suppressPressRef = useRef(false);
|
|
183
|
+
|
|
184
|
+
const isSelectable = useCallback(
|
|
185
|
+
(day: Date) => isDateSelectable(day, { minDate, maxDate, isDateDisabled }),
|
|
186
|
+
[minDate, maxDate, isDateDisabled],
|
|
187
|
+
);
|
|
188
|
+
const beginSweep = useCallback((day: Date) => {
|
|
189
|
+
const time = startOfDay(day).getTime();
|
|
190
|
+
suppressPressRef.current = false;
|
|
191
|
+
movedRef.current = false;
|
|
192
|
+
setSweep({ anchor: time, hover: time });
|
|
193
|
+
}, []);
|
|
194
|
+
const extendSweep = useCallback(
|
|
195
|
+
(day: Date) => {
|
|
196
|
+
const current = sweepRef.current;
|
|
197
|
+
if (!current || !isSelectable(day)) return;
|
|
198
|
+
const time = startOfDay(day).getTime();
|
|
199
|
+
if (time === current.hover) return;
|
|
200
|
+
movedRef.current = true;
|
|
201
|
+
setSweep((c) => (c ? { ...c, hover: time } : c));
|
|
202
|
+
const [lo, hi] = current.anchor <= time ? [current.anchor, time] : [time, current.anchor];
|
|
203
|
+
onSelectDrag?.(new Date(lo), new Date(hi));
|
|
204
|
+
},
|
|
205
|
+
[isSelectable, onSelectDrag],
|
|
206
|
+
);
|
|
207
|
+
const finishSweep = useCallback(() => {
|
|
208
|
+
const current = sweepRef.current;
|
|
209
|
+
setSweep(null);
|
|
210
|
+
if (!current || !movedRef.current) return;
|
|
211
|
+
if (isWeb) suppressPressRef.current = true;
|
|
212
|
+
const range = monthCreateRange(new Date(current.anchor), new Date(current.hover));
|
|
213
|
+
onCreateEvent?.(range.start, range.end);
|
|
214
|
+
}, [onCreateEvent]);
|
|
215
|
+
|
|
216
|
+
// Web: a released pointer ends the sweep wherever it lands.
|
|
217
|
+
useEffect(() => {
|
|
218
|
+
if (!isWeb || !sweepEnabled) return;
|
|
219
|
+
const target = globalThis as unknown as {
|
|
220
|
+
addEventListener?: (type: string, cb: () => void) => void;
|
|
221
|
+
removeEventListener?: (type: string, cb: () => void) => void;
|
|
222
|
+
};
|
|
223
|
+
target.addEventListener?.("pointerup", finishSweep);
|
|
224
|
+
return () => target.removeEventListener?.("pointerup", finishSweep);
|
|
225
|
+
}, [sweepEnabled, finishSweep]);
|
|
226
|
+
|
|
227
|
+
// Swallow the click trailing a committed web sweep, so it doesn't open a day too.
|
|
228
|
+
const pressDay = useCallback(
|
|
229
|
+
(day: Date) => {
|
|
230
|
+
if (suppressPressRef.current) {
|
|
231
|
+
suppressPressRef.current = false;
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
onPressDay?.(day);
|
|
235
|
+
},
|
|
236
|
+
[onPressDay],
|
|
237
|
+
);
|
|
238
|
+
|
|
111
239
|
return (
|
|
112
|
-
<ScrollView
|
|
240
|
+
<ScrollView
|
|
241
|
+
onLayout={handleLayout}
|
|
242
|
+
showsVerticalScrollIndicator
|
|
243
|
+
// Native only: once the hold has armed a sweep, stop the list scrolling
|
|
244
|
+
// under the finger. On the web a press arms it immediately, so freezing
|
|
245
|
+
// there would cost every touch scroll; the browser arbitrates instead.
|
|
246
|
+
scrollEnabled={isWeb || sweep == null}
|
|
247
|
+
>
|
|
113
248
|
<View {...slot("grid", { base: styles.grid })}>
|
|
114
|
-
{months.map((month) => {
|
|
115
|
-
const weeks =
|
|
249
|
+
{months.map((month, monthIndex) => {
|
|
250
|
+
const weeks = monthWeeks[monthIndex];
|
|
116
251
|
const title = format(month, "MMMM", { locale });
|
|
117
252
|
const monthLabel = format(month, "MMMM yyyy", { locale });
|
|
118
253
|
const titleText = (
|
|
119
254
|
<Text
|
|
120
|
-
{...slot("monthTitle", {
|
|
255
|
+
{...slot<TextStyle>("monthTitle", {
|
|
121
256
|
base: styles.monthTitle,
|
|
122
257
|
themed: [styles.monthTitleText, { color: theme.colors.todayBackground }],
|
|
123
258
|
})}
|
|
@@ -155,7 +290,7 @@ function YearViewInner<T>({
|
|
|
155
290
|
{weekdayLabels.map((label, i) => (
|
|
156
291
|
<Text
|
|
157
292
|
key={i}
|
|
158
|
-
{...slot("weekday", {
|
|
293
|
+
{...slot<TextStyle>("weekday", {
|
|
159
294
|
base: styles.weekday,
|
|
160
295
|
themed: [styles.weekdayText, { color: theme.colors.textMuted }],
|
|
161
296
|
})}
|
|
@@ -165,82 +300,153 @@ function YearViewInner<T>({
|
|
|
165
300
|
</Text>
|
|
166
301
|
))}
|
|
167
302
|
</View>
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
303
|
+
<MiniMonthGrid
|
|
304
|
+
testID={`year-month-grid-${month.getMonth()}`}
|
|
305
|
+
month={month}
|
|
306
|
+
weeks={weeks}
|
|
307
|
+
sweepEnabled={sweepEnabled && !isWeb}
|
|
308
|
+
isSelectable={isSelectable}
|
|
309
|
+
onBeginSweep={beginSweep}
|
|
310
|
+
onExtendSweep={extendSweep}
|
|
311
|
+
onFinishSweep={finishSweep}
|
|
312
|
+
>
|
|
313
|
+
{weeks.map((week) => (
|
|
314
|
+
<View key={week[0].toISOString()} {...slot("week", { base: styles.week })}>
|
|
315
|
+
{week.map((day) => {
|
|
316
|
+
// Adjacent-month days keep the grid shape but stay blank,
|
|
317
|
+
// like the mini months of other year views.
|
|
318
|
+
if (day.getMonth() !== month.getMonth()) {
|
|
319
|
+
return <View key={day.toISOString()} style={styles.day} />;
|
|
320
|
+
}
|
|
321
|
+
const isToday = getIsToday(day);
|
|
322
|
+
const isHighlighted = activeDate
|
|
323
|
+
? isSameCalendarDay(day, activeDate)
|
|
324
|
+
: isToday;
|
|
325
|
+
const hasEvents = eventDays.has(startOfDay(day).toISOString());
|
|
326
|
+
// Selection/disabled flags come from core, so the year grid
|
|
327
|
+
// can't disagree with the month grid about a day's state.
|
|
328
|
+
const state = daySelectionState(
|
|
329
|
+
day,
|
|
330
|
+
{ selectedDates, selectedRange },
|
|
331
|
+
{ minDate, maxDate, isDateDisabled },
|
|
332
|
+
);
|
|
333
|
+
const dayTime = startOfDay(day).getTime();
|
|
334
|
+
const inSweep =
|
|
335
|
+
sweep != null &&
|
|
336
|
+
dayTime >= Math.min(sweep.anchor, sweep.hover) &&
|
|
337
|
+
dayTime <= Math.max(sweep.anchor, sweep.hover);
|
|
338
|
+
const label = `${format(day, "EEEE, d LLLL yyyy", { locale })}${
|
|
339
|
+
isToday ? ", today" : ""
|
|
340
|
+
}${state.isSelected ? ", selected" : ""}${
|
|
341
|
+
state.isDisabled ? ", unavailable" : ""
|
|
342
|
+
}${hasEvents ? ", has events" : ""}`;
|
|
343
|
+
// Today wins over a selection, matching the month grid.
|
|
344
|
+
const badge = dayBadgeKind(state, isHighlighted);
|
|
345
|
+
const daySlot = slot("day", { base: styles.day });
|
|
346
|
+
// The range draws as a band behind the badges. The mini cells
|
|
347
|
+
// sit flush in the row, so per-cell segments read as one strip;
|
|
348
|
+
// only the endpoints round their outer edge.
|
|
349
|
+
const showBand = rangeBandKind(state, true) !== "none";
|
|
350
|
+
const rounding = bandRounding(rangeBandKind(state, false));
|
|
351
|
+
const content = (
|
|
352
|
+
<>
|
|
353
|
+
{showBand || inSweep ? (
|
|
354
|
+
<View
|
|
355
|
+
{...slot("rangeBand", {
|
|
356
|
+
base: [
|
|
357
|
+
styles.rangeBand,
|
|
358
|
+
rounding.start && styles.rangeBandStart,
|
|
359
|
+
rounding.end && styles.rangeBandEnd,
|
|
360
|
+
],
|
|
361
|
+
themed: { backgroundColor: theme.colors.rangeBackground },
|
|
362
|
+
})}
|
|
363
|
+
/>
|
|
364
|
+
) : null}
|
|
208
365
|
<View
|
|
209
|
-
{...slot("
|
|
210
|
-
base: styles.
|
|
211
|
-
themed:
|
|
212
|
-
|
|
213
|
-
?
|
|
214
|
-
:
|
|
215
|
-
|
|
366
|
+
{...slot("dayBadge", {
|
|
367
|
+
base: styles.dayBadge,
|
|
368
|
+
themed:
|
|
369
|
+
badge === "none"
|
|
370
|
+
? undefined
|
|
371
|
+
: {
|
|
372
|
+
backgroundColor:
|
|
373
|
+
badge === "today"
|
|
374
|
+
? theme.colors.todayBackground
|
|
375
|
+
: theme.colors.selectedBackground,
|
|
376
|
+
borderRadius: theme.todayBadgeRadius,
|
|
377
|
+
},
|
|
216
378
|
})}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
379
|
+
>
|
|
380
|
+
<Text
|
|
381
|
+
{...slot<TextStyle>("dayText", {
|
|
382
|
+
themed: [
|
|
383
|
+
styles.dayText,
|
|
384
|
+
{
|
|
385
|
+
color: state.isDisabled
|
|
386
|
+
? theme.colors.textDisabled
|
|
387
|
+
: badge === "today"
|
|
388
|
+
? theme.colors.todayText
|
|
389
|
+
: badge === "selected"
|
|
390
|
+
? theme.colors.selectedText
|
|
391
|
+
: theme.colors.text,
|
|
392
|
+
},
|
|
393
|
+
],
|
|
394
|
+
})}
|
|
395
|
+
allowFontScaling={false}
|
|
396
|
+
>
|
|
397
|
+
{day.getDate()}
|
|
398
|
+
</Text>
|
|
399
|
+
</View>
|
|
400
|
+
{hasEvents ? (
|
|
401
|
+
<View
|
|
402
|
+
{...slot("eventDot", {
|
|
403
|
+
base: styles.eventDot,
|
|
404
|
+
themed: {
|
|
405
|
+
backgroundColor:
|
|
406
|
+
badge === "none"
|
|
407
|
+
? theme.colors.todayBackground
|
|
408
|
+
: theme.colors.todayText,
|
|
409
|
+
},
|
|
410
|
+
})}
|
|
411
|
+
/>
|
|
412
|
+
) : null}
|
|
413
|
+
</>
|
|
414
|
+
);
|
|
415
|
+
return onPressDay || sweepEnabled ? (
|
|
416
|
+
<Pressable
|
|
417
|
+
key={day.toISOString()}
|
|
418
|
+
onPress={() => pressDay(day)}
|
|
419
|
+
disabled={state.isDisabled}
|
|
420
|
+
accessibilityRole="button"
|
|
421
|
+
accessibilityLabel={label}
|
|
422
|
+
// Web only: the cells drive the sweep themselves, since a
|
|
423
|
+
// pan can't reach past the pressables there.
|
|
424
|
+
{...(isWeb && sweepEnabled
|
|
425
|
+
? {
|
|
426
|
+
onPointerDown: () => {
|
|
427
|
+
if (isSelectable(day)) beginSweep(day);
|
|
428
|
+
},
|
|
429
|
+
onPointerEnter: () => extendSweep(day),
|
|
430
|
+
}
|
|
431
|
+
: null)}
|
|
432
|
+
{...daySlot}
|
|
433
|
+
>
|
|
434
|
+
{content}
|
|
435
|
+
</Pressable>
|
|
436
|
+
) : (
|
|
437
|
+
<View
|
|
438
|
+
key={day.toISOString()}
|
|
439
|
+
accessible
|
|
440
|
+
accessibilityLabel={label}
|
|
441
|
+
{...daySlot}
|
|
442
|
+
>
|
|
443
|
+
{content}
|
|
444
|
+
</View>
|
|
445
|
+
);
|
|
446
|
+
})}
|
|
447
|
+
</View>
|
|
448
|
+
))}
|
|
449
|
+
</MiniMonthGrid>
|
|
244
450
|
</View>
|
|
245
451
|
);
|
|
246
452
|
})}
|
|
@@ -249,11 +455,84 @@ function YearViewInner<T>({
|
|
|
249
455
|
);
|
|
250
456
|
}
|
|
251
457
|
|
|
458
|
+
// One mini month's week rows, wrapped in a pan when drag-to-select is on. The
|
|
459
|
+
// gesture is bound per month (rather than to the whole year) because that keeps
|
|
460
|
+
// the hit-test to a single measured box, so a sweep runs across the days of the
|
|
461
|
+
// month it started in.
|
|
462
|
+
function MiniMonthGrid({
|
|
463
|
+
testID,
|
|
464
|
+
month,
|
|
465
|
+
weeks,
|
|
466
|
+
sweepEnabled,
|
|
467
|
+
isSelectable,
|
|
468
|
+
onBeginSweep,
|
|
469
|
+
onExtendSweep,
|
|
470
|
+
onFinishSweep,
|
|
471
|
+
children,
|
|
472
|
+
}: {
|
|
473
|
+
testID: string;
|
|
474
|
+
month: Date;
|
|
475
|
+
weeks: Date[][];
|
|
476
|
+
sweepEnabled: boolean;
|
|
477
|
+
isSelectable: (day: Date) => boolean;
|
|
478
|
+
onBeginSweep: (day: Date) => void;
|
|
479
|
+
onExtendSweep: (day: Date) => void;
|
|
480
|
+
onFinishSweep: () => void;
|
|
481
|
+
children: React.ReactNode;
|
|
482
|
+
}) {
|
|
483
|
+
const sizeRef = useRef({ width: 0, height: 0 });
|
|
484
|
+
const onLayout = useCallback((event: LayoutChangeEvent) => {
|
|
485
|
+
const { width, height } = event.nativeEvent.layout;
|
|
486
|
+
sizeRef.current = { width, height };
|
|
487
|
+
}, []);
|
|
488
|
+
// The gesture reads its inputs from here rather than closing over them, so a
|
|
489
|
+
// consumer's inline handler (or the re-render each swept day causes) can't
|
|
490
|
+
// rebuild the pan and cancel the drag that is running through it.
|
|
491
|
+
const latest = useRef({ month, weeks, isSelectable, onBeginSweep, onExtendSweep, onFinishSweep });
|
|
492
|
+
latest.current = { month, weeks, isSelectable, onBeginSweep, onExtendSweep, onFinishSweep };
|
|
493
|
+
// The day under a grid-relative point, or null for a point on a blanked
|
|
494
|
+
// adjacent-month cell or an unselectable day: neither can anchor a sweep or
|
|
495
|
+
// be swept onto. Mirrors MonthView's `hitTest`.
|
|
496
|
+
const dayAt = useCallback((x: number, y: number): Date | null => {
|
|
497
|
+
const { width, height } = sizeRef.current;
|
|
498
|
+
const { month: current, weeks: rows, isSelectable: selectable } = latest.current;
|
|
499
|
+
if (width <= 0 || height <= 0 || rows.length === 0) return null;
|
|
500
|
+
const row = Math.min(rows.length - 1, Math.max(0, Math.floor(y / (height / rows.length))));
|
|
501
|
+
const cols = rows[row].length;
|
|
502
|
+
const col = Math.min(cols - 1, Math.max(0, Math.floor(x / (width / cols))));
|
|
503
|
+
const day = rows[row][col];
|
|
504
|
+
if (!day) return null;
|
|
505
|
+
if (day.getMonth() !== current.getMonth()) return null;
|
|
506
|
+
return selectable(day) ? day : null;
|
|
507
|
+
}, []);
|
|
508
|
+
const gesture = useMemo<PanGesture | undefined>(() => {
|
|
509
|
+
if (!sweepEnabled) return undefined;
|
|
510
|
+
return Gesture.Pan()
|
|
511
|
+
.activateAfterLongPress(DRAG_HOLD_MS)
|
|
512
|
+
.runOnJS(true)
|
|
513
|
+
.onStart((event) => {
|
|
514
|
+
const day = dayAt(event.x, event.y);
|
|
515
|
+
if (day) latest.current.onBeginSweep(day);
|
|
516
|
+
})
|
|
517
|
+
.onUpdate((event) => {
|
|
518
|
+
const day = dayAt(event.x, event.y);
|
|
519
|
+
if (day) latest.current.onExtendSweep(day);
|
|
520
|
+
})
|
|
521
|
+
.onFinalize(() => latest.current.onFinishSweep());
|
|
522
|
+
}, [sweepEnabled, dayAt]);
|
|
523
|
+
const grid = (
|
|
524
|
+
<View testID={testID} onLayout={onLayout}>
|
|
525
|
+
{children}
|
|
526
|
+
</View>
|
|
527
|
+
);
|
|
528
|
+
return gesture ? <GestureDetector gesture={gesture}>{grid}</GestureDetector> : grid;
|
|
529
|
+
}
|
|
530
|
+
|
|
252
531
|
/**
|
|
253
532
|
* A year at a glance: the twelve months as compact mini grids, with today
|
|
254
533
|
* highlighted and a dot under days that hold events. Tap a day or a month
|
|
255
|
-
* title to drill into a denser view
|
|
256
|
-
* `mode="year"`.
|
|
534
|
+
* title to drill into a denser view, or hold and drag to sweep out a date
|
|
535
|
+
* range. It's the view `Calendar` renders for `mode="year"`.
|
|
257
536
|
*
|
|
258
537
|
* @example
|
|
259
538
|
* ```tsx
|
|
@@ -281,6 +560,15 @@ const styles = StyleSheet.create({
|
|
|
281
560
|
weekdayText: { fontSize: 9, fontWeight: "600" },
|
|
282
561
|
week: { flexDirection: "row" },
|
|
283
562
|
day: { flex: 1, aspectRatio: 1, alignItems: "center", justifyContent: "center" },
|
|
563
|
+
dayBadge: {
|
|
564
|
+
width: "100%",
|
|
565
|
+
height: "100%",
|
|
566
|
+
alignItems: "center",
|
|
567
|
+
justifyContent: "center",
|
|
568
|
+
},
|
|
284
569
|
dayText: { fontSize: 11 },
|
|
570
|
+
rangeBand: { position: "absolute", top: 0, bottom: 0, left: 0, right: 0 },
|
|
571
|
+
rangeBandStart: { borderTopLeftRadius: 999, borderBottomLeftRadius: 999 },
|
|
572
|
+
rangeBandEnd: { borderTopRightRadius: 999, borderBottomRightRadius: 999 },
|
|
285
573
|
eventDot: { position: "absolute", bottom: 1, width: 3, height: 3, borderRadius: 2 },
|
|
286
574
|
});
|
package/src/types.ts
CHANGED
|
@@ -26,6 +26,25 @@ export type {
|
|
|
26
26
|
WeekStartsOn,
|
|
27
27
|
} from "@super-calendar/core";
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Called when an event is dragged (moved or resized) to new start/end times.
|
|
31
|
+
* Return `false` to reject the drop — the event snaps back to where it started
|
|
32
|
+
* (e.g. to forbid overlaps or out-of-bounds slots). Any other return accepts it.
|
|
33
|
+
*/
|
|
34
|
+
export type EventDragHandler<T> = (
|
|
35
|
+
event: CalendarEvent<T>,
|
|
36
|
+
start: Date,
|
|
37
|
+
end: Date,
|
|
38
|
+
) => void | boolean;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Called when a move or resize gesture begins, before any change is committed:
|
|
42
|
+
* on grab for a move (after the long-press), and when the resize drag starts.
|
|
43
|
+
* Handy for haptic feedback (e.g. `expo-haptics`). Requires drag to be enabled
|
|
44
|
+
* via `onDragEvent`.
|
|
45
|
+
*/
|
|
46
|
+
export type EventDragStartHandler<T> = (event: CalendarEvent<T>) => void;
|
|
47
|
+
|
|
29
48
|
// The React Native render contract lives here, not in core: it references
|
|
30
49
|
// Reanimated (`SharedValue`) and React Native (`ViewStyle`) types, which must
|
|
31
50
|
// not leak into the platform-free core.
|
package/src/utils/slots.ts
CHANGED
|
@@ -15,7 +15,17 @@
|
|
|
15
15
|
// prop React Native silently ignores, so the props are safe to pass everywhere.
|
|
16
16
|
|
|
17
17
|
import { createContext, createElement, type ReactNode, useContext, useMemo } from "react";
|
|
18
|
-
import type { StyleProp, TextStyle } from "react-native";
|
|
18
|
+
import type { ImageStyle, StyleProp, TextStyle, ViewStyle } from "react-native";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The style shapes a slot can resolve to. A slot carries the style type of the
|
|
22
|
+
* element it is spread on, because React Native's style types stop being
|
|
23
|
+
* mutually assignable as soon as a consumer's types diverge them: Expo's
|
|
24
|
+
* react-native-web types widen `cursor` on `TextStyle` and `userSelect` on
|
|
25
|
+
* `ViewStyle`, so a `View` slot typed as `TextStyle` fails to compile in any
|
|
26
|
+
* Expo app.
|
|
27
|
+
*/
|
|
28
|
+
export type SlotStyle = ViewStyle | TextStyle | ImageStyle;
|
|
19
29
|
|
|
20
30
|
/**
|
|
21
31
|
* Per-slot styling overrides. Give a slot a Tailwind class (uniwind/NativeWind)
|
|
@@ -30,20 +40,20 @@ export interface SlotStyleProps<Slot extends string> {
|
|
|
30
40
|
*/
|
|
31
41
|
classNames?: Partial<Record<Slot, string>>;
|
|
32
42
|
/** Style overrides per slot, merged last (win over defaults and classes). */
|
|
33
|
-
styles?: Partial<Record<Slot, StyleProp<
|
|
43
|
+
styles?: Partial<Record<Slot, StyleProp<SlotStyle>>>;
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
/** A slot's built-in styling, split so classes can replace the look but not the layout. */
|
|
37
|
-
export interface SlotDefault {
|
|
47
|
+
export interface SlotDefault<Style extends SlotStyle = ViewStyle> {
|
|
38
48
|
/** Structural styles kept even when a class is supplied. */
|
|
39
|
-
base?: StyleProp<
|
|
49
|
+
base?: StyleProp<Style>;
|
|
40
50
|
/** Themed styles (colour, type, spacing) dropped when a class is supplied. */
|
|
41
|
-
themed?: StyleProp<
|
|
51
|
+
themed?: StyleProp<Style>;
|
|
42
52
|
}
|
|
43
53
|
|
|
44
54
|
/** The props a resolved slot spreads onto an element. */
|
|
45
|
-
export interface ResolvedSlot {
|
|
46
|
-
style: StyleProp<
|
|
55
|
+
export interface ResolvedSlot<Style extends SlotStyle = ViewStyle> {
|
|
56
|
+
style: StyleProp<Style>;
|
|
47
57
|
className?: string;
|
|
48
58
|
}
|
|
49
59
|
|
|
@@ -53,10 +63,24 @@ export interface ResolvedSlot {
|
|
|
53
63
|
* consumer supplied one, a `className` for the Tailwind runtime to pick up.
|
|
54
64
|
*/
|
|
55
65
|
export function createSlots<Slot extends string>({ classNames, styles }: SlotStyleProps<Slot>) {
|
|
56
|
-
|
|
66
|
+
// `NoInfer` keeps the element type a decision, not a guess: without it the
|
|
67
|
+
// literal type of a `StyleSheet.create` entry wins the inference and every
|
|
68
|
+
// themed style passed alongside it is then measured against that one entry.
|
|
69
|
+
// Slots default to a `View`; `slot<TextStyle>(...)` marks the text ones.
|
|
70
|
+
return <Style extends SlotStyle = ViewStyle>(
|
|
71
|
+
name: Slot,
|
|
72
|
+
defaults?: SlotDefault<NoInfer<Style>>,
|
|
73
|
+
): ResolvedSlot<Style> => {
|
|
57
74
|
const slotClass = classNames?.[name];
|
|
58
75
|
return {
|
|
59
|
-
style: [
|
|
76
|
+
style: [
|
|
77
|
+
defaults?.base,
|
|
78
|
+
slotClass ? undefined : defaults?.themed,
|
|
79
|
+
// A consumer's override is declared for any element's style, so it is
|
|
80
|
+
// narrowed to this slot's element here. React Native flattens the array
|
|
81
|
+
// and drops properties the element does not support either way.
|
|
82
|
+
styles?.[name] as StyleProp<Style>,
|
|
83
|
+
],
|
|
60
84
|
...(slotClass ? { className: slotClass } : null),
|
|
61
85
|
};
|
|
62
86
|
};
|