@super-calendar/native 2.7.0 → 2.8.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.
@@ -1,6 +1,7 @@
1
1
  import { format, type Locale, isSameMonth, startOfDay } from "date-fns";
2
2
  import { memo, type ReactElement, useMemo, useState } from "react";
3
3
  import {
4
+ type DimensionValue,
4
5
  type LayoutChangeEvent,
5
6
  Modal,
6
7
  Platform,
@@ -39,6 +40,7 @@ import {
39
40
  isWeekend,
40
41
  } from "@super-calendar/core";
41
42
  import { monthEventCapacity, monthVisibleCount } from "@super-calendar/core";
43
+ import { layoutMonthWeek, type MonthWeekEvents } from "@super-calendar/core";
42
44
  import {
43
45
  compareDayEvents,
44
46
  groupEventsByDay,
@@ -54,12 +56,41 @@ const DATE_BADGE_HEIGHT = 24;
54
56
  const BAND_CENTER_Y = DAY_CELL_PADDING_TOP + DATE_BADGE_HEIGHT / 2;
55
57
  const CELL_ROW_GAP = 2;
56
58
  const CHIP_PADDING_V = 2;
59
+ // Where the first event row sits below the date badge (padding + badge + gap).
60
+ const BADGE_AREA = DAY_CELL_PADDING_TOP + DATE_BADGE_HEIGHT + CELL_ROW_GAP;
61
+ // Stable empty layout for the events-free picker (avoids a fresh object per render).
62
+ const EMPTY_LAYOUT = { segments: [], laneCount: 0 } as const;
63
+ // Horizontal inset of a chip within its cell (mirrors `styles.monthEvent`).
64
+ const CHIP_INSET_H = 4;
57
65
  // Pre-measure fallback so the first paint isn't empty or overflowing.
58
66
  const FALLBACK_VISIBLE_COUNT = 3;
59
67
 
60
68
  const numericStyle = (value: number | string | undefined, fallback: number) =>
61
69
  typeof value === "number" ? value : fallback;
62
70
 
71
+ // The distinct events touching a week row, in the per-day index's sorted order.
72
+ // A multi-day event lives in each covered day's bucket, so dedupe by reference.
73
+ function collectRowEvents<T>(
74
+ week: Date[],
75
+ eventsByDay: ReadonlyMap<string, CalendarEvent<T>[]>,
76
+ ): CalendarEvent<T>[] {
77
+ const seen = new Set<CalendarEvent<T>>();
78
+ const out: CalendarEvent<T>[] = [];
79
+ for (const day of week) {
80
+ const list = eventsByDay.get(startOfDay(day).toISOString());
81
+ if (!list) continue;
82
+ for (const event of list) {
83
+ if (!seen.has(event)) {
84
+ seen.add(event);
85
+ out.push(event);
86
+ }
87
+ }
88
+ }
89
+ return out;
90
+ }
91
+
92
+ const pct = (n: number): DimensionValue => `${n}%` as DimensionValue;
93
+
63
94
  /**
64
95
  * The styleable parts of {@link MonthView}. Mirrors the dom renderer's slot
65
96
  * names where the structure matches; `dayBadgeText` is native-only (React
@@ -84,9 +115,12 @@ export type MonthViewProps<T> = SlotStyleProps<MonthViewSlot> & {
84
115
  date: Date;
85
116
  events: CalendarEvent<T>[];
86
117
  /**
87
- * Max event chips per day cell. Omit to auto-fit as many as the cell height
88
- * allows (the default); set a number for a fixed cap. Extra events collapse
89
- * into a "+N more" label. Auto-fit assumes the built-in chip size — pass an
118
+ * Max event lanes (stacked rows) shown per week before the rest of a day's
119
+ * events collapse into a "+N more" label. Because multi-day events draw as one
120
+ * bar spanning a lane across the week, this caps lanes per row, not chips per
121
+ * day, so a long bar can push a lightly-booked day's own events into "+N more".
122
+ * Omit to auto-fit as many lanes as the cell height allows (the default); set a
123
+ * number for a fixed cap. Auto-fit assumes the built-in chip size — pass an
90
124
  * explicit value when using a custom `renderEvent`.
91
125
  */
92
126
  maxVisibleEventCount?: number;
@@ -235,6 +269,16 @@ function MonthViewInner<T>({
235
269
  return isRTL ? days.reverse() : days;
236
270
  }, [date, weekStartsOn, isRTL, hiddenDays]);
237
271
 
272
+ // The built-in chip's height and per-row stride, from the theme's title type.
273
+ // Shared by the capacity estimate and the spanning-bar overlay so the bars line
274
+ // up exactly with the rows the cells reserve for them.
275
+ const chipMetrics = useMemo(() => {
276
+ const fontSize = numericStyle(theme.text.eventTitle.fontSize, 12);
277
+ const lineHeight = numericStyle(theme.text.eventTitle.lineHeight, Math.ceil(fontSize * 1.3));
278
+ const chipHeight = lineHeight + CHIP_PADDING_V * 2;
279
+ return { chipHeight, chipRowHeight: chipHeight + CELL_ROW_GAP };
280
+ }, [theme]);
281
+
238
282
  // How many chips fit per cell: a fixed cap when `maxVisibleEventCount` is set,
239
283
  // else derived from the measured cell height and the (default) chip metrics.
240
284
  const capacity = useMemo(() => {
@@ -245,14 +289,11 @@ function MonthViewInner<T>({
245
289
  return { full: FALLBACK_VISIBLE_COUNT, withMore: FALLBACK_VISIBLE_COUNT };
246
290
  }
247
291
  const rowHeight = gridHeight / weeks.length;
248
- const fontSize = numericStyle(theme.text.eventTitle.fontSize, 12);
249
- const lineHeight = numericStyle(theme.text.eventTitle.lineHeight, Math.ceil(fontSize * 1.3));
250
- const chipRowHeight = lineHeight + CHIP_PADDING_V * 2 + CELL_ROW_GAP;
251
292
  const moreFontSize = numericStyle(theme.text.more.fontSize, 11);
252
293
  const moreRowHeight = Math.ceil(moreFontSize * 1.3) + CELL_ROW_GAP;
253
294
  const available = rowHeight - DAY_CELL_PADDING_TOP - DATE_BADGE_HEIGHT;
254
- return monthEventCapacity(available, chipRowHeight, moreRowHeight);
255
- }, [maxVisibleEventCount, gridHeight, weeks.length, theme]);
295
+ return monthEventCapacity(available, chipMetrics.chipRowHeight, moreRowHeight);
296
+ }, [maxVisibleEventCount, gridHeight, weeks.length, theme, chipMetrics]);
256
297
 
257
298
  // Group events by calendar day once per `events` change (shared with the dom
258
299
  // renderer via core's `groupEventsByDay`), rather than scanning the whole list
@@ -272,7 +313,21 @@ function MonthViewInner<T>({
272
313
  // picker reads cleaner without it (matching the dom renderer).
273
314
  const showGrid = events.length > 0;
274
315
 
275
- const renderDay = (day: Date) => {
316
+ // Lay out each week row's spanning bars once per data change, not per render.
317
+ const weekLayouts = useMemo(
318
+ () =>
319
+ showGrid
320
+ ? weeks.map((week) => layoutMonthWeek(week, collectRowEvents(week, eventsByDay)))
321
+ : [],
322
+ [showGrid, weeks, eventsByDay],
323
+ );
324
+
325
+ const renderDay = (
326
+ day: Date,
327
+ dayCol: number,
328
+ rowLayout: MonthWeekEvents<T>,
329
+ visibleLanes: number,
330
+ ) => {
276
331
  const isCurrentMonth = isSameMonth(day, date);
277
332
 
278
333
  // Blank out adjacent-month days when they're hidden, keeping the grid shape.
@@ -308,8 +363,12 @@ function MonthViewInner<T>({
308
363
  { selectedDates, selectedRange },
309
364
  { minDate, maxDate, isDateDisabled },
310
365
  );
311
- const visibleCount = monthVisibleCount(dayEvents.length, capacity);
312
- const hiddenCount = dayEvents.length - visibleCount;
366
+ // Events past the visible lanes (this day's segments the row can't show)
367
+ // collapse into "+N more"; the popover still lists the whole day.
368
+ const hiddenEvents = rowLayout.segments
369
+ .filter((s) => s.startCol <= dayCol && s.endCol >= dayCol && s.lane >= visibleLanes)
370
+ .map((s) => s.event);
371
+ const hiddenCount = hiddenEvents.length;
313
372
 
314
373
  // The range shows as a band behind the days; endpoints and discrete selected
315
374
  // days get a filled badge on top. Today's badge wins when it coincides. The
@@ -476,23 +535,14 @@ function MonthViewInner<T>({
476
535
  </Text>
477
536
  </View>
478
537
  )}
479
- {dayEvents.slice(0, visibleCount).map((event, index) => (
538
+ {/* Reserve one row per visible lane so the overlay bars have space and
539
+ "+more" sits below them; the bars themselves render in the row overlay
540
+ (they span cells). */}
541
+ {Array.from({ length: visibleLanes }, (_, i) => (
480
542
  <View
481
- key={keyExtractor(event, index)}
482
- style={[styles.monthEvent, theme.containers.monthEvent]}
483
- >
484
- <RenderEventComponent
485
- event={event}
486
- mode="month"
487
- isAllDay={isAllDayEvent(event)}
488
- onPress={disableMonthEventCellPress ? () => {} : () => onPressEvent(event)}
489
- onLongPress={
490
- disableMonthEventCellPress || !onLongPressEvent
491
- ? undefined
492
- : () => onLongPressEvent(event)
493
- }
494
- />
495
- </View>
543
+ key={`lane-${i}`}
544
+ style={{ height: chipMetrics.chipHeight, pointerEvents: "none" }}
545
+ />
496
546
  ))}
497
547
  {hiddenCount > 0 ? (
498
548
  <Text
@@ -600,14 +650,73 @@ function MonthViewInner<T>({
600
650
  </Modal>
601
651
  ) : null}
602
652
  <View {...slot("grid", { base: styles.container })} onLayout={handleLayout}>
603
- {weeks.map((week) => (
604
- <View
605
- {...slot("week", { base: styles.weekRow, themed: theme.containers.weekRow })}
606
- key={week[0].toISOString()}
607
- >
608
- {week.map((day) => renderDay(day))}
609
- </View>
610
- ))}
653
+ {weeks.map((week, weekIndex) => {
654
+ // Spanning bars for this row (laid out once in `weekLayouts`). A multi-day
655
+ // event is one bar across its columns, stacked into lanes; the bars render
656
+ // in an overlay so they can span cells, and the cells reserve the lane rows
657
+ // so "+more" sits below them. `visibleLanes` mirrors the per-day cap.
658
+ const rowLayout = weekLayouts[weekIndex] ?? EMPTY_LAYOUT;
659
+ const visibleLanes = monthVisibleCount(rowLayout.laneCount, capacity);
660
+ const cols = week.length;
661
+ // With adjacent months hidden, clamp bars to the current-month columns so
662
+ // none draw over the blank leading/trailing cells.
663
+ let firstVisCol = 0;
664
+ let lastVisCol = cols - 1;
665
+ if (showGrid && !showAdjacentMonths) {
666
+ const first = week.findIndex((d) => isSameMonth(d, date));
667
+ if (first !== -1) {
668
+ firstVisCol = first;
669
+ lastVisCol = cols - 1 - [...week].reverse().findIndex((d) => isSameMonth(d, date));
670
+ }
671
+ }
672
+ return (
673
+ <View
674
+ {...slot("week", { base: styles.weekRow, themed: theme.containers.weekRow })}
675
+ key={week[0].toISOString()}
676
+ >
677
+ {week.map((day, dayCol) => renderDay(day, dayCol, rowLayout, visibleLanes))}
678
+ {showGrid ? (
679
+ <View style={[StyleSheet.absoluteFill, { pointerEvents: "box-none" }]}>
680
+ {rowLayout.segments
681
+ .filter((seg) => seg.lane < visibleLanes)
682
+ .map((seg) => {
683
+ const startCol = Math.max(seg.startCol, firstVisCol);
684
+ const endCol = Math.min(seg.endCol, lastVisCol);
685
+ if (startCol > endCol) return null;
686
+ return (
687
+ <View
688
+ key={`bar-${seg.event.start.toISOString()}:${seg.event.title}:${seg.lane}`}
689
+ style={{
690
+ position: "absolute",
691
+ left: pct((startCol / cols) * 100),
692
+ width: pct(((endCol - startCol + 1) / cols) * 100),
693
+ top: BADGE_AREA + seg.lane * chipMetrics.chipRowHeight,
694
+ height: chipMetrics.chipHeight,
695
+ paddingHorizontal: CHIP_INSET_H,
696
+ pointerEvents: "box-none",
697
+ }}
698
+ >
699
+ <RenderEventComponent
700
+ event={seg.event}
701
+ mode="month"
702
+ isAllDay={isAllDayEvent(seg.event)}
703
+ onPress={
704
+ disableMonthEventCellPress ? () => {} : () => onPressEvent(seg.event)
705
+ }
706
+ onLongPress={
707
+ disableMonthEventCellPress || !onLongPressEvent
708
+ ? undefined
709
+ : () => onLongPressEvent(seg.event)
710
+ }
711
+ />
712
+ </View>
713
+ );
714
+ })}
715
+ </View>
716
+ ) : null}
717
+ </View>
718
+ );
719
+ })}
611
720
  </View>
612
721
  </View>
613
722
  );
@@ -151,6 +151,14 @@ export interface ResourceTimelineProps<T = unknown> {
151
151
  * band stays non-interactive and hidden from assistive tech.
152
152
  */
153
153
  renderBusinessHours?: (band: BusinessHoursBand & { resource: Resource }) => ReactNode;
154
+ /**
155
+ * Render each resource's header cell yourself (an avatar, a subtitle, a status
156
+ * dot). Receives the resource and its index; return the cell's content. The board
157
+ * keeps the header cell's sizing so columns stay aligned, so this replaces only
158
+ * the label content. Falls back to the resource title (or id). Applies to both
159
+ * orientations: the top header cells when vertical, the row labels when horizontal.
160
+ */
161
+ renderResourceHeader?: (resource: Resource, index: number) => ReactNode;
154
162
  /** Snap dragged events to this many minutes (default 15). */
155
163
  dragStepMinutes?: number;
156
164
  /** Show the current-time line when `date` is today (default true). */
@@ -707,6 +715,7 @@ export function ResourceTimeline<T = unknown>({
707
715
  onCreateEvent,
708
716
  businessHours,
709
717
  renderBusinessHours,
718
+ renderResourceHeader,
710
719
  dragStepMinutes = 15,
711
720
  showNowIndicator = true,
712
721
  now: nowProp,
@@ -799,15 +808,19 @@ export function ResourceTimeline<T = unknown>({
799
808
  <View style={styles.vroot}>
800
809
  <View style={[styles.header, { borderBottomColor: theme.colors.gridLine }]}>
801
810
  <View style={{ width: GUTTER_WIDTH }} />
802
- {resources.map((resource) => (
811
+ {resources.map((resource, index) => (
803
812
  <View key={resource.id} style={[styles.vheaderCell, theme.containers.resourceLabel]}>
804
- <Text
805
- numberOfLines={1}
806
- style={[styles.vheaderText, { color: theme.colors.text }]}
807
- allowFontScaling={false}
808
- >
809
- {resource.title ?? resource.id}
810
- </Text>
813
+ {renderResourceHeader ? (
814
+ renderResourceHeader(resource, index)
815
+ ) : (
816
+ <Text
817
+ numberOfLines={1}
818
+ style={[styles.vheaderText, { color: theme.colors.text }]}
819
+ allowFontScaling={false}
820
+ >
821
+ {resource.title ?? resource.id}
822
+ </Text>
823
+ )}
811
824
  </View>
812
825
  ))}
813
826
  </View>
@@ -1016,13 +1029,17 @@ export function ResourceTimeline<T = unknown>({
1016
1029
  theme.containers.resourceLabel,
1017
1030
  ]}
1018
1031
  >
1019
- <Text
1020
- numberOfLines={1}
1021
- style={{ color: theme.colors.text }}
1022
- allowFontScaling={false}
1023
- >
1024
- {resource.title ?? resource.id}
1025
- </Text>
1032
+ {renderResourceHeader ? (
1033
+ renderResourceHeader(resource, laneIndex)
1034
+ ) : (
1035
+ <Text
1036
+ numberOfLines={1}
1037
+ style={{ color: theme.colors.text }}
1038
+ allowFontScaling={false}
1039
+ >
1040
+ {resource.title ?? resource.id}
1041
+ </Text>
1042
+ )}
1026
1043
  </View>
1027
1044
  <View style={{ width: trackWidth }}>
1028
1045
  {/* Closed-hours shade, behind the grid lines and bars. */}