@super-calendar/native 2.7.0 → 2.8.1

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;
@@ -102,6 +136,8 @@ export type MonthViewProps<T> = SlotStyleProps<MonthViewSlot> & {
102
136
  moreLabel?: string;
103
137
  /** Show dimmed days from adjacent months in the grid. Default true. */
104
138
  showAdjacentMonths?: boolean;
139
+ /** Tint weekend day cells with the theme's weekend background. Default true. */
140
+ highlightWeekends?: boolean;
105
141
  /** Ignore taps on month-cell events (day-cell taps still fire). Default false. */
106
142
  disableMonthEventCellPress?: boolean;
107
143
  /** Reverse the day order within each week (right-to-left). Default false. */
@@ -166,6 +202,7 @@ function MonthViewInner<T>({
166
202
  sortedMonthView = true,
167
203
  moreLabel = "{moreCount} More",
168
204
  showAdjacentMonths = true,
205
+ highlightWeekends = true,
169
206
  disableMonthEventCellPress = false,
170
207
  isRTL = false,
171
208
  showSixWeeks = false,
@@ -235,6 +272,16 @@ function MonthViewInner<T>({
235
272
  return isRTL ? days.reverse() : days;
236
273
  }, [date, weekStartsOn, isRTL, hiddenDays]);
237
274
 
275
+ // The built-in chip's height and per-row stride, from the theme's title type.
276
+ // Shared by the capacity estimate and the spanning-bar overlay so the bars line
277
+ // up exactly with the rows the cells reserve for them.
278
+ const chipMetrics = useMemo(() => {
279
+ const fontSize = numericStyle(theme.text.eventTitle.fontSize, 12);
280
+ const lineHeight = numericStyle(theme.text.eventTitle.lineHeight, Math.ceil(fontSize * 1.3));
281
+ const chipHeight = lineHeight + CHIP_PADDING_V * 2;
282
+ return { chipHeight, chipRowHeight: chipHeight + CELL_ROW_GAP };
283
+ }, [theme]);
284
+
238
285
  // How many chips fit per cell: a fixed cap when `maxVisibleEventCount` is set,
239
286
  // else derived from the measured cell height and the (default) chip metrics.
240
287
  const capacity = useMemo(() => {
@@ -245,14 +292,11 @@ function MonthViewInner<T>({
245
292
  return { full: FALLBACK_VISIBLE_COUNT, withMore: FALLBACK_VISIBLE_COUNT };
246
293
  }
247
294
  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
295
  const moreFontSize = numericStyle(theme.text.more.fontSize, 11);
252
296
  const moreRowHeight = Math.ceil(moreFontSize * 1.3) + CELL_ROW_GAP;
253
297
  const available = rowHeight - DAY_CELL_PADDING_TOP - DATE_BADGE_HEIGHT;
254
- return monthEventCapacity(available, chipRowHeight, moreRowHeight);
255
- }, [maxVisibleEventCount, gridHeight, weeks.length, theme]);
298
+ return monthEventCapacity(available, chipMetrics.chipRowHeight, moreRowHeight);
299
+ }, [maxVisibleEventCount, gridHeight, weeks.length, theme, chipMetrics]);
256
300
 
257
301
  // Group events by calendar day once per `events` change (shared with the dom
258
302
  // renderer via core's `groupEventsByDay`), rather than scanning the whole list
@@ -272,7 +316,21 @@ function MonthViewInner<T>({
272
316
  // picker reads cleaner without it (matching the dom renderer).
273
317
  const showGrid = events.length > 0;
274
318
 
275
- const renderDay = (day: Date) => {
319
+ // Lay out each week row's spanning bars once per data change, not per render.
320
+ const weekLayouts = useMemo(
321
+ () =>
322
+ showGrid
323
+ ? weeks.map((week) => layoutMonthWeek(week, collectRowEvents(week, eventsByDay)))
324
+ : [],
325
+ [showGrid, weeks, eventsByDay],
326
+ );
327
+
328
+ const renderDay = (
329
+ day: Date,
330
+ dayCol: number,
331
+ rowLayout: MonthWeekEvents<T>,
332
+ visibleLanes: number,
333
+ ) => {
276
334
  const isCurrentMonth = isSameMonth(day, date);
277
335
 
278
336
  // Blank out adjacent-month days when they're hidden, keeping the grid shape.
@@ -308,8 +366,12 @@ function MonthViewInner<T>({
308
366
  { selectedDates, selectedRange },
309
367
  { minDate, maxDate, isDateDisabled },
310
368
  );
311
- const visibleCount = monthVisibleCount(dayEvents.length, capacity);
312
- const hiddenCount = dayEvents.length - visibleCount;
369
+ // Events past the visible lanes (this day's segments the row can't show)
370
+ // collapse into "+N more"; the popover still lists the whole day.
371
+ const hiddenEvents = rowLayout.segments
372
+ .filter((s) => s.startCol <= dayCol && s.endCol >= dayCol && s.lane >= visibleLanes)
373
+ .map((s) => s.event);
374
+ const hiddenCount = hiddenEvents.length;
313
375
 
314
376
  // The range shows as a band behind the days; endpoints and discrete selected
315
377
  // days get a filled badge on top. Today's badge wins when it coincides. The
@@ -353,7 +415,7 @@ function MonthViewInner<T>({
353
415
  borderRightWidth: StyleSheet.hairlineWidth,
354
416
  borderColor: theme.colors.gridLine,
355
417
  },
356
- isWeekend(day) && { backgroundColor: theme.colors.weekendBackground },
418
+ highlightWeekends && isWeekend(day) && { backgroundColor: theme.colors.weekendBackground },
357
419
  theme.containers.dayCell,
358
420
  ],
359
421
  });
@@ -476,23 +538,14 @@ function MonthViewInner<T>({
476
538
  </Text>
477
539
  </View>
478
540
  )}
479
- {dayEvents.slice(0, visibleCount).map((event, index) => (
541
+ {/* Reserve one row per visible lane so the overlay bars have space and
542
+ "+more" sits below them; the bars themselves render in the row overlay
543
+ (they span cells). */}
544
+ {Array.from({ length: visibleLanes }, (_, i) => (
480
545
  <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>
546
+ key={`lane-${i}`}
547
+ style={{ height: chipMetrics.chipHeight, pointerEvents: "none" }}
548
+ />
496
549
  ))}
497
550
  {hiddenCount > 0 ? (
498
551
  <Text
@@ -600,14 +653,73 @@ function MonthViewInner<T>({
600
653
  </Modal>
601
654
  ) : null}
602
655
  <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
- ))}
656
+ {weeks.map((week, weekIndex) => {
657
+ // Spanning bars for this row (laid out once in `weekLayouts`). A multi-day
658
+ // event is one bar across its columns, stacked into lanes; the bars render
659
+ // in an overlay so they can span cells, and the cells reserve the lane rows
660
+ // so "+more" sits below them. `visibleLanes` mirrors the per-day cap.
661
+ const rowLayout = weekLayouts[weekIndex] ?? EMPTY_LAYOUT;
662
+ const visibleLanes = monthVisibleCount(rowLayout.laneCount, capacity);
663
+ const cols = week.length;
664
+ // With adjacent months hidden, clamp bars to the current-month columns so
665
+ // none draw over the blank leading/trailing cells.
666
+ let firstVisCol = 0;
667
+ let lastVisCol = cols - 1;
668
+ if (showGrid && !showAdjacentMonths) {
669
+ const first = week.findIndex((d) => isSameMonth(d, date));
670
+ if (first !== -1) {
671
+ firstVisCol = first;
672
+ lastVisCol = cols - 1 - [...week].reverse().findIndex((d) => isSameMonth(d, date));
673
+ }
674
+ }
675
+ return (
676
+ <View
677
+ {...slot("week", { base: styles.weekRow, themed: theme.containers.weekRow })}
678
+ key={week[0].toISOString()}
679
+ >
680
+ {week.map((day, dayCol) => renderDay(day, dayCol, rowLayout, visibleLanes))}
681
+ {showGrid ? (
682
+ <View style={[StyleSheet.absoluteFill, { pointerEvents: "box-none" }]}>
683
+ {rowLayout.segments
684
+ .filter((seg) => seg.lane < visibleLanes)
685
+ .map((seg) => {
686
+ const startCol = Math.max(seg.startCol, firstVisCol);
687
+ const endCol = Math.min(seg.endCol, lastVisCol);
688
+ if (startCol > endCol) return null;
689
+ return (
690
+ <View
691
+ key={`bar-${seg.event.start.toISOString()}:${seg.event.title}:${seg.lane}`}
692
+ style={{
693
+ position: "absolute",
694
+ left: pct((startCol / cols) * 100),
695
+ width: pct(((endCol - startCol + 1) / cols) * 100),
696
+ top: BADGE_AREA + seg.lane * chipMetrics.chipRowHeight,
697
+ height: chipMetrics.chipHeight,
698
+ paddingHorizontal: CHIP_INSET_H,
699
+ pointerEvents: "box-none",
700
+ }}
701
+ >
702
+ <RenderEventComponent
703
+ event={seg.event}
704
+ mode="month"
705
+ isAllDay={isAllDayEvent(seg.event)}
706
+ onPress={
707
+ disableMonthEventCellPress ? () => {} : () => onPressEvent(seg.event)
708
+ }
709
+ onLongPress={
710
+ disableMonthEventCellPress || !onLongPressEvent
711
+ ? undefined
712
+ : () => onLongPressEvent(seg.event)
713
+ }
714
+ />
715
+ </View>
716
+ );
717
+ })}
718
+ </View>
719
+ ) : null}
720
+ </View>
721
+ );
722
+ })}
611
723
  </View>
612
724
  </View>
613
725
  );
@@ -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. */}