@super-calendar/native 2.16.0 → 2.18.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.
@@ -4,9 +4,20 @@ import {
4
4
  type LegendListRenderItemProps,
5
5
  type OnViewableItemsChangedInfo,
6
6
  } from "@legendapp/list/react-native";
7
- import { addMonths, differenceInCalendarMonths, format, type Locale, startOfMonth } from "date-fns";
7
+ import {
8
+ addDays,
9
+ addMonths,
10
+ addWeeks,
11
+ differenceInCalendarMonths,
12
+ differenceInCalendarWeeks,
13
+ format,
14
+ type Locale,
15
+ startOfMonth,
16
+ startOfWeek,
17
+ } from "date-fns";
8
18
  import {
9
19
  type ComponentRef,
20
+ type ComponentType,
10
21
  memo,
11
22
  type ReactElement,
12
23
  useCallback,
@@ -36,7 +47,9 @@ import type {
36
47
  } from "../types";
37
48
  import {
38
49
  type CalendarSelection,
50
+ type CreateGhostArgs,
39
51
  CalendarSelectionProvider,
52
+ buildMultiWeekGrid,
40
53
  type DateRange,
41
54
  type WeekdayFormat,
42
55
  filterHiddenDays,
@@ -46,7 +59,7 @@ import {
46
59
  import type { CalendarLabels } from "@super-calendar/core";
47
60
  import { createSlots, type SlotStyleProps } from "../utils/slots";
48
61
  import { useWebPagerKeys } from "../utils/useWebPagerKeys";
49
- import { MonthView, type MonthViewSlot } from "./MonthView";
62
+ import { DateRows, type DateRowsSource, type MonthViewSlot } from "./MonthView";
50
63
 
51
64
  // Horizontal swipe paging doesn't translate to web; there we disable it and page
52
65
  // with the arrow keys instead.
@@ -75,6 +88,8 @@ export type MonthPagerProps<T> = {
75
88
  hiddenDays?: number[];
76
89
  weekdayFormat?: WeekdayFormat;
77
90
  locale?: Locale;
91
+ now?: Date;
92
+ timeZone?: string;
78
93
  sortedMonthView?: boolean;
79
94
  moreLabel?: string;
80
95
  /** Translations for the screen-reader words and gutter text; omitted keys use English. */
@@ -95,6 +110,8 @@ export type MonthPagerProps<T> = {
95
110
  onPressMore?: (events: CalendarEvent<T>[], date: Date) => void;
96
111
  /** Drag across empty day cells to sweep out an all-day span for a new event. */
97
112
  onCreateEvent?: (start: Date, end: Date) => void;
113
+ /** Content for each row of the live creation preview; requires `onCreateEvent`. */
114
+ renderCreateGhost?: ComponentType<CreateGhostArgs>;
98
115
  /** Drag-to-select: reports the swept `[start, end]` days live. */
99
116
  onSelectDrag?: (start: Date, end: Date) => void;
100
117
  /** Days drawn as selected (a filled badge). */
@@ -130,7 +147,41 @@ export type MonthPagerProps<T> = {
130
147
  renderCustomDateForMonth?: (date: Date) => React.ReactNode;
131
148
  } & SlotStyleProps<MonthViewSlot>;
132
149
 
150
+ /**
151
+ * Which pages a {@link DateRowsPager} swipes through. Internal to the package:
152
+ * the public pagers and `Calendar` adapt their own props to it.
153
+ */
154
+ export type DateRowsPagerSpec =
155
+ | {
156
+ kind: "month";
157
+ showAdjacentMonths: boolean;
158
+ showSixWeeks: boolean;
159
+ }
160
+ | { kind: "multiweek"; numberOfWeeks: number };
161
+
162
+ /** Props for the private {@link DateRowsPager}. */
163
+ export type DateRowsPagerProps<T> = Omit<
164
+ MonthPagerProps<T>,
165
+ "showAdjacentMonths" | "showSixWeeks"
166
+ > & {
167
+ spec: DateRowsPagerSpec;
168
+ };
169
+
133
170
  function MonthPagerInner<T>({
171
+ showAdjacentMonths = true,
172
+ showSixWeeks = false,
173
+ ...rest
174
+ }: MonthPagerProps<T>): ReactElement {
175
+ const spec = useMemo<DateRowsPagerSpec>(
176
+ () => ({ kind: "month", showAdjacentMonths, showSixWeeks }),
177
+ [showAdjacentMonths, showSixWeeks],
178
+ );
179
+ return <DateRowsPager<T> {...rest} spec={spec} />;
180
+ }
181
+
182
+ function DateRowsPagerInner<T>({
183
+ spec,
184
+ initialAnchor,
134
185
  date,
135
186
  events,
136
187
  maxVisibleEventCount,
@@ -140,10 +191,11 @@ function MonthPagerInner<T>({
140
191
  hiddenDays,
141
192
  weekdayFormat = "short",
142
193
  locale,
194
+ now,
195
+ timeZone,
143
196
  sortedMonthView,
144
197
  moreLabel,
145
198
  labels,
146
- showAdjacentMonths,
147
199
  highlightWeekends,
148
200
  disableMonthEventCellPress,
149
201
  isRTL,
@@ -157,6 +209,7 @@ function MonthPagerInner<T>({
157
209
  onLongPressEvent,
158
210
  onPressMore,
159
211
  onCreateEvent,
212
+ renderCreateGhost,
160
213
  onSelectDrag,
161
214
  selectedDates,
162
215
  selectedRange,
@@ -171,14 +224,13 @@ function MonthPagerInner<T>({
171
224
  onChangeDate,
172
225
  freeSwipe = false,
173
226
  swipeEnabled = true,
174
- showSixWeeks = false,
175
227
  showTitle = true,
176
228
  activeDate,
177
229
  renderHeaderForMonthView,
178
230
  renderCustomDateForMonth,
179
231
  classNames,
180
232
  styles: styleOverrides,
181
- }: MonthPagerProps<T>): ReactElement {
233
+ }: DateRowsPagerProps<T> & { initialAnchor?: Date }): ReactElement {
182
234
  const theme = useCalendarTheme();
183
235
  // Stable across renders (it feeds the memoized renderItem below).
184
236
  const slot = useMemo(
@@ -221,15 +273,37 @@ function MonthPagerInner<T>({
221
273
  // A fixed window of months, anchored once and aligned to the month start. The
222
274
  // array never shifts as the date changes, so paging never re-renders a page's
223
275
  // content — LegendList virtualises and keys by month.
224
- const [anchorDate] = useState(date);
225
- const anchor = useMemo(() => startOfMonth(anchorDate), [anchorDate]);
226
- const monthDates = useMemo(
227
- () => Array.from({ length: PAGE_WINDOW * 2 + 1 }, (_, i) => addMonths(anchor, i - PAGE_WINDOW)),
228
- [anchor],
276
+ const [anchorDate] = useState(initialAnchor ?? date);
277
+ const numberOfWeeks = spec.kind === "multiweek" ? spec.numberOfWeeks : undefined;
278
+ const anchor = useMemo(
279
+ () =>
280
+ numberOfWeeks === undefined
281
+ ? startOfMonth(anchorDate)
282
+ : startOfWeek(anchorDate, { weekStartsOn }),
283
+ [anchorDate, numberOfWeeks, weekStartsOn],
229
284
  );
285
+ const monthDates = useMemo(() => {
286
+ const dates = Array.from({ length: PAGE_WINDOW * 2 + 1 }, (_, i) =>
287
+ numberOfWeeks === undefined
288
+ ? addMonths(anchor, i - PAGE_WINDOW)
289
+ : addWeeks(anchor, (i - PAGE_WINDOW) * numberOfWeeks),
290
+ );
291
+ // The requested window is valid, but preloaded neighbours can run past
292
+ // Date's endpoints. Keep only complete representable multi-week pages.
293
+ return numberOfWeeks === undefined
294
+ ? dates
295
+ : dates.filter(
296
+ (page) =>
297
+ Number.isFinite(page.getTime()) &&
298
+ Number.isFinite(addWeeks(page, numberOfWeeks).getTime()),
299
+ );
300
+ }, [anchor, numberOfWeeks]);
230
301
  const indexOfMonth = useCallback(
231
- (target: Date) => differenceInCalendarMonths(startOfMonth(target), anchor) + PAGE_WINDOW,
232
- [anchor],
302
+ (target: Date) =>
303
+ numberOfWeeks === undefined
304
+ ? differenceInCalendarMonths(startOfMonth(target), anchor) + PAGE_WINDOW
305
+ : differenceInCalendarWeeks(target, monthDates[0], { weekStartsOn }) / numberOfWeeks,
306
+ [anchor, monthDates, numberOfWeeks, weekStartsOn],
233
307
  );
234
308
 
235
309
  // The committed month's page is the centred/active one. Derived (not stored)
@@ -305,16 +379,37 @@ function MonthPagerInner<T>({
305
379
  // `fillCellOnSelection` and `onSelectDrag` ride along because they reach the
306
380
  // grids as renderItem props, not through the selection context below: toggling
307
381
  // a "pick dates" mode on must reach the pages already rendered.
308
- const listExtraData = useMemo(
382
+ // `now` and `timeZone` move the today highlight on the pages already rendered.
383
+ const nowTime = now?.getTime();
384
+ const monthExtraData = useMemo(
309
385
  () => ({
310
386
  events,
311
387
  activeDate,
388
+ nowTime,
389
+ timeZone,
312
390
  fillCellOnSelection,
313
391
  dragSelect: onSelectDrag != null,
314
392
  chipHeight,
315
393
  chipGap,
394
+ renderCreateGhost,
395
+ onCreateEvent,
396
+ createGhostClassName: classNames?.createGhost,
397
+ createGhostStyle: styleOverrides?.createGhost,
316
398
  }),
317
- [events, activeDate, fillCellOnSelection, onSelectDrag, chipHeight, chipGap],
399
+ [
400
+ events,
401
+ activeDate,
402
+ nowTime,
403
+ timeZone,
404
+ fillCellOnSelection,
405
+ onSelectDrag,
406
+ chipHeight,
407
+ chipGap,
408
+ renderCreateGhost,
409
+ onCreateEvent,
410
+ classNames?.createGhost,
411
+ styleOverrides?.createGhost,
412
+ ],
318
413
  );
319
414
 
320
415
  // Selection reaches the grids through context, not `renderItem`, so a change
@@ -325,14 +420,28 @@ function MonthPagerInner<T>({
325
420
  [selectedDates, selectedRange, minDate, maxDate, isDateDisabled],
326
421
  );
327
422
 
423
+ // One source object per page date, so a memoized page skips re-rendering when
424
+ // the list calls `renderItem` again with unchanged inputs.
425
+ const sourceFor = useMemo(() => {
426
+ const cache = new WeakMap<Date, DateRowsSource>();
427
+ return (item: Date): DateRowsSource => {
428
+ let source = cache.get(item);
429
+ if (!source) {
430
+ source = { ...spec, date: item };
431
+ cache.set(item, source);
432
+ }
433
+ return source;
434
+ };
435
+ }, [spec]);
436
+
328
437
  const snapToIndices = useMemo(() => monthDates.map((_, index) => index), [monthDates]);
329
438
  const keyExtractorList = useCallback((item: Date) => item.toISOString(), []);
330
439
  const getFixedItemSize = useCallback(() => containerWidth, [containerWidth]);
331
440
  const renderItem = useCallback(
332
441
  ({ item }: LegendListRenderItemProps<Date>) => (
333
442
  <View style={{ width: containerWidth, height: pageHeight }}>
334
- <MonthView
335
- date={item}
443
+ <DateRows
444
+ source={sourceFor(item)}
336
445
  events={events}
337
446
  // The pager shows one shared weekday header and the month title above it
338
447
  // (see below), so each page's grid omits its own title and weekday row.
@@ -344,14 +453,14 @@ function MonthPagerInner<T>({
344
453
  weekStartsOn={weekStartsOn}
345
454
  hiddenDays={hiddenDays}
346
455
  locale={locale}
456
+ now={now}
457
+ timeZone={timeZone}
347
458
  sortedMonthView={sortedMonthView}
348
459
  moreLabel={moreLabel}
349
460
  labels={labels}
350
- showAdjacentMonths={showAdjacentMonths}
351
461
  highlightWeekends={highlightWeekends}
352
462
  disableMonthEventCellPress={disableMonthEventCellPress}
353
463
  isRTL={isRTL}
354
- showSixWeeks={showSixWeeks}
355
464
  activeDate={activeDate}
356
465
  calendarCellStyle={calendarCellStyle}
357
466
  renderEvent={renderEvent}
@@ -363,6 +472,7 @@ function MonthPagerInner<T>({
363
472
  onLongPressEvent={onLongPressEvent}
364
473
  onPressMore={onPressMore}
365
474
  onCreateEvent={onCreateEvent}
475
+ renderCreateGhost={renderCreateGhost}
366
476
  onSelectDrag={onSelectDrag}
367
477
  fillCellOnSelection={fillCellOnSelection}
368
478
  onDragEvent={onDragEvent}
@@ -385,14 +495,15 @@ function MonthPagerInner<T>({
385
495
  weekStartsOn,
386
496
  hiddenDays,
387
497
  locale,
498
+ now,
499
+ timeZone,
388
500
  sortedMonthView,
389
501
  moreLabel,
390
502
  labels,
391
- showAdjacentMonths,
503
+ sourceFor,
392
504
  highlightWeekends,
393
505
  disableMonthEventCellPress,
394
506
  isRTL,
395
- showSixWeeks,
396
507
  activeDate,
397
508
  calendarCellStyle,
398
509
  renderEvent,
@@ -404,6 +515,7 @@ function MonthPagerInner<T>({
404
515
  onLongPressEvent,
405
516
  onPressMore,
406
517
  onCreateEvent,
518
+ renderCreateGhost,
407
519
  onSelectDrag,
408
520
  fillCellOnSelection,
409
521
  onDragEvent,
@@ -416,6 +528,24 @@ function MonthPagerInner<T>({
416
528
  ],
417
529
  );
418
530
 
531
+ // The multi-week view invalidates cached pages for every input that reaches
532
+ // renderItem, including callbacks and layout. LegendList does not track a new
533
+ // renderItem identity on its own.
534
+ const listExtraData = useMemo(
535
+ () => (numberOfWeeks === undefined ? monthExtraData : { renderItem }),
536
+ [numberOfWeeks, monthExtraData, renderItem],
537
+ );
538
+ const visible = useMemo(
539
+ () =>
540
+ numberOfWeeks === undefined
541
+ ? null
542
+ : buildMultiWeekGrid(date, { numberOfWeeks, weekStartsOn, hiddenDays }).visibleRange,
543
+ [date, numberOfWeeks, weekStartsOn, hiddenDays],
544
+ );
545
+ const title = visible
546
+ ? `${format(visible.start, "PP", { locale })} – ${format(addDays(visible.endExclusive, -1), "PP", { locale })}`
547
+ : format(date, "MMMM yyyy", { locale });
548
+
419
549
  return (
420
550
  <CalendarSelectionProvider value={selection}>
421
551
  <View ref={containerRef} style={[styles.container, theme.containers.monthContainer]}>
@@ -429,7 +559,7 @@ function MonthPagerInner<T>({
429
559
  })}
430
560
  allowFontScaling={false}
431
561
  >
432
- {format(date, "MMMM yyyy", locale ? { locale } : undefined)}
562
+ {title}
433
563
  </Text>
434
564
  ) : null}
435
565
  {renderHeaderForMonthView ? (
@@ -506,6 +636,67 @@ function MonthPagerInner<T>({
506
636
  */
507
637
  export const MonthPager = memo(MonthPagerInner) as typeof MonthPagerInner;
508
638
 
639
+ /** The swipeable pager behind `MonthPager` and `Calendar`'s row modes. */
640
+ function DateRowsPagerRoot<T>(props: DateRowsPagerProps<T>): ReactElement {
641
+ const count = props.spec.kind === "multiweek" ? props.spec.numberOfWeeks : undefined;
642
+ const target = useMemo(
643
+ () =>
644
+ count === undefined
645
+ ? props.date
646
+ : buildMultiWeekGrid(props.date, { numberOfWeeks: count, weekStartsOn: props.weekStartsOn })
647
+ .period.start,
648
+ [props.date, count, props.weekStartsOn],
649
+ );
650
+ const [series, setSeries] = useState(() => ({
651
+ anchor: target,
652
+ count,
653
+ weekStartsOn: props.weekStartsOn,
654
+ }));
655
+ let current = series;
656
+ if (count !== undefined) {
657
+ const offset =
658
+ differenceInCalendarWeeks(target, series.anchor, { weekStartsOn: props.weekStartsOn }) /
659
+ count;
660
+ if (
661
+ series.count !== count ||
662
+ series.weekStartsOn !== props.weekStartsOn ||
663
+ !Number.isInteger(offset) ||
664
+ Math.abs(offset) > PAGE_WINDOW
665
+ ) {
666
+ current = { anchor: target, count, weekStartsOn: props.weekStartsOn };
667
+ setSeries(current);
668
+ }
669
+ }
670
+ const key =
671
+ count === undefined ? "month" : `${current.anchor.getTime()}:${count}:${props.weekStartsOn}`;
672
+ const generation = useRef(key);
673
+ generation.current = key;
674
+ useEffect(() => {
675
+ generation.current = key;
676
+ return () => {
677
+ generation.current = "unmounted";
678
+ };
679
+ }, [key]);
680
+ const onChangeDate = useCallback(
681
+ (date: Date) => {
682
+ // A remounted series can still receive a queued viewability callback from
683
+ // the old list. Never let it change the controlled date.
684
+ if (generation.current === key) props.onChangeDate(date);
685
+ },
686
+ [key, props.onChangeDate],
687
+ );
688
+ return (
689
+ <DateRowsPagerInner
690
+ {...props}
691
+ key={key}
692
+ initialAnchor={count === undefined ? undefined : current.anchor}
693
+ onChangeDate={onChangeDate}
694
+ />
695
+ );
696
+ }
697
+
698
+ export const DateRowsPager = memo(DateRowsPagerRoot) as typeof DateRowsPagerRoot;
699
+
509
700
  type MonthWeekdayHeaderProps = {
510
701
  weekDays: Date[];
511
702
  weekdayFormat?: WeekdayFormat;