@klinking/colander 0.0.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/dist/formats/date.cjs +49 -0
- package/dist/formats/date.d.cts +45 -0
- package/dist/formats/date.d.mts +45 -0
- package/dist/formats/date.mjs +2 -0
- package/dist/formats/object.cjs +49 -0
- package/dist/formats/object.d.cts +45 -0
- package/dist/formats/object.d.mts +45 -0
- package/dist/formats/object.mjs +2 -0
- package/dist/formats/plain-date-time.cjs +49 -0
- package/dist/formats/plain-date-time.d.cts +45 -0
- package/dist/formats/plain-date-time.d.mts +45 -0
- package/dist/formats/plain-date-time.mjs +2 -0
- package/dist/formats/plain-date.cjs +49 -0
- package/dist/formats/plain-date.d.cts +45 -0
- package/dist/formats/plain-date.d.mts +45 -0
- package/dist/formats/plain-date.mjs +2 -0
- package/dist/formats/plain-month-day.cjs +49 -0
- package/dist/formats/plain-month-day.d.cts +45 -0
- package/dist/formats/plain-month-day.d.mts +45 -0
- package/dist/formats/plain-month-day.mjs +2 -0
- package/dist/formats/plain-year-month.cjs +49 -0
- package/dist/formats/plain-year-month.d.cts +45 -0
- package/dist/formats/plain-year-month.d.mts +45 -0
- package/dist/formats/plain-year-month.mjs +2 -0
- package/dist/formats/zoned-date-time.cjs +49 -0
- package/dist/formats/zoned-date-time.d.cts +45 -0
- package/dist/formats/zoned-date-time.d.mts +45 -0
- package/dist/formats/zoned-date-time.mjs +2 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/temporal-polyfill-DTx0VIX5.mjs +4556 -0
- package/dist/temporal-polyfill-DU8VfleL.cjs +4860 -0
- package/dist/weeks-view-context-BN6qR-83.d.mts +2022 -0
- package/dist/weeks-view-context-aHGgYkpt.d.cts +2022 -0
- package/package.json +89 -0
- package/src/calendar-context.ts +22 -0
- package/src/calendar-provider.test.tsx +123 -0
- package/src/calendar-provider.tsx +538 -0
- package/src/calendar-types.ts +212 -0
- package/src/compute-weeks-in-window.test.ts +83 -0
- package/src/compute-weeks-in-window.ts +40 -0
- package/src/context.ts +34 -0
- package/src/day-cell.tsx +635 -0
- package/src/drag-handle.tsx +137 -0
- package/src/formats/date.ts +110 -0
- package/src/formats/formats.test.ts +18 -0
- package/src/formats/object.ts +110 -0
- package/src/formats/plain-date-time.ts +110 -0
- package/src/formats/plain-date.ts +110 -0
- package/src/formats/plain-month-day.ts +110 -0
- package/src/formats/plain-year-month.ts +110 -0
- package/src/formats/zoned-date-time.ts +110 -0
- package/src/grid-header.tsx +141 -0
- package/src/grid.event-perf.test.tsx +282 -0
- package/src/grid.perf.test.tsx +467 -0
- package/src/grid.tsx +608 -0
- package/src/index.ts +116 -0
- package/src/keyboard.test.ts +498 -0
- package/src/keyboard.ts +142 -0
- package/src/month-separator.test.tsx +183 -0
- package/src/month-separator.tsx +238 -0
- package/src/month-view-context.ts +26 -0
- package/src/month-view-types.ts +89 -0
- package/src/month-view.test.tsx +1433 -0
- package/src/month-view.tsx +373 -0
- package/src/navigation.tsx +350 -0
- package/src/overflow.test.ts +124 -0
- package/src/overflow.ts +233 -0
- package/src/range-preview.tsx +90 -0
- package/src/resolve-first-week.test.ts +132 -0
- package/src/resolve-first-week.ts +145 -0
- package/src/root-selection.ts +621 -0
- package/src/selected-range.tsx +163 -0
- package/src/temporal-polyfill.test.ts +317 -0
- package/src/temporal-polyfill.ts +566 -0
- package/src/test-temporal.ts +17 -0
- package/src/types.ts +814 -0
- package/src/utils.bench.ts +183 -0
- package/src/utils.test.ts +983 -0
- package/src/utils.ts +493 -0
- package/src/view-context.ts +28 -0
- package/src/week-number.tsx +118 -0
- package/src/weeks-grid.tsx +265 -0
- package/src/weeks-keyboard.test.ts +222 -0
- package/src/weeks-keyboard.ts +156 -0
- package/src/weeks-navigation.tsx +209 -0
- package/src/weeks-view-context.ts +26 -0
- package/src/weeks-view-state.ts +421 -0
- package/src/weeks-view-types.ts +102 -0
- package/src/weeks-view.test.tsx +479 -0
- package/src/weeks-view.tsx +127 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { mergeProps } from "@base-ui/react/merge-props";
|
|
2
|
+
import { useRender } from "@base-ui/react/use-render";
|
|
3
|
+
import { forwardRef, useId, useMemo, useEffect } from "react";
|
|
4
|
+
import { useCalendarStable, useCalendarState } from "./calendar-context";
|
|
5
|
+
import { useMonthViewStable, useMonthViewState } from "./month-view-context";
|
|
6
|
+
import type { StateAttributesMapping } from "./types";
|
|
7
|
+
import type {
|
|
8
|
+
ValueFormat,
|
|
9
|
+
DateStringState,
|
|
10
|
+
DateStringProps,
|
|
11
|
+
TimeStringState,
|
|
12
|
+
TimeStringProps,
|
|
13
|
+
MonthYearStringState,
|
|
14
|
+
MonthYearStringProps,
|
|
15
|
+
PrevMonthButtonProps,
|
|
16
|
+
NextMonthButtonProps,
|
|
17
|
+
NavButtonState,
|
|
18
|
+
} from "./types";
|
|
19
|
+
import { selectedToZdt, zdtToNativeDate, calendarForLocale } from "./utils";
|
|
20
|
+
|
|
21
|
+
const dateStringStateAttributesMapping = {
|
|
22
|
+
root: () => null,
|
|
23
|
+
month: () => null,
|
|
24
|
+
year: () => null,
|
|
25
|
+
day: () => null,
|
|
26
|
+
} as const satisfies StateAttributesMapping<DateStringState>;
|
|
27
|
+
|
|
28
|
+
const timeStringStateAttributesMapping = {
|
|
29
|
+
root: () => null,
|
|
30
|
+
hour: () => null,
|
|
31
|
+
minute: () => null,
|
|
32
|
+
second: () => null,
|
|
33
|
+
} as const satisfies StateAttributesMapping<TimeStringState>;
|
|
34
|
+
|
|
35
|
+
const monthYearStringStateAttributesMapping = {
|
|
36
|
+
root: () => null,
|
|
37
|
+
month: () => null,
|
|
38
|
+
year: () => null,
|
|
39
|
+
} as const satisfies StateAttributesMapping<MonthYearStringState>;
|
|
40
|
+
|
|
41
|
+
function DateStringFn(props: DateStringProps, ref: React.ForwardedRef<HTMLSpanElement>) {
|
|
42
|
+
const { render, locales, options, ...otherProps } = props;
|
|
43
|
+
const { locale, temporal: T, timeZone } = useCalendarStable();
|
|
44
|
+
const { selected } = useCalendarState();
|
|
45
|
+
const { currentDateTime, rootState } = useMonthViewState();
|
|
46
|
+
|
|
47
|
+
const selectedZdt = selectedToZdt(selected, timeZone, T);
|
|
48
|
+
const displayDate = selectedZdt
|
|
49
|
+
? zdtToNativeDate(selectedZdt)
|
|
50
|
+
: new Date(currentDateTime.year, currentDateTime.month - 1, 1);
|
|
51
|
+
|
|
52
|
+
const formatted = displayDate.toLocaleDateString(locales ?? locale, options);
|
|
53
|
+
|
|
54
|
+
const month = displayDate.getMonth() + 1;
|
|
55
|
+
const year = displayDate.getFullYear();
|
|
56
|
+
const day = displayDate.getDate();
|
|
57
|
+
|
|
58
|
+
const state = useMemo<DateStringState>(
|
|
59
|
+
() => ({
|
|
60
|
+
root: rootState as any,
|
|
61
|
+
month,
|
|
62
|
+
year,
|
|
63
|
+
day,
|
|
64
|
+
}),
|
|
65
|
+
[rootState, month, year, day],
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const defaultProps: Record<string, unknown> = {
|
|
69
|
+
children: formatted,
|
|
70
|
+
"aria-live": "polite",
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return useRender({
|
|
74
|
+
defaultTagName: "span",
|
|
75
|
+
render,
|
|
76
|
+
ref: ref ? [ref] : [],
|
|
77
|
+
state,
|
|
78
|
+
stateAttributesMapping: dateStringStateAttributesMapping,
|
|
79
|
+
props: mergeProps<"span">(defaultProps, otherProps),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Displays the currently selected (or current) date as localized text.
|
|
85
|
+
* Renders a `<span>` with `aria-live="polite"`.
|
|
86
|
+
*/
|
|
87
|
+
export const DateString = forwardRef(DateStringFn) as <F extends ValueFormat = ValueFormat>(
|
|
88
|
+
props: DateStringProps<F> & React.RefAttributes<HTMLSpanElement>,
|
|
89
|
+
) => React.ReactElement | null;
|
|
90
|
+
|
|
91
|
+
function TimeStringFn(props: TimeStringProps, ref: React.ForwardedRef<HTMLSpanElement>) {
|
|
92
|
+
const { render, locales, options, ...otherProps } = props;
|
|
93
|
+
const { locale, temporal: T, timeZone } = useCalendarStable();
|
|
94
|
+
const { selected } = useCalendarState();
|
|
95
|
+
const { rootState } = useMonthViewState();
|
|
96
|
+
|
|
97
|
+
const selZdt = selectedToZdt(selected, timeZone, T);
|
|
98
|
+
const displayDate = selZdt
|
|
99
|
+
? zdtToNativeDate(selZdt)
|
|
100
|
+
: zdtToNativeDate(T.Now.zonedDateTimeISO(timeZone));
|
|
101
|
+
|
|
102
|
+
const mergedOptions: Intl.DateTimeFormatOptions = { timeZone, ...options };
|
|
103
|
+
const formatted = displayDate.toLocaleTimeString(locales ?? locale, mergedOptions);
|
|
104
|
+
|
|
105
|
+
const nowZdt = selZdt ?? T.Now.zonedDateTimeISO(timeZone);
|
|
106
|
+
const hour = nowZdt.hour;
|
|
107
|
+
const minute = nowZdt.minute;
|
|
108
|
+
const second = nowZdt.second;
|
|
109
|
+
|
|
110
|
+
const state = useMemo<TimeStringState>(
|
|
111
|
+
() => ({
|
|
112
|
+
root: rootState as any,
|
|
113
|
+
hour,
|
|
114
|
+
minute,
|
|
115
|
+
second,
|
|
116
|
+
}),
|
|
117
|
+
[rootState, hour, minute, second],
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const defaultProps: Record<string, unknown> = {
|
|
121
|
+
children: formatted,
|
|
122
|
+
"aria-live": "polite",
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
return useRender({
|
|
126
|
+
defaultTagName: "span",
|
|
127
|
+
render,
|
|
128
|
+
ref: ref ? [ref] : [],
|
|
129
|
+
state,
|
|
130
|
+
stateAttributesMapping: timeStringStateAttributesMapping,
|
|
131
|
+
props: mergeProps<"span">(defaultProps, otherProps),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Displays the currently selected time as localized text.
|
|
137
|
+
* Falls back to the current time when nothing is selected.
|
|
138
|
+
* Renders a `<span>` with `aria-live="polite"`.
|
|
139
|
+
*/
|
|
140
|
+
export const TimeString = forwardRef(TimeStringFn) as <F extends ValueFormat = ValueFormat>(
|
|
141
|
+
props: TimeStringProps<F> & React.RefAttributes<HTMLSpanElement>,
|
|
142
|
+
) => React.ReactElement | null;
|
|
143
|
+
|
|
144
|
+
function MonthYearStringFn(props: MonthYearStringProps, ref: React.ForwardedRef<HTMLSpanElement>) {
|
|
145
|
+
const { render, locales, options, monthIndex: monthIndexProp, ...otherProps } = props;
|
|
146
|
+
const monthIndex = monthIndexProp ?? 0;
|
|
147
|
+
const { locale } = useCalendarStable();
|
|
148
|
+
const monthViewStable = useMonthViewStable();
|
|
149
|
+
const { currentMonth: currentDateTime, allMonths, rootState } = useMonthViewState();
|
|
150
|
+
const { setGridLabelId } = monthViewStable;
|
|
151
|
+
|
|
152
|
+
const id = useId();
|
|
153
|
+
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
setGridLabelId(monthIndex, id);
|
|
156
|
+
return () => setGridLabelId(monthIndex, undefined);
|
|
157
|
+
}, [id, setGridLabelId, monthIndex]);
|
|
158
|
+
|
|
159
|
+
const monthData = allMonths[monthIndex];
|
|
160
|
+
const displayYear = monthData?.year ?? currentDateTime.year;
|
|
161
|
+
const displayMonth = monthData?.month ?? currentDateTime.month;
|
|
162
|
+
|
|
163
|
+
const displayDate = new Date(displayYear, displayMonth - 1, 1);
|
|
164
|
+
const defaultOptions: Intl.DateTimeFormatOptions = options ?? {
|
|
165
|
+
month: "long",
|
|
166
|
+
year: "numeric",
|
|
167
|
+
};
|
|
168
|
+
const formatted = displayDate.toLocaleDateString(locales ?? locale, defaultOptions);
|
|
169
|
+
|
|
170
|
+
const state = useMemo<MonthYearStringState>(
|
|
171
|
+
() => ({
|
|
172
|
+
root: rootState as any,
|
|
173
|
+
month: displayMonth,
|
|
174
|
+
year: displayYear,
|
|
175
|
+
}),
|
|
176
|
+
[rootState, displayMonth, displayYear],
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
const defaultProps: Record<string, unknown> = {
|
|
180
|
+
id,
|
|
181
|
+
children: formatted,
|
|
182
|
+
"aria-live": "polite",
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
return useRender({
|
|
186
|
+
defaultTagName: "span",
|
|
187
|
+
render,
|
|
188
|
+
ref: ref ? [ref] : [],
|
|
189
|
+
state,
|
|
190
|
+
stateAttributesMapping: monthYearStringStateAttributesMapping,
|
|
191
|
+
props: mergeProps<"span">(defaultProps, otherProps),
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Displays the currently viewed month and year as a live-region heading for the
|
|
197
|
+
* calendar grid. Defaults to `{ month: "long", year: "numeric" }` (e.g.
|
|
198
|
+
* "March 2026"). Automatically registers a unique `id` so that `Grid` can
|
|
199
|
+
* reference it via `aria-labelledby`.
|
|
200
|
+
*
|
|
201
|
+
* **Accessibility requirement:** When using a custom `render` function or custom
|
|
202
|
+
* `options` that change the displayed format, both the month and year **must**
|
|
203
|
+
* remain accessible to screen readers within this component. This element is
|
|
204
|
+
* referenced by the calendar grid's `aria-labelledby` and serves as its
|
|
205
|
+
* accessible name.
|
|
206
|
+
*/
|
|
207
|
+
export const MonthYearString = forwardRef(MonthYearStringFn) as <
|
|
208
|
+
F extends ValueFormat = ValueFormat,
|
|
209
|
+
>(
|
|
210
|
+
props: MonthYearStringProps<F> & React.RefAttributes<HTMLSpanElement>,
|
|
211
|
+
) => React.ReactElement | null;
|
|
212
|
+
|
|
213
|
+
function useNavButton<F extends ValueFormat = ValueFormat>(direction: "prev" | "next") {
|
|
214
|
+
const { disabled: globalDisabled, minValue, maxValue, locale, temporal: T } = useCalendarStable();
|
|
215
|
+
const monthViewStable = useMonthViewStable();
|
|
216
|
+
const monthViewState = useMonthViewState();
|
|
217
|
+
const {
|
|
218
|
+
goNextMonth: goToNextMonth,
|
|
219
|
+
goPrevMonth: goToPrevMonth,
|
|
220
|
+
numberOfMonths,
|
|
221
|
+
} = monthViewStable;
|
|
222
|
+
const { currentMonth: currentDateTime, allMonths, rootState } = monthViewState;
|
|
223
|
+
|
|
224
|
+
// For "next", compute destination from the last visible month
|
|
225
|
+
// For "prev", compute destination from the first visible month
|
|
226
|
+
const refMonth =
|
|
227
|
+
direction === "next" ? (allMonths[numberOfMonths - 1] ?? currentDateTime) : currentDateTime;
|
|
228
|
+
|
|
229
|
+
const destMonth =
|
|
230
|
+
direction === "prev"
|
|
231
|
+
? refMonth.month === 1
|
|
232
|
+
? 12
|
|
233
|
+
: refMonth.month - 1
|
|
234
|
+
: refMonth.month === 12
|
|
235
|
+
? 1
|
|
236
|
+
: refMonth.month + 1;
|
|
237
|
+
|
|
238
|
+
const destYear =
|
|
239
|
+
direction === "prev"
|
|
240
|
+
? refMonth.month === 1
|
|
241
|
+
? refMonth.year - 1
|
|
242
|
+
: refMonth.year
|
|
243
|
+
: refMonth.month === 12
|
|
244
|
+
? refMonth.year + 1
|
|
245
|
+
: refMonth.year;
|
|
246
|
+
|
|
247
|
+
const boundValue = direction === "prev" ? minValue : maxValue;
|
|
248
|
+
|
|
249
|
+
const isDisabled = useMemo(() => {
|
|
250
|
+
if (globalDisabled) return true;
|
|
251
|
+
if (!boundValue) return false;
|
|
252
|
+
if (direction === "prev") {
|
|
253
|
+
return (
|
|
254
|
+
destYear < boundValue.year || (destYear === boundValue.year && destMonth < boundValue.month)
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
return (
|
|
258
|
+
destYear > boundValue.year || (destYear === boundValue.year && destMonth > boundValue.month)
|
|
259
|
+
);
|
|
260
|
+
}, [globalDisabled, destYear, destMonth, boundValue, direction]);
|
|
261
|
+
|
|
262
|
+
const localeCalendar = useMemo(() => calendarForLocale(locale), [locale]);
|
|
263
|
+
|
|
264
|
+
const target = useMemo(
|
|
265
|
+
() =>
|
|
266
|
+
T.PlainYearMonth.from({
|
|
267
|
+
year: destYear,
|
|
268
|
+
month: destMonth,
|
|
269
|
+
calendar: localeCalendar,
|
|
270
|
+
}),
|
|
271
|
+
[destYear, destMonth, T, localeCalendar],
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
const state = useMemo<NavButtonState<F>>(
|
|
275
|
+
() => ({ root: rootState as any, direction, disabled: isDisabled, target }),
|
|
276
|
+
[rootState, direction, isDisabled, target],
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
const goFn = direction === "prev" ? goToPrevMonth : goToNextMonth;
|
|
280
|
+
|
|
281
|
+
const defaultProps: Record<string, unknown> = {
|
|
282
|
+
type: "button",
|
|
283
|
+
"aria-label": `Go to ${direction === "prev" ? "previous" : "next"} month`,
|
|
284
|
+
disabled: isDisabled,
|
|
285
|
+
onClick: isDisabled ? undefined : goFn,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
return { state, defaultProps };
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const navButtonStateAttributesMapping = {
|
|
292
|
+
root: () => null,
|
|
293
|
+
direction: (v) => ({ "data-direction": v }),
|
|
294
|
+
disabled: () => null,
|
|
295
|
+
target: () => null,
|
|
296
|
+
} as const satisfies StateAttributesMapping<NavButtonState>;
|
|
297
|
+
|
|
298
|
+
function PrevMonthButtonFn(
|
|
299
|
+
props: PrevMonthButtonProps,
|
|
300
|
+
ref: React.ForwardedRef<HTMLButtonElement>,
|
|
301
|
+
) {
|
|
302
|
+
const { render, ...otherProps } = props;
|
|
303
|
+
const { state, defaultProps } = useNavButton("prev");
|
|
304
|
+
|
|
305
|
+
return useRender({
|
|
306
|
+
defaultTagName: "button",
|
|
307
|
+
render,
|
|
308
|
+
ref: ref ? [ref] : [],
|
|
309
|
+
state,
|
|
310
|
+
stateAttributesMapping: navButtonStateAttributesMapping,
|
|
311
|
+
props: mergeProps<"button">(defaultProps, otherProps),
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Button that navigates to the previous month. Automatically disabled
|
|
317
|
+
* when the previous month falls before `min`. Exposes `data-direction="prev"`.
|
|
318
|
+
*/
|
|
319
|
+
export const PrevMonthButton = forwardRef(PrevMonthButtonFn) as <
|
|
320
|
+
F extends ValueFormat = ValueFormat,
|
|
321
|
+
>(
|
|
322
|
+
props: PrevMonthButtonProps<F> & React.RefAttributes<HTMLButtonElement>,
|
|
323
|
+
) => React.ReactElement | null;
|
|
324
|
+
|
|
325
|
+
function NextMonthButtonFn(
|
|
326
|
+
props: NextMonthButtonProps,
|
|
327
|
+
ref: React.ForwardedRef<HTMLButtonElement>,
|
|
328
|
+
) {
|
|
329
|
+
const { render, ...otherProps } = props;
|
|
330
|
+
const { state, defaultProps } = useNavButton("next");
|
|
331
|
+
|
|
332
|
+
return useRender({
|
|
333
|
+
defaultTagName: "button",
|
|
334
|
+
render,
|
|
335
|
+
ref: ref ? [ref] : [],
|
|
336
|
+
state,
|
|
337
|
+
stateAttributesMapping: navButtonStateAttributesMapping,
|
|
338
|
+
props: mergeProps<"button">(defaultProps, otherProps),
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Button that navigates to the next month. Automatically disabled
|
|
344
|
+
* when the next month falls after `max`. Exposes `data-direction="next"`.
|
|
345
|
+
*/
|
|
346
|
+
export const NextMonthButton = forwardRef(NextMonthButtonFn) as <
|
|
347
|
+
F extends ValueFormat = ValueFormat,
|
|
348
|
+
>(
|
|
349
|
+
props: NextMonthButtonProps<F> & React.RefAttributes<HTMLButtonElement>,
|
|
350
|
+
) => React.ReactElement | null;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { describe, it, expect } from "vitest";
|
|
3
|
+
import { applyOverflow, canShift, type OverflowBehavior } from "./overflow";
|
|
4
|
+
|
|
5
|
+
const T = Temporal;
|
|
6
|
+
const pd = (s: string) => T.PlainDate.from(s);
|
|
7
|
+
|
|
8
|
+
describe("applyOverflow", () => {
|
|
9
|
+
const min = pd("2025-12-01");
|
|
10
|
+
const max = pd("2026-01-10");
|
|
11
|
+
|
|
12
|
+
it.each([
|
|
13
|
+
{
|
|
14
|
+
description: "unbounded — no adjustment",
|
|
15
|
+
targetFirstWeek: pd("2026-02-01"),
|
|
16
|
+
weekCount: 8,
|
|
17
|
+
behavior: "unbounded" as OverflowBehavior,
|
|
18
|
+
useMinMax: true,
|
|
19
|
+
expected: { firstWeek: "2026-02-01", weekCount: 8 },
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
description: "stop — target has valid weeks, allowed",
|
|
23
|
+
targetFirstWeek: pd("2025-12-07"),
|
|
24
|
+
weekCount: 8,
|
|
25
|
+
behavior: "stop" as OverflowBehavior,
|
|
26
|
+
useMinMax: true,
|
|
27
|
+
expected: { firstWeek: "2025-12-07", weekCount: 8 },
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
description: "snap — pulls back when target overshoots max",
|
|
31
|
+
targetFirstWeek: pd("2026-01-11"),
|
|
32
|
+
weekCount: 8,
|
|
33
|
+
behavior: "snap" as OverflowBehavior,
|
|
34
|
+
useMinMax: true,
|
|
35
|
+
expected: {
|
|
36
|
+
firstWeek: "2025-11-16",
|
|
37
|
+
weekCount: 8,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
description: "snap-shrink — pulls back and trims disabled rows",
|
|
42
|
+
targetFirstWeek: pd("2026-01-11"),
|
|
43
|
+
weekCount: 8,
|
|
44
|
+
behavior: "snap-shrink" as OverflowBehavior,
|
|
45
|
+
useMinMax: true,
|
|
46
|
+
expected: {
|
|
47
|
+
firstWeek: "2025-11-30",
|
|
48
|
+
weekCount: 6,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
description: "stop-shrink — trims disabled rows",
|
|
53
|
+
targetFirstWeek: pd("2025-12-07"),
|
|
54
|
+
weekCount: 8,
|
|
55
|
+
behavior: "stop-shrink" as OverflowBehavior,
|
|
56
|
+
useMinMax: true,
|
|
57
|
+
expected: {
|
|
58
|
+
firstWeek: "2025-12-07",
|
|
59
|
+
weekCount: 5,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
description: "no min/max — all modes behave as unbounded",
|
|
64
|
+
targetFirstWeek: pd("2026-06-01"),
|
|
65
|
+
weekCount: 8,
|
|
66
|
+
behavior: "snap" as OverflowBehavior,
|
|
67
|
+
useMinMax: false,
|
|
68
|
+
expected: { firstWeek: "2026-06-01", weekCount: 8 },
|
|
69
|
+
},
|
|
70
|
+
])("$description", ({ targetFirstWeek, weekCount, behavior, useMinMax, expected }) => {
|
|
71
|
+
const result = applyOverflow({
|
|
72
|
+
targetFirstWeek,
|
|
73
|
+
weekCount,
|
|
74
|
+
behavior,
|
|
75
|
+
min: useMinMax ? min : undefined,
|
|
76
|
+
max: useMinMax ? max : undefined,
|
|
77
|
+
weekStartDay: 0,
|
|
78
|
+
T,
|
|
79
|
+
});
|
|
80
|
+
expect(result.firstWeek.toString()).toBe(expected.firstWeek);
|
|
81
|
+
expect(result.weekCount).toBe(expected.weekCount);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("canShift", () => {
|
|
86
|
+
const min = pd("2025-12-01");
|
|
87
|
+
const max = pd("2026-01-10");
|
|
88
|
+
|
|
89
|
+
it.each([
|
|
90
|
+
{
|
|
91
|
+
description: "unbounded — always true",
|
|
92
|
+
behavior: "unbounded" as OverflowBehavior,
|
|
93
|
+
currentFirstWeek: pd("2026-06-01"),
|
|
94
|
+
direction: 1 as const,
|
|
95
|
+
expected: true,
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
description: "stop — false when target has no valid weeks",
|
|
99
|
+
behavior: "stop" as OverflowBehavior,
|
|
100
|
+
currentFirstWeek: pd("2025-12-28"),
|
|
101
|
+
direction: 1 as const,
|
|
102
|
+
expected: false,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
description: "snap — false when already at clamp position",
|
|
106
|
+
behavior: "snap" as OverflowBehavior,
|
|
107
|
+
currentFirstWeek: pd("2025-11-16"),
|
|
108
|
+
direction: 1 as const,
|
|
109
|
+
expected: false,
|
|
110
|
+
},
|
|
111
|
+
])("$description", ({ behavior, currentFirstWeek, direction, expected }) => {
|
|
112
|
+
const result = canShift({
|
|
113
|
+
currentFirstWeek,
|
|
114
|
+
weekCount: 8,
|
|
115
|
+
direction,
|
|
116
|
+
behavior,
|
|
117
|
+
min,
|
|
118
|
+
max,
|
|
119
|
+
weekStartDay: 0,
|
|
120
|
+
T,
|
|
121
|
+
});
|
|
122
|
+
expect(result).toBe(expected);
|
|
123
|
+
});
|
|
124
|
+
});
|
package/src/overflow.ts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
2
|
+
import type { TemporalNamespace, WeekStartDay } from "./types";
|
|
3
|
+
|
|
4
|
+
export type OverflowBehavior = "unbounded" | "stop" | "stop-shrink" | "snap" | "snap-shrink";
|
|
5
|
+
|
|
6
|
+
export type MonthOverflowBehavior = "unbounded" | "stop";
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Internal helpers
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
/** Returns true if any day in the 7-day span starting at weekStart falls within [min, max]. */
|
|
13
|
+
function weekHasValidDay(
|
|
14
|
+
weekStart: Temporal.PlainDate,
|
|
15
|
+
min: Temporal.PlainDate,
|
|
16
|
+
max: Temporal.PlainDate,
|
|
17
|
+
T: TemporalNamespace,
|
|
18
|
+
): boolean {
|
|
19
|
+
// Week spans [weekStart, weekStart+6]. Overlaps with [min, max] iff
|
|
20
|
+
// weekStart <= max AND weekStart+6 >= min.
|
|
21
|
+
const weekEnd = weekStart.add({ days: 6 });
|
|
22
|
+
return T.PlainDate.compare(weekStart, max) <= 0 && T.PlainDate.compare(weekEnd, min) >= 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns the start of the week (per weekStartDay) that contains the given date.
|
|
27
|
+
* Uses dayOfWeek % 7 → Sun=0…Sat=6 convention.
|
|
28
|
+
*/
|
|
29
|
+
function weekStartContaining(
|
|
30
|
+
date: Temporal.PlainDate,
|
|
31
|
+
weekStartDay: WeekStartDay,
|
|
32
|
+
_T: TemporalNamespace,
|
|
33
|
+
): Temporal.PlainDate {
|
|
34
|
+
const dow = date.dayOfWeek % 7; // Sun=0…Sat=6
|
|
35
|
+
const daysFromStart = (dow - weekStartDay + 7) % 7;
|
|
36
|
+
return date.subtract({ days: daysFromStart });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Find the week start (per weekStartDay) whose 7-day span contains min. */
|
|
40
|
+
function findFirstValidWeekStart(
|
|
41
|
+
min: Temporal.PlainDate,
|
|
42
|
+
weekStartDay: WeekStartDay,
|
|
43
|
+
T: TemporalNamespace,
|
|
44
|
+
): Temporal.PlainDate {
|
|
45
|
+
return weekStartContaining(min, weekStartDay, T);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Find the week start (per weekStartDay) whose 7-day span contains max. */
|
|
49
|
+
function findLastValidWeekStart(
|
|
50
|
+
max: Temporal.PlainDate,
|
|
51
|
+
weekStartDay: WeekStartDay,
|
|
52
|
+
T: TemporalNamespace,
|
|
53
|
+
): Temporal.PlainDate {
|
|
54
|
+
return weekStartContaining(max, weekStartDay, T);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Trim leading and trailing fully-disabled weeks from a window.
|
|
59
|
+
* Returns new { firstWeek, weekCount }.
|
|
60
|
+
*/
|
|
61
|
+
function shrinkWindow(
|
|
62
|
+
firstWeek: Temporal.PlainDate,
|
|
63
|
+
weekCount: number,
|
|
64
|
+
min: Temporal.PlainDate,
|
|
65
|
+
max: Temporal.PlainDate,
|
|
66
|
+
T: TemporalNamespace,
|
|
67
|
+
): { firstWeek: Temporal.PlainDate; weekCount: number } {
|
|
68
|
+
let start = 0;
|
|
69
|
+
let end = weekCount - 1;
|
|
70
|
+
|
|
71
|
+
// Trim leading disabled weeks
|
|
72
|
+
while (start <= end) {
|
|
73
|
+
const ws = firstWeek.add({ days: start * 7 });
|
|
74
|
+
if (weekHasValidDay(ws, min, max, T)) break;
|
|
75
|
+
start++;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Trim trailing disabled weeks
|
|
79
|
+
while (end >= start) {
|
|
80
|
+
const ws = firstWeek.add({ days: end * 7 });
|
|
81
|
+
if (weekHasValidDay(ws, min, max, T)) break;
|
|
82
|
+
end--;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (start > end) {
|
|
86
|
+
// No valid weeks at all — return as-is (degenerate case)
|
|
87
|
+
return { firstWeek, weekCount };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
firstWeek: firstWeek.add({ days: start * 7 }),
|
|
92
|
+
weekCount: end - start + 1,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
// Public API
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
|
|
100
|
+
export interface ApplyOverflowInput {
|
|
101
|
+
targetFirstWeek: Temporal.PlainDate;
|
|
102
|
+
weekCount: number;
|
|
103
|
+
behavior: OverflowBehavior;
|
|
104
|
+
min?: Temporal.PlainDate;
|
|
105
|
+
max?: Temporal.PlainDate;
|
|
106
|
+
weekStartDay: WeekStartDay;
|
|
107
|
+
T: TemporalNamespace;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Given a target firstWeek and weekCount, apply overflow behavior and return
|
|
112
|
+
* the adjusted { firstWeek, weekCount }.
|
|
113
|
+
*/
|
|
114
|
+
export function applyOverflow({
|
|
115
|
+
targetFirstWeek,
|
|
116
|
+
weekCount,
|
|
117
|
+
behavior,
|
|
118
|
+
min,
|
|
119
|
+
max,
|
|
120
|
+
weekStartDay,
|
|
121
|
+
T,
|
|
122
|
+
}: ApplyOverflowInput): { firstWeek: Temporal.PlainDate; weekCount: number } {
|
|
123
|
+
// Without bounds, all modes behave as unbounded
|
|
124
|
+
if (!min || !max || behavior === "unbounded") {
|
|
125
|
+
return { firstWeek: targetFirstWeek, weekCount };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (behavior === "stop") {
|
|
129
|
+
// Caller checks canShift; here just return the target as-is
|
|
130
|
+
return { firstWeek: targetFirstWeek, weekCount };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (behavior === "stop-shrink") {
|
|
134
|
+
return shrinkWindow(targetFirstWeek, weekCount, min, max, T);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (behavior === "snap" || behavior === "snap-shrink") {
|
|
138
|
+
// Compute the snapped position: window ends at lastValidWeekStart
|
|
139
|
+
const lastValid = findLastValidWeekStart(max, weekStartDay, T);
|
|
140
|
+
|
|
141
|
+
// The snapped firstWeek positions the window so it ends at lastValid
|
|
142
|
+
// i.e. firstWeek = lastValid - (weekCount - 1) * 7
|
|
143
|
+
const snappedFirst = lastValid.subtract({ days: (weekCount - 1) * 7 });
|
|
144
|
+
|
|
145
|
+
// Determine which firstWeek to use: if target is already within bounds
|
|
146
|
+
// (doesn't overshoot max), use the target; otherwise snap.
|
|
147
|
+
// "Overshoots max" means the target window extends past the last valid week.
|
|
148
|
+
// The last week of the target window is targetFirstWeek + (weekCount-1)*7.
|
|
149
|
+
const targetLastWeek = targetFirstWeek.add({ days: (weekCount - 1) * 7 });
|
|
150
|
+
|
|
151
|
+
let resolvedFirst: Temporal.PlainDate;
|
|
152
|
+
if (T.PlainDate.compare(targetLastWeek, lastValid) <= 0) {
|
|
153
|
+
// Target window fits within max — also check if it goes before min
|
|
154
|
+
const firstValid = findFirstValidWeekStart(min, weekStartDay, T);
|
|
155
|
+
if (T.PlainDate.compare(targetFirstWeek, firstValid) >= 0) {
|
|
156
|
+
resolvedFirst = targetFirstWeek;
|
|
157
|
+
} else {
|
|
158
|
+
// Overshoots min: snap forward so window starts at firstValid
|
|
159
|
+
resolvedFirst = firstValid;
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
// Overshoots max: snap back
|
|
163
|
+
resolvedFirst = snappedFirst;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (behavior === "snap-shrink") {
|
|
167
|
+
return shrinkWindow(resolvedFirst, weekCount, min, max, T);
|
|
168
|
+
}
|
|
169
|
+
return { firstWeek: resolvedFirst, weekCount };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return { firstWeek: targetFirstWeek, weekCount };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface CanShiftInput {
|
|
176
|
+
currentFirstWeek: Temporal.PlainDate;
|
|
177
|
+
weekCount: number;
|
|
178
|
+
direction: 1 | -1;
|
|
179
|
+
shiftBy?: number;
|
|
180
|
+
behavior: OverflowBehavior;
|
|
181
|
+
min?: Temporal.PlainDate;
|
|
182
|
+
max?: Temporal.PlainDate;
|
|
183
|
+
weekStartDay: WeekStartDay;
|
|
184
|
+
T: TemporalNamespace;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Returns whether the navigation button for the given direction should be enabled.
|
|
189
|
+
*/
|
|
190
|
+
export function canShift({
|
|
191
|
+
currentFirstWeek,
|
|
192
|
+
weekCount,
|
|
193
|
+
direction,
|
|
194
|
+
shiftBy,
|
|
195
|
+
behavior,
|
|
196
|
+
min,
|
|
197
|
+
max,
|
|
198
|
+
weekStartDay,
|
|
199
|
+
T,
|
|
200
|
+
}: CanShiftInput): boolean {
|
|
201
|
+
if (behavior === "unbounded" || !min || !max) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const shift = shiftBy ?? weekCount;
|
|
206
|
+
const targetFirstWeek = currentFirstWeek.add({ days: direction * shift * 7 });
|
|
207
|
+
|
|
208
|
+
if (behavior === "stop" || behavior === "stop-shrink") {
|
|
209
|
+
// Check if any week in the target window has valid days
|
|
210
|
+
for (let i = 0; i < weekCount; i++) {
|
|
211
|
+
const ws = targetFirstWeek.add({ days: i * 7 });
|
|
212
|
+
if (weekHasValidDay(ws, min, max, T)) return true;
|
|
213
|
+
}
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (behavior === "snap" || behavior === "snap-shrink") {
|
|
218
|
+
// Disabled when the current position is already the snapped position
|
|
219
|
+
// (i.e. applying overflow to the target produces the same firstWeek)
|
|
220
|
+
const applied = applyOverflow({
|
|
221
|
+
targetFirstWeek,
|
|
222
|
+
weekCount,
|
|
223
|
+
behavior,
|
|
224
|
+
min,
|
|
225
|
+
max,
|
|
226
|
+
weekStartDay,
|
|
227
|
+
T,
|
|
228
|
+
});
|
|
229
|
+
return T.PlainDate.compare(applied.firstWeek, currentFirstWeek) !== 0;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return true;
|
|
233
|
+
}
|