@klinking/colander 3.1.0-alpha.5 → 3.1.0-alpha.7
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/dist/formats/date.cjs +1 -1
- package/dist/formats/date.d.cts +1 -1
- package/dist/formats/date.d.mts +1 -1
- package/dist/formats/date.mjs +1 -1
- package/dist/formats/object.cjs +1 -1
- package/dist/formats/object.d.cts +1 -1
- package/dist/formats/object.d.mts +1 -1
- package/dist/formats/object.mjs +1 -1
- package/dist/formats/plain-date-time.cjs +1 -1
- package/dist/formats/plain-date-time.d.cts +1 -1
- package/dist/formats/plain-date-time.d.mts +1 -1
- package/dist/formats/plain-date-time.mjs +1 -1
- package/dist/formats/plain-date.cjs +1 -1
- package/dist/formats/plain-date.d.cts +1 -1
- package/dist/formats/plain-date.d.mts +1 -1
- package/dist/formats/plain-date.mjs +1 -1
- package/dist/formats/plain-month-day.cjs +1 -1
- package/dist/formats/plain-month-day.d.cts +1 -1
- package/dist/formats/plain-month-day.d.mts +1 -1
- package/dist/formats/plain-month-day.mjs +1 -1
- package/dist/formats/plain-year-month.cjs +1 -1
- package/dist/formats/plain-year-month.d.cts +1 -1
- package/dist/formats/plain-year-month.d.mts +1 -1
- package/dist/formats/plain-year-month.mjs +1 -1
- package/dist/formats/zoned-date-time.cjs +1 -1
- package/dist/formats/zoned-date-time.d.cts +1 -1
- package/dist/formats/zoned-date-time.d.mts +1 -1
- package/dist/formats/zoned-date-time.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{weeks-navigation-CEV8w_69.mjs → weeks-navigation-BSKwFvdP.mjs} +367 -413
- package/dist/{weeks-navigation-q3beZn2n.cjs → weeks-navigation-CPevz3Xg.cjs} +367 -413
- package/dist/{weeks-view-context-BLZZm1fq.d.mts → weeks-view-context-BTojXkys.d.cts} +51 -10
- package/dist/{weeks-view-context-CkTcdZpj.d.cts → weeks-view-context-CFXV8orh.d.mts} +51 -10
- package/package.json +1 -1
- package/src/calendar-provider.tsx +10 -55
- package/src/day-cell-state.test.ts +214 -0
- package/src/day-cell-state.ts +314 -0
- package/src/day-cell.tsx +29 -383
- package/src/grid.tsx +1 -1
- package/src/out-of-range.test.ts +33 -53
- package/src/out-of-range.ts +47 -123
- package/src/range-preview.test.ts +120 -0
- package/src/root-selection.ts +99 -97
- package/src/weeks-navigation.tsx +12 -28
- package/src/weeks-view-state.ts +36 -84
- package/src/weeks-view-types.ts +4 -1
- package/src/weeks-view.bounds.test.tsx +246 -0
- package/src/weeks-window.test.ts +131 -0
- package/src/weeks-window.ts +102 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { useContext } from "react";
|
|
3
|
+
import { useCalendarStable, useCalendarState } from "./calendar-context";
|
|
4
|
+
import { WeekDataContext, GridContext } from "./context";
|
|
5
|
+
import type { GridOrientation } from "./context";
|
|
6
|
+
import type {
|
|
7
|
+
DayCellTemplateState,
|
|
8
|
+
OutsideDays,
|
|
9
|
+
TemporalNamespace,
|
|
10
|
+
} from "./types";
|
|
11
|
+
import { isInRange as isInRangeUtil } from "./utils";
|
|
12
|
+
import { useViewContext, useViewStable, useViewState } from "./view-context";
|
|
13
|
+
|
|
14
|
+
/** A day cell's state: the public {@link DayCellTemplateState} plus the roving-tabindex flag. */
|
|
15
|
+
export type DayCellState = DayCellTemplateState & { isTabTarget: boolean };
|
|
16
|
+
|
|
17
|
+
type RangeState = Pick<
|
|
18
|
+
DayCellTemplateState,
|
|
19
|
+
| "rangeStart"
|
|
20
|
+
| "rangeEnd"
|
|
21
|
+
| "rangeBoundary"
|
|
22
|
+
| "inRange"
|
|
23
|
+
| "rangeIndex"
|
|
24
|
+
| "rangeLength"
|
|
25
|
+
| "rangeHasStart"
|
|
26
|
+
| "rangeHasEnd"
|
|
27
|
+
| "rangePreviewStart"
|
|
28
|
+
| "rangePreviewEnd"
|
|
29
|
+
| "rangePreviewBoundary"
|
|
30
|
+
| "rangePreviewInRange"
|
|
31
|
+
| "rangePreviewIndex"
|
|
32
|
+
| "rangePreviewLength"
|
|
33
|
+
| "rangePreviewHasStart"
|
|
34
|
+
| "rangePreviewHasEnd"
|
|
35
|
+
>;
|
|
36
|
+
|
|
37
|
+
const NO_RANGE: RangeState = {
|
|
38
|
+
rangeStart: false,
|
|
39
|
+
rangeEnd: false,
|
|
40
|
+
rangeBoundary: false,
|
|
41
|
+
inRange: false,
|
|
42
|
+
rangeIndex: false,
|
|
43
|
+
rangeLength: false,
|
|
44
|
+
rangeHasStart: false,
|
|
45
|
+
rangeHasEnd: false,
|
|
46
|
+
rangePreviewStart: false,
|
|
47
|
+
rangePreviewEnd: false,
|
|
48
|
+
rangePreviewBoundary: false,
|
|
49
|
+
rangePreviewInRange: false,
|
|
50
|
+
rangePreviewIndex: false,
|
|
51
|
+
rangePreviewLength: false,
|
|
52
|
+
rangePreviewHasStart: false,
|
|
53
|
+
rangePreviewHasEnd: false,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** Everything that is the same for every cell of one render. */
|
|
57
|
+
export interface DayCellInputs {
|
|
58
|
+
T: TemporalNamespace;
|
|
59
|
+
today: Temporal.PlainDate;
|
|
60
|
+
orientation: GridOrientation;
|
|
61
|
+
rootState: DayCellTemplateState["root"];
|
|
62
|
+
selectionMode: "single" | "range" | "multiple";
|
|
63
|
+
selectedDates: Temporal.PlainDate[];
|
|
64
|
+
rangeStart: Temporal.PlainDate | undefined;
|
|
65
|
+
rangeEnd: Temporal.PlainDate | undefined;
|
|
66
|
+
previewStart: Temporal.PlainDate | undefined;
|
|
67
|
+
previewEnd: Temporal.PlainDate | undefined;
|
|
68
|
+
disabled: boolean;
|
|
69
|
+
isDateDisabled: ((date: Temporal.PlainDate) => boolean) | undefined;
|
|
70
|
+
focusedDate: Temporal.PlainDate;
|
|
71
|
+
tabTargetDate: Temporal.PlainDate;
|
|
72
|
+
outsideDays: OutsideDays;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Where one cell sits: its column and the month the grid it is in belongs to. */
|
|
76
|
+
export interface DayCellPosition {
|
|
77
|
+
columnIndex: number;
|
|
78
|
+
month: { year: number; month: number };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function computeRangeState(
|
|
82
|
+
date: Temporal.PlainDate,
|
|
83
|
+
inputs: DayCellInputs,
|
|
84
|
+
): RangeState {
|
|
85
|
+
const { T, selectionMode, rangeStart, rangeEnd, previewStart, previewEnd } =
|
|
86
|
+
inputs;
|
|
87
|
+
if (selectionMode !== "range") return NO_RANGE;
|
|
88
|
+
|
|
89
|
+
const isRangeStart = rangeStart
|
|
90
|
+
? T.PlainDate.compare(date, rangeStart) === 0
|
|
91
|
+
: false;
|
|
92
|
+
const isRangeEnd = rangeEnd
|
|
93
|
+
? T.PlainDate.compare(date, rangeEnd) === 0
|
|
94
|
+
: false;
|
|
95
|
+
const inRange = isInRangeUtil(date, rangeStart, rangeEnd, T) !== false;
|
|
96
|
+
const effectiveStart = rangeStart ?? rangeEnd;
|
|
97
|
+
const effectiveEnd = rangeEnd ?? rangeStart;
|
|
98
|
+
|
|
99
|
+
const isPreviewStart = previewStart
|
|
100
|
+
? T.PlainDate.compare(date, previewStart) === 0
|
|
101
|
+
: false;
|
|
102
|
+
const isPreviewEnd = previewEnd
|
|
103
|
+
? T.PlainDate.compare(date, previewEnd) === 0
|
|
104
|
+
: false;
|
|
105
|
+
const inPreview =
|
|
106
|
+
previewStart && previewEnd
|
|
107
|
+
? isInRangeUtil(date, previewStart, previewEnd, T) !== false
|
|
108
|
+
: false;
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
rangeStart: isRangeStart,
|
|
112
|
+
rangeEnd: isRangeEnd,
|
|
113
|
+
rangeBoundary: isRangeStart || isRangeEnd,
|
|
114
|
+
inRange,
|
|
115
|
+
rangeIndex:
|
|
116
|
+
inRange && effectiveStart ? date.since(effectiveStart).days : false,
|
|
117
|
+
rangeLength:
|
|
118
|
+
inRange && effectiveStart && effectiveEnd
|
|
119
|
+
? effectiveEnd.since(effectiveStart).days + 1
|
|
120
|
+
: false,
|
|
121
|
+
rangeHasStart: inRange && rangeStart !== undefined,
|
|
122
|
+
rangeHasEnd: inRange && rangeEnd !== undefined,
|
|
123
|
+
rangePreviewStart: isPreviewStart,
|
|
124
|
+
rangePreviewEnd: isPreviewEnd,
|
|
125
|
+
rangePreviewBoundary: isPreviewStart || isPreviewEnd,
|
|
126
|
+
rangePreviewInRange: inPreview,
|
|
127
|
+
rangePreviewIndex:
|
|
128
|
+
inPreview && previewStart ? date.since(previewStart).days : false,
|
|
129
|
+
rangePreviewLength:
|
|
130
|
+
inPreview && previewStart && previewEnd
|
|
131
|
+
? previewEnd.since(previewStart).days + 1
|
|
132
|
+
: false,
|
|
133
|
+
rangePreviewHasStart: inPreview && previewStart !== undefined,
|
|
134
|
+
rangePreviewHasEnd: inPreview && previewEnd !== undefined,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Derives the state of one day cell. Pure function, no hooks. */
|
|
139
|
+
export function computeDayCellState(
|
|
140
|
+
date: Temporal.PlainDate,
|
|
141
|
+
position: DayCellPosition,
|
|
142
|
+
inputs: DayCellInputs,
|
|
143
|
+
): DayCellState {
|
|
144
|
+
const { T, outsideDays } = inputs;
|
|
145
|
+
const { columnIndex, month } = position;
|
|
146
|
+
const isCurrentMonth = date.year === month.year && date.month === month.month;
|
|
147
|
+
const base = {
|
|
148
|
+
root: inputs.rootState,
|
|
149
|
+
date,
|
|
150
|
+
columnIndex,
|
|
151
|
+
orientation: inputs.orientation,
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
if (!isCurrentMonth && outsideDays === "hidden") {
|
|
155
|
+
return {
|
|
156
|
+
...base,
|
|
157
|
+
...NO_RANGE,
|
|
158
|
+
outsideMonth: true,
|
|
159
|
+
hidden: true,
|
|
160
|
+
selected: false,
|
|
161
|
+
today: false,
|
|
162
|
+
disabled: true,
|
|
163
|
+
focused: false,
|
|
164
|
+
isTabTarget: false,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const isToday = T.PlainDate.compare(date, inputs.today) === 0;
|
|
169
|
+
|
|
170
|
+
if (!isCurrentMonth && outsideDays !== "enabled") {
|
|
171
|
+
// `readOnly` outside days keep their range state; `disabled` ones don't.
|
|
172
|
+
const suppressRange = outsideDays === "disabled";
|
|
173
|
+
return {
|
|
174
|
+
...base,
|
|
175
|
+
...(suppressRange ? NO_RANGE : computeRangeState(date, inputs)),
|
|
176
|
+
outsideMonth: true,
|
|
177
|
+
hidden: false,
|
|
178
|
+
selected: false,
|
|
179
|
+
today: isToday,
|
|
180
|
+
disabled: true,
|
|
181
|
+
focused: false,
|
|
182
|
+
isTabTarget: false,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
...base,
|
|
188
|
+
...computeRangeState(date, inputs),
|
|
189
|
+
outsideMonth: !isCurrentMonth,
|
|
190
|
+
hidden: false,
|
|
191
|
+
selected:
|
|
192
|
+
inputs.selectionMode !== "range"
|
|
193
|
+
? inputs.selectedDates.some((d) => T.PlainDate.compare(d, date) === 0)
|
|
194
|
+
: false,
|
|
195
|
+
today: isToday,
|
|
196
|
+
disabled: inputs.disabled || (inputs.isDateDisabled?.(date) ?? false),
|
|
197
|
+
focused: T.PlainDate.compare(date, inputs.focusedDate) === 0,
|
|
198
|
+
isTabTarget: T.PlainDate.compare(date, inputs.tabTargetDate) === 0,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Every field of {@link DayCellState} that decides whether a cell re-renders.
|
|
204
|
+
* `root`, `date`, `columnIndex` and `orientation` are not listed: the memo
|
|
205
|
+
* compares `date`/`columnIndex` as props, and the other two never change a
|
|
206
|
+
* cell on their own. Adding a field to the state fails to compile until it is
|
|
207
|
+
* listed here.
|
|
208
|
+
*/
|
|
209
|
+
const COMPARED_STATE: Record<
|
|
210
|
+
Exclude<keyof DayCellState, "root" | "date" | "columnIndex" | "orientation">,
|
|
211
|
+
true
|
|
212
|
+
> = {
|
|
213
|
+
outsideMonth: true,
|
|
214
|
+
hidden: true,
|
|
215
|
+
selected: true,
|
|
216
|
+
today: true,
|
|
217
|
+
disabled: true,
|
|
218
|
+
focused: true,
|
|
219
|
+
rangeStart: true,
|
|
220
|
+
rangeEnd: true,
|
|
221
|
+
rangeBoundary: true,
|
|
222
|
+
inRange: true,
|
|
223
|
+
rangeIndex: true,
|
|
224
|
+
rangeLength: true,
|
|
225
|
+
rangeHasStart: true,
|
|
226
|
+
rangeHasEnd: true,
|
|
227
|
+
rangePreviewStart: true,
|
|
228
|
+
rangePreviewEnd: true,
|
|
229
|
+
rangePreviewBoundary: true,
|
|
230
|
+
rangePreviewInRange: true,
|
|
231
|
+
rangePreviewIndex: true,
|
|
232
|
+
rangePreviewLength: true,
|
|
233
|
+
rangePreviewHasStart: true,
|
|
234
|
+
rangePreviewHasEnd: true,
|
|
235
|
+
isTabTarget: true,
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const COMPARED_KEYS = Object.keys(
|
|
239
|
+
COMPARED_STATE,
|
|
240
|
+
) as (keyof typeof COMPARED_STATE)[];
|
|
241
|
+
|
|
242
|
+
/** Whether two cell states render the same cell. */
|
|
243
|
+
export function dayCellStateEqual(a: DayCellState, b: DayCellState): boolean {
|
|
244
|
+
for (const key of COMPARED_KEYS) {
|
|
245
|
+
if (a[key] !== b[key]) return false;
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Reads the contexts a day cell depends on and returns a function that derives
|
|
252
|
+
* the state of any cell. `columnIndex` defaults to the date's column in a
|
|
253
|
+
* week starting on the calendar's `weekStartDay`.
|
|
254
|
+
*/
|
|
255
|
+
export function useDayCellDeriver(): (
|
|
256
|
+
date: Temporal.PlainDate,
|
|
257
|
+
columnIndex?: number,
|
|
258
|
+
) => DayCellState {
|
|
259
|
+
const { orientation } = useContext(GridContext);
|
|
260
|
+
const weekData = useContext(WeekDataContext);
|
|
261
|
+
const {
|
|
262
|
+
disabled,
|
|
263
|
+
isDateDisabled,
|
|
264
|
+
selectionMode,
|
|
265
|
+
temporal: T,
|
|
266
|
+
today,
|
|
267
|
+
weekStartDay,
|
|
268
|
+
} = useCalendarStable();
|
|
269
|
+
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } =
|
|
270
|
+
useCalendarState();
|
|
271
|
+
const viewCtx = useViewContext();
|
|
272
|
+
const { focusedDate, tabTargetDate } = viewCtx;
|
|
273
|
+
const { rootState, currentDateTime } = useViewState();
|
|
274
|
+
const { outsideDays } = useViewStable();
|
|
275
|
+
|
|
276
|
+
const inputs: DayCellInputs = {
|
|
277
|
+
T,
|
|
278
|
+
today: today(),
|
|
279
|
+
orientation,
|
|
280
|
+
rootState,
|
|
281
|
+
selectionMode,
|
|
282
|
+
selectedDates,
|
|
283
|
+
rangeStart,
|
|
284
|
+
rangeEnd,
|
|
285
|
+
previewStart,
|
|
286
|
+
previewEnd,
|
|
287
|
+
disabled,
|
|
288
|
+
isDateDisabled,
|
|
289
|
+
focusedDate,
|
|
290
|
+
tabTargetDate,
|
|
291
|
+
outsideDays,
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
// A weeks view has no "current month": every cell is in its own month, so
|
|
295
|
+
// `outsideMonth` is always false. Otherwise a cell belongs to the month of
|
|
296
|
+
// the grid it sits in (set per grid in multi-month mode).
|
|
297
|
+
const isWeeksView = viewCtx.viewType === "weeks";
|
|
298
|
+
const gridMonth = weekData?.gridMonth ?? currentDateTime;
|
|
299
|
+
|
|
300
|
+
return (date, columnIndex) => {
|
|
301
|
+
const daysInWeek = date.daysInWeek;
|
|
302
|
+
return computeDayCellState(
|
|
303
|
+
date,
|
|
304
|
+
{
|
|
305
|
+
columnIndex:
|
|
306
|
+
columnIndex ??
|
|
307
|
+
((date.dayOfWeek % daysInWeek) - weekStartDay + daysInWeek) %
|
|
308
|
+
daysInWeek,
|
|
309
|
+
month: isWeeksView ? { year: date.year, month: date.month } : gridMonth,
|
|
310
|
+
},
|
|
311
|
+
inputs,
|
|
312
|
+
);
|
|
313
|
+
};
|
|
314
|
+
}
|